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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1664 - (download) (as text) (annotate)
Tue Dec 9 02:42:28 2003 UTC (9 years, 5 months ago) by sh002i
File size: 5084 byte(s)
added "count" methods to the rest of the schemas. updated documentation.

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright © 2000-2003 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork-modperl/lib/WeBWorK/DB/Schema/Auth1Hash.pm,v 1.9 2003/12/09 01:12:32 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 package WeBWorK::DB::Schema::Auth1Hash;
   18 use base qw(WeBWorK::DB::Schema);
   19 
   20 =head1 NAME
   21 
   22 WeBWorK::DB::Schema::Auth1Hash - support access to the password, permission,
   23 and key tables with a WWDBv1 hash-style backend.
   24 
   25 =cut
   26 
   27 use strict;
   28 use warnings;
   29 
   30 use constant TABLES => qw(password permission key);
   31 use constant STYLE  => "hash";
   32 
   33 ################################################################################
   34 # table access functions
   35 #  Auth1Hash provides access to three tables, so it checks the $self->{table}
   36 #  field to know what data its dealing with.
   37 ################################################################################
   38 
   39 sub count {
   40   my ($self, @keyparts) = @_;
   41   my ($matchUserID) = @keyparts;
   42   my $count;
   43   $self->{driver}->connect("ro");
   44   if (defined $matchUserID) {
   45     $count = exists $self->{driver}->hash()->{$matchUserID};
   46   } else {
   47     $count = scalar keys %{ $self->{driver}->hash() };
   48   }
   49   $self->{driver}->disconnect();
   50   return $count;
   51 }
   52 
   53 sub list {
   54   my ($self, @keyparts) = @_;
   55   my ($matchUserID) = @keyparts;
   56   $self->{driver}->connect("ro");
   57   my @keys = keys %{ $self->{driver}->hash() };
   58   $self->{driver}->disconnect();
   59   if (defined $matchUserID) {
   60     @keys = grep { $_ eq $matchUserID } @keys;
   61   }
   62   return map { [$_] } @keys;
   63 }
   64 
   65 sub exists {
   66   my ($self, $userID) = @_;
   67   $self->{driver}->connect("ro");
   68   my $result;
   69   if (defined $userID) {
   70     $result = exists $self->{driver}->hash()->{$userID};
   71   } else {
   72     $result = keys %{$self->{driver}->hash()} ? 1 : 0;
   73   }
   74   $self->{driver}->disconnect();
   75   return $result;
   76 }
   77 
   78 sub add {
   79   my ($self, $Record) = @_;
   80   my $valueName = $self->{table};
   81   $self->{driver}->connect("rw");
   82   my $hash = $self->{driver}->hash();
   83   die $Record->user_id, ": $valueName exists"
   84     if exists $hash->{$Record->user_id};
   85   if ($self->{table} eq "key") {
   86     # key's value contains two fields
   87     $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp();
   88   } else {
   89     $hash->{$Record->user_id} = $Record->$valueName();
   90   }
   91   $self->{driver}->disconnect();
   92 }
   93 
   94 sub get {
   95   my ($self, $userID) = @_;
   96 # $self->{driver}->connect("ro");
   97 # my $value = $self->{driver}->hash()->{$userID};
   98 # $self->{driver}->disconnect();
   99 # return undef unless defined $value;
  100 # if ($self->{table} eq "key") {
  101 #   # key's value contains two fields
  102 #   my ($key, $timestamp) = $value =~ m/^(\S+)\s+(.*)$/;
  103 #   return $self->{record}->new(
  104 #     user_id => $userID,
  105 #     key => $key,
  106 #     timestamp => $timestamp,
  107 #   );
  108 # } else {
  109 #   return $self->{record}->new(
  110 #     user_id => $userID,
  111 #     $self->{table} => $value,
  112 #   );
  113 # }
  114   my @results =
  115   return ($self->gets([$userID]))[0];
  116 }
  117 
  118 sub gets {
  119   my ($self, @keypartsRefList) = @_;
  120   $self->{driver}->connect("ro");
  121   my @records;
  122   foreach my $keypartsRef (@keypartsRefList) {
  123     my $userID = $keypartsRef->[0];
  124     my $string = $self->{driver}->hash()->{$userID};
  125     if (defined $string) {
  126       if ($self->{table} eq "key") {
  127         # key's value contains two fields
  128         my ($key, $timestamp) = $string =~ m/^(\S+)\s+(.*)$/;
  129         push @records, $self->{record}->new(
  130           user_id => $userID,
  131           key => $key,
  132           timestamp => $timestamp,
  133         );
  134       } else {
  135         push @records, $self->{record}->new(
  136           user_id => $userID,
  137           $self->{table} => $string,
  138         );
  139       }
  140     } else {
  141       push @records, undef;
  142     }
  143   }
  144   $self->{driver}->disconnect();
  145   return @records;
  146 }
  147 
  148 sub put {
  149   my ($self, $Record) = @_;
  150   my $valueName = $self->{table};
  151   $self->{driver}->connect("rw");
  152   my $hash = $self->{driver}->hash();
  153   die $Record->user_id, ": $valueName not found"
  154     unless exists $hash->{$Record->user_id};
  155   if ($self->{table} eq "key") {
  156     # key's value contains two fields
  157     $hash->{$Record->user_id} = $Record->key() . " " . $Record->timestamp();
  158   } else {
  159     $hash->{$Record->user_id} = $Record->$valueName();
  160   }
  161   $self->{driver}->disconnect();
  162 }
  163 
  164 sub delete {
  165   my ($self, $userID) = @_;
  166   my $valueName = $self->{table};
  167   return 0 unless $self->{driver}->connect("rw");
  168   my $hash = $self->{driver}->hash();
  169   if (defined $userID) {
  170     delete $hash->{$userID};
  171   } else {
  172     # delete all elements
  173     delete @$hash{keys %$hash};
  174   }
  175   $self->{driver}->disconnect();
  176   return 1;
  177 }
  178 
  179 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9