import rt 2.0.14
[freeside.git] / rt / lib / RT / CurrentUser.pm
1 # $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/CurrentUser.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $
2 # (c) 1996-1999 Jesse Vincent <jesse@fsck.com>
3 # This software is redistributable under the terms of the GNU GPL
4
5 =head1 NAME
6
7   RT::CurrentUser - an RT object representing the current user
8
9 =head1 SYNOPSIS
10
11   use RT::CurrentUser
12
13
14 =head1 DESCRIPTION
15
16
17 =head1 METHODS
18
19
20 =begin testing
21
22 ok (require RT::TestHarness);
23 ok (require RT::CurrentUser);
24
25 =end testing
26
27 =cut
28
29
30 package RT::CurrentUser;
31 use RT::Record;
32 @ISA= qw(RT::Record);
33
34
35 # {{{ sub _Init 
36
37 #The basic idea here is that $self->CurrentUser is always supposed
38 # to be a CurrentUser object. but that's hard to do when we're trying to load
39 # the CurrentUser object
40
41 sub _Init  {
42   my $self = shift;
43   my $Name = shift;
44
45   $self->{'table'} = "Users";
46
47   if (defined($Name)) {
48     $self->Load($Name);
49   }
50   
51   $self->_MyCurrentUser($self);
52
53 }
54 # }}}
55
56 # {{{ sub Create
57
58 sub Create {
59     return (0, 'Permission Denied');
60 }
61
62 # }}}
63
64 # {{{ sub Delete
65
66 sub Delete {
67     return (0, 'Permission Denied');
68 }
69
70 # }}}
71
72 # {{{ sub UserObj
73
74 =head2 UserObj
75
76   Returns the RT::User object associated with this CurrentUser object.
77
78 =cut
79
80 sub UserObj {
81     my $self = shift;
82     
83     unless ($self->{'UserObj'}) {
84         use RT::User;
85         $self->{'UserObj'} = RT::User->new($self);
86         unless ($self->{'UserObj'}->Load($self->Id)) {
87             $RT::Logger->err("Couldn't load ".$self->Id. "from the users database.\n");
88         }
89         
90     }
91     return ($self->{'UserObj'});
92 }
93 # }}}
94
95 # {{{ sub _Accessible 
96 sub _Accessible  {
97   my $self = shift;
98   my %Cols = (
99               Name => 'read',
100               Gecos => 'read',
101               RealName => 'read',
102               Password => 'neither',
103               EmailAddress => 'read',
104               Privileged => 'read',
105               IsAdministrator => 'read'
106              );
107   return($self->SUPER::_Accessible(@_, %Cols));
108 }
109 # }}}
110
111 # {{{ sub LoadByEmail
112
113 =head2 LoadByEmail
114
115 Loads a User into this CurrentUser object.
116 Takes the email address of the user to load.
117
118 =cut
119
120 sub LoadByEmail  {
121     my $self = shift;
122     my $identifier = shift;
123         
124     $self->LoadByCol("EmailAddress",$identifier);
125     
126 }
127 # }}}
128
129 # {{{ sub LoadByGecos
130
131 =head2 LoadByGecos
132
133 Loads a User into this CurrentUser object.
134 Takes a unix username as its only argument.
135
136 =cut
137
138 sub LoadByGecos  {
139     my $self = shift;
140     my $identifier = shift;
141         
142     $self->LoadByCol("Gecos",$identifier);
143     
144 }
145 # }}}
146
147 # {{{ sub LoadByName
148
149 =head2 LoadByName
150
151 Loads a User into this CurrentUser object.
152 Takes a Name.
153 =cut
154
155 sub LoadByName {
156     my $self = shift;
157     my $identifier = shift;
158     $self->LoadByCol("Name",$identifier);
159     
160 }
161 # }}}
162
163 # {{{ sub Load 
164
165 =head2 Load
166
167 Loads a User into this CurrentUser object.
168 Takes either an integer (users id column reference) or a Name
169 The latter is deprecated. Instead, you should use LoadByName.
170 Formerly, this routine also took email addresses. 
171
172 =cut
173
174 sub Load  {
175   my $self = shift;
176   my $identifier = shift;
177
178   #if it's an int, load by id. otherwise, load by name.
179   if ($identifier !~ /\D/) {
180     $self->SUPER::LoadById($identifier);
181   }
182   else {
183       # This is a bit dangerous, we might get false authen if somebody
184       # uses ambigous userids or real names:
185       $self->LoadByCol("Name",$identifier);
186   }
187 }
188
189 # }}}
190
191 # {{{ sub IsPassword
192
193 =head2 IsPassword
194
195 Takes a password as a string.  Passes it off to IsPassword in this
196 user's UserObj.  If it is the user's password and the user isn't
197 disabled, returns 1.
198
199 Otherwise, returns undef.
200
201 =cut
202
203 sub IsPassword { 
204   my $self = shift;
205   my $value = shift;
206   
207   return ($self->UserObj->IsPassword($value)); 
208 }
209
210 # }}}
211
212 # {{{ sub Privileged
213
214 =head2 Privileged
215
216 Returns true if the current user can be granted rights and be
217 a member of groups.
218
219 =cut
220
221 sub Privileged {
222     my $self = shift;
223     return ($self->UserObj->Privileged());
224 }
225
226 # }}}
227
228 # {{{ Convenient ACL methods
229
230 =head2 HasQueueRight
231
232 calls $self->UserObj->HasQueueRight with the arguments passed in
233
234 =cut
235
236 sub HasQueueRight {
237         my $self = shift;
238         return ($self->UserObj->HasQueueRight(@_));
239 }
240
241 =head2 HasSystemRight
242
243 calls $self->UserObj->HasSystemRight with the arguments passed in
244
245 =cut
246
247
248 sub HasSystemRight {
249         my $self = shift;
250         return ($self->UserObj->HasSystemRight(@_));
251 }
252 # }}}
253
254 # {{{ sub HasRight
255
256 =head2 HasSystemRight
257
258 calls $self->UserObj->HasRight with the arguments passed in
259
260 =cut
261
262 sub HasRight {
263   my $self = shift;
264   return ($self->UserObj->HasRight(@_));
265 }
266
267 # }}}
268
269 1;
270