a7b84a435ec57d26ff736f21bf78a2ca4728af03
[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     $self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
267         if $args{'Recursively'};
268 }
269
270 sub WithoutMember {
271     my $self = shift;
272     my %args = (
273         PrincipalId => undef,
274         Recursively => undef,
275         @_
276     );
277
278     my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
279     my $members_alias = $self->Join(
280         TYPE   => 'LEFT',
281         FIELD1 => 'id',
282         TABLE2 => $members,
283         FIELD2 => 'GroupId',
284     );
285     $self->Limit(
286         LEFTJOIN => $members_alias,
287         ALIAS    => $members_alias,
288         FIELD    => 'MemberId',
289         OPERATOR => '=',
290         VALUE    => $args{'PrincipalId'},
291     );
292     $self->Limit(
293         LEFTJOIN => $members_alias,
294         ALIAS    => $members_alias,
295         FIELD    => 'Disabled',
296         VALUE    => 0
297     ) if $args{'Recursively'};
298     $self->Limit(
299         ALIAS    => $members_alias,
300         FIELD    => 'MemberId',
301         OPERATOR => 'IS',
302         VALUE    => 'NULL',
303         QUOTEVALUE => 0,
304     );
305 }
306
307 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
308
309
310 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.
311
312
313
314 =cut
315
316 #XXX: should be generilized
317 sub WithRight {
318     my $self = shift;
319     my %args = ( Right                  => undef,
320                  Object =>              => undef,
321                  IncludeSystemRights    => 1,
322                  IncludeSuperusers      => undef,
323                  IncludeSubgroupMembers => 0,
324                  EquivObjects           => [ ],
325                  @_ );
326
327     my $from_role = $self->Clone;
328     $from_role->WithRoleRight( %args );
329
330     my $from_group = $self->Clone;
331     $from_group->WithGroupRight( %args );
332
333     #XXX: DIRTY HACK
334     use DBIx::SearchBuilder 1.50; #no version on ::Union :(
335     use DBIx::SearchBuilder::Union;
336     my $union = new DBIx::SearchBuilder::Union;
337     $union->add($from_role);
338     $union->add($from_group);
339     %$self = %$union;
340     bless $self, ref($union);
341
342     return;
343 }
344
345 #XXX: methods are active aliases to Users class to prevent code duplication
346 # should be generalized
347 sub _JoinGroups {
348     my $self = shift;
349     my %args = (@_);
350     return 'main' unless $args{'IncludeSubgroupMembers'};
351     return $self->RT::Users::_JoinGroups( %args );
352 }
353 sub _JoinGroupMembers {
354     my $self = shift;
355     my %args = (@_);
356     return 'main' unless $args{'IncludeSubgroupMembers'};
357     return $self->RT::Users::_JoinGroupMembers( %args );
358 }
359 sub _JoinGroupMembersForGroupRights {
360     my $self = shift;
361     my %args = (@_);
362     my $group_members = $self->_JoinGroupMembers( %args );
363     unless( $group_members eq 'main' ) {
364         return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
365     }
366     $self->Limit( ALIAS => $args{'ACLAlias'},
367                   FIELD => 'PrincipalId',
368                   VALUE => "main.id",
369                   QUOTEVALUE => 0,
370                 );
371 }
372 sub _JoinACL                  { return (shift)->RT::Users::_JoinACL( @_ ) }
373 sub _RoleClauses              { return (shift)->RT::Users::_RoleClauses( @_ ) }
374 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
375 sub _GetEquivObjects          { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
376 sub WithGroupRight            { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
377 sub WithRoleRight             { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
378
379 # {{{ sub LimitToEnabled
380
381 =head2 LimitToEnabled
382
383 Only find items that haven\'t been disabled
384
385 =cut
386
387 sub LimitToEnabled {
388     my $self = shift;
389
390     $self->{'handled_disabled_column'} = 1;
391     $self->Limit(
392         ALIAS => $self->PrincipalsAlias,
393         FIELD => 'Disabled',
394         VALUE => '0',
395     );
396 }
397 # }}}
398
399 # {{{ sub LimitToDisabled
400
401 =head2 LimitToDeleted
402
403 Only find items that have been deleted.
404
405 =cut
406
407 sub LimitToDeleted {
408     my $self = shift;
409     
410     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
411     $self->Limit(
412         ALIAS => $self->PrincipalsAlias,
413         FIELD => 'Disabled',
414         VALUE => 1,
415     );
416 }
417
418 # }}}
419
420 # {{{ sub Next
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 1;
455