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.
107 unless ($self->{'UserObj'}) {
109 $self->{'UserObj'} = RT::User->new($self);
110 unless ($self->{'UserObj'}->Load($self->Id)) {
111 $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id));
115 return ($self->{'UserObj'});
119 # {{{ sub PrincipalObj
123 Returns this user's principal object. this is just a helper routine for
124 $self->UserObj->PrincipalObj
130 return($self->UserObj->PrincipalObj);
137 # {{{ sub PrincipalId
141 Returns this user's principal Id. this is just a helper routine for
142 $self->UserObj->PrincipalId
148 return($self->UserObj->PrincipalId);
155 # {{{ sub _Accessible
162 Password => 'neither',
163 EmailAddress => 'read',
164 Privileged => 'read',
165 IsAdministrator => 'read'
167 return($self->SUPER::_Accessible(@_, %Cols));
171 # {{{ sub LoadByEmail
175 Loads a User into this CurrentUser object.
176 Takes the email address of the user to load.
182 my $identifier = shift;
184 $identifier = RT::User::CanonicalizeEmailAddress(undef, $identifier);
186 $self->LoadByCol("EmailAddress",$identifier);
191 # {{{ sub LoadByGecos
195 Loads a User into this CurrentUser object.
196 Takes a unix username as its only argument.
202 my $identifier = shift;
204 $self->LoadByCol("Gecos",$identifier);
213 Loads a User into this CurrentUser object.
219 my $identifier = shift;
220 $self->LoadByCol("Name",$identifier);
229 Loads a User into this CurrentUser object.
230 Takes either an integer (users id column reference) or a Name
231 The latter is deprecated. Instead, you should use LoadByName.
232 Formerly, this routine also took email addresses.
238 my $identifier = shift;
240 #if it's an int, load by id. otherwise, load by name.
241 if ($identifier !~ /\D/) {
242 $self->SUPER::LoadById($identifier);
245 # This is a bit dangerous, we might get false authen if somebody
246 # uses ambigous userids or real names:
247 $self->LoadByCol("Name",$identifier);
257 Takes a password as a string. Passes it off to IsPassword in this
258 user's UserObj. If it is the user's password and the user isn't
261 Otherwise, returns undef.
269 return ($self->UserObj->IsPassword($value));
278 Returns true if the current user can be granted rights and be
285 return ($self->UserObj->Privileged());
295 calls $self->UserObj->HasRight with the arguments passed in
301 return ($self->UserObj->HasRight(@_));
308 =head2 LanguageHandle
310 Returns this current user's langauge handle. Should take a language
311 specification. but currently doesn't
315 ok (my $cu = RT::CurrentUser->new('root'));
316 ok (my $lh = $cu->LanguageHandle);
318 ok ($lh->isa('Locale::Maketext'));
319 ok ($cu->loc('TEST_STRING') eq "Concrete Mixer", "Localized TEST_STRING into English");
320 ok ($lh = $cu->LanguageHandle('fr'));
321 ok ($cu->loc('Before') eq "Avant", "Localized TEST_STRING into Frenc");
329 if ((!defined $self->{'LangHandle'}) ||
330 (!UNIVERSAL::can($self->{'LangHandle'}, 'maketext')) ||
332 $self->{'LangHandle'} = RT::I18N->get_handle(@_);
334 # Fall back to english.
335 unless ($self->{'LangHandle'}) {
336 die "We couldn't get a dictionary. Nye mogu naidti slovar. No puedo encontrar dictionario.";
338 return ($self->{'LangHandle'});
343 return '' if $_[0] eq '';
345 my $handle = $self->LanguageHandle;
348 # pre-scan the lexicon hashes to return _AUTO keys verbatim,
349 # to keep locstrings containing '[' and '~' from tripping over Maketext
350 return $_[0] unless grep { exists $_->{$_[0]} } @{ $handle->_lex_refs };
353 return $handle->maketext(@_);
358 return '' if $_[0] eq '';
360 # XXX: work around perl's deficiency when matching utf8 data
361 return $_[0] if Encode::is_utf8($_[0]);
362 my $result = $self->LanguageHandle->maketext_fuzzy(@_);
368 eval "require RT::CurrentUser_Vendor";
369 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm});
370 eval "require RT::CurrentUser_Local";
371 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Local.pm});