1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2022 Best Practical Solutions, LLC
# <sales@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
#
#
# LICENSE:
#
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
# from www.gnu.org.
#
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
#
#
# CONTRIBUTION SUBMISSION POLICY:
#
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
#
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
=head1 NAME
RT::Groups - a collection of RT::Group objects
=head1 SYNOPSIS
use RT::Groups;
my $groups = RT::Groups->new($CurrentUser);
$groups->UnLimit();
while (my $group = $groups->Next()) {
print $group->Id ." is a group id\n";
}
=head1 DESCRIPTION
=head1 METHODS
=cut
package RT::Groups;
use strict;
use warnings;
use base 'RT::SearchBuilder';
sub Table { 'Groups'}
use RT::Group;
use RT::Users;
# XXX: below some code is marked as subject to generalize in Groups, Users classes.
# RUZ suggest name Principals::Generic or Principals::Base as abstract class, but
# Jesse wants something that doesn't imply it's a Principals.pm subclass.
# See comments below for candidats.
sub _Init {
my $self = shift;
$self->{'with_disabled_column'} = 1;
my @result = $self->SUPER::_Init(@_);
$self->OrderBy( ALIAS => 'main',
FIELD => 'Name',
ORDER => 'ASC');
# XXX: this code should be generalized
$self->{'princalias'} = $self->Join(
ALIAS1 => 'main',
FIELD1 => 'id',
TABLE2 => 'Principals',
FIELD2 => 'id'
);
# even if this condition is useless and ids in the Groups table
# only match principals with type 'Group' this could speed up
# searches in some DBs.
$self->Limit( ALIAS => $self->{'princalias'},
FIELD => 'PrincipalType',
VALUE => 'Group',
);
return (@result);
}
=head2 PrincipalsAlias
Returns the string that represents this Users object's primary "Principals" alias.
=cut
# XXX: should be generalized, code duplication
sub PrincipalsAlias {
my $self = shift;
return($self->{'princalias'});
}
=head2 LimitToSystemInternalGroups
Return only SystemInternal Groups, such as "privileged" "unprivileged" and "everyone"
=cut
sub LimitToSystemInternalGroups {
my $self = shift;
$self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'SystemInternal', CASESENSITIVE => 0 );
# All system internal groups have the same instance. No reason to limit down further
#$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '0');
}
=head2 LimitToUserDefinedGroups
Return only UserDefined Groups
=cut
sub LimitToUserDefinedGroups {
my $self = shift;
$self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => 'UserDefined', CASESENSITIVE => 0 );
# All user-defined groups have the same instance. No reason to limit down further
#$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => '');
}
=head2 LimitToRolesForObject OBJECT
Limits the set of groups to role groups specifically for the object in question
based on the object's class and ID. If the object has no ID, the roles are not
limited by group C<Instance>. That is, calling this method on an unloaded
object will find all role groups for that class of object.
Replaces L</LimitToRolesForQueue>, L</LimitToRolesForTicket>, and
L</LimitToRolesForSystem>.
=cut
sub LimitToRolesForObject {
my $self = shift;
my $object = shift;
$self->Limit(FIELD => 'Domain', OPERATOR => '=', VALUE => ref($object) . "-Role", CASESENSITIVE => 0 );
$self->Limit(FIELD => 'Instance', OPERATOR => '=', VALUE => $object->id);
}
=head2 WithMember {PrincipalId => PRINCIPAL_ID, Recursively => undef}
Limits the set of groups returned to groups which have
Principal PRINCIPAL_ID as a member. Returns the alias used for the join.
=cut
sub WithMember {
my $self = shift;
my %args = ( PrincipalId => undef,
Recursively => undef,
@_);
my $members = $self->Join(
ALIAS1 => 'main', FIELD1 => 'id',
$args{'Recursively'}
? (TABLE2 => 'CachedGroupMembers')
# (GroupId, MemberId) is unique in GM table
: (TABLE2 => 'GroupMembers', DISTINCT => 1)
,
FIELD2 => 'GroupId',
);
$self->Limit(ALIAS => $members, FIELD => 'MemberId', OPERATOR => '=', VALUE => $args{'PrincipalId'});
$self->Limit(ALIAS => $members, FIELD => 'Disabled', VALUE => 0)
if $args{'Recursively'};
return $members;
}
sub WithCurrentUser {
my $self = shift;
$self->{with_current_user} = 1;
return $self->WithMember(
PrincipalId => $self->CurrentUser->PrincipalId,
Recursively => 1,
);
}
sub WithoutMember {
my $self = shift;
my %args = (
PrincipalId => undef,
Recursively => undef,
@_
);
my $members = $args{'Recursively'} ? 'CachedGroupMembers' : 'GroupMembers';
my $members_alias = $self->Join(
TYPE => 'LEFT',
FIELD1 => 'id',
TABLE2 => $members,
FIELD2 => 'GroupId',
DISTINCT => $members eq 'GroupMembers',
);
$self->Limit(
LEFTJOIN => $members_alias,
ALIAS => $members_alias,
FIELD => 'MemberId',
OPERATOR => '=',
VALUE => $args{'PrincipalId'},
);
$self->Limit(
LEFTJOIN => $members_alias,
ALIAS => $members_alias,
FIELD => 'Disabled',
VALUE => 0
) if $args{'Recursively'};
$self->Limit(
ALIAS => $members_alias,
FIELD => 'MemberId',
OPERATOR => 'IS',
VALUE => 'NULL',
QUOTEVALUE => 0,
);
}
=head2 WithRight { Right => RIGHTNAME, Object => RT::Record, IncludeSystemRights => 1, IncludeSuperusers => 0, EquivObjects => [ ] }
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.
=cut
#XXX: should be generilized
sub WithRight {
my $self = shift;
my %args = ( Right => undef,
Object => => undef,
IncludeSystemRights => 1,
IncludeSuperusers => undef,
IncludeSubgroupMembers => 0,
EquivObjects => [ ],
@_ );
my $from_role = $self->Clone;
$from_role->WithRoleRight( %args );
my $from_group = $self->Clone;
$from_group->WithGroupRight( %args );
#XXX: DIRTY HACK
use DBIx::SearchBuilder::Union;
my $union = DBIx::SearchBuilder::Union->new();
$union->add($from_role);
$union->add($from_group);
%$self = %$union;
bless $self, ref($union);
return;
}
#XXX: methods are active aliases to Users class to prevent code duplication
# should be generalized
sub _JoinGroups {
my $self = shift;
my %args = (@_);
return 'main' unless $args{'IncludeSubgroupMembers'};
return $self->RT::Users::_JoinGroups( %args );
}
sub _JoinGroupMembers {
my $self = shift;
my %args = (@_);
return 'main' unless $args{'IncludeSubgroupMembers'};
return $self->RT::Users::_JoinGroupMembers( %args );
}
sub _JoinGroupMembersForGroupRights {
my $self = shift;
my %args = (@_);
my $group_members = $self->_JoinGroupMembers( %args );
unless( $group_members eq 'main' ) {
return $self->RT::Users::_JoinGroupMembersForGroupRights( %args );
}
$self->Limit( ALIAS => $args{'ACLAlias'},
FIELD => 'PrincipalId',
VALUE => "main.id",
QUOTEVALUE => 0,
);
}
sub _JoinACL { return (shift)->RT::Users::_JoinACL( @_ ) }
sub _RoleClauses { return (shift)->RT::Users::_RoleClauses( @_ ) }
sub _WhoHaveRoleRightSplitted { return (shift)->RT::Users::_WhoHaveRoleRightSplitted( @_ ) }
sub _GetEquivObjects { return (shift)->RT::Users::_GetEquivObjects( @_ ) }
sub WithGroupRight { return (shift)->RT::Users::WhoHaveGroupRight( @_ ) }
sub WithRoleRight { return (shift)->RT::Users::WhoHaveRoleRight( @_ ) }
sub ForWhichCurrentUserHasRight {
my $self = shift;
my %args = (
Right => undef,
IncludeSuperusers => undef,
@_,
);
# Non-disabled groups...
$self->LimitToEnabled;
# ...which are the target object of an ACL with that right, or
# where the target is the system object (a global right)
my $acl = $self->_JoinACL( %args );
$self->_AddSubClause(
ACLObjects => "( (main.id = $acl.ObjectId AND $acl.ObjectType = 'RT::Group')"
. " OR $acl.ObjectType = 'RT::System')");
# ...and where that right is granted to any group..
my $member = $self->Join(
ALIAS1 => $acl,
FIELD1 => 'PrincipalId',
TABLE2 => 'CachedGroupMembers',
FIELD2 => 'GroupId',
);
$self->Limit(
ALIAS => $member,
FIELD => 'Disabled',
VALUE => '0',
);
# ...with the current user in it
$self->Limit(
ALIAS => $member,
FIELD => 'MemberId',
VALUE => $self->CurrentUser->Id,
);
return;
}
=head2 LimitToEnabled
Only find items that haven't been disabled
=cut
sub LimitToEnabled {
my $self = shift;
$self->{'handled_disabled_column'} = 1;
$self->Limit(
ALIAS => $self->PrincipalsAlias,
FIELD => 'Disabled',
VALUE => '0',
);
}
=head2 LimitToDeleted
Only find items that have been deleted.
=cut
sub LimitToDeleted {
my $self = shift;
$self->{'handled_disabled_column'} = $self->{'find_disabled_rows'} = 1;
$self->Limit(
ALIAS => $self->PrincipalsAlias,
FIELD => 'Disabled',
VALUE => 1,
);
}
sub AddRecord {
my $self = shift;
my ($record) = @_;
# If we've explicitly limited to groups the user is a member of (for
# dashboard or savedsearch privacy objects), skip the ACL.
return unless $self->{with_current_user}
or $record->CurrentUserHasRight('SeeGroup');
return $self->SUPER::AddRecord( $record );
}
sub _DoSearch {
my $self = shift;
#unless we really want to find disabled rows, make sure we're only finding enabled ones.
unless($self->{'find_disabled_rows'}) {
$self->LimitToEnabled();
}
return($self->SUPER::_DoSearch(@_));
}
=head2 SimpleSearch
Does a 'simple' search of Groups against a specified Term.
This Term is compared to a number of fields using various types of SQL
comparison operators.
Ensures that the returned collection of Groups will have a value for Return.
This method is passed the following. You must specify a Term and a Return.
Fields - Hashref of data - defaults to C<$GroupSearchFields> emulate that if you want to override
Term - String that is in the fields specified by Fields
Return - What field on the User you want to be sure isn't empty
Exclude - Array reference of ids to exclude
Max - Size to limit this collection to
=cut
sub SimpleSearch {
my $self = shift;
my %args = (
Fields => RT->Config->Get('GroupSearchFields'),
Term => undef,
Exclude => [],
Return => 'Name',
Max => 10,
@_
);
return $self unless defined $args{Return}
and defined $args{Term}
and length $args{Term};
$self->RowsPerPage( $args{Max} );
$self->LimitToUserDefinedGroups();
while (my ($name, $op) = each %{$args{Fields}}) {
$op = 'STARTSWITH'
unless $op =~ /^(?:LIKE|(?:START|END)SWITH|=|!=)$/i;
if ($name =~ /^CF\.(?:\{(.*)}|(.*))$/) {
my $cfname = $1 || $2;
my $cf = RT::CustomField->new(RT->SystemUser);
my ($ok, $msg) = $cf->LoadByName( Name => $cfname, LookupType => 'RT::Group');
if ( $ok ) {
$self->LimitCustomField(
CUSTOMFIELD => $cf->Id,
OPERATOR => $op,
VALUE => $args{Term},
ENTRYAGGREGATOR => 'OR',
SUBCLAUSE => 'autocomplete',
CASESENSITIVE => 0,
);
} else {
RT->Logger->warning("Asked to search custom field $name but unable to load a Group CF with the name $cfname: $msg");
}
} elsif ($name eq 'id' and $op =~ /(?:LIKE|(?:START|END)SWITH)$/i) {
$self->Limit(
FUNCTION => "CAST( main.$name AS TEXT )",
OPERATOR => $op,
VALUE => $args{Term},
ENTRYAGGREGATOR => 'OR',
SUBCLAUSE => 'autocomplete',
CASESENSITIVE => 0,
) if $args{Term} =~ /^\d+$/;
} else {
$self->Limit(
FIELD => $name,
OPERATOR => $op,
VALUE => $args{Term},
ENTRYAGGREGATOR => 'OR',
SUBCLAUSE => 'autocomplete',
CASESENSITIVE => 0,
) unless $args{Term} =~ /\D/ and $name eq 'id';
}
}
# Exclude groups we don't want
$self->Limit(FIELD => 'id', OPERATOR => 'NOT IN', VALUE => $args{Exclude} )
if @{$args{Exclude}};
if ( RT->Config->Get('DatabaseType') eq 'Oracle' ) {
$self->Limit(
FIELD => $args{Return},
OPERATOR => 'IS NOT',
VALUE => 'NULL',
);
}
else {
$self->Limit( FIELD => $args{Return}, OPERATOR => '!=', VALUE => '', CASESENSITIVE => 0, );
$self->Limit(
FIELD => $args{Return},
OPERATOR => 'IS NOT',
VALUE => 'NULL',
ENTRYAGGREGATOR => 'AND',
CASESENSITIVE => 0,
);
}
return $self;
}
sub CurrentUserCanSeeAll {
my $self = shift;
return $self->CurrentUser->HasRight( Right => 'SeeGroup', Object => RT->System ) ? 1 : 0;
}
RT::Base->_ImportOverlays();
1;
|