3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
26 RT::CurrentUser - an RT object representing the current user
41 ok (require RT::CurrentUser);
48 package RT::CurrentUser;
59 #The basic idea here is that $self->CurrentUser is always supposed
60 # to be a CurrentUser object. but that's hard to do when we're trying to load
61 # the CurrentUser object
67 $self->{'table'} = "Users";
73 # $self->CurrentUser($self);
82 return (0, $self->loc('Permission Denied'));
91 return (0, $self->loc('Permission Denied'));
100 Returns the RT::User object associated with this CurrentUser object.
108 my $user = RT::User->new($self);
110 unless ($user->Load($self->Id)) {
111 $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id));
117 # {{{ sub PrincipalObj
121 Returns this user's principal object. this is just a helper routine for
122 $self->UserObj->PrincipalObj
128 return($self->UserObj->PrincipalObj);
135 # {{{ sub PrincipalId
139 Returns this user's principal Id. this is just a helper routine for
140 $self->UserObj->PrincipalId
146 return($self->UserObj->PrincipalId);
153 # {{{ sub _Accessible
160 Password => 'neither',
162 EmailAddress => 'read',
163 Privileged => 'read',
164 IsAdministrator => 'read'
166 return($self->SUPER::_Accessible(@_, %Cols));
170 # {{{ sub LoadByEmail
174 Loads a User into this CurrentUser object.
175 Takes the email address of the user to load.
181 my $identifier = shift;
183 $identifier = RT::User::CanonicalizeEmailAddress(undef, $identifier);
185 $self->LoadByCol("EmailAddress",$identifier);
190 # {{{ sub LoadByGecos
194 Loads a User into this CurrentUser object.
195 Takes a unix username as its only argument.
201 my $identifier = shift;
203 $self->LoadByCol("Gecos",$identifier);
212 Loads a User into this CurrentUser object.
218 my $identifier = shift;
219 $self->LoadByCol("Name",$identifier);
228 Loads a User into this CurrentUser object.
229 Takes either an integer (users id column reference) or a Name
230 The latter is deprecated. Instead, you should use LoadByName.
231 Formerly, this routine also took email addresses.
237 my $identifier = shift;
239 #if it's an int, load by id. otherwise, load by name.
240 if ($identifier !~ /\D/) {
241 $self->SUPER::LoadById($identifier);
244 elsif (UNIVERSAL::isa($identifier,"RT::User")) {
245 # DWIM if they pass a user in
246 $self->SUPER::LoadById($identifier->Id);
249 # This is a bit dangerous, we might get false authen if somebody
250 # uses ambigous userids or real names:
251 $self->LoadByCol("Name",$identifier);
261 Takes a password as a string. Passes it off to IsPassword in this
262 user's UserObj. If it is the user's password and the user isn't
265 Otherwise, returns undef.
273 return ($self->UserObj->IsPassword($value));
282 Returns true if the current user can be granted rights and be
289 return ($self->UserObj->Privileged());
299 calls $self->UserObj->HasRight with the arguments passed in
305 return ($self->UserObj->HasRight(@_));
312 =head2 LanguageHandle
314 Returns this current user's langauge handle. Should take a language
315 specification. but currently doesn't
319 ok (my $cu = RT::CurrentUser->new('root'));
320 ok (my $lh = $cu->LanguageHandle);
322 ok ($lh->isa('Locale::Maketext'));
323 ok ($cu->loc('TEST_STRING') eq "Concrete Mixer", "Localized TEST_STRING into English");
324 ok ($lh = $cu->LanguageHandle('fr'));
325 ok ($cu->loc('Before') eq "Avant", "Localized TEST_STRING into Frenc");
333 if ( ( !defined $self->{'LangHandle'} )
334 || ( !UNIVERSAL::can( $self->{'LangHandle'}, 'maketext' ) )
336 if ( (!$RT::SystemUser || $self->id == $RT::SystemUser->id() )) {
340 elsif ( $self->Lang ) {
341 push @_, $self->Lang;
343 $self->{'LangHandle'} = RT::I18N->get_handle(@_);
346 # Fall back to english.
347 unless ( $self->{'LangHandle'} ) {
348 die "We couldn't get a dictionary. Nye mogu naidti slovar. No puedo encontrar dictionario.";
350 return ( $self->{'LangHandle'} );
355 return '' if $_[0] eq '';
357 my $handle = $self->LanguageHandle;
360 # pre-scan the lexicon hashes to return _AUTO keys verbatim,
361 # to keep locstrings containing '[' and '~' from tripping over Maketext
362 return $_[0] unless grep { exists $_->{$_[0]} } @{ $handle->_lex_refs };
365 return $handle->maketext(@_);
370 return '' if $_[0] eq '';
372 # XXX: work around perl's deficiency when matching utf8 data
373 return $_[0] if Encode::is_utf8($_[0]);
374 my $result = $self->LanguageHandle->maketext_fuzzy(@_);
383 Return the current currentuser object
393 eval "require RT::CurrentUser_Vendor";
394 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm});
395 eval "require RT::CurrentUser_Local";
396 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Local.pm});