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

View of /trunk/webwork2/lib/WeBWorK/DB/Auth.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 339 - (download) (as text) (annotate)
Wed Jun 5 19:09:31 2002 UTC (17 years, 6 months ago) by sh002i
File size: 5834 byte(s)
try this too
-sam

    1 ################################################################################
    2 # WeBWorK mod_perl (c) 1995-2002 WeBWorK Team, Univeristy of Rochester
    3 # $Id$
    4 ################################################################################
    5 
    6 package WeBWorK::DB::Auth;
    7 
    8 # there should be a `use' line for each database type
    9 use WeBWorK::DB::GDBM;
   10 
   11 # new($invocant, $courseEnv)
   12 # $invocant implicitly set by caller
   13 # $courseEnv  an instance of CourseEnvironment
   14 sub new($$) {
   15   my $invocant = shift;
   16   my $class = ref($invocant) || $invocant;
   17   my $courseEnv = shift;
   18   my $dbModule = fullyQualifiedPackageName($courseEnv->{dbInfo}->{auth_type});
   19   my $self = {
   20     password_file    => $courseEnv->{dbInfo}->{auth_passwd_file},
   21     permissions_file => $courseEnv->{dbInfo}->{auth_perm_file},
   22     keys_file        => $courseEnv->{dbInfo}->{auth_keys_file},
   23     key_timeout      => $courseEnv->{sessionKeyTimeout},
   24   };
   25   $self->{password_db}    = $dbModule->new($self->{password_file});
   26   $self->{permissions_db} = $dbModule->new($self->{permissions_file});
   27   $self->{keys_db}        = $dbModule->new($self->{keys_file});
   28   bless $self, $class;
   29   return $self;
   30 }
   31 
   32 sub fullyQualifiedPackageName($) {
   33   my $n = shift;
   34   my $package = __PACKAGE__;
   35   $package =~ s/([^:]*)$/$n/;
   36   return $package;
   37 }
   38 
   39 =pod
   40 sub connect($$$) {
   41   my $self = shift;
   42   my $db = shift;
   43   my $mode = shift;
   44   return if defined $self->{$db."_db"};
   45   $self->{$db."_db"} = $self->{dbModule}->new($db."_file", $mode);
   46   $self->{$db."_db"}->connect();
   47 }
   48 
   49 sub disconnect($$) {
   50   my $self = shift;
   51   my $db = shift;
   52   return unless defined $self->{$db."_db"};
   53   $self->{$db."_db"}->disconnect;
   54 }
   55 =cut
   56 
   57 # -----
   58 
   59 =pod
   60 sub getUsers($) {
   61   my $self = shift;
   62   my @password_users, @permissions_users, @keys_users;
   63   if ($self->{password_db}->connect("ro")) {
   64     @password_users = keys %{$self->{password_db}->hashRef};
   65     $self->{password_db}->disconnect;
   66   }
   67   if ($self->{permissions_db}->connect("ro")) {
   68     @permissions_users = keys %{$self->{permissions_db}->hashRef};
   69     $self->{permissions_db}->disconnect;
   70   }
   71   if ($self->{keys_db}->connect("ro")) {
   72     @keys_users = keys %{$self->{keys_db}->hashRef};
   73     $self->{keys_db}->disconnect;
   74   }
   75   my %all_users;
   76   foreach (@password_users, @permissions_users, @keys_users) {
   77     $all_users{$_}++;
   78   }
   79   return keys %all_users;
   80 }
   81 =cut
   82 
   83 # -----
   84 
   85 sub getPassword($$) {
   86   my $self = shift;
   87   my $user = shift;
   88   return unless $self->{password_db}->connect("ro");
   89   my $result = $self->{password_db}->hashRef->{$user};
   90   $self->{password_db}->disconnect;
   91   return $result;
   92 }
   93 
   94 sub setPassword($$$) {
   95   my $self = shift;
   96   my $user = shift;
   97   my $password = crypt shift, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64];
   98   $self->{password_db}->connect("rw");
   99   $self->{password_db}->hashRef->{$user} = $password;
  100   $self->{password_db}->disconnect;
  101 }
  102 
  103 sub verifyPassword($$$) {
  104   my $self = shift;
  105   my $user = shift;
  106   my $password = shift;
  107   my $real_password = $self->getPassword($user);
  108   return unless defined $real_password;
  109   $password = crypt $password, $real_password;
  110   return $password eq $real_password;
  111 }
  112 
  113 sub deletePassword($$) {
  114   my $self = shift;
  115   my $user = shift;
  116   $self->{password_db}->connect("rw");
  117   delete $self->{password_db}->hashRef->{$user};
  118   $self->{password_db}->disconnect;
  119 }
  120 
  121 # -----
  122 
  123 sub getKey($$) {
  124   my $self = shift;
  125   my $user = shift;
  126   return unless $self->{keys_db}->connect("ro");
  127   my $result = $self->{keys_db}->hashRef->{$user};
  128   $self->{keys_db}->disconnect;
  129   my ($key, $timestamp) = defined $result ? split /\s+/, $result : (undef, undef);
  130   return defined $result ? split /\s+/, $result : undef;
  131 }
  132 
  133 sub setKey($$$$) {
  134   my $self = shift;
  135   my $user = shift;
  136   my $key = shift;
  137   my $timestamp = shift;
  138   my $key_string = "$key $timestamp";
  139   $self->{keys_db}->connect("rw");
  140   $self->{keys_db}->hashRef->{$user} = $key_string;
  141   $self->{keys_db}->disconnect;
  142 }
  143 
  144 sub verifyKey($$$$$) {
  145   my $self = shift;
  146   my $user = shift;
  147   my $key = shift;
  148   my $timestamp = shift;
  149 
  150   my ($real_key, $real_timestamp) = $self->getKey($user);
  151   return unless defined $real_key and defined $real_timestamp;
  152   if ($key eq $real_key and $timestamp <= $real_timestamp+$self->{key_timeout}) {
  153     $self->setKey($user, $key, $timestamp);
  154     return 1;
  155   } else {
  156     return 0;
  157   }
  158 }
  159 
  160 sub deleteKey($$) {
  161   my $self = shift;
  162   my $user = shift;
  163   $self->{keys_db}->connect("rw");
  164   delete $self->{keys_db}->hashRef->{$user};
  165   $self->{keys_db}->disconnect;
  166 }
  167 
  168 # -----
  169 
  170 sub getPermissions($$) {
  171   my $self = shift;
  172   my $user = shift;
  173   return unless $self->{permissions_db}->connect("ro");
  174   my $result = $self->{permissions_db}->hashRef->{$user};
  175   $self->{permissions_db}->disconnect;
  176   return $result;
  177 }
  178 
  179 sub setPermissions($$$) {
  180   my $self = shift;
  181   my $user = shift;
  182   my $permissions = shift;
  183   $self->{permissions_db}->connect("rw");
  184   $self->{permissions_db}->hashRef->{$user} = $permissions;
  185   $self->{permissions_db}->disconnect;
  186 }
  187 
  188 sub deletePermissions($$) {
  189   my $self = shift;
  190   my $user = shift;
  191   $self->{permissions_db}->connect("rw");
  192   delete $self->{permissions_db}->hashRef->{$user};
  193   $self->{permissions_db}->disconnect;
  194 }
  195 
  196 # ----- ghetto for stupid functions -----
  197 
  198 sub change_user_in_password_file($$$) {
  199   my $self = shift;
  200   my $user = shift;
  201   my $new_user = shift;
  202   $self->{password_db}->connect("rw");
  203   my $pwhash = $self->{password_db}->hashRef; # make things easier
  204   if (exists $pwhash->{$user}) {
  205     $pwhash->{$new_user} = $pwhash->{$user};
  206     delete $pwhash->{$user};
  207   }
  208   $self->{password_db}->disconnect;
  209 }
  210 
  211 sub change_user_in_permissions_file($$$) {
  212   my $self = shift;
  213   my $user = shift;
  214   my $new_user = shift;
  215   $self->{permissions_db}->connect("rw");
  216   my $permhash = $self->{permissions_db}->hashRef; # make things easier
  217   if (exists $permhash->{$user}) {
  218     $permhash->{$new_user} = $permhash->{$user};
  219     delete $permhash->{$user};
  220   }
  221   $self->{permissions_db}->disconnect;
  222 }
  223 
  224 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9