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::Groups - a collection of RT::Group objects
53 my $groups = $RT::Groups->new($CurrentUser);
54 $groups->LimitToReal();
55 while (my $group = $groups->Next()) {
56 print $group->Id ." is a group id\n";
67 ok (require RT::Groups);
74 no warnings qw(redefine);
81 $self->{'table'} = "Groups";
82 $self->{'primary_key'} = "id";
84 $self->OrderBy( ALIAS => 'main',
89 return ( $self->SUPER::_Init(@_));
93 # {{{ LimiToSystemInternalGroups
95 =head2 LimitToSystemInternalGroups
97 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
102 sub LimitToSystemInternalGroups {
104 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
105 # All system internal groups have the same instance. No reason to limit down further
106 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
112 # {{{ LimiToUserDefinedGroups
114 =head2 LimitToUserDefined Groups
116 Return only UserDefined Groups
121 sub LimitToUserDefinedGroups {
123 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
124 # All user-defined groups have the same instance. No reason to limit down further
125 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
131 # {{{ LimiToPersonalGroups
133 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
135 Return only Personal Groups for the user whose principal id
141 sub LimitToPersonalGroupsFor {
145 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
146 $self->Limit( FIELD => 'Instance',
149 ENTRY_AGGREGATOR => 'OR');
155 # {{{ LimitToRolesForQueue
157 =item LimitToRolesForQueue QUEUE_ID
159 Limits the set of groups found to role groups for queue QUEUE_ID
163 sub LimitToRolesForQueue {
166 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
167 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
172 # {{{ LimitToRolesForTicket
174 =item LimitToRolesForTicket Ticket_ID
176 Limits the set of groups found to role groups for Ticket Ticket_ID
180 sub LimitToRolesForTicket {
183 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
184 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
189 # {{{ LimitToRolesForSystem
191 =item LimitToRolesForSystem System_ID
193 Limits the set of groups found to role groups for System System_ID
197 sub LimitToRolesForSystem {
199 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
204 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
206 Limits the set of groups returned to groups which have
207 Principal PRINCIPAL_ID as a member
211 my $u = RT::User->new($RT::SystemUser);
212 $u->Create(Name => 'Membertests');
213 my $g = RT::Group->new($RT::SystemUser);
214 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
217 my ($aid, $amsg) =$g->AddMember($u->id);
219 ok($g->HasMember($u->PrincipalObj),"G has member u");
221 my $groups = RT::Groups->new($RT::SystemUser);
222 $groups->LimitToUserDefinedGroups();
223 $groups->WithMember(PrincipalId => $u->id);
224 ok ($groups->Count == 1,"found the 1 group - " . $groups->Count);
225 ok ($groups->First->Id == $g->Id, "it's the right one");
237 my %args = ( PrincipalId => undef,
238 Recursively => undef,
242 if ($args{'Recursively'}) {
243 $members = $self->NewAlias('CachedGroupMembers');
245 $members = $self->NewAlias('GroupMembers');
247 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
248 ALIAS2 => $members, FIELD2 => 'GroupId');
250 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
254 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0 }
257 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.
261 my $q = RT::Queue->new($RT::SystemUser);
262 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
265 my $testuser = RT::User->new($RT::SystemUser);
266 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
269 my $global_admin_cc = RT::Group->new($RT::SystemUser);
270 $global_admin_cc->LoadSystemRoleGroup('AdminCc');
271 ok($global_admin_cc->id, "Found the global admincc group");
272 my $groups = RT::Groups->new($RT::SystemUser);
273 $groups->WithRight(Right => 'OwnTicket', Object => $q);
274 is($groups->Count, 1);
275 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> $RT::System);
277 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
278 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
280 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
282 $groups = RT::Groups->new($RT::SystemUser);
283 $groups->WithRight(Right => 'OwnTicket', Object => $q);
285 is($groups->Count, 2);
295 my %args = ( Right => undef,
297 IncludeSystemRights => 1,
298 IncludeSuperusers => undef,
301 my $acl = $self->NewAlias('ACL');
303 # {{{ Find only rows where the right granted is the one we're looking up or _possibly_ superuser
304 $self->Limit( ALIAS => $acl,
305 FIELD => 'RightName',
306 OPERATOR => ($args{Right} ? '=' : 'IS NOT'),
307 VALUE => $args{Right} || 'NULL',
308 ENTRYAGGREGATOR => 'OR' );
310 if ( $args{'IncludeSuperusers'} and $args{'Right'} ) {
311 $self->Limit( ALIAS => $acl,
312 FIELD => 'RightName',
314 VALUE => 'SuperUser',
315 ENTRYAGGREGATOR => 'OR' );
319 my ($or_check_ticket_roles, $or_check_roles);
320 my $which_object = "$acl.ObjectType = 'RT::System'";
322 if ( defined $args{'Object'} ) {
323 if ( ref($args{'Object'}) eq 'RT::Ticket' ) {
324 $or_check_ticket_roles =
325 " OR ( main.Domain = 'RT::Ticket-Role' AND main.Instance = " . $args{'Object'}->Id . ") ";
327 # If we're looking at ticket rights, we also want to look at the associated queue rights.
328 # this is a little bit hacky, but basically, now that we've done the ticket roles magic,
329 # we load the queue object and ask all the rest of our questions about the queue.
330 $args{'Object'} = $args{'Object'}->QueueObj;
332 # TODO XXX This really wants some refactoring
333 if ( ref($args{'Object'}) eq 'RT::Queue' ) {
335 " OR ( ( (main.Domain = 'RT::Queue-Role' AND main.Instance = " .
336 $args{'Object'}->Id . ") $or_check_ticket_roles ) " .
337 " AND main.Type = $acl.PrincipalType) ";
340 if ( $args{'IncludeSystemRights'} ) {
341 $which_object .= ' OR ';
347 " ($acl.ObjectType = '" . ref($args{'Object'}) . "'" .
348 " AND $acl.ObjectId = " . $args{'Object'}->Id . ") ";
351 $self->_AddSubClause( "WhichObject", "($which_object)" );
353 $self->_AddSubClause( "WhichGroup",
355 ( ( $acl.PrincipalId = main.id
356 AND $acl.PrincipalType = 'Group'
357 AND ( main.Domain = 'SystemInternal'
358 OR main.Domain = 'UserDefined'
359 OR main.Domain = 'ACLEquivalence'))
365 # {{{ sub LimitToEnabled
367 =head2 LimitToEnabled
369 Only find items that haven\'t been disabled
376 my $alias = $self->Join(
380 TABLE2 => 'Principals',
384 $self->Limit( ALIAS => $alias,
391 # {{{ sub LimitToDisabled
393 =head2 LimitToDeleted
395 Only find items that have been deleted.
402 my $alias = $self->Join(
406 TABLE2 => 'Principals',
410 $self->{'find_disabled_rows'} = 1;
411 $self->Limit( ALIAS => $alias,
422 #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
423 unless($self->{'find_disabled_rows'}) {
424 $self->LimitToEnabled();
427 return($self->SUPER::_DoSearch(@_));