1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
6 # <sales@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";
81 use base 'RT::SearchBuilder';
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.
96 $self->{'with_disabled_column'} = 1;
98 my @result = $self->SUPER::_Init(@_);
100 $self->OrderBy( ALIAS => 'main',
104 # XXX: this code should be generalized
105 $self->{'princalias'} = $self->Join(
108 TABLE2 => 'Principals',
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',
123 =head2 PrincipalsAlias
125 Returns the string that represents this Users object's primary "Principals" alias.
129 # XXX: should be generalized, code duplication
130 sub PrincipalsAlias {
132 return($self->{'princalias'});
138 =head2 LimitToSystemInternalGroups
140 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
145 sub LimitToSystemInternalGroups {
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');
155 =head2 LimitToUserDefinedGroups
157 Return only UserDefined Groups
162 sub LimitToUserDefinedGroups {
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 => '');
172 =head2 LimitToRolesForQueue QUEUE_ID
174 Limits the set of groups found to role groups for queue QUEUE_ID
178 sub LimitToRolesForQueue {
181 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
182 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
187 =head2 LimitToRolesForTicket Ticket_ID
189 Limits the set of groups found to role groups for Ticket Ticket_ID
193 sub LimitToRolesForTicket {
196 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
197 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
202 =head2 LimitToRolesForSystem System_ID
204 Limits the set of groups found to role groups for System System_ID
208 sub LimitToRolesForSystem {
210 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
214 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
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.
223 my %args = ( PrincipalId => undef,
224 Recursively => undef,
228 if ($args{'Recursively'}) {
229 $members = $self->NewAlias('CachedGroupMembers');
231 $members = $self->NewAlias('GroupMembers');
233 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
234 ALIAS2 => $members, FIELD2 => 'GroupId');
236 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
237 $self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
238 if $args{'Recursively'};
246 PrincipalId => undef,
247 Recursively => undef,
251 my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
252 my $members_alias = $self->Join(
259 LEFTJOIN => $members_alias,
260 ALIAS => $members_alias,
263 VALUE => $args{'PrincipalId'},
266 LEFTJOIN => $members_alias,
267 ALIAS => $members_alias,
270 ) if $args{'Recursively'};
272 ALIAS => $members_alias,
280 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
283 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.
289 #XXX: should be generilized
292 my %args = ( Right => undef,
294 IncludeSystemRights => 1,
295 IncludeSuperusers => undef,
296 IncludeSubgroupMembers => 0,
300 my $from_role = $self->Clone;
301 $from_role->WithRoleRight( %args );
303 my $from_group = $self->Clone;
304 $from_group->WithGroupRight( %args );
307 use DBIx::SearchBuilder 1.50; #no version on ::Union :(
308 use DBIx::SearchBuilder::Union;
309 my $union = DBIx::SearchBuilder::Union->new();
310 $union->add($from_role);
311 $union->add($from_group);
313 bless $self, ref($union);
318 #XXX: methods are active aliases to Users class to prevent code duplication
319 # should be generalized
323 return 'main' unless $args{'IncludeSubgroupMembers'};
324 return $self->RT::Users::_JoinGroups( %args );
326 sub _JoinGroupMembers {
329 return 'main' unless $args{'IncludeSubgroupMembers'};
330 return $self->RT::Users::_JoinGroupMembers( %args );
332 sub _JoinGroupMembersForGroupRights {
335 my $group_members = $self->_JoinGroupMembers( %args );
336 unless( $group_members eq 'main' ) {
337 return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
339 $self->Limit( ALIAS => $args{'ACLAlias'},
340 FIELD => 'PrincipalId',
345 sub _JoinACL { return (shift)->RT::Users::_JoinACL( @_ ) }
346 sub _RoleClauses { return (shift)->RT::Users::_RoleClauses( @_ ) }
347 sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
348 sub _GetEquivObjects { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
349 sub WithGroupRight { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
350 sub WithRoleRight { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
352 sub ForWhichCurrentUserHasRight {
356 IncludeSuperusers => undef,
360 # Non-disabled groups...
361 $self->LimitToEnabled;
363 # ...which are the target object of an ACL with that right, or
364 # where the target is the system object (a global right)
365 my $acl = $self->_JoinACL( %args );
366 $self->_AddSubClause(
367 ACLObjects => "( (main.id = $acl.ObjectId AND $acl.ObjectType = 'RT::Group')"
368 . " OR $acl.ObjectType = 'RT::System')");
370 # ...and where that right is granted to any group..
371 my $member = $self->Join(
373 FIELD1 => 'PrincipalId',
374 TABLE2 => 'CachedGroupMembers',
383 # ...with the current user in it
387 VALUE => $self->CurrentUser->Id,
393 =head2 LimitToEnabled
395 Only find items that haven't been disabled
402 $self->{'handled_disabled_column'} = 1;
404 ALIAS => $self->PrincipalsAlias,
411 =head2 LimitToDeleted
413 Only find items that have been deleted.
420 $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
422 ALIAS => $self->PrincipalsAlias,
433 # Don't show groups which the user isn't allowed to see.
435 my $Group = $self->SUPER::Next();
436 if ((defined($Group)) and (ref($Group))) {
437 unless ($Group->CurrentUserHasRight('SeeGroup')) {
438 return $self->Next();
453 #unless we really want to find disabled rows, make sure we're only finding enabled ones.
454 unless($self->{'find_disabled_rows'}) {
455 $self->LimitToEnabled();
458 return($self->SUPER::_DoSearch(@_));
466 Returns an empty new RT::Group item
472 return(RT::Group->new($self->CurrentUser));
474 RT::Base->_ImportOverlays();