merging rt \3.8.8 to HEAD
[freeside.git] / rt / lib / RT / Groups_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4
5 # This software is Copyright (c) 1996-2009 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., 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 1.50; #no version on ::Union :(
327     use DBIx::SearchBuilder::Union;
328     my $union = new DBIx::SearchBuilder::Union;
329     $union->add($from_role);
330     $union->add($from_group);
331     %$self = %$union;
332     bless $self, ref($union);
333
334     return;
335 }
336
337 #XXX: methods are active aliases to Users class to prevent code duplication
338 # should be generalized
339 sub _JoinGroups {
340     my $self = shift;
341     my %args = (@_);
342     return 'main' unless $args{'IncludeSubgroupMembers'};
343     return $self->RT::Users::_JoinGroups( %args );
344 }
345 sub _JoinGroupMembers {
346     my $self = shift;
347     my %args = (@_);
348     return 'main' unless $args{'IncludeSubgroupMembers'};
349     return $self->RT::Users::_JoinGroupMembers( %args );
350 }
351 sub _JoinGroupMembersForGroupRights {
352     my $self = shift;
353     my %args = (@_);
354     my $group_members = $self->_JoinGroupMembers( %args );
355     unless( $group_members eq 'main' ) {
356         return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
357     }
358     $self->Limit( ALIAS => $args{'ACLAlias'},
359                   FIELD => 'PrincipalId',
360                   VALUE => "main.id",
361                   QUOTEVALUE => 0,
362                 );
363 }
364 sub _JoinACL                  { return (shift)->RT::Users::_JoinACL( @_ ) }
365 sub _RoleClauses              { return (shift)->RT::Users::_RoleClauses( @_ ) }
366 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
367 sub _GetEquivObjects          { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
368 sub WithGroupRight            { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
369 sub WithRoleRight             { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
370
371 # {{{ sub LimitToEnabled
372
373 =head2 LimitToEnabled
374
375 Only find items that haven\'t been disabled
376
377 =cut
378
379 sub LimitToEnabled {
380     my $self = shift;
381
382     $self->{'handled_disabled_column'} = 1;
383     $self->Limit(
384         ALIAS => $self->PrincipalsAlias,
385         FIELD => 'Disabled',
386         VALUE => '0',
387     );
388 }
389 # }}}
390
391 # {{{ sub LimitToDisabled
392
393 =head2 LimitToDeleted
394
395 Only find items that have been deleted.
396
397 =cut
398
399 sub LimitToDeleted {
400     my $self = shift;
401     
402     $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
403     $self->Limit(
404         ALIAS => $self->PrincipalsAlias,
405         FIELD => 'Disabled',
406         VALUE => 1,
407     );
408 }
409
410 # }}}
411
412 # {{{ sub Next
413
414 sub Next {
415     my $self = shift;
416
417     # Don't show groups which the user isn't allowed to see.
418
419     my $Group = $self->SUPER::Next();
420     if ((defined($Group)) and (ref($Group))) {
421         unless ($Group->CurrentUserHasRight('SeeGroup')) {
422             return $self->Next();
423         }
424         
425         return $Group;
426     }
427     else {
428         return undef;
429     }
430 }
431
432
433
434 sub _DoSearch {
435     my $self = shift;
436     
437     #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
438     unless($self->{'find_disabled_rows'}) {
439         $self->LimitToEnabled();
440     }
441     
442     return($self->SUPER::_DoSearch(@_));
443     
444 }
445
446 1;
447