This commit was generated by cvs2svn to compensate for changes in r3921,
[freeside.git] / rt / lib / RT / Groups_Overlay.pm
1 # {{{ BEGIN BPS TAGGED BLOCK
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC 
6 #                                          <jesse@bestpractical.com>
7
8 # (Except where explicitly superseded by other copyright notices)
9
10
11 # LICENSE:
12
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
16 # from www.gnu.org.
17
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.
22
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.
26
27
28 # CONTRIBUTION SUBMISSION POLICY:
29
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.)
35
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.
44
45 # }}} END BPS TAGGED BLOCK
46 =head1 NAME
47
48   RT::Groups - a collection of RT::Group objects
49
50 =head1 SYNOPSIS
51
52   use RT::Groups;
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";
57   }
58
59 =head1 DESCRIPTION
60
61
62 =head1 METHODS
63
64
65 =begin testing
66
67 ok (require RT::Groups);
68
69 =end testing
70
71 =cut
72
73 use strict;
74 no warnings qw(redefine);
75
76
77 # {{{ sub _Init
78
79 sub _Init { 
80   my $self = shift;
81   $self->{'table'} = "Groups";
82   $self->{'primary_key'} = "id";
83
84   $self->OrderBy( ALIAS => 'main',
85                   FIELD => 'Name',
86                   ORDER => 'ASC');
87
88
89   return ( $self->SUPER::_Init(@_));
90 }
91 # }}}
92
93 # {{{ LimiToSystemInternalGroups
94
95 =head2 LimitToSystemInternalGroups
96
97 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone" 
98
99 =cut
100
101
102 sub LimitToSystemInternalGroups {
103     my $self = shift;
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');
107 }
108
109
110 # }}}
111
112 # {{{ LimiToUserDefinedGroups
113
114 =head2 LimitToUserDefined Groups
115
116 Return only UserDefined Groups
117
118 =cut
119
120
121 sub LimitToUserDefinedGroups {
122     my $self = shift;
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 => '');
126 }
127
128
129 # }}}
130
131 # {{{ LimiToPersonalGroups
132
133 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
134
135 Return only Personal Groups for the user whose principal id 
136 is PRINCIPAL_ID
137
138 =cut
139
140
141 sub LimitToPersonalGroupsFor {
142     my $self = shift;
143     my $princ = shift;
144
145     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
146     $self->Limit(   FIELD => 'Instance',   
147                     OPERATOR => '=', 
148                     VALUE => $princ,
149                     ENTRY_AGGREGATOR => 'OR');
150 }
151
152
153 # }}}
154
155 # {{{ LimitToRolesForQueue
156
157 =item LimitToRolesForQueue QUEUE_ID
158
159 Limits the set of groups found to role groups for queue QUEUE_ID
160
161 =cut
162
163 sub LimitToRolesForQueue {
164     my $self = shift;
165     my $queue = shift;
166     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
167     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
168 }
169
170 # }}}
171
172 # {{{ LimitToRolesForTicket
173
174 =item LimitToRolesForTicket Ticket_ID
175
176 Limits the set of groups found to role groups for Ticket Ticket_ID
177
178 =cut
179
180 sub LimitToRolesForTicket {
181     my $self = shift;
182     my $Ticket = shift;
183     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
184     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
185 }
186
187 # }}}
188
189 # {{{ LimitToRolesForSystem
190
191 =item LimitToRolesForSystem System_ID
192
193 Limits the set of groups found to role groups for System System_ID
194
195 =cut
196
197 sub LimitToRolesForSystem {
198     my $self = shift;
199     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
200 }
201
202 # }}}
203
204 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
205
206 Limits the set of groups returned to groups which have
207 Principal PRINCIPAL_ID as a member
208    
209 =begin testing
210
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');
215 ok ($id,$msg);
216
217 my ($aid, $amsg) =$g->AddMember($u->id);
218 ok ($aid, $amsg);
219 ok($g->HasMember($u->PrincipalObj),"G has member u");
220
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");
226
227
228
229
230 =end testing
231
232
233 =cut
234
235 sub WithMember {
236     my $self = shift;
237     my %args = ( PrincipalId => undef,
238                  Recursively => undef,
239                  @_);
240     my $members;
241
242     if ($args{'Recursively'}) {
243         $members = $self->NewAlias('CachedGroupMembers');
244     } else {
245         $members = $self->NewAlias('GroupMembers');
246     }
247     $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
248                 ALIAS2 => $members, FIELD2 => 'GroupId');
249
250     $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
251 }
252
253
254 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0 }
255
256
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.
258
259 =begin testing
260
261 my $q = RT::Queue->new($RT::SystemUser);
262 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
263 ok ($id, $msg);
264
265 my $testuser = RT::User->new($RT::SystemUser);
266 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
267 ok ($id,$msg);
268
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);
276 ok ($id,$msg);
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);
279 ok($id,$msg);
280 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
281
282 $groups = RT::Groups->new($RT::SystemUser);
283 $groups->WithRight(Right => 'OwnTicket', Object => $q);
284 ok ($id,$msg);
285 is($groups->Count, 2);
286
287 =end testing
288
289
290 =cut
291
292
293 sub WithRight {
294     my $self = shift;
295     my %args = ( Right                  => undef,
296                  Object =>              => undef,
297                  IncludeSystemRights    => 1,
298                  IncludeSuperusers      => undef,
299                  @_ );
300
301     my $acl        = $self->NewAlias('ACL');
302
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' );
309
310     if ( $args{'IncludeSuperusers'} and $args{'Right'} ) {
311         $self->Limit( ALIAS           => $acl,
312                       FIELD           => 'RightName',
313                       OPERATOR        => '=',
314                       VALUE           => 'SuperUser',
315                       ENTRYAGGREGATOR => 'OR' );
316     }
317     # }}}
318
319     my ($or_check_ticket_roles, $or_check_roles);
320     my $which_object = "$acl.ObjectType = 'RT::System'";
321
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 . ") ";
326
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;
331         }
332         # TODO XXX This really wants some refactoring
333         if ( ref($args{'Object'}) eq 'RT::Queue' ) {
334             $or_check_roles =
335                 " OR ( ( (main.Domain = 'RT::Queue-Role' AND main.Instance = " .
336                 $args{'Object'}->Id . ") $or_check_ticket_roles ) " .
337                 " AND main.Type = $acl.PrincipalType) ";
338         }
339
340         if ( $args{'IncludeSystemRights'} ) {
341             $which_object .= ' OR ';
342         }
343         else {
344             $which_object = '';
345         }
346         $which_object .=
347             " ($acl.ObjectType = '" . ref($args{'Object'}) . "'" .
348             " AND $acl.ObjectId = " . $args{'Object'}->Id . ") ";
349     }
350
351     $self->_AddSubClause( "WhichObject", "($which_object)" );
352
353     $self->_AddSubClause( "WhichGroup",
354         qq{
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'))
360            $or_check_roles)
361         }
362     );
363 }
364
365 # {{{ sub LimitToEnabled
366
367 =head2 LimitToEnabled
368
369 Only find items that haven\'t been disabled
370
371 =cut
372
373 sub LimitToEnabled {
374     my $self = shift;
375     
376     my $alias = $self->Join(
377         TYPE   => 'left',
378         ALIAS1 => 'main',
379         FIELD1 => 'id',
380         TABLE2 => 'Principals',
381         FIELD2 => 'id'
382     );
383
384     $self->Limit( ALIAS => $alias,
385                   FIELD => 'Disabled',
386                   VALUE => '0',
387                   OPERATOR => '=' );
388 }
389 # }}}
390
391 # {{{ sub LimitToDisabled
392
393 =head2 LimitToDeleted
394
395 Only find items that have been deleted.
396
397 =cut
398
399 sub LimitToDeleted {
400     my $self = shift;
401     
402     my $alias = $self->Join(
403         TYPE   => 'left',
404         ALIAS1 => 'main',
405         FIELD1 => 'id',
406         TABLE2 => 'Principals',
407         FIELD2 => 'id'
408     );
409
410     $self->{'find_disabled_rows'} = 1;
411     $self->Limit( ALIAS => $alias,
412                   FIELD => 'Disabled',
413                   OPERATOR => '=',
414                   VALUE => '1'
415                 );
416 }
417 # }}}
418
419 sub _DoSearch {
420     my $self = shift;
421     
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();
425     }
426     
427     return($self->SUPER::_DoSearch(@_));
428     
429 }
430
431 1;
432