[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 338 - (download) (as text) (annotate)
Wed Jun 5 19:07:21 2002 UTC (17 years, 6 months ago) by sh002i
File size: 5772 byte(s)
try this

    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   if ($key eq $real_key and $timestamp <= $real_timestamp+$self->{key_timeout}) {
  152     $self->setKey($user, $key, $timestamp);
  153     return 1;
  154   } else {
  155     return 0;
  156   }
  157 }
  158 
  159 sub deleteKey($$) {
  160   my $self = shift;
  161   my $user = shift;
  162   $self->{keys_db}->connect("rw");
  163   delete $self->{keys_db}->hashRef->{$user};
  164   $self->{keys_db}->disconnect;
  165 }
  166 
  167 # -----
  168 
  169 sub getPermissions($$) {
  170   my $self = shift;
  171   my $user = shift;
  172   return unless $self->{permissions_db}->connect("ro");
  173   my $result = $self->{permissions_db}->hashRef->{$user};
  174   $self->{permissions_db}->disconnect;
  175   return $result;
  176 }
  177 
  178 sub setPermissions($$$) {
  179   my $self = shift;
  180   my $user = shift;
  181   my $permissions = shift;
  182   $self->{permissions_db}->connect("rw");
  183   $self->{permissions_db}->hashRef->{$user} = $permissions;
  184   $self->{permissions_db}->disconnect;
  185 }
  186 
  187 sub deletePermissions($$) {
  188   my $self = shift;
  189   my $user = shift;
  190   $self->{permissions_db}->connect("rw");
  191   delete $self->{permissions_db}->hashRef->{$user};
  192   $self->{permissions_db}->disconnect;
  193 }
  194 
  195 # ----- ghetto for stupid functions -----
  196 
  197 sub change_user_in_password_file($$$) {
  198   my $self = shift;
  199   my $user = shift;
  200   my $new_user = shift;
  201   $self->{password_db}->connect("rw");
  202   my $pwhash = $self->{password_db}->hashRef; # make things easier
  203   if (exists $pwhash->{$user}) {
  204     $pwhash->{$new_user} = $pwhash->{$user};
  205     delete $pwhash->{$user};
  206   }
  207   $self->{password_db}->disconnect;
  208 }
  209 
  210 sub change_user_in_permissions_file($$$) {
  211   my $self = shift;
  212   my $user = shift;
  213   my $new_user = shift;
  214   $self->{permissions_db}->connect("rw");
  215   my $permhash = $self->{permissions_db}->hashRef; # make things easier
  216   if (exists $permhash->{$user}) {
  217     $permhash->{$new_user} = $permhash->{$user};
  218     delete $permhash->{$user};
  219   }
  220   $self->{permissions_db}->disconnect;
  221 }
  222 
  223 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9