import rt 3.0.12
[freeside.git] / rt / lib / RT / Groups_Overlay.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
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
10 # from www.gnu.org.
11
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.
16
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.
21
22
23 # END LICENSE BLOCK
24 =head1 NAME
25
26   RT::Groups - a collection of RT::Group objects
27
28 =head1 SYNOPSIS
29
30   use RT::Groups;
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";
35   }
36
37 =head1 DESCRIPTION
38
39
40 =head1 METHODS
41
42
43 =begin testing
44
45 ok (require RT::Groups);
46
47 =end testing
48
49 =cut
50
51 use strict;
52 no warnings qw(redefine);
53
54
55 # {{{ sub _Init
56
57 sub _Init { 
58   my $self = shift;
59   $self->{'table'} = "Groups";
60   $self->{'primary_key'} = "id";
61
62   $self->OrderBy( ALIAS => 'main',
63                   FIELD => 'Name',
64                   ORDER => 'ASC');
65
66
67   return ( $self->SUPER::_Init(@_));
68 }
69 # }}}
70
71 # {{{ LimiToSystemInternalGroups
72
73 =head2 LimitToSystemInternalGroups
74
75 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone" 
76
77 =cut
78
79
80 sub LimitToSystemInternalGroups {
81     my $self = shift;
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');
85 }
86
87
88 # }}}
89
90 # {{{ LimiToUserDefinedGroups
91
92 =head2 LimitToUserDefined Groups
93
94 Return only UserDefined Groups
95
96 =cut
97
98
99 sub LimitToUserDefinedGroups {
100     my $self = shift;
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 => '');
104 }
105
106
107 # }}}
108
109 # {{{ LimiToPersonalGroups
110
111 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
112
113 Return only Personal Groups for the user whose principal id 
114 is PRINCIPAL_ID
115
116 =cut
117
118
119 sub LimitToPersonalGroupsFor {
120     my $self = shift;
121     my $princ = shift;
122
123     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
124     $self->Limit(   FIELD => 'Instance',   
125                     OPERATOR => '=', 
126                     VALUE => $princ,
127                     ENTRY_AGGREGATOR => 'OR');
128 }
129
130
131 # }}}
132
133 # {{{ LimitToRolesForQueue
134
135 =item LimitToRolesForQueue QUEUE_ID
136
137 Limits the set of groups found to role groups for queue QUEUE_ID
138
139 =cut
140
141 sub LimitToRolesForQueue {
142     my $self = shift;
143     my $queue = shift;
144     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
145     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
146 }
147
148 # }}}
149
150 # {{{ LimitToRolesForTicket
151
152 =item LimitToRolesForTicket Ticket_ID
153
154 Limits the set of groups found to role groups for Ticket Ticket_ID
155
156 =cut
157
158 sub LimitToRolesForTicket {
159     my $self = shift;
160     my $Ticket = shift;
161     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
162     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
163 }
164
165 # }}}
166
167 # {{{ LimitToRolesForSystem
168
169 =item LimitToRolesForSystem System_ID
170
171 Limits the set of groups found to role groups for System System_ID
172
173 =cut
174
175 sub LimitToRolesForSystem {
176     my $self = shift;
177     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
178 }
179
180 # }}}
181
182 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
183
184 Limits the set of groups returned to groups which have
185 Principal PRINCIPAL_ID as a member
186    
187 =begin testing
188
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');
193 ok ($id,$msg);
194
195 my ($aid, $amsg) =$g->AddMember($u->id);
196 ok ($aid, $amsg);
197 ok($g->HasMember($u->PrincipalObj),"G has member u");
198
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");
204
205
206
207
208 =end testing
209
210
211 =cut
212
213 sub WithMember {
214     my $self = shift;
215     my %args = ( PrincipalId => undef,
216                  Recursively => undef,
217                  @_);
218     my $members;
219
220     if ($args{'Recursively'}) {
221         $members = $self->NewAlias('CachedGroupMembers');
222     } else {
223         $members = $self->NewAlias('GroupMembers');
224     }
225     $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
226                 ALIAS2 => $members, FIELD2 => 'GroupId');
227
228     $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
229 }
230
231
232 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0 }
233
234
235 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.
236
237 =begin testing
238
239 my $q = RT::Queue->new($RT::SystemUser);
240 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
241 ok ($id, $msg);
242
243 my $testuser = RT::User->new($RT::SystemUser);
244 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
245 ok ($id,$msg);
246
247 my $global_admin_cc = RT::Group->new($RT::SystemUser);
248 $global_admin_cc->LoadSystemRoleGroup('AdminCc');
249 ok($global_admin_cc->id, "Found the global admincc group");
250 my $groups = RT::Groups->new($RT::SystemUser);
251 $groups->WithRight(Right => 'OwnTicket', Object => $q);
252 is($groups->Count, 1);
253 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> $RT::System);
254 ok ($id,$msg);
255 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
256 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
257 ok($id,$msg);
258 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
259
260 $groups = RT::Groups->new($RT::SystemUser);
261 $groups->WithRight(Right => 'OwnTicket', Object => $q);
262 ok ($id,$msg);
263 is($groups->Count, 2);
264
265 =end testing
266
267
268 =cut
269
270
271 sub WithRight {
272     my $self = shift;
273     my %args = ( Right                  => undef,
274                  Object =>              => undef,
275                  IncludeSystemRights    => 1,
276                  IncludeSuperusers      => undef,
277                  @_ );
278
279     my $acl        = $self->NewAlias('ACL');
280
281     # {{{ Find only rows where the right granted is the one we're looking up or _possibly_ superuser 
282     $self->Limit( ALIAS           => $acl,
283                   FIELD           => 'RightName',
284                   OPERATOR        => ($args{Right} ? '=' : 'IS NOT'),
285                   VALUE           => $args{Right} || 'NULL',
286                   ENTRYAGGREGATOR => 'OR' );
287
288     if ( $args{'IncludeSuperusers'} and $args{'Right'} ) {
289         $self->Limit( ALIAS           => $acl,
290                       FIELD           => 'RightName',
291                       OPERATOR        => '=',
292                       VALUE           => 'SuperUser',
293                       ENTRYAGGREGATOR => 'OR' );
294     }
295     # }}}
296
297     my ($or_check_ticket_roles, $or_check_roles);
298     my $which_object = "$acl.ObjectType = 'RT::System'";
299
300     if ( defined $args{'Object'} ) {
301         if ( ref($args{'Object'}) eq 'RT::Ticket' ) {
302             $or_check_ticket_roles =
303                 " OR ( main.Domain = 'RT::Ticket-Role' AND main.Instance = " . $args{'Object'}->Id . ") ";
304
305             # If we're looking at ticket rights, we also want to look at the associated queue rights.
306             # this is a little bit hacky, but basically, now that we've done the ticket roles magic,
307             # we load the queue object and ask all the rest of our questions about the queue.
308             $args{'Object'}   = $args{'Object'}->QueueObj;
309         }
310         # TODO XXX This really wants some refactoring
311         if ( ref($args{'Object'}) eq 'RT::Queue' ) {
312             $or_check_roles =
313                 " OR ( ( (main.Domain = 'RT::Queue-Role' AND main.Instance = " .
314                 $args{'Object'}->Id . ") $or_check_ticket_roles ) " .
315                 " AND main.Type = $acl.PrincipalType) ";
316         }
317
318         if ( $args{'IncludeSystemRights'} ) {
319             $which_object .= ' OR ';
320         }
321         else {
322             $which_object = '';
323         }
324         $which_object .=
325             " ($acl.ObjectType = '" . ref($args{'Object'}) . "'" .
326             " AND $acl.ObjectId = " . $args{'Object'}->Id . ") ";
327     }
328
329     $self->_AddSubClause( "WhichObject", "($which_object)" );
330
331     $self->_AddSubClause( "WhichGroup",
332         qq{
333           ( (    $acl.PrincipalId = main.id
334              AND $acl.PrincipalType = 'Group'
335              AND (   main.Domain = 'SystemInternal'
336                   OR main.Domain = 'UserDefined'
337                   OR main.Domain = 'ACLEquivalence'))
338            $or_check_roles)
339         }
340     );
341 }
342
343 # {{{ sub LimitToEnabled
344
345 =head2 LimitToEnabled
346
347 Only find items that haven\'t been disabled
348
349 =cut
350
351 sub LimitToEnabled {
352     my $self = shift;
353     
354     my $alias = $self->Join(
355         TYPE   => 'left',
356         ALIAS1 => 'main',
357         FIELD1 => 'id',
358         TABLE2 => 'Principals',
359         FIELD2 => 'id'
360     );
361
362     $self->Limit( ALIAS => $alias,
363                   FIELD => 'Disabled',
364                   VALUE => '0',
365                   OPERATOR => '=' );
366 }
367 # }}}
368
369 # {{{ sub LimitToDisabled
370
371 =head2 LimitToDeleted
372
373 Only find items that have been deleted.
374
375 =cut
376
377 sub LimitToDeleted {
378     my $self = shift;
379     
380     my $alias = $self->Join(
381         TYPE   => 'left',
382         ALIAS1 => 'main',
383         FIELD1 => 'id',
384         TABLE2 => 'Principals',
385         FIELD2 => 'id'
386     );
387
388     $self->{'find_disabled_rows'} = 1;
389     $self->Limit( ALIAS => $alias,
390                   FIELD => 'Disabled',
391                   OPERATOR => '=',
392                   VALUE => '1'
393                 );
394 }
395 # }}}
396
397 sub _DoSearch {
398     my $self = shift;
399     
400     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
401     unless($self->{'find_disabled_rows'}) {
402         $self->LimitToEnabled();
403     }
404     
405     return($self->SUPER::_DoSearch(@_));
406     
407 }
408
409 1;
410