rt 4.0.23
[freeside.git] / rt / lib / RT / Groups.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@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/licenses/old-licenses/gpl-2.0.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
49 =head1 NAME
50
51   RT::Groups - a collection of RT::Group objects
52
53 =head1 SYNOPSIS
54
55   use RT::Groups;
56   my $groups = RT::Groups->new($CurrentUser);
57   $groups->UnLimit();
58   while (my $group = $groups->Next()) {
59      print $group->Id ." is a group id\n";
60   }
61
62 =head1 DESCRIPTION
63
64
65 =head1 METHODS
66
67
68
69 =cut
70
71
72 package RT::Groups;
73
74 use strict;
75 use warnings;
76
77
78
79 use RT::Group;
80
81 use base 'RT::SearchBuilder';
82
83 sub Table { 'Groups'}
84
85 use RT::Users;
86
87 # XXX: below some code is marked as subject to generalize in Groups, Users classes.
88 # RUZ suggest name Principals::Generic or Principals::Base as abstract class, but
89 # Jesse wants something that doesn't imply it's a Principals.pm subclass.
90 # See comments below for candidats.
91
92
93
94 sub _Init { 
95   my $self = shift;
96   $self->{'with_disabled_column'} = 1;
97
98   my @result = $self->SUPER::_Init(@_);
99
100   $self->OrderBy( ALIAS => 'main',
101                   FIELD => 'Name',
102                   ORDER => 'ASC');
103
104   # XXX: this code should be generalized
105   $self->{'princalias'} = $self->Join(
106     ALIAS1 => 'main',
107     FIELD1 => 'id',
108     TABLE2 => 'Principals',
109     FIELD2 => 'id'
110   );
111
112   # even if this condition is useless and ids in the Groups table
113   # only match principals with type 'Group' this could speed up
114   # searches in some DBs.
115   $self->Limit( ALIAS => $self->{'princalias'},
116                 FIELD => 'PrincipalType',
117                 VALUE => 'Group',
118               );
119
120   return (@result);
121 }
122
123 =head2 PrincipalsAlias
124
125 Returns the string that represents this Users object's primary "Principals" alias.
126
127 =cut
128
129 # XXX: should be generalized, code duplication
130 sub PrincipalsAlias {
131     my $self = shift;
132     return($self->{'princalias'});
133
134 }
135
136
137
138 =head2 LimitToSystemInternalGroups
139
140 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone" 
141
142 =cut
143
144
145 sub LimitToSystemInternalGroups {
146     my $self = shift;
147     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
148     # All system internal groups have the same instance. No reason to limit down further
149     #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
150 }
151
152
153
154
155 =head2 LimitToUserDefinedGroups
156
157 Return only UserDefined Groups
158
159 =cut
160
161
162 sub LimitToUserDefinedGroups {
163     my $self = shift;
164     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
165     # All user-defined groups have the same instance. No reason to limit down further
166     #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
167 }
168
169
170
171
172 =head2 LimitToRolesForQueue QUEUE_ID
173
174 Limits the set of groups found to role groups for queue QUEUE_ID
175
176 =cut
177
178 sub LimitToRolesForQueue {
179     my $self = shift;
180     my $queue = shift;
181     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
182     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
183 }
184
185
186
187 =head2 LimitToRolesForTicket Ticket_ID
188
189 Limits the set of groups found to role groups for Ticket Ticket_ID
190
191 =cut
192
193 sub LimitToRolesForTicket {
194     my $self = shift;
195     my $Ticket = shift;
196     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
197     $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
198 }
199
200
201
202 =head2 LimitToRolesForSystem System_ID
203
204 Limits the set of groups found to role groups for System System_ID
205
206 =cut
207
208 sub LimitToRolesForSystem {
209     my $self = shift;
210     $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
211 }
212
213
214 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
215
216 Limits the set of groups returned to groups which have
217 Principal PRINCIPAL_ID as a member. Returns the alias used for the join.
218
219 =cut
220
221 sub WithMember {
222     my $self = shift;
223     my %args = ( PrincipalId => undef,
224                  Recursively => undef,
225                  @_);
226     my $members;
227
228     if ($args{'Recursively'}) {
229         $members = $self->NewAlias('CachedGroupMembers');
230     } else {
231         $members = $self->NewAlias('GroupMembers');
232     }
233     $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
234                 ALIAS2 => $members, FIELD2 => 'GroupId');
235
236     $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
237     $self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
238         if $args{'Recursively'};
239
240     return $members;
241 }
242
243 sub WithCurrentUser {
244     my $self = shift;
245     $self->{with_current_user} = 1;
246     return $self->WithMember(
247         PrincipalId => $self->CurrentUser->PrincipalId,
248         Recursively => 1,
249     );
250 }
251
252 sub WithoutMember {
253     my $self = shift;
254     my %args = (
255         PrincipalId => undef,
256         Recursively => undef,
257         @_
258     );
259
260     my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
261     my $members_alias = $self->Join(
262         TYPE   => 'LEFT',
263         FIELD1 => 'id',
264         TABLE2 => $members,
265         FIELD2 => 'GroupId',
266     );
267     $self->Limit(
268         LEFTJOIN => $members_alias,
269         ALIAS    => $members_alias,
270         FIELD    => 'MemberId',
271         OPERATOR => '=',
272         VALUE    => $args{'PrincipalId'},
273     );
274     $self->Limit(
275         LEFTJOIN => $members_alias,
276         ALIAS    => $members_alias,
277         FIELD    => 'Disabled',
278         VALUE    => 0
279     ) if $args{'Recursively'};
280     $self->Limit(
281         ALIAS    => $members_alias,
282         FIELD    => 'MemberId',
283         OPERATOR => 'IS',
284         VALUE    => 'NULL',
285         QUOTEVALUE => 0,
286     );
287 }
288
289 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
290
291
292 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.
293
294
295
296 =cut
297
298 #XXX: should be generilized
299 sub WithRight {
300     my $self = shift;
301     my %args = ( Right                  => undef,
302                  Object =>              => undef,
303                  IncludeSystemRights    => 1,
304                  IncludeSuperusers      => undef,
305                  IncludeSubgroupMembers => 0,
306                  EquivObjects           => [ ],
307                  @_ );
308
309     my $from_role = $self->Clone;
310     $from_role->WithRoleRight( %args );
311
312     my $from_group = $self->Clone;
313     $from_group->WithGroupRight( %args );
314
315     #XXX: DIRTY HACK
316     use DBIx::SearchBuilder 1.50; #no version on ::Union :(
317     use DBIx::SearchBuilder::Union;
318     my $union = DBIx::SearchBuilder::Union->new();
319     $union->add($from_role);
320     $union->add($from_group);
321     %$self = %$union;
322     bless $self, ref($union);
323
324     return;
325 }
326
327 #XXX: methods are active aliases to Users class to prevent code duplication
328 # should be generalized
329 sub _JoinGroups {
330     my $self = shift;
331     my %args = (@_);
332     return 'main' unless $args{'IncludeSubgroupMembers'};
333     return $self->RT::Users::_JoinGroups( %args );
334 }
335 sub _JoinGroupMembers {
336     my $self = shift;
337     my %args = (@_);
338     return 'main' unless $args{'IncludeSubgroupMembers'};
339     return $self->RT::Users::_JoinGroupMembers( %args );
340 }
341 sub _JoinGroupMembersForGroupRights {
342     my $self = shift;
343     my %args = (@_);
344     my $group_members = $self->_JoinGroupMembers( %args );
345     unless( $group_members eq 'main' ) {
346         return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
347     }
348     $self->Limit( ALIAS => $args{'ACLAlias'},
349                   FIELD => 'PrincipalId',
350                   VALUE => "main.id",
351                   QUOTEVALUE => 0,
352                 );
353 }
354 sub _JoinACL                  { return (shift)->RT::Users::_JoinACL( @_ ) }
355 sub _RoleClauses              { return (shift)->RT::Users::_RoleClauses( @_ ) }
356 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
357 sub _GetEquivObjects          { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
358 sub WithGroupRight            { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
359 sub WithRoleRight             { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
360
361 sub ForWhichCurrentUserHasRight {
362     my $self = shift;
363     my %args = (
364         Right => undef,
365         IncludeSuperusers => undef,
366         @_,
367     );
368
369     # Non-disabled groups...
370     $self->LimitToEnabled;
371
372     # ...which are the target object of an ACL with that right, or
373     # where the target is the system object (a global right)
374     my $acl = $self->_JoinACL( %args );
375     $self->_AddSubClause(
376         ACLObjects => "( (main.id = $acl.ObjectId AND $acl.ObjectType = 'RT::Group')"
377                    . " OR $acl.ObjectType = 'RT::System')");
378
379     # ...and where that right is granted to any group..
380     my $member = $self->Join(
381         ALIAS1 => $acl,
382         FIELD1 => 'PrincipalId',
383         TABLE2 => 'CachedGroupMembers',
384         FIELD2 => 'GroupId',
385     );
386     $self->Limit(
387         ALIAS => $member,
388         FIELD => 'Disabled',
389         VALUE => '0',
390     );
391
392     # ...with the current user in it
393     $self->Limit(
394         ALIAS => $member,
395         FIELD => 'MemberId',
396         VALUE => $self->CurrentUser->Id,
397     );
398
399     return;
400 }
401
402 =head2 LimitToEnabled
403
404 Only find items that haven't been disabled
405
406 =cut
407
408 sub LimitToEnabled {
409     my $self = shift;
410
411     $self->{'handled_disabled_column'} = 1;
412     $self->Limit(
413         ALIAS => $self->PrincipalsAlias,
414         FIELD => 'Disabled',
415         VALUE => '0',
416     );
417 }
418
419
420 =head2 LimitToDeleted
421
422 Only find items that have been deleted.
423
424 =cut
425
426 sub LimitToDeleted {
427     my $self = shift;
428     
429     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
430     $self->Limit(
431         ALIAS => $self->PrincipalsAlias,
432         FIELD => 'Disabled',
433         VALUE => 1,
434     );
435 }
436
437
438
439 sub AddRecord {
440     my $self = shift;
441     my ($record) = @_;
442
443     # If we've explicitly limited to groups the user is a member of (for
444     # dashboard or savedsearch privacy objects), skip the ACL.
445     return unless $self->{with_current_user}
446         or $record->CurrentUserHasRight('SeeGroup');
447
448     return $self->SUPER::AddRecord( $record );
449 }
450
451
452
453 sub _DoSearch {
454     my $self = shift;
455     
456     #unless we really want to find disabled rows, make sure we're only finding enabled ones.
457     unless($self->{'find_disabled_rows'}) {
458         $self->LimitToEnabled();
459     }
460     
461     return($self->SUPER::_DoSearch(@_));
462     
463 }
464
465
466
467 =head2 NewItem
468
469 Returns an empty new RT::Group item
470
471 =cut
472
473 sub NewItem {
474     my $self = shift;
475     return(RT::Group->new($self->CurrentUser));
476 }
477 RT::Base->_ImportOverlays();
478
479 1;