import rt 3.6.6
[freeside.git] / rt / lib / RT / Groups_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/copyleft/gpl.html.
28
29
30 # CONTRIBUTION SUBMISSION POLICY:
31
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46
47 # END BPS TAGGED BLOCK }}}
48 =head1 NAME
49
50   RT::Groups - a collection of RT::Group objects
51
52 =head1 SYNOPSIS
53
54   use RT::Groups;
55   my $groups = $RT::Groups->new($CurrentUser);
56   $groups->UnLimit();
57   while (my $group = $groups->Next()) {
58      print $group->Id ." is a group id\n";
59   }
60
61 =head1 DESCRIPTION
62
63
64 =head1 METHODS
65
66
67 =begin testing
68
69 ok (require RT::Groups);
70
71 =end testing
72
73 =cut
74
75
76 package RT::Groups;
77
78 use strict;
79 no warnings qw(redefine);
80
81 use RT::Users;
82
83 # XXX: below some code is marked as subject to generalize in Groups, Users classes.
84 # RUZ suggest name Principals::Generic or Principals::Base as abstract class, but
85 # Jesse wants something that doesn't imply it's a Principals.pm subclass.
86 # See comments below for candidats.
87
88
89 # {{{ sub _Init
90
91 =begin testing
92
93 # next had bugs
94 # Groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => xx );
95 my $g = RT::Group->new($RT::SystemUser);
96 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'GroupsNotEqualTest');
97 ok ($id, "created group #". $g->id) or diag("error: $msg");
98
99 my $groups = RT::Groups->new($RT::SystemUser);
100 $groups->Limit( FIELD => 'id', OPERATOR => '!=', VALUE => $g->id );
101 $groups->LimitToUserDefinedGroups();
102 my $bug = grep $_->id == $g->id, @{$groups->ItemsArrayRef};
103 ok (!$bug, "didn't find group");
104
105 =end testing
106
107 =cut
108
109 sub _Init { 
110   my $self = shift;
111   $self->{'table'} = "Groups";
112   $self->{'primary_key'} = "id";
113
114   my @result = $self->SUPER::_Init(@_);
115
116   $self->OrderBy( ALIAS => 'main',
117                   FIELD => 'Name',
118                   ORDER => 'ASC');
119
120   # XXX: this code should be generalized
121   $self->{'princalias'} = $self->Join(
122     ALIAS1 => 'main',
123     FIELD1 => 'id',
124     TABLE2 => 'Principals',
125     FIELD2 => 'id'
126   );
127
128   # even if this condition is useless and ids in the Groups table
129   # only match principals with type 'Group' this could speed up
130   # searches in some DBs.
131   $self->Limit( ALIAS => $self->{'princalias'},
132                 FIELD => 'PrincipalType',
133                 VALUE => 'Group',
134               );
135
136   return (@result);
137 }
138 # }}}
139
140 =head2 PrincipalsAlias
141
142 Returns the string that represents this Users object's primary "Principals" alias.
143
144 =cut
145
146 # XXX: should be generalized, code duplication
147 sub PrincipalsAlias {
148     my $self = shift;
149     return($self->{'princalias'});
150
151 }
152
153
154 # {{{ LimiToSystemInternalGroups
155
156 =head2 LimitToSystemInternalGroups
157
158 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone" 
159
160 =cut
161
162
163 sub LimitToSystemInternalGroups {
164     my $self = shift;
165     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
166     # All system internal groups have the same instance. No reason to limit down further
167     #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
168 }
169
170
171 # }}}
172
173 # {{{ LimiToUserDefinedGroups
174
175 =head2 LimitToUserDefined Groups
176
177 Return only UserDefined Groups
178
179 =cut
180
181
182 sub LimitToUserDefinedGroups {
183     my $self = shift;
184     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
185     # All user-defined groups have the same instance. No reason to limit down further
186     #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
187 }
188
189
190 # }}}
191
192 # {{{ LimiToPersonalGroups
193
194 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
195
196 Return only Personal Groups for the user whose principal id 
197 is PRINCIPAL_ID
198
199 =cut
200
201
202 sub LimitToPersonalGroupsFor {
203     my $self = shift;
204     my $princ = shift;
205
206     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
207     $self->Limit(   FIELD => 'Instance',   
208                     OPERATOR => '=', 
209                     VALUE => $princ);
210 }
211
212
213 # }}}
214
215 # {{{ LimitToRolesForQueue
216
217 =head2 LimitToRolesForQueue QUEUE_ID
218
219 Limits the set of groups found to role groups for queue QUEUE_ID
220
221 =cut
222
223 sub LimitToRolesForQueue {
224     my $self = shift;
225     my $queue = shift;
226     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
227     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
228 }
229
230 # }}}
231
232 # {{{ LimitToRolesForTicket
233
234 =head2 LimitToRolesForTicket Ticket_ID
235
236 Limits the set of groups found to role groups for Ticket Ticket_ID
237
238 =cut
239
240 sub LimitToRolesForTicket {
241     my $self = shift;
242     my $Ticket = shift;
243     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
244     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
245 }
246
247 # }}}
248
249 # {{{ LimitToRolesForSystem
250
251 =head2 LimitToRolesForSystem System_ID
252
253 Limits the set of groups found to role groups for System System_ID
254
255 =cut
256
257 sub LimitToRolesForSystem {
258     my $self = shift;
259     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
260 }
261
262 # }}}
263
264 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
265
266 Limits the set of groups returned to groups which have
267 Principal PRINCIPAL_ID as a member
268    
269 =begin testing
270
271 my $u = RT::User->new($RT::SystemUser);
272 $u->Create(Name => 'Membertests');
273 my $g = RT::Group->new($RT::SystemUser);
274 my ($id, $msg) = $g->CreateUserDefinedGroup(Name => 'Membertests');
275 ok ($id, $msg);
276
277 my ($aid, $amsg) =$g->AddMember($u->id);
278 ok ($aid, $amsg);
279 ok($g->HasMember($u->PrincipalObj),"G has member u");
280
281 my $groups = RT::Groups->new($RT::SystemUser);
282 $groups->LimitToUserDefinedGroups();
283 $groups->WithMember(PrincipalId => $u->id);
284 ok ($groups->Count == 1,"found the 1 group - " . $groups->Count);
285 ok ($groups->First->Id == $g->Id, "it's the right one");
286
287 =end testing
288
289
290 =cut
291
292 sub WithMember {
293     my $self = shift;
294     my %args = ( PrincipalId => undef,
295                  Recursively => undef,
296                  @_);
297     my $members;
298
299     if ($args{'Recursively'}) {
300         $members = $self->NewAlias('CachedGroupMembers');
301     } else {
302         $members = $self->NewAlias('GroupMembers');
303     }
304     $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
305                 ALIAS2 => $members, FIELD2 => 'GroupId');
306
307     $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
308 }
309
310
311 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
312
313
314 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.
315
316 =begin testing
317
318 my $q = RT::Queue->new($RT::SystemUser);
319 my ($id, $msg) =$q->Create( Name => 'GlobalACLTest');
320 ok ($id, $msg);
321
322 my $testuser = RT::User->new($RT::SystemUser);
323 ($id,$msg) = $testuser->Create(Name => 'JustAnAdminCc');
324 ok ($id,$msg);
325
326 my $global_admin_cc = RT::Group->new($RT::SystemUser);
327 $global_admin_cc->LoadSystemRoleGroup('AdminCc');
328 ok($global_admin_cc->id, "Found the global admincc group");
329 my $groups = RT::Groups->new($RT::SystemUser);
330 $groups->WithRight(Right => 'OwnTicket', Object => $q);
331 is($groups->Count, 1);
332 ($id, $msg) = $global_admin_cc->PrincipalObj->GrantRight(Right =>'OwnTicket', Object=> $RT::System);
333 ok ($id,$msg);
334 ok (!$testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does not have the right to own tickets in the test queue");
335 ($id, $msg) = $q->AddWatcher(Type => 'AdminCc', PrincipalId => $testuser->id);
336 ok($id,$msg);
337 ok ($testuser->HasRight(Object => $q, Right => 'OwnTicket') , "The test user does have the right to own tickets now. thank god.");
338
339 $groups = RT::Groups->new($RT::SystemUser);
340 $groups->WithRight(Right => 'OwnTicket', Object => $q);
341 ok ($id,$msg);
342 is($groups->Count, 3);
343
344 my $RTxGroup = RT::Group->new($RT::SystemUser);
345 ($id, $msg) = $RTxGroup->CreateUserDefinedGroup( Name => 'RTxGroup', Description => "RTx extension group");
346 ok ($id,$msg);
347
348 my $RTxSysObj = {};
349 bless $RTxSysObj, 'RTx::System';
350 *RTx::System::Id = sub { 1; };
351 *RTx::System::id = *RTx::System::Id;
352 my $ace = RT::Record->new($RT::SystemUser);
353 $ace->Table('ACL');
354 $ace->_BuildTableAttributes unless ($_TABLE_ATTR->{ref($self)});
355 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System', ObjectId  => 1);
356 ok ($id, "ACL for RTxSysObj created");
357
358 my $RTxObj = {};
359 bless $RTxObj, 'RTx::System::Record';
360 *RTx::System::Record::Id = sub { 4; };
361 *RTx::System::Record::id = *RTx::System::Record::Id;
362
363 $groups = RT::Groups->new($RT::SystemUser);
364 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxSysObj);
365 is($groups->Count, 1, "RTxGroupRight found for RTxSysObj");
366
367 $groups = RT::Groups->new($RT::SystemUser);
368 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj);
369 is($groups->Count, 0, "RTxGroupRight not found for RTxObj");
370
371 $groups = RT::Groups->new($RT::SystemUser);
372 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj, EquivObjects => [ $RTxSysObj ]);
373 is($groups->Count, 1, "RTxGroupRight found for RTxObj using EquivObjects");
374
375 $ace = RT::Record->new($RT::SystemUser);
376 $ace->Table('ACL');
377 $ace->_BuildTableAttributes unless ($_TABLE_ATTR->{ref($self)});
378 ($id, $msg) = $ace->Create( PrincipalId => $RTxGroup->id, PrincipalType => 'Group', RightName => 'RTxGroupRight', ObjectType => 'RTx::System::Record', ObjectId  => 5 );
379 ok ($id, "ACL for RTxObj created");
380
381 my $RTxObj2 = {};
382 bless $RTxObj2, 'RTx::System::Record';
383 *RTx::System::Record::Id = sub { 5; };
384
385 $groups = RT::Groups->new($RT::SystemUser);
386 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2);
387 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
388
389 $groups = RT::Groups->new($RT::SystemUser);
390 $groups->WithRight(Right => 'RTxGroupRight', Object => $RTxObj2, EquivObjects => [ $RTxSysObj ]);
391 is($groups->Count, 1, "RTxGroupRight found for RTxObj2");
392
393
394
395 =end testing
396
397
398 =cut
399
400 #XXX: should be generilized
401 sub WithRight {
402     my $self = shift;
403     my %args = ( Right                  => undef,
404                  Object =>              => undef,
405                  IncludeSystemRights    => 1,
406                  IncludeSuperusers      => undef,
407                  IncludeSubgroupMembers => 0,
408                  EquivObjects           => [ ],
409                  @_ );
410
411     my $from_role = $self->Clone;
412     $from_role->WithRoleRight( %args );
413
414     my $from_group = $self->Clone;
415     $from_group->WithGroupRight( %args );
416
417     #XXX: DIRTY HACK
418     use DBIx::SearchBuilder::Union;
419     my $union = new DBIx::SearchBuilder::Union;
420     $union->add($from_role);
421     $union->add($from_group);
422     %$self = %$union;
423     bless $self, ref($union);
424
425     return;
426 }
427
428 #XXX: methods are active aliases to Users class to prevent code duplication
429 # should be generalized
430 sub _JoinGroups {
431     my $self = shift;
432     my %args = (@_);
433     return 'main' unless $args{'IncludeSubgroupMembers'};
434     return $self->RT::Users::_JoinGroups( %args );
435 }
436 sub _JoinGroupMembers {
437     my $self = shift;
438     my %args = (@_);
439     return 'main' unless $args{'IncludeSubgroupMembers'};
440     return $self->RT::Users::_JoinGroupMembers( %args );
441 }
442 sub _JoinGroupMembersForGroupRights {
443     my $self = shift;
444     my %args = (@_);
445     my $group_members = $self->_JoinGroupMembers( %args );
446     unless( $group_members eq 'main' ) {
447         return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
448     }
449     $self->Limit( ALIAS => $args{'ACLAlias'},
450                   FIELD => 'PrincipalId',
451                   VALUE => "main.id",
452                   QUOTEVALUE => 0,
453                 );
454 }
455 sub _JoinACL                  { return (shift)->RT::Users::_JoinACL( @_ ) }
456 sub _RoleClauses              { return (shift)->RT::Users::_RoleClauses( @_ ) }
457 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
458 sub _GetEquivObjects          { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
459 sub WithGroupRight            { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
460 sub WithRoleRight             { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
461
462 # {{{ sub LimitToEnabled
463
464 =head2 LimitToEnabled
465
466 Only find items that haven\'t been disabled
467
468 =cut
469
470 sub LimitToEnabled {
471     my $self = shift;
472     
473     $self->Limit( ALIAS => $self->PrincipalsAlias,
474                           FIELD => 'Disabled',
475                           VALUE => '0',
476                           OPERATOR => '=',
477                 );
478 }
479 # }}}
480
481 # {{{ sub LimitToDisabled
482
483 =head2 LimitToDeleted
484
485 Only find items that have been deleted.
486
487 =cut
488
489 sub LimitToDeleted {
490     my $self = shift;
491     
492     $self->{'find_disabled_rows'} = 1;
493     $self->Limit( ALIAS => $self->PrincipalsAlias,
494                   FIELD => 'Disabled',
495                   OPERATOR => '=',
496                   VALUE => 1,
497                 );
498 }
499 # }}}
500
501 # {{{ sub Next
502
503 sub Next {
504     my $self = shift;
505
506     # Don't show groups which the user isn't allowed to see.
507
508     my $Group = $self->SUPER::Next();
509     if ((defined($Group)) and (ref($Group))) {
510         unless ($Group->CurrentUserHasRight('SeeGroup')) {
511             return $self->Next();
512         }
513         
514         return $Group;
515     }
516     else {
517         return undef;
518     }
519 }
520
521
522
523 sub _DoSearch {
524     my $self = shift;
525     
526     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
527     unless($self->{'find_disabled_rows'}) {
528         $self->LimitToEnabled();
529     }
530     
531     return($self->SUPER::_DoSearch(@_));
532     
533 }
534
535 1;
536