starting to work...
[freeside.git] / rt / lib / RT / Groups.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 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
238     return $members;
239 }
240
241 sub WithoutMember {
242     my $self = shift;
243     my %args = (
244         PrincipalId => undef,
245         Recursively => undef,
246         @_
247     );
248
249     my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
250     my $members_alias = $self->Join(
251         TYPE   => 'LEFT',
252         FIELD1 => 'id',
253         TABLE2 => $members,
254         FIELD2 => 'GroupId',
255     );
256     $self->Limit(
257         LEFTJOIN => $members_alias,
258         ALIAS    => $members_alias,
259         FIELD    => 'MemberId',
260         OPERATOR => '=',
261         VALUE    => $args{'PrincipalId'},
262     );
263     $self->Limit(
264         ALIAS    => $members_alias,
265         FIELD    => 'MemberId',
266         OPERATOR => 'IS',
267         VALUE    => 'NULL',
268         QUOTEVALUE => 0,
269     );
270 }
271
272 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
273
274
275 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.
276
277
278
279 =cut
280
281 #XXX: should be generilized
282 sub WithRight {
283     my $self = shift;
284     my %args = ( Right                  => undef,
285                  Object =>              => undef,
286                  IncludeSystemRights    => 1,
287                  IncludeSuperusers      => undef,
288                  IncludeSubgroupMembers => 0,
289                  EquivObjects           => [ ],
290                  @_ );
291
292     my $from_role = $self->Clone;
293     $from_role->WithRoleRight( %args );
294
295     my $from_group = $self->Clone;
296     $from_group->WithGroupRight( %args );
297
298     #XXX: DIRTY HACK
299     use DBIx::SearchBuilder 1.50; #no version on ::Union :(
300     use DBIx::SearchBuilder::Union;
301     my $union = DBIx::SearchBuilder::Union->new();
302     $union->add($from_role);
303     $union->add($from_group);
304     %$self = %$union;
305     bless $self, ref($union);
306
307     return;
308 }
309
310 #XXX: methods are active aliases to Users class to prevent code duplication
311 # should be generalized
312 sub _JoinGroups {
313     my $self = shift;
314     my %args = (@_);
315     return 'main' unless $args{'IncludeSubgroupMembers'};
316     return $self->RT::Users::_JoinGroups( %args );
317 }
318 sub _JoinGroupMembers {
319     my $self = shift;
320     my %args = (@_);
321     return 'main' unless $args{'IncludeSubgroupMembers'};
322     return $self->RT::Users::_JoinGroupMembers( %args );
323 }
324 sub _JoinGroupMembersForGroupRights {
325     my $self = shift;
326     my %args = (@_);
327     my $group_members = $self->_JoinGroupMembers( %args );
328     unless( $group_members eq 'main' ) {
329         return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
330     }
331     $self->Limit( ALIAS => $args{'ACLAlias'},
332                   FIELD => 'PrincipalId',
333                   VALUE => "main.id",
334                   QUOTEVALUE => 0,
335                 );
336 }
337 sub _JoinACL                  { return (shift)->RT::Users::_JoinACL( @_ ) }
338 sub _RoleClauses              { return (shift)->RT::Users::_RoleClauses( @_ ) }
339 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
340 sub _GetEquivObjects          { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
341 sub WithGroupRight            { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
342 sub WithRoleRight             { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
343
344 sub ForWhichCurrentUserHasRight {
345     my $self = shift;
346     my %args = (
347         Right => undef,
348         IncludeSuperusers => undef,
349         @_,
350     );
351
352     # Non-disabled groups...
353     $self->LimitToEnabled;
354
355     # ...which are the target object of an ACL with that right, or
356     # where the target is the system object (a global right)
357     my $acl = $self->_JoinACL( %args );
358     $self->_AddSubClause(
359         ACLObjects => "( (main.id = $acl.ObjectId AND $acl.ObjectType = 'RT::Group')"
360                    . " OR $acl.ObjectType = 'RT::System')");
361
362     # ...and where that right is granted to any group..
363     my $member = $self->Join(
364         ALIAS1 => $acl,
365         FIELD1 => 'PrincipalId',
366         TABLE2 => 'CachedGroupMembers',
367         FIELD2 => 'GroupId',
368     );
369     $self->Limit(
370         ALIAS => $member,
371         FIELD => 'Disabled',
372         VALUE => '0',
373     );
374
375     # ...with the current user in it
376     $self->Limit(
377         ALIAS => $member,
378         FIELD => 'MemberId',
379         VALUE => $self->CurrentUser->Id,
380     );
381
382     return;
383 }
384
385 =head2 LimitToEnabled
386
387 Only find items that haven\'t been disabled
388
389 =cut
390
391 sub LimitToEnabled {
392     my $self = shift;
393
394     $self->{'handled_disabled_column'} = 1;
395     $self->Limit(
396         ALIAS => $self->PrincipalsAlias,
397         FIELD => 'Disabled',
398         VALUE => '0',
399     );
400 }
401
402
403 =head2 LimitToDeleted
404
405 Only find items that have been deleted.
406
407 =cut
408
409 sub LimitToDeleted {
410     my $self = shift;
411     
412     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
413     $self->Limit(
414         ALIAS => $self->PrincipalsAlias,
415         FIELD => 'Disabled',
416         VALUE => 1,
417     );
418 }
419
420
421
422 sub Next {
423     my $self = shift;
424
425     # Don't show groups which the user isn't allowed to see.
426
427     my $Group = $self->SUPER::Next();
428     if ((defined($Group)) and (ref($Group))) {
429         unless ($Group->CurrentUserHasRight('SeeGroup')) {
430             return $self->Next();
431         }
432         
433         return $Group;
434     }
435     else {
436         return undef;
437     }
438 }
439
440
441
442 sub _DoSearch {
443     my $self = shift;
444     
445     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
446     unless($self->{'find_disabled_rows'}) {
447         $self->LimitToEnabled();
448     }
449     
450     return($self->SUPER::_DoSearch(@_));
451     
452 }
453
454
455
456 =head2 NewItem
457
458 Returns an empty new RT::Group item
459
460 =cut
461
462 sub NewItem {
463     my $self = shift;
464     return(RT::Group->new($self->CurrentUser));
465 }
466 RT::Base->_ImportOverlays();
467
468 1;