1 # {{{ BEGIN BPS TAGGED BLOCK
5 # This software is Copyright (c) 1996-2004 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
48 RT::CurrentUser - an RT object representing the current user
63 ok (require RT::CurrentUser);
70 package RT::CurrentUser;
76 use base qw/RT::Record/;
80 #The basic idea here is that $self->CurrentUser is always supposed
81 # to be a CurrentUser object. but that's hard to do when we're trying to load
82 # the CurrentUser object
88 $self->{'table'} = "Users";
90 if ( defined($User) ) {
92 if ( UNIVERSAL::isa( $User, 'RT::User' )
93 || UNIVERSAL::isa( $User, 'RT::CurrentUser' ) )
95 $self->Load( $User->id );
98 elsif ( ref($User) ) {
100 "RT::CurrentUser->new() called with a bogus argument: $User");
107 $self->_BuildTableAttributes();
116 return (0, $self->loc('Permission Denied'));
125 return (0, $self->loc('Permission Denied'));
134 Returns the RT::User object associated with this CurrentUser object.
142 my $user = RT::User->new($self);
144 unless ($user->Load($self->Id)) {
145 $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id));
151 # {{{ sub PrincipalObj
155 Returns this user's principal object. this is just a helper routine for
156 $self->UserObj->PrincipalObj
162 return($self->UserObj->PrincipalObj);
169 # {{{ sub PrincipalId
173 Returns this user's principal Id. this is just a helper routine for
174 $self->UserObj->PrincipalId
180 return($self->UserObj->PrincipalId);
187 # {{{ sub _Accessible
190 sub _CoreAccessible {
192 Name => { 'read' => 1 },
193 Gecos => { 'read' => 1 },
194 RealName => { 'read' => 1 },
195 Lang => { 'read' => 1 },
196 Password => { 'read' => 0, 'write' => 0 },
197 EmailAddress => { 'read' => 1, 'write' => 0 }
203 # {{{ sub LoadByEmail
207 Loads a User into this CurrentUser object.
208 Takes the email address of the user to load.
214 my $identifier = shift;
216 $identifier = RT::User::CanonicalizeEmailAddress(undef, $identifier);
218 $self->LoadByCol("EmailAddress",$identifier);
223 # {{{ sub LoadByGecos
227 Loads a User into this CurrentUser object.
228 Takes a unix username as its only argument.
234 my $identifier = shift;
236 $self->LoadByCol("Gecos",$identifier);
245 Loads a User into this CurrentUser object.
251 my $identifier = shift;
252 $self->LoadByCol("Name",$identifier);
261 Loads a User into this CurrentUser object.
262 Takes either an integer (users id column reference) or a Name
263 The latter is deprecated. Instead, you should use LoadByName.
264 Formerly, this routine also took email addresses.
270 my $identifier = shift;
272 #if it's an int, load by id. otherwise, load by name.
273 if ($identifier !~ /\D/) {
274 $self->SUPER::LoadById($identifier);
277 elsif (UNIVERSAL::isa($identifier,"RT::User")) {
278 # DWIM if they pass a user in
279 $self->SUPER::LoadById($identifier->Id);
282 # This is a bit dangerous, we might get false authen if somebody
283 # uses ambigous userids or real names:
284 $self->LoadByCol("Name",$identifier);
294 Takes a password as a string. Passes it off to IsPassword in this
295 user's UserObj. If it is the user's password and the user isn't
298 Otherwise, returns undef.
306 return ($self->UserObj->IsPassword($value));
315 Returns true if the current user can be granted rights and be
322 return ($self->UserObj->Privileged());
332 calls $self->UserObj->HasRight with the arguments passed in
338 return ($self->UserObj->HasRight(@_));
345 =head2 LanguageHandle
347 Returns this current user's langauge handle. Should take a language
348 specification. but currently doesn't
352 ok (my $cu = RT::CurrentUser->new('root'));
353 ok (my $lh = $cu->LanguageHandle);
355 ok ($lh->isa('Locale::Maketext'));
356 ok ($cu->loc('TEST_STRING') eq "Concrete Mixer", "Localized TEST_STRING into English");
357 ok ($lh = $cu->LanguageHandle('fr'));
358 ok ($cu->loc('Before') eq "Avant", "Localized TEST_STRING into Frenc");
366 if ( ( !defined $self->{'LangHandle'} )
367 || ( !UNIVERSAL::can( $self->{'LangHandle'}, 'maketext' ) )
369 if ( (!$RT::SystemUser || $self->id == $RT::SystemUser->id() )) {
373 elsif ( $self->Lang ) {
374 push @_, $self->Lang;
376 $self->{'LangHandle'} = RT::I18N->get_handle(@_);
379 # Fall back to english.
380 unless ( $self->{'LangHandle'} ) {
381 die "We couldn't get a dictionary. Nye mogu naidti slovar. No puedo encontrar dictionario.";
383 return ( $self->{'LangHandle'} );
388 return '' if $_[0] eq '';
390 my $handle = $self->LanguageHandle;
393 # pre-scan the lexicon hashes to return _AUTO keys verbatim,
394 # to keep locstrings containing '[' and '~' from tripping over Maketext
395 return $_[0] unless grep { exists $_->{$_[0]} } @{ $handle->_lex_refs };
398 return $handle->maketext(@_);
403 return '' if $_[0] eq '';
405 # XXX: work around perl's deficiency when matching utf8 data
406 return $_[0] if Encode::is_utf8($_[0]);
407 my $result = $self->LanguageHandle->maketext_fuzzy(@_);
416 Return the current currentuser object
427 eval "require RT::CurrentUser_Vendor";
428 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm});
429 eval "require RT::CurrentUser_Local";
430 die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Local.pm});