From 3ef62a0570055da710328937e7f65dbb2c027c62 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 12 Aug 2002 06:17:09 +0000 Subject: import rt 2.0.14 --- rt/lib/RT/CurrentUser.pm | 270 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100755 rt/lib/RT/CurrentUser.pm (limited to 'rt/lib/RT/CurrentUser.pm') diff --git a/rt/lib/RT/CurrentUser.pm b/rt/lib/RT/CurrentUser.pm new file mode 100755 index 000000000..6997ddbac --- /dev/null +++ b/rt/lib/RT/CurrentUser.pm @@ -0,0 +1,270 @@ +# $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/CurrentUser.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $ +# (c) 1996-1999 Jesse Vincent +# This software is redistributable under the terms of the GNU GPL + +=head1 NAME + + RT::CurrentUser - an RT object representing the current user + +=head1 SYNOPSIS + + use RT::CurrentUser + + +=head1 DESCRIPTION + + +=head1 METHODS + + +=begin testing + +ok (require RT::TestHarness); +ok (require RT::CurrentUser); + +=end testing + +=cut + + +package RT::CurrentUser; +use RT::Record; +@ISA= qw(RT::Record); + + +# {{{ sub _Init + +#The basic idea here is that $self->CurrentUser is always supposed +# to be a CurrentUser object. but that's hard to do when we're trying to load +# the CurrentUser object + +sub _Init { + my $self = shift; + my $Name = shift; + + $self->{'table'} = "Users"; + + if (defined($Name)) { + $self->Load($Name); + } + + $self->_MyCurrentUser($self); + +} +# }}} + +# {{{ sub Create + +sub Create { + return (0, 'Permission Denied'); +} + +# }}} + +# {{{ sub Delete + +sub Delete { + return (0, 'Permission Denied'); +} + +# }}} + +# {{{ sub UserObj + +=head2 UserObj + + Returns the RT::User object associated with this CurrentUser object. + +=cut + +sub UserObj { + my $self = shift; + + unless ($self->{'UserObj'}) { + use RT::User; + $self->{'UserObj'} = RT::User->new($self); + unless ($self->{'UserObj'}->Load($self->Id)) { + $RT::Logger->err("Couldn't load ".$self->Id. "from the users database.\n"); + } + + } + return ($self->{'UserObj'}); +} +# }}} + +# {{{ sub _Accessible +sub _Accessible { + my $self = shift; + my %Cols = ( + Name => 'read', + Gecos => 'read', + RealName => 'read', + Password => 'neither', + EmailAddress => 'read', + Privileged => 'read', + IsAdministrator => 'read' + ); + return($self->SUPER::_Accessible(@_, %Cols)); +} +# }}} + +# {{{ sub LoadByEmail + +=head2 LoadByEmail + +Loads a User into this CurrentUser object. +Takes the email address of the user to load. + +=cut + +sub LoadByEmail { + my $self = shift; + my $identifier = shift; + + $self->LoadByCol("EmailAddress",$identifier); + +} +# }}} + +# {{{ sub LoadByGecos + +=head2 LoadByGecos + +Loads a User into this CurrentUser object. +Takes a unix username as its only argument. + +=cut + +sub LoadByGecos { + my $self = shift; + my $identifier = shift; + + $self->LoadByCol("Gecos",$identifier); + +} +# }}} + +# {{{ sub LoadByName + +=head2 LoadByName + +Loads a User into this CurrentUser object. +Takes a Name. +=cut + +sub LoadByName { + my $self = shift; + my $identifier = shift; + $self->LoadByCol("Name",$identifier); + +} +# }}} + +# {{{ sub Load + +=head2 Load + +Loads a User into this CurrentUser object. +Takes either an integer (users id column reference) or a Name +The latter is deprecated. Instead, you should use LoadByName. +Formerly, this routine also took email addresses. + +=cut + +sub Load { + my $self = shift; + my $identifier = shift; + + #if it's an int, load by id. otherwise, load by name. + if ($identifier !~ /\D/) { + $self->SUPER::LoadById($identifier); + } + else { + # This is a bit dangerous, we might get false authen if somebody + # uses ambigous userids or real names: + $self->LoadByCol("Name",$identifier); + } +} + +# }}} + +# {{{ sub IsPassword + +=head2 IsPassword + +Takes a password as a string. Passes it off to IsPassword in this +user's UserObj. If it is the user's password and the user isn't +disabled, returns 1. + +Otherwise, returns undef. + +=cut + +sub IsPassword { + my $self = shift; + my $value = shift; + + return ($self->UserObj->IsPassword($value)); +} + +# }}} + +# {{{ sub Privileged + +=head2 Privileged + +Returns true if the current user can be granted rights and be +a member of groups. + +=cut + +sub Privileged { + my $self = shift; + return ($self->UserObj->Privileged()); +} + +# }}} + +# {{{ Convenient ACL methods + +=head2 HasQueueRight + +calls $self->UserObj->HasQueueRight with the arguments passed in + +=cut + +sub HasQueueRight { + my $self = shift; + return ($self->UserObj->HasQueueRight(@_)); +} + +=head2 HasSystemRight + +calls $self->UserObj->HasSystemRight with the arguments passed in + +=cut + + +sub HasSystemRight { + my $self = shift; + return ($self->UserObj->HasSystemRight(@_)); +} +# }}} + +# {{{ sub HasRight + +=head2 HasSystemRight + +calls $self->UserObj->HasRight with the arguments passed in + +=cut + +sub HasRight { + my $self = shift; + return ($self->UserObj->HasRight(@_)); +} + +# }}} + +1; + -- cgit v1.2.1 From 945721f48f74d5cfffef7c7cf3a3d6bc2521f5dd Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 15 Jul 2003 13:16:32 +0000 Subject: import of rt 3.0.4 --- rt/lib/RT/CurrentUser.pm | 166 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 135 insertions(+), 31 deletions(-) (limited to 'rt/lib/RT/CurrentUser.pm') diff --git a/rt/lib/RT/CurrentUser.pm b/rt/lib/RT/CurrentUser.pm index 6997ddbac..4ca2f9891 100755 --- a/rt/lib/RT/CurrentUser.pm +++ b/rt/lib/RT/CurrentUser.pm @@ -1,7 +1,26 @@ -# $Header: /home/cvs/cvsroot/freeside/rt/lib/RT/CurrentUser.pm,v 1.1 2002-08-12 06:17:07 ivan Exp $ -# (c) 1996-1999 Jesse Vincent -# This software is redistributable under the terms of the GNU GPL - +# BEGIN LICENSE BLOCK +# +# Copyright (c) 1996-2003 Jesse Vincent +# +# (Except where explictly superceded by other copyright notices) +# +# This work is made available to you under the terms of Version 2 of +# the GNU General Public License. A copy of that license should have +# been provided with this software, but in any event can be snarfed +# from www.gnu.org. +# +# This work is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# Unless otherwise specified, all modifications, corrections or +# extensions to this work which alter its source code become the +# property of Best Practical Solutions, LLC when submitted for +# inclusion in the work. +# +# +# END LICENSE BLOCK =head1 NAME RT::CurrentUser - an RT object representing the current user @@ -19,7 +38,6 @@ =begin testing -ok (require RT::TestHarness); ok (require RT::CurrentUser); =end testing @@ -28,9 +46,13 @@ ok (require RT::CurrentUser); package RT::CurrentUser; + use RT::Record; -@ISA= qw(RT::Record); +use RT::I18N; +use strict; +use vars qw/@ISA/; +@ISA= qw(RT::Record); # {{{ sub _Init @@ -48,7 +70,7 @@ sub _Init { $self->Load($Name); } - $self->_MyCurrentUser($self); + $self->CurrentUser($self); } # }}} @@ -56,7 +78,8 @@ sub _Init { # {{{ sub Create sub Create { - return (0, 'Permission Denied'); + my $self = shift; + return (0, $self->loc('Permission Denied')); } # }}} @@ -64,7 +87,8 @@ sub Create { # {{{ sub Delete sub Delete { - return (0, 'Permission Denied'); + my $self = shift; + return (0, $self->loc('Permission Denied')); } # }}} @@ -84,7 +108,7 @@ sub UserObj { use RT::User; $self->{'UserObj'} = RT::User->new($self); unless ($self->{'UserObj'}->Load($self->Id)) { - $RT::Logger->err("Couldn't load ".$self->Id. "from the users database.\n"); + $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id)); } } @@ -92,6 +116,42 @@ sub UserObj { } # }}} +# {{{ sub PrincipalObj + +=head2 PrincipalObj + + Returns this user's principal object. this is just a helper routine for + $self->UserObj->PrincipalObj + +=cut + +sub PrincipalObj { + my $self = shift; + return($self->UserObj->PrincipalObj); +} + + +# }}} + + +# {{{ sub PrincipalId + +=head2 PrincipalId + + Returns this user's principal Id. this is just a helper routine for + $self->UserObj->PrincipalId + +=cut + +sub PrincipalId { + my $self = shift; + return($self->UserObj->PrincipalId); +} + + +# }}} + + # {{{ sub _Accessible sub _Accessible { my $self = shift; @@ -120,6 +180,8 @@ Takes the email address of the user to load. sub LoadByEmail { my $self = shift; my $identifier = shift; + + $identifier = RT::User::CanonicalizeEmailAddress(undef, $identifier); $self->LoadByCol("EmailAddress",$identifier); @@ -225,46 +287,88 @@ sub Privileged { # }}} -# {{{ Convenient ACL methods -=head2 HasQueueRight +# {{{ sub HasRight -calls $self->UserObj->HasQueueRight with the arguments passed in +=head2 HasRight + +calls $self->UserObj->HasRight with the arguments passed in =cut -sub HasQueueRight { - my $self = shift; - return ($self->UserObj->HasQueueRight(@_)); +sub HasRight { + my $self = shift; + return ($self->UserObj->HasRight(@_)); } -=head2 HasSystemRight +# }}} -calls $self->UserObj->HasSystemRight with the arguments passed in +# {{{ Localization -=cut +=head2 LanguageHandle +Returns this current user's langauge handle. Should take a language +specification. but currently doesn't -sub HasSystemRight { - my $self = shift; - return ($self->UserObj->HasSystemRight(@_)); -} -# }}} +=begin testing -# {{{ sub HasRight +ok (my $cu = RT::CurrentUser->new('root')); +ok (my $lh = $cu->LanguageHandle); +ok ($lh != undef); +ok ($lh->isa('Locale::Maketext')); +ok ($cu->loc('TEST_STRING') eq "Concrete Mixer", "Localized TEST_STRING into English"); +ok ($lh = $cu->LanguageHandle('fr')); +ok ($cu->loc('Before') eq "Avant", "Localized TEST_STRING into Frenc"); -=head2 HasSystemRight +=end testing -calls $self->UserObj->HasRight with the arguments passed in +=cut -=cut +sub LanguageHandle { + my $self = shift; + if ((!defined $self->{'LangHandle'}) || + (!UNIVERSAL::can($self->{'LangHandle'}, 'maketext')) || + (@_)) { + $self->{'LangHandle'} = RT::I18N->get_handle(@_); + } + # Fall back to english. + unless ($self->{'LangHandle'}) { + die "We couldn't get a dictionary. Nye mogu naidti slovar. No puedo encontrar dictionario."; + } + return ($self->{'LangHandle'}); +} -sub HasRight { - my $self = shift; - return ($self->UserObj->HasRight(@_)); +sub loc { + my $self = shift; + return '' if $_[0] eq ''; + + my $handle = $self->LanguageHandle; + + if (@_ == 1) { + # pre-scan the lexicon hashes to return _AUTO keys verbatim, + # to keep locstrings containing '[' and '~' from tripping over Maketext + return $_[0] unless grep { exists $_->{$_[0]} } @{ $handle->_lex_refs }; + } + + return $handle->maketext(@_); } +sub loc_fuzzy { + my $self = shift; + return '' if $_[0] eq ''; + + # XXX: work around perl's deficiency when matching utf8 data + return $_[0] if Encode::is_utf8($_[0]); + my $result = $self->LanguageHandle->maketext_fuzzy(@_); + + return($result); +} # }}} +eval "require RT::CurrentUser_Vendor"; +die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm}); +eval "require RT::CurrentUser_Local"; +die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Local.pm}); + 1; -- cgit v1.2.1 From 289340780927b5bac2c7604d7317c3063c6dd8cc Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 11 Mar 2004 02:05:38 +0000 Subject: import of rt 3.0.9 --- rt/lib/RT/CurrentUser.pm | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'rt/lib/RT/CurrentUser.pm') diff --git a/rt/lib/RT/CurrentUser.pm b/rt/lib/RT/CurrentUser.pm index 4ca2f9891..7fcc65ce3 100755 --- a/rt/lib/RT/CurrentUser.pm +++ b/rt/lib/RT/CurrentUser.pm @@ -70,7 +70,7 @@ sub _Init { $self->Load($Name); } - $self->CurrentUser($self); + # $self->CurrentUser($self); } # }}} @@ -104,15 +104,13 @@ sub Delete { sub UserObj { my $self = shift; - unless ($self->{'UserObj'}) { use RT::User; - $self->{'UserObj'} = RT::User->new($self); - unless ($self->{'UserObj'}->Load($self->Id)) { + my $user = RT::User->new($self); + + unless ($user->Load($self->Id)) { $RT::Logger->err($self->loc("Couldn't load [_1] from the users database.\n", $self->Id)); } - - } - return ($self->{'UserObj'}); + return ($user); } # }}} @@ -160,6 +158,7 @@ sub _Accessible { Gecos => 'read', RealName => 'read', Password => 'neither', + Lang => 'read', EmailAddress => 'read', Privileged => 'read', IsAdministrator => 'read' @@ -241,6 +240,11 @@ sub Load { if ($identifier !~ /\D/) { $self->SUPER::LoadById($identifier); } + + elsif (UNIVERSAL::isa($identifier,"RT::User")) { + # DWIM if they pass a user in + $self->SUPER::LoadById($identifier->Id); + } else { # This is a bit dangerous, we might get false authen if somebody # uses ambigous userids or real names: @@ -329,6 +333,9 @@ sub LanguageHandle { if ((!defined $self->{'LangHandle'}) || (!UNIVERSAL::can($self->{'LangHandle'}, 'maketext')) || (@_)) { + if ( $self->Lang) { + push @_, $self->Lang; + } $self->{'LangHandle'} = RT::I18N->get_handle(@_); } # Fall back to english. @@ -365,6 +372,19 @@ sub loc_fuzzy { } # }}} + +=head2 CurrentUser + +Return the current currentuser object + +=cut + +sub CurrentUser { + my $self = shift; + return($self); + +} + eval "require RT::CurrentUser_Vendor"; die $@ if ($@ && $@ !~ qr{^Can't locate RT/CurrentUser_Vendor.pm}); eval "require RT::CurrentUser_Local"; -- cgit v1.2.1