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

Annotation of /trunk/webwork2/lib/WeBWorK/DB/Schema/Auth1Hash.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9