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