1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
51 RT::Groups - a collection of RT::Group objects
56 my $groups = RT::Groups->new($CurrentUser);
58 while (my $group = $groups->Next()) {
59 print $group->Id ." is a group id\n";
75 no warnings qw(redefine);
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.
89 $self->{'table'} = "Groups";
90 $self->{'primary_key'} = "id";
92 my @result = $self->SUPER::_Init(@_);
94 $self->OrderBy( ALIAS => 'main',
98 # XXX: this code should be generalized
99 $self->{'princalias'} = $self->Join(
102 TABLE2 => 'Principals',
106 # even if this condition is useless and ids in the Groups table
107 # only match principals with type 'Group' this could speed up
108 # searches in some DBs.
109 $self->Limit( ALIAS => $self->{'princalias'},
110 FIELD => 'PrincipalType',
118 =head2 PrincipalsAlias
120 Returns the string that represents this Users object's primary "Principals" alias.
124 # XXX: should be generalized, code duplication
125 sub PrincipalsAlias {
127 return($self->{'princalias'});
132 # {{{ LimiToSystemInternalGroups
134 =head2 LimitToSystemInternalGroups
136 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
141 sub LimitToSystemInternalGroups {
143 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal');
144 # All system internal groups have the same instance. No reason to limit down further
145 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
151 # {{{ LimiToUserDefinedGroups
153 =head2 LimitToUserDefinedGroups
155 Return only UserDefined Groups
160 sub LimitToUserDefinedGroups {
162 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined');
163 # All user-defined groups have the same instance. No reason to limit down further
164 #$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
170 # {{{ LimiToPersonalGroupsFor
172 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
174 Return only Personal Groups for the user whose principal id
180 sub LimitToPersonalGroupsFor {
184 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
185 $self->Limit( FIELD => 'Instance',
193 # {{{ LimitToRolesForQueue
195 =head2 LimitToRolesForQueue QUEUE_ID
197 Limits the set of groups found to role groups for queue QUEUE_ID
201 sub LimitToRolesForQueue {
204 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
205 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
210 # {{{ LimitToRolesForTicket
212 =head2 LimitToRolesForTicket Ticket_ID
214 Limits the set of groups found to role groups for Ticket Ticket_ID
218 sub LimitToRolesForTicket {
221 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
222 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
227 # {{{ LimitToRolesForSystem
229 =head2 LimitToRolesForSystem System_ID
231 Limits the set of groups found to role groups for System System_ID
235 sub LimitToRolesForSystem {
237 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
242 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
244 Limits the set of groups returned to groups which have
245 Principal PRINCIPAL_ID as a member
251 my %args = ( PrincipalId => undef,
252 Recursively => undef,
256 if ($args{'Recursively'}) {
257 $members = $self->NewAlias('CachedGroupMembers');
259 $members = $self->NewAlias('GroupMembers');
261 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
262 ALIAS2 => $members, FIELD2 => 'GroupId');
264 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
270 PrincipalId => undef,
271 Recursively => undef,
275 my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
276 my $members_alias = $self->Join(
283 LEFTJOIN => $members_alias,
284 ALIAS => $members_alias,
287 VALUE => $args{'PrincipalId'},
290 ALIAS => $members_alias,
298 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
301 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.
307 #XXX: should be generilized
310 my %args = ( Right => undef,
312 IncludeSystemRights => 1,
313 IncludeSuperusers => undef,
314 IncludeSubgroupMembers => 0,
318 my $from_role = $self->Clone;
319 $from_role->WithRoleRight( %args );
321 my $from_group = $self->Clone;
322 $from_group->WithGroupRight( %args );
325 use DBIx::SearchBuilder 1.50; #no version on ::Union :(
326 use DBIx::SearchBuilder::Union;
327 my $union = new DBIx::SearchBuilder::Union;
328 $union->add($from_role);
329 $union->add($from_group);
331 bless $self, ref($union);
336 #XXX: methods are active aliases to Users class to prevent code duplication
337 # should be generalized
341 return 'main' unless $args{'IncludeSubgroupMembers'};
342 return $self->RT::Users::_JoinGroups( %args );
344 sub _JoinGroupMembers {
347 return 'main' unless $args{'IncludeSubgroupMembers'};
348 return $self->RT::Users::_JoinGroupMembers( %args );
350 sub _JoinGroupMembersForGroupRights {
353 my $group_members = $self->_JoinGroupMembers( %args );
354 unless( $group_members eq 'main' ) {
355 return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
357 $self->Limit( ALIAS => $args{'ACLAlias'},
358 FIELD => 'PrincipalId',
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( @_ ) }
370 # {{{ sub LimitToEnabled
372 =head2 LimitToEnabled
374 Only find items that haven\'t been disabled
381 $self->Limit( ALIAS => $self->PrincipalsAlias,
389 # {{{ sub LimitToDisabled
391 =head2 LimitToDeleted
393 Only find items that have been deleted.
400 $self->{'find_disabled_rows'} = 1;
401 $self->Limit( ALIAS => $self->PrincipalsAlias,
414 # Don't show groups which the user isn't allowed to see.
416 my $Group = $self->SUPER::Next();
417 if ((defined($Group)) and (ref($Group))) {
418 unless ($Group->CurrentUserHasRight('SeeGroup')) {
419 return $self->Next();
434 #unless we really want to find disabled rows, make sure we\'re only finding enabled ones.
435 unless($self->{'find_disabled_rows'}) {
436 $self->LimitToEnabled();
439 return($self->SUPER::_DoSearch(@_));