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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809
|
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
#
# This software is Copyright (c) 1996-2018 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 SYNOPSIS
use RT::ACE;
my $ace = RT::ACE->new($CurrentUser);
=head1 DESCRIPTION
=head1 METHODS
=cut
package RT::ACE;
use base 'RT::Record';
sub Table {'ACL'}
use strict;
use warnings;
require RT::Principals;
require RT::Queues;
require RT::Groups;
our %RIGHTS;
my (@_ACL_CACHE_HANDLERS);
=head1 Rights
# Queue rights are the sort of queue rights that can only be granted
# to real people or groups
=cut
=head2 LoadByValues PARAMHASH
Load an ACE by specifying a paramhash with the following fields:
PrincipalId => undef,
PrincipalType => undef,
RightName => undef,
And either:
Object => undef,
OR
ObjectType => undef,
ObjectId => undef
=cut
sub LoadByValues {
my $self = shift;
my %args = ( PrincipalId => undef,
PrincipalType => undef,
RightName => undef,
Object => undef,
ObjectId => undef,
ObjectType => undef,
@_ );
if ( $args{'RightName'} ) {
my $canonic_name = $self->CanonicalizeRightName( $args{'RightName'} );
unless ( $canonic_name ) {
return wantarray ? ( 0, $self->loc("Invalid right. Couldn't canonicalize right '[_1]'", $args{'RightName'}) ) : 0;
}
$args{'RightName'} = $canonic_name;
}
my $princ_obj;
( $princ_obj, $args{'PrincipalType'} ) =
$self->_CanonicalizePrincipal( $args{'PrincipalId'},
$args{'PrincipalType'} );
unless ( $princ_obj->id ) {
return wantarray ? ( 0,
$self->loc( 'Principal [_1] not found.', $args{'PrincipalId'} )
) : 0;
}
my ($object, $object_type, $object_id) = $self->_ParseObjectArg( %args );
unless( $object ) {
return wantarray ? ( 0, $self->loc("System error. Right not granted.")) : 0;
}
$self->LoadByCols( PrincipalId => $princ_obj->Id,
PrincipalType => $args{'PrincipalType'},
RightName => $args{'RightName'},
ObjectType => $object_type,
ObjectId => $object_id);
#If we couldn't load it.
unless ( $self->Id ) {
return wantarray ? ( 0, $self->loc("ACE not found") ) : 0;
}
# if we could
return wantarray ? ( $self->Id, $self->loc("Right Loaded") ) : $self->Id;
}
=head2 Create <PARAMS>
PARAMS is a parameter hash with the following elements:
PrincipalId => The id of an RT::Principal object
PrincipalType => "User" "Group" or any Role type
RightName => the name of a right. in any case
Either:
Object => An object to create rights for. ususally, an RT::Queue or RT::Group
This should always be a DBIx::SearchBuilder::Record subclass
OR
ObjectType => the type of the object in question (ref ($object))
ObjectId => the id of the object in question $object->Id
Returns a tuple of (STATUS, MESSAGE); If the call succeeded, STATUS is true. Otherwise it's false.
=cut
sub Create {
my $self = shift;
my %args = (
PrincipalId => undef,
PrincipalType => undef,
RightName => undef,
Object => undef,
@_
);
unless ( $args{'RightName'} ) {
return ( 0, $self->loc('No right specified') );
}
#if we haven't specified any sort of right, we're talking about a global right
if (!defined $args{'Object'} && !defined $args{'ObjectId'} && !defined $args{'ObjectType'}) {
$args{'Object'} = $RT::System;
}
($args{'Object'}, $args{'ObjectType'}, $args{'ObjectId'}) = $self->_ParseObjectArg( %args );
unless( $args{'Object'} ) {
return ( 0, $self->loc("System error. Right not granted.") );
}
# Validate the principal
my $princ_obj;
( $princ_obj, $args{'PrincipalType'} ) =
$self->_CanonicalizePrincipal( $args{'PrincipalId'},
$args{'PrincipalType'} );
unless ( $princ_obj->id ) {
return ( 0,
$self->loc( 'Principal [_1] not found.', $args{'PrincipalId'} )
);
}
# }}}
# Check the ACL
if (ref( $args{'Object'}) eq 'RT::Group' ) {
unless ( $self->CurrentUser->HasRight( Object => $args{'Object'},
Right => 'AdminGroup' )
) {
return ( 0, $self->loc('Permission Denied') );
}
}
else {
unless ( $self->CurrentUser->HasRight( Object => $args{'Object'}, Right => 'ModifyACL' )) {
return ( 0, $self->loc('Permission Denied') );
}
}
# }}}
# Canonicalize and check the right name
my $canonic_name = $self->CanonicalizeRightName( $args{'RightName'} );
unless ( $canonic_name ) {
return ( 0, $self->loc("Invalid right. Couldn't canonicalize right '[_1]'", $args{'RightName'}) );
}
$args{'RightName'} = $canonic_name;
#check if it's a valid RightName
if ( $args{'Object'}->can('AvailableRights') ) {
my $available = $args{'Object'}->AvailableRights($princ_obj);
unless ( grep $_ eq $args{'RightName'}, map $self->CanonicalizeRightName( $_ ), keys %$available ) {
$RT::Logger->warning(
"Couldn't validate right name '$args{'RightName'}'"
." for object of ". ref( $args{'Object'} ) ." class"
);
return ( 0, $self->loc('Invalid right') );
}
}
# }}}
# Make sure the right doesn't already exist.
$self->LoadByCols( PrincipalId => $princ_obj->id,
PrincipalType => $args{'PrincipalType'},
RightName => $args{'RightName'},
ObjectType => $args{'ObjectType'},
ObjectId => $args{'ObjectId'},
);
if ( $self->Id ) {
return ( 0, $self->loc('[_1] already has the right [_2] on [_3] [_4]',
$princ_obj->DisplayName, $args{'RightName'}, $args{'ObjectType'}, $args{'ObjectId'}) );
}
my $id = $self->SUPER::Create( PrincipalId => $princ_obj->id,
PrincipalType => $args{'PrincipalType'},
RightName => $args{'RightName'},
ObjectType => ref( $args{'Object'} ),
ObjectId => $args{'Object'}->id,
);
if ( $id ) {
RT::ACE->InvalidateCaches(
Action => "Grant",
RightName => $self->RightName,
ACE => $self,
);
return ( $id, $self->loc("Granted right '[_1]' to [_2].", $self->RightName, $princ_obj->DisplayName));
}
else {
return ( 0, $self->loc('System error. Right not granted.') );
}
}
=head2 Delete { InsideTransaction => undef}
Delete this object. This method should ONLY ever be called from RT::User or RT::Group (or from itself)
If this is being called from within a transaction, specify a true value for the parameter InsideTransaction.
Really, DBIx::SearchBuilder should use and/or fake subtransactions
This routine will also recurse and delete any delegations of this right
=cut
sub Delete {
my $self = shift;
unless ( $self->Id ) {
return ( 0, $self->loc('Right not loaded.') );
}
# A user can delete an ACE if the current user has the right to modify it and it's not a delegated ACE
# or if it's a delegated ACE and it was delegated by the current user
unless ($self->CurrentUser->HasRight(Right => 'ModifyACL', Object => $self->Object)) {
return ( 0, $self->loc('Permission Denied') );
}
$self->_Delete(@_);
}
# Helper for Delete with no ACL check
sub _Delete {
my $self = shift;
my %args = ( InsideTransaction => undef,
@_ );
my $InsideTransaction = $args{'InsideTransaction'};
$RT::Handle->BeginTransaction() unless $InsideTransaction;
my $right = $self->RightName;
my ( $val, $msg ) = $self->SUPER::Delete(@_);
if ($val) {
RT::ACE->InvalidateCaches( Action => "Revoke", RightName => $right );
$RT::Handle->Commit() unless $InsideTransaction;
return ( $val, $self->loc("Revoked right '[_1]' from [_2].", $right, $self->PrincipalObj->DisplayName));
}
$RT::Handle->Rollback() unless $InsideTransaction;
return ( 0, $self->loc('Right could not be revoked') );
}
=head2 _BootstrapCreate
Grant a right with no error checking and no ACL. this is _only_ for
installation. If you use this routine without the author's explicit
written approval, he will hunt you down and make you spend eternity
translating mozilla's code into FORTRAN or intercal.
If you think you need this routine, you've mistaken.
=cut
sub _BootstrapCreate {
my $self = shift;
my %args = (@_);
# When bootstrapping, make sure we get the _right_ users
if ( $args{'UserId'} ) {
my $user = RT::User->new( $self->CurrentUser );
$user->Load( $args{'UserId'} );
delete $args{'UserId'};
$args{'PrincipalId'} = $user->PrincipalId;
$args{'PrincipalType'} = 'User';
}
my $id = $self->SUPER::Create(%args);
if ( $id > 0 ) {
return ($id);
}
else {
$RT::Logger->err('System error. right not granted.');
return (undef);
}
}
=head2 InvalidateCaches
Calls any registered ACL cache handlers (see L</RegisterCacheHandler>).
Usually called from L</Create> and L</Delete>.
=cut
sub InvalidateCaches {
my $class = shift;
for my $handler (@_ACL_CACHE_HANDLERS) {
next unless ref($handler) eq "CODE";
$handler->(@_);
}
}
=head2 RegisterCacheHandler
Class method. Takes a coderef and adds it to the ACL cache handlers. These
handlers are called by L</InvalidateCaches>, usually called itself from
L</Create> and L</Delete>.
The handlers are passed a hash which may contain any (or none) of these
optional keys:
=over
=item Action
A string indicating the action that (may have) invalidated the cache. Expected
values are currently:
=over
=item Grant
=item Revoke
=back
However, other values may be passed in the future.
=item RightName
The (canonicalized) right being granted or revoked.
=item ACE
The L<RT::ACE> object just created.
=back
Your handler should be flexible enough to account for additional arguments
being passed in the future.
=cut
sub RegisterCacheHandler {
push @_ACL_CACHE_HANDLERS, $_[1];
}
sub RightName {
my $self = shift;
my $val = $self->_Value('RightName');
return $val unless $val;
my $available = $self->Object->AvailableRights;
foreach my $right ( keys %$available ) {
return $right if $val eq $self->CanonicalizeRightName($right);
}
$RT::Logger->error("Invalid right. Couldn't canonicalize right '$val'");
return $val;
}
=head2 CanonicalizeRightName <RIGHT>
Takes a queue or system right name in any case and returns it in
the correct case. If it's not found, will return undef.
=cut
sub CanonicalizeRightName {
my $self = shift;
my $name = shift;
for my $class (sort keys %RIGHTS) {
return $RIGHTS{$class}{ lc $name }{Name}
if $RIGHTS{$class}{ lc $name };
}
return undef;
}
=head2 Object
If the object this ACE applies to is a queue, returns the queue object.
If the object this ACE applies to is a group, returns the group object.
If it's the system object, returns undef.
If the user has no rights, returns undef.
=cut
sub Object {
my $self = shift;
my $appliesto_obj;
if ($self->__Value('ObjectType') && $self->__Value('ObjectType')->DOES('RT::Record::Role::Rights') ) {
$appliesto_obj = $self->__Value('ObjectType')->new($self->CurrentUser);
unless (ref( $appliesto_obj) eq $self->__Value('ObjectType')) {
return undef;
}
$appliesto_obj->Load( $self->__Value('ObjectId') );
return ($appliesto_obj);
}
else {
$RT::Logger->warning( "$self -> Object called for an object "
. "of an unknown type:"
. $self->__Value('ObjectType') );
return (undef);
}
}
=head2 PrincipalObj
Returns the RT::Principal object for this ACE.
=cut
sub PrincipalObj {
my $self = shift;
my $princ_obj = RT::Principal->new( $self->CurrentUser );
$princ_obj->Load( $self->__Value('PrincipalId') );
unless ( $princ_obj->Id ) {
$RT::Logger->err(
"ACE " . $self->Id . " couldn't load its principal object" );
}
return ($princ_obj);
}
sub _Set {
my $self = shift;
return ( 0, $self->loc("ACEs can only be created and deleted.") );
}
sub _Value {
my $self = shift;
if ( $self->PrincipalObj->IsGroup
&& $self->PrincipalObj->Object->HasMemberRecursively(
$self->CurrentUser->PrincipalObj
)
) {
return ( $self->__Value(@_) );
}
elsif ( $self->CurrentUser->HasRight(Right => 'ShowACL', Object => $self->Object) ) {
return ( $self->__Value(@_) );
}
else {
return undef;
}
}
=head2 _CanonicalizePrincipal (PrincipalId, PrincipalType)
Takes a principal id and a principal type.
If the principal is a user, resolves it to the proper acl equivalence group.
Returns a tuple of (RT::Principal, PrincipalType) for the principal we really want to work with
=cut
sub _CanonicalizePrincipal {
my $self = shift;
my $princ_id = shift;
my $princ_type = shift || '';
my $princ_obj = RT::Principal->new(RT->SystemUser);
$princ_obj->Load($princ_id);
unless ( $princ_obj->Id ) {
use Carp;
$RT::Logger->crit(Carp::longmess);
$RT::Logger->crit("Can't load a principal for id $princ_id");
return ( $princ_obj, undef );
}
# Rights never get granted to users. they get granted to their
# ACL equivalence groups
if ( $princ_type eq 'User' ) {
my $equiv_group = RT::Group->new( $self->CurrentUser );
$equiv_group->LoadACLEquivalenceGroup($princ_obj);
unless ( $equiv_group->Id ) {
$RT::Logger->crit( "No ACL equiv group for princ " . $princ_obj->id );
return ( RT::Principal->new(RT->SystemUser), undef );
}
$princ_obj = $equiv_group->PrincipalObj();
$princ_type = 'Group';
}
return ( $princ_obj, $princ_type );
}
sub _ParseObjectArg {
my $self = shift;
my %args = ( Object => undef,
ObjectId => undef,
ObjectType => undef,
@_ );
if( $args{'Object'} && ($args{'ObjectId'} || $args{'ObjectType'}) ) {
$RT::Logger->crit( "Method called with an ObjectType or an ObjectId and Object args" );
return ();
} elsif( $args{'Object'} && ref($args{'Object'}) && !$args{'Object'}->can('id') ) {
$RT::Logger->crit( "Method called called Object that has no id method" );
return ();
} elsif( $args{'Object'} ) {
my $obj = $args{'Object'};
return ($obj, ref $obj, $obj->id);
} elsif ( $args{'ObjectType'} ) {
my $obj = $args{'ObjectType'}->new( $self->CurrentUser );
$obj->Load( $args{'ObjectId'} );
return ($obj, ref $obj, $obj->id);
} else {
$RT::Logger->crit( "Method called with wrong args" );
return ();
}
}
# }}}
=head2 id
Returns the current value of id.
(In the database, id is stored as int(11).)
=cut
=head2 PrincipalType
Returns the current value of PrincipalType.
(In the database, PrincipalType is stored as varchar(25).)
=head2 SetPrincipalType VALUE
Set PrincipalType to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, PrincipalType will be stored as a varchar(25).)
=cut
=head2 PrincipalId
Returns the current value of PrincipalId.
(In the database, PrincipalId is stored as int(11).)
=head2 SetPrincipalId VALUE
Set PrincipalId to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, PrincipalId will be stored as a int(11).)
=cut
=head2 RightName
Returns the current value of RightName.
(In the database, RightName is stored as varchar(25).)
=head2 SetRightName VALUE
Set RightName to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, RightName will be stored as a varchar(25).)
=cut
=head2 ObjectType
Returns the current value of ObjectType.
(In the database, ObjectType is stored as varchar(25).)
=head2 SetObjectType VALUE
Set ObjectType to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, ObjectType will be stored as a varchar(25).)
=cut
=head2 ObjectId
Returns the current value of ObjectId.
(In the database, ObjectId is stored as int(11).)
=head2 SetObjectId VALUE
Set ObjectId to VALUE.
Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
(In the database, ObjectId will be stored as a int(11).)
=cut
=head2 Creator
Returns the current value of Creator.
(In the database, Creator is stored as int(11).)
=cut
=head2 Created
Returns the current value of Created.
(In the database, Created is stored as datetime.)
=cut
=head2 LastUpdatedBy
Returns the current value of LastUpdatedBy.
(In the database, LastUpdatedBy is stored as int(11).)
=cut
=head2 LastUpdated
Returns the current value of LastUpdated.
(In the database, LastUpdated is stored as datetime.)
=cut
sub _CoreAccessible {
{
id =>
{read => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => ''},
PrincipalType =>
{read => 1, write => 1, sql_type => 12, length => 25, is_blob => 0, is_numeric => 0, type => 'varchar(25)', default => ''},
PrincipalId =>
{read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
RightName =>
{read => 1, write => 1, sql_type => 12, length => 25, is_blob => 0, is_numeric => 0, type => 'varchar(25)', default => ''},
ObjectType =>
{read => 1, write => 1, sql_type => 12, length => 25, is_blob => 0, is_numeric => 0, type => 'varchar(25)', default => ''},
ObjectId =>
{read => 1, write => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
Creator =>
{read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
Created =>
{read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
LastUpdatedBy =>
{read => 1, auto => 1, sql_type => 4, length => 11, is_blob => 0, is_numeric => 1, type => 'int(11)', default => '0'},
LastUpdated =>
{read => 1, auto => 1, sql_type => 11, length => 0, is_blob => 0, is_numeric => 0, type => 'datetime', default => ''},
}
};
sub FindDependencies {
my $self = shift;
my ($walker, $deps) = @_;
$self->SUPER::FindDependencies($walker, $deps);
$deps->Add( out => $self->PrincipalObj->Object );
$deps->Add( out => $self->Object );
}
RT::Base->_ImportOverlays();
1;
|