| 1 | package Apache::UploadSvr::User; | ||
| 2 | |||
| 3 | use DBI; | ||
| 4 | use strict; | ||
| 5 | use vars qw( $Userref $VERSION ); | ||
| 6 | |||
| 7 | $Userref = undef; | ||
| 8 | |||
| 9 | $VERSION = sprintf "%d.%03d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my($class, $caller) = @_; | ||
| 13 | my $r = $caller->request; | ||
| 14 | my $time = time; | ||
| 15 | my $data_source = $r->dir_config('Auth_DBI_data_source'); | ||
| 16 | my $user_table = $r->dir_config('Auth_DBI_pwd_table'); | ||
| 17 | my $user_field = $r->dir_config('Auth_DBI_uid_field'); | ||
| 18 | |||
| 19 | my $userref; | ||
| 20 | my $user = $r->connection->user or return {}; | ||
| 21 | my $db; | ||
| 22 | if (exists $Userref->{user} and $Userref->{user} eq $user) { | ||
| 23 | return $Userref; | ||
| 24 | } | ||
| 25 | if ($db = DBI->connect($data_source)) { | ||
| 26 | my $sth = $db->prepare( | ||
| 27 | qq{select * from $user_table where $user_field='$user'} | ||
| 28 | ) or die $db->errstr; | ||
| 29 | my $rv = $sth->execute; | ||
| 30 | $userref = $sth->fetchrow_hashref; | ||
| 31 | $sth->finish; | ||
| 32 | my $query = "select * from perms where $user_field = '$user'"; | ||
| 33 | # warn "query[$query]"; | ||
| 34 | $sth = $db->prepare($query) or die $db->errstr; | ||
| 35 | $rv = $sth->execute; | ||
| 36 | my $numrows = $sth->rows; | ||
| 37 | # warn "rv[$rv] numrows[$numrows]"; | ||
| 38 | my $permref; | ||
| 39 | while ($permref = $sth->fetchrow_hashref) { | ||
| 40 | # warn join "*", %$permref; | ||
| 41 | push @{$userref->{permitted}}, $permref->{permitted}; | ||
| 42 | # warn "permitted[@{$userref->{permitted}}]"; | ||
| 43 | } | ||
| 44 | $sth->finish; | ||
| 45 | } else { | ||
| 46 | die $DBI::errstr; | ||
| 47 | } | ||
| 48 | my $update = "update $user_table set lastlogin='$time' | ||
| 49 | where $user_field = '$user'"; | ||
| 50 | my $rv = $db->do($update); | ||
| 51 | $db->disconnect; | ||
| 52 | warn "rv was not 1. rv[$rv]update[$update]errstr[$DBI::errstr]" unless $rv; | ||
| 53 | $Userref = bless $userref, $class; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub has_perms { | ||
| 57 | my($self,$f) = @_; | ||
| 58 | # warn "f[$f]"; | ||
| 59 | for my $d (@{$self->{permitted}}) { | ||
| 60 | # warn "has_perms d[$d]"; | ||
| 61 | if (substr($f,0,length($d)) eq $d) { | ||
| 62 | return 1; | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | Apache::UploadSvr::User - Identify users and permissions Apache::UploadSvr | ||
| 73 | |||
| 74 | =head1 SYNOPSIS | ||
| 75 | |||
| 76 | Apache::UploadSvr::User-E<gt>new($mgr); | ||
| 77 | |||
| 78 | =head1 DESCRIPTION | ||
| 79 | |||
| 80 | This class implements a mapping between user-ID and user attributes. | ||
| 81 | The backend is provided by a mSQL-1 database with the following structure: | ||
| 82 | |||
| 83 | CREATE TABLE usertable ( | ||
| 84 | user CHAR(12) NOT NULL PRIMARY KEY, | ||
| 85 | email CHAR(64), | ||
| 86 | firstname CHAR(32), | ||
| 87 | lastname CHAR(32), | ||
| 88 | fullname CHAR(64), | ||
| 89 | salut CHAR(4), | ||
| 90 | lastlogin CHAR(10), | ||
| 91 | introduced CHAR(10), | ||
| 92 | password CHAR(13), | ||
| 93 | changedon CHAR(10), | ||
| 94 | changedby CHAR(10) | ||
| 95 | ) | ||
| 96 | |||
| 97 | C<user> corresponds to the username with which the users identify | ||
| 98 | in the authentication stage. C<email> is their email address where | ||
| 99 | the transaction tickets are delivered to. C<lastname> is their family name. | ||
| 100 | <fullname> is whatever the fullname is composed of in the local culture. | ||
| 101 | C<salut> is the salutation like C<Herr> or C<Mister>. C<lastlogin> | ||
| 102 | is the timestamp that is updated with every request. C<introduced> is | ||
| 103 | the timestamp when the user got registered (not used in this uploadserver). | ||
| 104 | C<password> is the crypted | ||
| 105 | password in the default upload server. If the authentication handler | ||
| 106 | uses a different table, then this field is not needed. C<changedon> | ||
| 107 | and C<changedby> are not used in this application, they are only used as | ||
| 108 | interesting facts for the administrator. | ||
| 109 | |||
| 110 | CREATE TABLE perms ( | ||
| 111 | user CHAR(12), | ||
| 112 | permitted CHAR(32) | ||
| 113 | ) | ||
| 114 | |||
| 115 | This table has a 1 to N mapping of users to directories they | ||
| 116 | have write access to. | ||
| 117 | |||
| 118 | The constructor -E<gt>new takes as a single argument an | ||
| 119 | Apache::UploadSvr object and returns an object that has the | ||
| 120 | above described fields as object attributes. The attribute C<permitted> | ||
| 121 | is computed from the C<perms> table so that its value is an anonymous | ||
| 122 | list of the directories the user has write permission to. A typical | ||
| 123 | structure of such an object would be: | ||
| 124 | |||
| 125 | bless( { | ||
| 126 | 'introduced' => 875601758, | ||
| 127 | 'password' => 'rtthXtbR5tjit', | ||
| 128 | 'fullname' => 'Andreas J. König', | ||
| 129 | 'changedby' => 'andreas', | ||
| 130 | 'lastname' => 'König', | ||
| 131 | 'changedon' => 875601758, | ||
| 132 | 'email' => 'k', | ||
| 133 | 'firstname' => 'Andreas', | ||
| 134 | 'salut' => 'Herr', | ||
| 135 | 'lastlogin' => '0903739665', | ||
| 136 | 'permitted' => [ | ||
| 137 | '/' | ||
| 138 | ], | ||
| 139 | 'user' => 'andreas' | ||
| 140 | }, 'Apache::UploadSvr::User' ) | ||
| 141 | |||
| 142 | The method ->has_perms($obj) returns true if the current user has write | ||
| 143 | access to a file or dircetory. What counts here are database entries, | ||
| 144 | not file system permissions. | ||
| 145 | |||
| 146 | =head1 CONFIGURATION | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | =head1 SECURITY | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | =head1 BUGS | ||
| 155 | |||
| 156 | |||
| 157 | |||
| 158 | =head1 AUTHOR | ||
| 159 | |||
| 160 | Andreas Koenig <koenig@kulturbox.de> | ||
| 161 | |||
| 162 | =head1 COPYRIGHT, LICENSE | ||
| 163 | |||
| 164 | These programs or modules are Copyright (C) 1997-1998 Kulturbox, | ||
| 165 | Berlin, Germany. | ||
| 166 | |||
| 167 | They are free software; you can redistribute them, use and/or modify them | ||
| 168 | under the same terms as Perl itself. | ||
| 169 |