1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
49 RT::CurrentUser - an RT object representing the current user
64 ok (require RT::CurrentUser);
71 package RT::CurrentUser;
77 use base qw/RT::Record/;
81 #The basic idea here is that $self->CurrentUser is always supposed
82 # to be a CurrentUser object. but that's hard to do when we're trying to load
83 # the CurrentUser object
89 $self->{'table'} = "Users";
91 if ( defined($User) ) {
93 if ( UNIVERSAL::isa( $User, 'RT::User' )
94 || UNIVERSAL::isa( $User, 'RT::CurrentUser' ) )
96 $self->Load( $User->id );
99 elsif ( ref($User) ) {
101 "RT::CurrentUser->new() called with a bogus argument: $User");
108 $self->_BuildTableAttributes();
117 return (0, $self->loc('Permission Denied'));
126 return (0, $self->loc('Permission Denied'));
135 Returns the RT::User object associated with this CurrentUser object.
143 my $user = RT::User->new($self);
145 unless ($user->Load($self->Id)) {
146 $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id));
152 # {{{ sub PrincipalObj
156 Returns this user's principal object. this is just a helper routine for
157 $self->UserObj->PrincipalObj
163 return($self->UserObj->PrincipalObj);
170 # {{{ sub PrincipalId
174 Returns this user's principal Id. this is just a helper routine for
175 $self->UserObj->PrincipalId
181 return($self->UserObj->PrincipalId);
188 # {{{ sub _Accessible
191 sub _CoreAccessible {
193 Name => { 'read' => 1 },
194 Gecos => { 'read' => 1 },
195 RealName => { 'read' => 1 },
196 Lang => { 'read' => 1 },
197 Password => { 'read' => 0, 'write' => 0 },
198 EmailAddress => { 'read' => 1, 'write' => 0 }
204 # {{{ sub LoadByEmail
208 Loads a User into this CurrentUser object.
209 Takes the email address of the user to load.
215 my $identifier = shift;
217 $identifier = RT::User::CanonicalizeEmailAddress(undef, $identifier);
219 $self->LoadByCol("EmailAddress",$identifier);
224 # {{{ sub LoadByGecos
228 Loads a User into this CurrentUser object.
229 Takes a unix username as its only argument.
235 my $identifier = shift;
237 $self->LoadByCol("Gecos",$identifier);
246 Loads a User into this CurrentUser object.
253 my $identifier = shift;
254 $self->LoadByCol("Name",$identifier);
263 Loads a User into this CurrentUser object.
264 Takes either an integer (users id column reference) or a Name
265 The latter is deprecated. Instead, you should use LoadByName.
266 Formerly, this routine also took email addresses.
272 my $identifier = shift;
274 #if it's an int, load by id. otherwise, load by name.
275 if ($identifier !~ /\D/) {
276 $self->SUPER::LoadById($identifier);
279 elsif (UNIVERSAL::isa($identifier,"RT::User")) {
280 # DWIM if they pass a user in
281 $self->SUPER::LoadById($identifier->Id);
284 # This is a bit dangerous, we might get false authen if somebody
285 # uses ambigous userids or real names:
286 $self->LoadByCol("Name",$identifier);
296 Takes a password as a string. Passes it off to IsPassword in this
297 user's UserObj. If it is the user's password and the user isn't
300 Otherwise, returns undef.
308 return ($self->UserObj->IsPassword($value));
317 Returns true if the current user can be granted rights and be
324 return ($self->UserObj->Privileged());
334 calls $self->UserObj->HasRight with the arguments passed in
340 return ($self->UserObj->HasRight(@_));
347 =head2 LanguageHandle
349 Returns this current user's langauge handle. Should take a language
350 specification. but currently doesn't
354 ok (my $cu = RT::CurrentUser->new('root'));
355 ok (my $lh = $cu->LanguageHandle('en-us'));
357 ok ($lh->isa('Locale::Maketext'));
358 is ($cu->loc('TEST_STRING'), "Concrete Mixer", "Localized TEST_STRING into English");
359 ok ($lh = $cu->LanguageHandle('fr'));
360 is ($cu->loc('Before'), "Avant", "Localized TEST_STRING into Frenc");
368 if ( ( !defined $self->{'LangHandle'} )
369 || ( !UNIVERSAL::can( $self->{'LangHandle'}, 'maketext' ) )
371 if ( !$RT::SystemUser or ($self->id || 0) == $RT::SystemUser->id() ) {
375 elsif ( $self->Lang ) {
376 push @_, $self->Lang;
378 $self->{'LangHandle'} = RT::I18N->get_handle(@_);
381 # Fall back to english.
382 unless ( $self->{'LangHandle'} ) {
383 die "We couldn't get a dictionary. Nye mogu naidti slovar. No puedo encontrar dictionario.";
385 return ( $self->{'LangHandle'} );
390 return '' if $_[0] eq '';
392 my $handle = $self->LanguageHandle;
395 # pre-scan the lexicon hashes to return _AUTO keys verbatim,
396 # to keep locstrings containing '[' and '~' from tripping over Maketext
397 return $_[0] unless grep { exists $_->{$_[0]} } @{ $handle->_lex_refs };
400 return $handle->maketext(@_);
405 return '' if (!$_[0] || $_[0] eq '');
407 # XXX: work around perl's deficiency when matching utf8 data
408 return $_[0] if Encode::is_utf8($_[0]);
409 my $result = $self->LanguageHandle->maketext_fuzzy(@_);
418 Return the current currentuser object
430 Takes $password, $created and $nonce, and returns a boolean value
431 representing whether the authentication succeeded.
433 If both $nonce and $created are specified, validate $password against:
438 sha1_hex( "$username:$realm:$server_pass" )
441 where $server_pass is the md5_hex(password) digest stored in the
442 database, $created is in ISO time format, and $nonce is a random
443 string no longer than 32 bytes.
448 my ($self, $password, $created, $nonce, $realm) = @_;
451 require Digest::SHA1;
452 require MIME::Base64;
454 my $username = $self->UserObj->Name or return;
455 my $server_pass = $self->UserObj->__Value('Password') or return;
456 my $auth_digest = MIME::Base64::encode_base64(Digest::SHA1::sha1(
459 Digest::MD5::md5_hex("$username:$realm:$server_pass")
465 return ($password eq $auth_digest);
471 eval "require RT::CurrentUser_Vendor";
472 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm});
473 eval "require RT::CurrentUser_Local";
474 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Local.pm});