[system] / trunk / webwork-modperl / lib / WeBWorK / DB / Schema / Auth1Hash.pm Repository:
ViewVC logotype

Annotation of /trunk/webwork-modperl/lib/WeBWorK/DB/Schema/Auth1Hash.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 808 - (view) (download) (as text)

1 : sh002i 798 ################################################################################
2 :     # WeBWorK mod_perl (c) 2000-2002 WeBWorK Project
3 :     # $Id$
4 :     ################################################################################
5 :    
6 :     package WeBWorK::DB::Schema::Auth1Hash;
7 :    
8 :     =head1 NAME
9 :    
10 :     WeBWorK::DB::Schema::Auth1Hash - support access to the password, permission,
11 : sh002i 808 and key tables with a WWDBv1 hash-style backend.
12 : sh002i 798
13 :     =cut
14 :    
15 :     use strict;
16 :     use warnings;
17 :    
18 :     use constant TABLES => qw(password permission key);
19 :     use constant STYLE => "hash";
20 :    
21 :     ################################################################################
22 :     # static functions
23 :     ################################################################################
24 :    
25 :     sub tables() {
26 :     return TABLES;
27 :     }
28 :    
29 :     sub style() {
30 :     return STYLE;
31 :     }
32 :    
33 :     ################################################################################
34 :     # constructor
35 :     ################################################################################
36 :    
37 :     sub new($$$$) {
38 : sh002i 808 my ($proto, $driver, $table, $record, $params) = @_;
39 : sh002i 798 my $class = ref($proto) || $proto;
40 :     die "$table: unsupported table"
41 :     unless grep { $_ eq $table } $proto->tables();
42 :     die $driver->style(), ": style mismatch"
43 :     unless $driver->style() eq $proto->style();
44 :     my $self = {
45 :     driver => $driver,
46 :     table => $table,
47 :     record => $record,
48 : sh002i 808 params => $params,
49 : sh002i 798 };
50 :     bless $self, $class;
51 :     return $self;
52 :     }
53 :    
54 :     ################################################################################
55 :     # table access functions
56 :     # Auth1Hash provides access to three tables, so it checks the $self->{table}
57 :     # field to know what data its dealing with.
58 :     ################################################################################
59 :    
60 : sh002i 808 sub list($@) {
61 :     my ($self, @keyparts) = @_;
62 :     my ($matchUserID) = @keyparts;
63 : sh002i 798 $self->{driver}->connect("ro");
64 :     my @keys = keys %{ $self->{driver}->hash() };
65 :     $self->{driver}->disconnect();
66 : sh002i 808 if (defined $matchUserID) {
67 :     @keys = grep { $_ eq $matchUserID } @keys;
68 :     }
69 :     return map { [$_] } @keys;
70 : sh002i 798 }
71 :    
72 :     sub exists($$) {
73 :     my ($self, $userID) = @_;
74 :     $self->{driver}->connect("ro");
75 :     my $exists = exists $self->{driver}->hash()->{$userID};
76 :     $self->{driver}->disconnect();
77 :     return $exists;
78 :     }
79 :    
80 :     sub add($$) {
81 :     my ($self, $Record) = @_;
82 :     my $valueName = $self->{table};
83 :     $self->{driver}->connect("rw");
84 :     my $hash = $self->{driver}->hash();
85 :     die $Record->user_id, ": $valueName exists"
86 :     if exists $hash->{$Record->user_id};
87 :     if ($self->{table} eq "key") {
88 :     # key's value contains two fields
89 :     $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp();
90 :     } else {
91 :     $hash->{$Record->user_id} = $Record->$valueName();
92 :     }
93 :     $self->{driver}->disconnect();
94 :     }
95 :    
96 :     sub get($$) {
97 :     my ($self, $userID) = @_;
98 :     $self->{driver}->connect("ro");
99 :     my $value = $self->{driver}->hash()->{$userID};
100 :     $self->{driver}->disconnect();
101 : sh002i 808 return undef unless defined $value;
102 : sh002i 798 if ($self->{table} eq "key") {
103 :     # key's value contains two fields
104 :     my ($key, $timestamp) = $value =~ m/^(\S+)\s+(.*)$/;
105 :     return $self->{record}->new(
106 :     user_id => $userID,
107 :     key => $key,
108 :     timestamp => $timestamp,
109 :     );
110 :     } else {
111 :     return $self->{record}->new(
112 :     user_id => $userID,
113 :     $self->{table} => $value,
114 :     );
115 :     }
116 :     }
117 :    
118 :     sub put($$) {
119 :     my ($self, $Record) = @_;
120 :     my $valueName = $self->{table};
121 :     $self->{driver}->connect("rw");
122 :     my $hash = $self->{driver}->hash();
123 :     die $Record->user_id, ": $valueName not found"
124 :     unless exists $hash->{$Record->user_id};
125 :     if ($self->{table} eq "key") {
126 :     # key's value contains two fields
127 :     $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp();
128 :     } else {
129 :     $hash->{$Record->user_id} = $Record->$valueName();
130 :     }
131 :     $self->{driver}->disconnect();
132 :     }
133 :    
134 :     sub delete($$) {
135 :     my ($self, $userID) = @_;
136 :     my $valueName = $self->{table};
137 :     $self->{driver}->connect("rw");
138 :     my $hash = $self->{driver}->hash();
139 :     die "$userID: $valueName not found"
140 :     unless exists $hash->{$userID};
141 :     delete $hash->{$userID};
142 :     $self->{driver}->disconnect();
143 :     }
144 :    
145 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9