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::Groups - a collection of RT::Group objects
54 my $groups = $RT::Groups->new($CurrentUser);
56 while (my $group = $groups->Next()) {
57 print $group->Id ." is a group id\n";
68 ok (require RT::Groups);
78 no warnings qw(redefine);
82 # XXX: below some code is marked as subject to generalize in Groups, Users classes.
83 # RUZ suggest name Principals::Generic or Principals::Base as abstract class, but
84 # Jesse wants something that doesn't imply it's a Principals.pm subclass.
85 # See comments below for candidats.
93 # Groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => xx );
94 my $g = RT::Group->new($RT::SystemUser);
95 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'GroupsNotEqualTest');
96 ok ($id, "created group #". $g->id) or diag("error: $msg");
98 my $groups = RT::Groups->new($RT::SystemUser);
99 $groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => $g->id );
100 $groups->LimitToUserDefinedGroups();
101 my $bug = grep $_->id == $g->id, @{$groups->ItemsArrayRef};
102 ok (!$bug, "didn't find group");
110 $self->{'table'} = "Groups";
111 $self->{'primary_key'} = "id";
113 my @result = $self->SUPER::_Init(@_);
115 $self->OrderBy( ALIAS => 'main',
119 # XXX: this code should be generalized
120 $self->{'princalias'} = $self->Join(
123 TABLE2 => 'Principals',
127 # even if this condition is useless and ids in the Groups table
128 # only match principals with type 'Group' this could speed up
129 # searches in some DBs.
130 $self->Limit( ALIAS => $self->{'princalias'},
131 FIELD => 'PrincipalType',
139 =head2 PrincipalsAlias
141 Returns the string that represents this Users object's primary "Principals" alias.
145 # XXX: should be generalized, code duplication
146 sub PrincipalsAlias {
148 return($self->{'princalias'});
153 # {{{ LimiToSystemInternalGroups
155 =head2 LimitToSystemInternalGroups
157 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
162 sub LimitToSystemInternalGroups {
164 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
165 # All system internal groups have the same instance. No reason to limit down further
166 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
172 # {{{ LimiToUserDefinedGroups
174 =head2 LimitToUserDefined Groups
176 Return only UserDefined Groups
181 sub LimitToUserDefinedGroups {
183 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
184 # All user-defined groups have the same instance. No reason to limit down further
185 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
191 # {{{ LimiToPersonalGroups
193 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
195 Return only Personal Groups for the user whose principal id
201 sub LimitToPersonalGroupsFor {
205 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
206 $self->Limit( FIELD => 'Instance',
214 # {{{ LimitToRolesForQueue
216 =head2 LimitToRolesForQueue QUEUE_ID
218 Limits the set of groups found to role groups for queue QUEUE_ID
222 sub LimitToRolesForQueue {
225 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
226 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
231 # {{{ LimitToRolesForTicket
233 =head2 LimitToRolesForTicket Ticket_ID
235 Limits the set of groups found to role groups for Ticket Ticket_ID
239 sub LimitToRolesForTicket {
242 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
243 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
248 # {{{ LimitToRolesForSystem
250 =head2 LimitToRolesForSystem System_ID
252 Limits the set of groups found to role groups for System System_ID
256 sub LimitToRolesForSystem {
258 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
263 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
265 Limits the set of groups returned to groups which have
266 Principal PRINCIPAL_ID as a member
270 my $u = RT::User->new($RT::SystemUser);
271 $u->Create(Name => 'Membertests');
272 my $g = RT::Group->new($RT::SystemUser);
273 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
276 my ($aid, $amsg) =$g->AddMember($u->id);
278 ok($g->HasMember($u->PrincipalObj),"G has member u");
280 my $groups = RT::Groups->new($RT::SystemUser);
281 $groups->LimitToUserDefinedGroups();
282 $groups->WithMember(PrincipalId => $u->id);
283 ok ($groups->Count == 1,"found the 1 group - " . $groups->Count);
284 ok ($groups->First->Id == $g->Id, "it's the right one");
293 my %args = ( PrincipalId => undef,
294 Recursively => undef,
298 if ($args{'Recursively'}) {
299 $members = $self->NewAlias('CachedGroupMembers');
301 $members = $self->NewAlias('GroupMembers');
303 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
304 ALIAS2 => $members, FIELD2 => 'GroupId');
306 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
310 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
313 Find all groups which have RIGHTNAME for RT::Record. Optionally include global rights and superusers. By default, include the global rights, but not the superusers.
317 my $q = RT::Queue->new($RT::SystemUser);
318 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
321 my $testuser = RT::User->new($RT::SystemUser);
322 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
325 my $global_admin_cc = RT::Group->new($RT::SystemUser);
326 $global_admin_cc->LoadSystemRoleGroup('AdminCc');
327 ok($global_admin_cc->id, "Found the global admincc group");
328 my $groups = RT::Groups->new($RT::SystemUser);
329 $groups->WithRight(Right => 'OwnTicket', Object => $q);
330 is($groups->Count, 1);
331 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> $RT::System);
333 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
334 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
336 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
338 $groups = RT::Groups->new($RT::SystemUser);
339 $groups->WithRight(Right => 'OwnTicket', Object => $q);
341 is($groups->Count, 3);
343 my $RTxGroup = RT::Group->new($RT::SystemUser);
344 ($id, $msg) = $RTxGroup->CreateUserDefinedGroup( Name => 'RTxGroup', Description => "RTx extension group");
348 bless $RTxSysObj, 'RTx::System';
349 *RTx::System::Id = sub { 1; };
350 *RTx::System::id = *RTx::System::Id;
351 my $ace = RT::Record->new($RT::SystemUser);
353 $ace->_BuildTableAttributes unless ($_TABLE_ATTR->{ref($self)});
354 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System', ObjectId => 1);
355 ok ($id, "ACL for RTxSysObj created");
358 bless $RTxObj, 'RTx::System::Record';
359 *RTx::System::Record::Id = sub { 4; };
360 *RTx::System::Record::id = *RTx::System::Record::Id;
362 $groups = RT::Groups->new($RT::SystemUser);
363 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxSysObj);
364 is($groups->Count, 1, "RTxGroupRight found for RTxSysObj");
366 $groups = RT::Groups->new($RT::SystemUser);
367 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj);
368 is($groups->Count, 0, "RTxGroupRight not found for RTxObj");
370 $groups = RT::Groups->new($RT::SystemUser);
371 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj, EquivObjects => [ $RTxSysObj ]);
372 is($groups->Count, 1, "RTxGroupRight found for RTxObj using EquivObjects");
374 $ace = RT::Record->new($RT::SystemUser);
376 $ace->_BuildTableAttributes unless ($_TABLE_ATTR->{ref($self)});
377 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System::Record', ObjectId => 5 );
378 ok ($id, "ACL for RTxObj created");
381 bless $RTxObj2, 'RTx::System::Record';
382 *RTx::System::Record::Id = sub { 5; };
384 $groups = RT::Groups->new($RT::SystemUser);
385 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2);
386 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
388 $groups = RT::Groups->new($RT::SystemUser);
389 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2, EquivObjects => [ $RTxSysObj ]);
390 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
399 #XXX: should be generilized
402 my %args = ( Right => undef,
404 IncludeSystemRights => 1,
405 IncludeSuperusers => undef,
406 IncludeSubgroupMembers => 0,
410 my $from_role = $self->Clone;
411 $from_role->WithRoleRight( %args );
413 my $from_group = $self->Clone;
414 $from_group->WithGroupRight( %args );
417 use DBIx::SearchBuilder::Union;
418 my $union = new DBIx::SearchBuilder::Union;
419 $union->add($from_role);
420 $union->add($from_group);
422 bless $self, ref($union);
427 #XXX: methods are active aliases to Users class to prevent code duplication
428 # should be generalized
432 return 'main' unless $args{'IncludeSubgroupMembers'};
433 return $self->RT::Users::_JoinGroups( %args );
435 sub _JoinGroupMembers {
438 return 'main' unless $args{'IncludeSubgroupMembers'};
439 return $self->RT::Users::_JoinGroupMembers( %args );
441 sub _JoinGroupMembersForGroupRights {
444 my $group_members = $self->_JoinGroupMembers( %args );
445 unless( $group_members eq 'main' ) {
446 return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
448 $self->Limit( ALIAS => $args{'ACLAlias'},
449 FIELD => 'PrincipalId',
454 sub _JoinACL { return (shift)->RT::Users::_JoinACL( @_ ) }
455 sub _GetEquivObjects { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
456 sub WithGroupRight { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
457 sub WithRoleRight { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
459 # {{{ sub LimitToEnabled
461 =head2 LimitToEnabled
463 Only find items that haven\'t been disabled
470 $self->Limit( ALIAS => $self->PrincipalsAlias,
478 # {{{ sub LimitToDisabled
480 =head2 LimitToDeleted
482 Only find items that have been deleted.
489 $self->{'find_disabled_rows'} = 1;
490 $self->Limit( ALIAS => $self->PrincipalsAlias,
503 # Don't show groups which the user isn't allowed to see.
505 my $Group = $self->SUPER::Next();
506 if ((defined($Group)) and (ref($Group))) {
507 unless ($Group->CurrentUserHasRight('SeeGroup')) {
508 return $self->Next();
523 #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
524 unless($self->{'find_disabled_rows'}) {
525 $self->LimitToEnabled();
528 return($self->SUPER::_DoSearch(@_));