[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 317 - (download) (as text) (annotate)
Thu May 30 19:14:40 2002 UTC (17 years, 7 months ago) by sh002i
File size: 5138 byte(s)
written, not debugged.
-sam

    1 package WeBWorK::DB::Auth;
    2 
    3 # there should be a `use' line for each database type
    4 use WeBWorK::DB::GDBM;
    5 
    6 # params: class, course environment
    7 sub new($$) {
    8   my $proto = shift;
    9   my $class = ref($proto) || $proto;
   10   my $courseEnv = shift;
   11   my $dbModule = fullyQualifiedPackageName($courseEnv->{auth_db_type});
   12   my $self = {
   13     password_file => $courseEnv->{something},
   14     permissions_file => $courseEnv->{something},
   15     keys_file => $courseEnv->{something},
   16   };
   17   $self->{password_db} = $self->{dbModule}->new($self->{password_file});
   18   $self->{permissions_db} = $self->{dbModule}->new($self->{permissions_file});
   19   $self->{keys_db} = $self->{dbModule}->new($self->{keys_file});
   20   bless $self, $class;
   21   return $self;
   22 }
   23 
   24 sub fullyQualifiedPackageName($) {
   25   my $n = shift;
   26   my $package = "__PACKAGE__";
   27   $package =~ s/([^:]*)$/$n/;
   28   return $package;
   29 }
   30 
   31 sub connect($$$) {
   32   my $self = shift;
   33   my $db = shift;
   34   my $mode = shift;
   35   return if defined $self->{$db."_db"};
   36   $self->{$db."_db"} = $self->{dbModule}->new($db."_file", $mode);
   37   $self->{$db."_db"}->connect();
   38 }
   39 
   40 sub disconnect($$) {
   41   my $self = shift;
   42   my $db = shift;
   43   return unless defined $self->{$db."_db"};
   44   $self->{$db."_db"}->disconnect();
   45 }
   46 
   47 # -----
   48 
   49 sub getPassword($$) {
   50   my $self = shift;
   51   my $user = shift;
   52   $self->{password_db}->connect("ro");
   53   my $result = $self->{password_db}->hashRef()->{$user};
   54   $self->{password_db}->disconnect();
   55   return $result;
   56 }
   57 
   58 sub setPassword($$$) {
   59   my $self = shift;
   60   my $user = shift;
   61   my $password = shift;
   62   $password = crypt $password, join "", ('.','/','0'..'9','A'..'Z','a'..'z')[rand 64, rand 64]
   63   $self->{password_db}->connect("rw");
   64   $self->{password_db}->hashRef()->{$user} = $password;
   65   $self->{password_db}->disconnect();
   66 }
   67 
   68 sub verifyPassword($$$) {
   69   my $self = shift;
   70   my $user = shift;
   71   my $password = shift;
   72   $self->{password_db}->connect("ro");
   73   my $result = $self->{password_db}->hashRef()->{$user} eq $password;
   74   $self->{password_db}->disconnect();
   75   return $result;
   76 }
   77 
   78 sub deletePassword($$) {
   79   my $self = shift;
   80   my $user = shift;
   81   $self->{password_db}->connect("rw");
   82   delete $self->{password_db}->hashRef()->{$user};
   83   $self->{password_db}->disconnect();
   84 }
   85 
   86 # -----
   87 
   88 sub getKey($$) {
   89   my $self = shift;
   90   my $user = shift;
   91   $self->{keys_db}->connect("ro");
   92   my $result = $self->{keys_db}->hashRef()->{$user};
   93   $self->{keys_db}->disconnect();
   94   my ($key, $timestamp) = split /\s+/, $result;
   95   return $key, $timestamp;
   96 }
   97 
   98 sub setKey($$$$) {
   99   my $self = shift;
  100   my $user = shift;
  101   my $key = shift;
  102   my $timestamp = shift;
  103   my $key_string = "$key $timestamp";
  104   $self->{keys_db}->connect("rw");
  105   $self->{keys_db}->hashRef()->{$user} = $key_string;
  106   $self->{keys_db}->disconnect();
  107 }
  108 
  109 sub verifyKey($$$) {
  110   my $self = shift;
  111   my $user = shift;
  112   my $key = shift;
  113   $self->{keys_db}->connect("ro");
  114   my $result = $self->{keys_db}->hashRef()->{$user};
  115   $self->{keys_db}->disconnect();
  116   my ($real_key, $timestamp) = split /\s+/, $result;
  117   return $key eq $real_key;
  118   # DANGER DANGER! this function no longer updates timestamp!
  119 }
  120 
  121 sub deleteKey($$) {
  122   my $self = shift;
  123   my $user = shift;
  124   $self->{keys_db}->connect("rw");
  125   delete $self->{keys_db}->hashRef()->{$user};
  126   $self->{keys_db}->disconnect();
  127 }
  128 
  129 # -----
  130 
  131 sub getPermissions($$) {
  132   my $self = shift;
  133   my $user = shift;
  134   $self->{permissions_db}->connect("ro");
  135   my $result = $self->{permissions_db}->hashRef()->{$user};
  136   $self->{permissions_db}->disconnect();
  137   return $result;
  138 }
  139 
  140 sub setPermissions($$$) {
  141   my $self = shift;
  142   my $user = shift;
  143   my $permissions = shift;
  144   $self->{permissions_db}->connect("rw");
  145   $self->{permissions_db}->hashRef()->{$user} = $key;
  146   $self->{permissions_db}->disconnect();
  147 }
  148 
  149 sub deletePermissions($$) {
  150   my $self = shift;
  151   my $user = shift;
  152   $self->{permissions_db}->connect("rw");
  153   delete $self->{permissions_db}->hashRef()->{$user};
  154   $self->{permissions_db}->disconnect();
  155 }
  156 
  157 # ----- ghetto for stupid functions -----
  158 
  159 sub change_user_in_password_file($$$) {
  160   my $self = shift;
  161   my $user = shift;
  162   my $new_user = shift;
  163   $self->{password_db}->connect("rw");
  164   my $pwhash = $self->{password_db}->hashRef(); # make things easier
  165   if (exists $pwhash->{user}) {
  166     $pwhash->{new_user} = $pwhash->{user};
  167     delete $pwhash->{user};
  168   }
  169   $self->{password_db}->disconnect();
  170 }
  171 
  172 sub change_user_in_permissions_file($$$) {
  173   my $self = shift;
  174   my $user = shift;
  175   my $new_user = shift;
  176   $self->{permissions_db}->connect("rw");
  177   my $permhash = $self->{permissions_db}->hashRef(); # make things easier
  178   if (exists $permhash->{user}) {
  179     $permhash->{new_user} = $permhash->{user};
  180     delete $permhash->{user};
  181   }
  182   $self->disconnect{permissions_db}->();
  183 }
  184 
  185 =pod
  186 sub create_db {
  187     my ($fileName, $permissions) =@_;
  188     my %pwhash;
  189     my $pw_obj;
  190     &Global::tie_hash('PW_FH',\$pw_obj,\%pwhash, $fileName,'W',$permissions);
  191     &Global::untie_hash('PW_FH',\$pw_obj,\%pwhash, $fileName);
  192 
  193     chmod($permissions, $fileName) or
  194                              wwerror($0, "Can't do chmod($permissions, $fileName)");
  195     chown(-1,$Global::numericalGroupID,$fileName)  or
  196                              wwerror($0, "Can't do chown(-1,$Global::numericalGroupID,$fileName)");
  197 
  198 }
  199 =cut

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9