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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 798 - (download) (as text) (annotate)
Fri Mar 21 23:30:16 2003 UTC (10 years, 2 months ago) by sh002i
File size: 3850 byte(s)
initial support for WWDBv2:
        - DB.pm finished (except for getGlobalUser{Set,Problem} methods)
        - schema modules for password, permission, key, and user with
          WWDBv1 hash-bashed backends
        - GDBM driver
        - wwdb command-line frontend
-sam

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9