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