3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 # (Except where explictly superceded by other copyright notices)
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
26 RT::Groups - a collection of RT::Group objects
31 my $groups = $RT::Groups->new($CurrentUser);
32 $groups->LimitToReal();
33 while (my $group = $groups->Next()) {
34 print $group->Id ." is a group id\n";
45 ok (require RT::Groups);
52 no warnings qw(redefine);
59 $self->{'table'} = "Groups";
60 $self->{'primary_key'} = "id";
62 $self->OrderBy( ALIAS => 'main',
67 return ( $self->SUPER::_Init(@_));
71 # {{{ LimiToSystemInternalGroups
73 =head2 LimitToSystemInternalGroups
75 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
80 sub LimitToSystemInternalGroups {
82 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
83 # All system internal groups have the same instance. No reason to limit down further
84 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
90 # {{{ LimiToUserDefinedGroups
92 =head2 LimitToUserDefined Groups
94 Return only UserDefined Groups
99 sub LimitToUserDefinedGroups {
101 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
102 # All user-defined groups have the same instance. No reason to limit down further
103 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
109 # {{{ LimiToPersonalGroups
111 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
113 Return only Personal Groups for the user whose principal id
119 sub LimitToPersonalGroupsFor {
123 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
124 $self->Limit( FIELD => 'Instance',
127 ENTRY_AGGREGATOR => 'OR');
133 # {{{ LimitToRolesForQueue
135 =item LimitToRolesForQueue QUEUE_ID
137 Limits the set of groups found to role groups for queue QUEUE_ID
141 sub LimitToRolesForQueue {
144 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
145 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
150 # {{{ LimitToRolesForTicket
152 =item LimitToRolesForTicket Ticket_ID
154 Limits the set of groups found to role groups for Ticket Ticket_ID
158 sub LimitToRolesForTicket {
161 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
162 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
167 # {{{ LimitToRolesForSystem
169 =item LimitToRolesForSystem System_ID
171 Limits the set of groups found to role groups for System System_ID
175 sub LimitToRolesForSystem {
177 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
182 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
184 Limits the set of groups returned to groups which have
185 Principal PRINCIPAL_ID as a member
189 my $u = RT::User->new($RT::SystemUser);
190 $u->Create(Name => 'Membertests');
191 my $g = RT::Group->new($RT::SystemUser);
192 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
195 my ($aid, $amsg) =$g->AddMember($u->id);
197 ok($g->HasMember($u->PrincipalObj),"G has member u");
199 my $groups = RT::Groups->new($RT::SystemUser);
200 $groups->LimitToUserDefinedGroups();
201 $groups->WithMember(PrincipalId => $u->id);
202 ok ($groups->Count == 1,"found the 1 group - " . $groups->Count);
203 ok ($groups->First->Id == $g->Id, "it's the right one");
215 my %args = ( PrincipalId => undef,
216 Recursively => undef,
220 if ($args{'Recursively'}) {
221 $members = $self->NewAlias('CachedGroupMembers');
223 $members = $self->NewAlias('GroupMembers');
225 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
226 ALIAS2 => $members, FIELD2 => 'GroupId');
228 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
234 my %args = ( Right => undef,
236 IncludeSystemRights => 1,
237 IncludeSuperusers => undef,
240 my $groupprinc = $self->NewAlias('Principals');
241 my $acl = $self->NewAlias('ACL');
243 # {{{ Find only rows where the right granted is the one we're looking up or _possibly_ superuser
244 $self->Limit( ALIAS => $acl,
245 FIELD => 'RightName',
246 OPERATOR => ($args{Right} ? '=' : 'IS NOT'),
247 VALUE => $args{Right} || 'NULL',
248 ENTRYAGGREGATOR => 'OR' );
250 if ( $args{'IncludeSuperusers'} and $args{'Right'} ) {
251 $self->Limit( ALIAS => $acl,
252 FIELD => 'RightName',
254 VALUE => 'SuperUser',
255 ENTRYAGGREGATOR => 'OR' );
259 my ($or_check_ticket_roles, $or_check_roles);
260 my $which_object = "$acl.ObjectType = 'RT::System'";
262 if ( defined $args{'Object'} ) {
263 if ( ref($args{'Object'}) eq 'RT::Ticket' ) {
264 $or_check_ticket_roles =
265 " OR ( main.Domain = 'RT::Ticket-Role' AND main.Instance = " . $args{'Object'}->Id . ") ";
267 # If we're looking at ticket rights, we also want to look at the associated queue rights.
268 # this is a little bit hacky, but basically, now that we've done the ticket roles magic,
269 # we load the queue object and ask all the rest of our questions about the queue.
270 $args{'Object'} = $args{'Object'}->QueueObj;
272 # TODO XXX This really wants some refactoring
273 if ( ref($args{'Object'}) eq 'RT::Queue' ) {
275 " OR ( ( (main.Domain = 'RT::Queue-Role' AND main.Instance = " .
276 $args{'Object'}->Id . ") $or_check_ticket_roles ) " .
277 " AND main.Type = $acl.PrincipalType AND main.id = $groupprinc.id) ";
280 if ( $args{'IncludeSystemRights'} ) {
281 $which_object .= ' OR ';
287 " ($acl.ObjectType = '" . ref($args{'Object'}) . "'" .
288 " AND $acl.ObjectId = " . $args{'Object'}->Id . ") ";
291 $self->_AddSubClause( "WhichObject", "($which_object)" );
293 $self->_AddSubClause( "WhichGroup",
295 ( ( $acl.PrincipalId = $groupprinc.id
296 AND $acl.PrincipalType = 'Group'
297 AND ( main.Domain = 'SystemInternal'
298 OR main.Domain = 'UserDefined'
299 OR main.Domain = 'ACLEquivalence')
300 AND main.id = $groupprinc.id)
306 # {{{ sub LimitToEnabled
308 =head2 LimitToEnabled
310 Only find items that haven\'t been disabled
317 my $alias = $self->Join(
321 TABLE2 => 'Principals',
325 $self->Limit( ALIAS => $alias,
332 # {{{ sub LimitToDisabled
334 =head2 LimitToDeleted
336 Only find items that have been deleted.
343 my $alias = $self->Join(
347 TABLE2 => 'Principals',
351 $self->{'find_disabled_rows'} = 1;
352 $self->Limit( ALIAS => $alias,