1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2011 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";
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";
91 $self->{'with_disabled_column'} = 1;
93 my @result = $self->SUPER::_Init(@_);
95 $self->OrderBy( ALIAS => 'main',
99 # XXX: this code should be generalized
100 $self->{'princalias'} = $self->Join(
103 TABLE2 => 'Principals',
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',
119 =head2 PrincipalsAlias
121 Returns the string that represents this Users object's primary "Principals" alias.
125 # XXX: should be generalized, code duplication
126 sub PrincipalsAlias {
128 return($self->{'princalias'});
133 # {{{ LimitToSystemInternalGroups
135 =head2 LimitToSystemInternalGroups
137 Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
142 sub LimitToSystemInternalGroups {
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');
152 # {{{ LimitToUserDefinedGroups
154 =head2 LimitToUserDefinedGroups
156 Return only UserDefined Groups
161 sub LimitToUserDefinedGroups {
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 => '');
171 # {{{ LimitToPersonalGroupsFor
173 =head2 LimitToPersonalGroupsFor PRINCIPAL_ID
175 Return only Personal Groups for the user whose principal id
181 sub LimitToPersonalGroupsFor {
185 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal');
186 $self->Limit( FIELD => 'Instance',
194 # {{{ LimitToRolesForQueue
196 =head2 LimitToRolesForQueue QUEUE_ID
198 Limits the set of groups found to role groups for queue QUEUE_ID
202 sub LimitToRolesForQueue {
205 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Queue-Role');
206 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $queue);
211 # {{{ LimitToRolesForTicket
213 =head2 LimitToRolesForTicket Ticket_ID
215 Limits the set of groups found to role groups for Ticket Ticket_ID
219 sub LimitToRolesForTicket {
222 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::Ticket-Role');
223 $self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '$Ticket');
228 # {{{ LimitToRolesForSystem
230 =head2 LimitToRolesForSystem System_ID
232 Limits the set of groups found to role groups for System System_ID
236 sub LimitToRolesForSystem {
238 $self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'RT::System-Role');
243 =head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
245 Limits the set of groups returned to groups which have
246 Principal PRINCIPAL_ID as a member
252 my %args = ( PrincipalId => undef,
253 Recursively => undef,
257 if ($args{'Recursively'}) {
258 $members = $self->NewAlias('CachedGroupMembers');
260 $members = $self->NewAlias('GroupMembers');
262 $self->Join(ALIAS1 => 'main', FIELD1 => 'id',
263 ALIAS2 => $members, FIELD2 => 'GroupId');
265 $self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
271 PrincipalId => undef,
272 Recursively => undef,
276 my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
277 my $members_alias = $self->Join(
284 LEFTJOIN => $members_alias,
285 ALIAS => $members_alias,
288 VALUE => $args{'PrincipalId'},
291 ALIAS => $members_alias,
299 =head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
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.
308 #XXX: should be generilized
311 my %args = ( Right => undef,
313 IncludeSystemRights => 1,
314 IncludeSuperusers => undef,
315 IncludeSubgroupMembers => 0,
319 my $from_role = $self->Clone;
320 $from_role->WithRoleRight( %args );
322 my $from_group = $self->Clone;
323 $from_group->WithGroupRight( %args );
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);
332 bless $self, ref($union);
337 #XXX: methods are active aliases to Users class to prevent code duplication
338 # should be generalized
342 return 'main' unless $args{'IncludeSubgroupMembers'};
343 return $self->RT::Users::_JoinGroups( %args );
345 sub _JoinGroupMembers {
348 return 'main' unless $args{'IncludeSubgroupMembers'};
349 return $self->RT::Users::_JoinGroupMembers( %args );
351 sub _JoinGroupMembersForGroupRights {
354 my $group_members = $self->_JoinGroupMembers( %args );
355 unless( $group_members eq 'main' ) {
356 return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
358 $self->Limit( ALIAS => $args{'ACLAlias'},
359 FIELD => 'PrincipalId',
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( @_ ) }
371 # {{{ sub LimitToEnabled
373 =head2 LimitToEnabled
375 Only find items that haven\'t been disabled
382 $self->{'handled_disabled_column'} = 1;
384 ALIAS => $self->PrincipalsAlias,
391 # {{{ sub LimitToDisabled
393 =head2 LimitToDeleted
395 Only find items that have been deleted.
402 $self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
404 ALIAS => $self->PrincipalsAlias,
417 # Don't show groups which the user isn't allowed to see.
419 my $Group = $self->SUPER::Next();
420 if ((defined($Group)) and (ref($Group))) {
421 unless ($Group->CurrentUserHasRight('SeeGroup')) {
422 return $self->Next();
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();
442 return($self->SUPER::_DoSearch(@_));