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 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
|
<?php
require_once 'Horde/DataTree.php';
/**
* The Perms_datatree:: class provides a DataTree driver for the Horde
* permissions system.
*
* $Horde: framework/Perms/Perms/datatree.php,v 1.6.2.12 2006/07/20 18:50:05 chuck Exp $
*
* Copyright 2001-2006 Chuck Hagenbuch <chuck@horde.org>
* Copyright 2004-2006 Jan Schneider <jan@horde.org>
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @since Horde 3.0
* @package Horde_Perms
*/
class Perms_datatree extends Perms {
/**
* Pointer to a DataTree instance to manage the different
* permissions.
*
* @var DataTree
*/
var $_datatree;
/**
* Constructor.
*/
function Perms_datatree()
{
global $conf;
if (empty($conf['datatree']['driver'])) {
Horde::fatal('You must configure a DataTree backend to use the Perms DataTree driver.', __FILE__, __LINE__);
}
$driver = $conf['datatree']['driver'];
$this->_datatree = &DataTree::singleton($driver,
array_merge(Horde::getDriverConfig('datatree', $driver),
array('group' => 'horde.perms')));
}
/**
* Returns the available permissions for a given level.
*
* @param string $name The permission's name.
*
* @return array An array of available permissions and their titles.
*/
function getAvailable($name)
{
if ($name == DATATREE_ROOT) {
$name = '';
}
return parent::getAvailable($name);
}
/**
* Given a permission name, returns the title for that permission by
* looking it up in the applications's permission api.
*
* @param string $name The permissions's name.
*
* @return string The title for the permission.
*/
function getTitle($name)
{
if ($name == DATATREE_ROOT) {
return _("All Permissions");
}
return parent::getTitle($name);
}
/**
* Returns a new permissions object.
*
* @param string $name The permission's name.
*
* @return DataTreeObject_Permissions A new permissions object.
*/
function &newPermission($name)
{
if (empty($name)) {
return PEAR::raiseError('Permission names must be non-empty');
}
$type = 'matrix';
$params = null;
if ($pos = strpos($name, ':')) {
$info = $this->getApplicationPermissions(substr($name, 0, $pos));
if (!is_a($info, 'PEAR_Error')) {
if (isset($info['type']) && isset($info['type'][$name])) {
$type = $info['type'][$name];
}
if (isset($info['params']) && isset($info['params'][$name])) {
$params = $info['params'][$name];
}
}
}
$perm = &new DataTreeObject_Permission($name, $type, $params);
$perm->setDataTree($this->_datatree);
return $perm;
}
/**
* Returns a DataTreeObject_Permission object corresponding to the
* named permission, with the users and other data retrieved
* appropriately.
*
* @param string $name The name of the permission to retrieve.
*/
function &getPermission($name)
{
/* Cache of previously retrieved permissions. */
static $permsCache = array();
if (isset($permsCache[$name])) {
return $permsCache[$name];
}
$permsCache[$name] = $this->_datatree->getObject($name, 'DataTreeObject_Permission');
return $permsCache[$name];
}
/**
* Returns a DataTreeObject_Permission object corresponding to the given
* unique ID, with the users and other data retrieved appropriately.
*
* @param integer $cid The unique ID of the permission to retrieve.
*/
function &getPermissionById($cid)
{
if ($cid == DATATREE_ROOT) {
$object = &$this->newPermission(DATATREE_ROOT);
} else {
$object = &$this->_datatree->getObjectById($cid, 'DataTreeObject_Permission');
}
return $object;
}
/**
* Adds a permission to the permissions system. The permission must first
* be created with Perm::newPermission(), and have any initial users
* added to it, before this function is called.
*
* @param DataTreeObject_Permission $perm The new perm object.
*/
function addPermission(&$perm)
{
if (!is_a($perm, 'DataTreeObject_Permission')) {
return PEAR::raiseError('Permissions must be DataTreeObject_Permission objects or extend that class.');
}
return $this->_datatree->add($perm);
}
/**
* Removes a permission from the permissions system permanently.
*
* @param DataTreeObject_Permission $perm The permission to remove.
* @param boolean $force Force to remove every child.
*/
function removePermission(&$perm, $force = false)
{
if (!is_a($perm, 'DataTreeObject_Permission')) {
return PEAR::raiseError('Permissions must be DataTreeObject_Permission objects or extend that class.');
}
return $this->_datatree->remove($perm->getName(), $force);
}
/**
* Finds out what rights the given user has to this object.
*
* @param mixed $permission The full permission name of the object to
* check the permissions of, or the
* DataTreeObject_Permission object.
* @param string $user The user to check for. Defaults to the current
* user.
* @param string $creator The user who created the object.
*
* @return mixed A bitmask of permissions, a permission value, or an array
* of permission values the user has, depending on the
* permission type and whether the permission value is
* ambiguous. False if there is no such permsission.
*/
function getPermissions($permission, $user = null, $creator = null)
{
if (!is_a($permission, 'DataTreeObject_Permission')) {
$permission = &$this->getPermission($permission);
if (is_a($permission, 'PEAR_Error')) {
Horde::logMessage($permission, __FILE__, __LINE__, PEAR_LOG_DEBUG);
return false;
}
}
if ($user === null) {
$user = Auth::getAuth();
}
// If this is a guest user, only check guest permissions.
if (empty($user)) {
return $permission->getGuestPermissions();
}
// If $creator was specified, check creator permissions.
if ($creator !== null) {
// If the user is the creator of the event see if there
// are creator permissions.
if (strlen($user) && $user === $creator &&
($perms = $permission->getCreatorPermissions()) !== null) {
return $perms;
}
}
// Check user-level permissions.
$userperms = $permission->getUserPermissions();
if (isset($userperms[$user])) {
return $userperms[$user];
}
// If no user permissions are found, try group permissions.
if (isset($permission->data['groups']) &&
is_array($permission->data['groups']) &&
count($permission->data['groups'])) {
require_once 'Horde/Group.php';
$groups = &Group::singleton();
$composite_perm = null;
$type = $permission->get('type');
foreach ($permission->data['groups'] as $group => $perm) {
if ($groups->userIsInGroup($user, $group)) {
if ($composite_perm === null) {
$composite_perm = $type == 'matrix' ? 0 : array();
}
if ($type == 'matrix') {
$composite_perm |= $perm;
} else {
$composite_perm[] = $perm;
}
}
}
if ($composite_perm !== null) {
return $composite_perm;
}
}
// If there are default permissions, return them.
if (($perms = $permission->getDefaultPermissions()) !== null) {
return $perms;
}
// Otherwise, deny all permissions to the object.
return false;
}
/**
* Returns the unique identifier of this permission.
*
* @param DataTreeObject_Permission $permission The permission object to
* get the ID of.
*
* @return integer The unique id.
*/
function getPermissionId($permission)
{
return $this->_datatree->getId($permission->getName());
}
/**
* Finds out if the user has the specified rights to the given object.
*
* @param string $permission The permission to check.
* @param string $user The user to check for.
* @param integer $perm The permission level that needs to be checked
* for.
* @param string $creator The creator of the event
*
* @return boolean True if the user has the specified permissions.
*/
function hasPermission($permission, $user, $perm, $creator = null)
{
return ($this->getPermissions($permission, $user, $creator) & $perm);
}
/**
* Checks if a permission exists in the system.
*
* @param string $permission The permission to check.
*
* @return boolean True if the permission exists.
*/
function exists($permission)
{
return $this->_datatree->exists($permission);
}
/**
* Returns a list of parent permissions.
*
* @param string $child The name of the child to retrieve parents for.
*
* @return array A hash with all parents in a tree format.
*/
function getParents($child)
{
return $this->_datatree->getParents($child);
}
/**
* Returns all permissions of the system in a tree format.
*
* @return array A hash with all permissions in a tree format.
*/
function &getTree()
{
$tree = $this->_datatree->get(DATATREE_FORMAT_FLAT, DATATREE_ROOT, true);
return $tree;
}
}
/**
* Extension of the DataTreeObject class for storing Permission information in
* the DataTree driver. If you want to store specialized Permission
* information, you should extend this class instead of extending
* DataTreeObject directly.
*
* @author Chuck Hagenbuch <chuck@horde.org>
* @author Jan Schneider <jan@horde.org>
* @since Horde 2.1
* @package Horde_Perms
*/
class DataTreeObject_Permission extends DataTreeObject {
/**
* The DataTreeObject_Permission constructor. Just makes sure to call the
* parent constructor so that the perm's name is set properly.
*
* @param string $name The name of the perm.
* @param string $type The permission type.
* @param array $params A hash with any parameters that the permission
* type needs.
*/
function DataTreeObject_Permission($name, $type = 'matrix', $params = null)
{
parent::DataTreeObject($name);
$this->data['type'] = $type;
if (is_array($params)) {
$this->data['params'] = $params;
}
}
/**
* Gets one of the attributes of the object, or null if it isn't defined.
*
* @param string $attribute The attribute to get.
*
* @return mixed The value of the attribute, or null.
*/
function get($attribute)
{
$value = parent::get($attribute);
if ($value === null && $attribute == 'type') {
$value = 'matrix';
}
return $value;
}
/**
* Updates the permissions based on data passed in the array.
*
* @param array $perms An array containing the permissions which are to be
* updated.
*/
function updatePermissions($perms)
{
$type = $this->get('type');
if ($type == 'matrix') {
/* Array of permission types to iterate through. */
$perm_types = Perms::getPermsArray();
}
foreach ($perms as $perm_class => $perm_values) {
switch ($perm_class) {
case 'default':
case 'guest':
case 'creator':
if ($type == 'matrix') {
foreach ($perm_types as $val => $label) {
if (!empty($perm_values[$val])) {
$this->setPerm($perm_class, $val, false);
} else {
$this->unsetPerm($perm_class, $val, false);
}
}
} elseif (!empty($perm_values)) {
$this->setPerm($perm_class, $perm_values, false);
} else {
$this->unsetPerm($perm_class, null, false);
}
break;
case 'u':
case 'g':
$permId = array('class' => $perm_class == 'u' ? 'users' : 'groups');
/* Figure out what names that are stored in this permission
* class have not been submitted for an update, ie. have been
* removed entirely. */
$current_names = isset($this->data[$permId['class']])
? array_keys($this->data[$permId['class']])
: array();
$updated_names = array_keys($perm_values);
$removed_names = array_diff($current_names, $updated_names);
/* Remove any names that have been completely unset. */
foreach ($removed_names as $name) {
unset($this->data[$permId['class']][$name]);
}
/* If nothing to actually update finish with this case. */
if ($perm_values === null) {
continue;
}
/* Loop through the names and update permissions for each. */
foreach ($perm_values as $name => $name_values) {
$permId['name'] = $name;
if ($type == 'matrix') {
foreach ($perm_types as $val => $label) {
if (!empty($name_values[$val])) {
$this->setPerm($permId, $val, false);
} else {
$this->unsetPerm($permId, $val, false);
}
}
} elseif (!empty($name_values)) {
$this->setPerm($permId, $name_values, false);
} else {
$this->unsetPerm($permId, null, false);
}
}
break;
}
}
}
/**
* FIXME: needs docs
*/
function setPerm($permId, $permission, $update = true)
{
if (is_array($permId)) {
if (empty($permId['name'])) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data[$permId['class']][$permId['name']])) {
$this->data[$permId['class']][$permId['name']] |= $permission;
} else {
$this->data[$permId['class']][$permId['name']] = $permission;
}
} else {
if ($this->get('type') == 'matrix' &&
isset($this->data[$permId])) {
$this->data[$permId] |= $permission;
} else {
$this->data[$permId] = $permission;
}
}
if ($update) {
$this->save();
}
}
/**
* FIXME: needs docs
*/
function unsetPerm($permId, $permission, $update = true)
{
if (is_array($permId)) {
if (empty($permId['name'])) {
return;
}
if ($this->get('type') == 'matrix') {
if (isset($this->data[$permId['class']][$permId['name']])) {
$this->data[$permId['class']][$permId['name']] &= ~$permission;
if (empty($this->data[$permId['class']][$permId['name']])) {
unset($this->data[$permId['class']][$permId['name']]);
}
if ($update) {
$this->save();
}
}
} else {
unset($this->data[$permId['class']][$permId['name']]);
if ($update) {
$this->save();
}
}
} else {
if ($this->get('type') == 'matrix') {
if (isset($this->data[$permId])) {
$this->data[$permId] &= ~$permission;
if ($update) {
$this->save();
}
}
} else {
unset($this->data[$permId]);
if ($update) {
$this->save();
}
}
}
}
/**
* Grants a user additional permissions to this object.
*
* @param string $user The user to grant additional permissions
* to.
* @param constant $permission The permission (PERMS_DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
function addUserPermission($user, $permission, $update = true)
{
if (empty($user)) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data['users'][$user])) {
$this->data['users'][$user] |= $permission;
} else {
$this->data['users'][$user] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants guests additional permissions to this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
function addGuestPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['guest'])) {
$this->data['guest'] |= $permission;
} else {
$this->data['guest'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants creators additional permissions to this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
function addCreatorPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['creator'])) {
$this->data['creator'] |= $permission;
} else {
$this->data['creator'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants additional default permissions to this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
function addDefaultPermission($permission, $update = true)
{
if ($this->get('type') == 'matrix' &&
isset($this->data['default'])) {
$this->data['default'] |= $permission;
} else {
$this->data['default'] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Grants a group additional permissions to this object.
*
* @param integer $groupId The id of the group to grant additional
* permissions to.
* @param constant $permission The permission (PERMS_DELETE, etc.) to add.
* @param boolean $update Whether to automatically update the
* backend.
*/
function addGroupPermission($groupId, $permission, $update = true)
{
if (empty($groupId)) {
return;
}
if ($this->get('type') == 'matrix' &&
isset($this->data['groups'][$groupId])) {
$this->data['groups'][$groupId] |= $permission;
} else {
$this->data['groups'][$groupId] = $permission;
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that a user currently has on this object.
*
* @param string $user The user to remove the permission from.
* @param constant $permission The permission (PERMS_DELETE, etc.) to
* remove.
* @param boolean $update Whether to automatically update the
* backend.
*/
function removeUserPermission($user, $permission, $update = true)
{
if (empty($user) || !isset($this->data['users'][$user])) {
return;
}
if ($this->get('type') == 'matrix') {
$this->data['users'][$user] &= ~$permission;
if (empty($this->data['users'][$user])) {
unset($this->data['users'][$user]);
}
} else {
unset($this->data['users'][$user]);
}
if ($update) {
$this->save();
}
}
/**
* Removes a permission that guests currently have on this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to
* remove.
* @param boolean $update Whether to automatically update the
* backend.
*/
function removeGuestPermission($permission, $update = true)
{
if (!isset($this->data['guest'])) {
return;
}
if ($this->get('type') == 'matrix') {
$this->data['guest'] &= ~$permission;
if ($update) {
$this->save();
}
} else {
unset($this->data['guest']);
if ($update) {
$this->save();
}
}
}
/**
* Removes a permission that creators currently have on this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to
* remove.
* @param boolean $update Whether to automatically update the
* backend.
*/
function removeCreatorPermission($permission, $update = true)
{
if (!isset($this->data['creator'])) {
return;
}
if ($this->get('type') == 'matrix') {
$this->data['creator'] &= ~$permission;
if ($update) {
$this->save();
}
} else {
unset($this->data['creator']);
if ($update) {
$this->save();
}
}
}
/**
* Removes a default permission on this object.
*
* @param constant $permission The permission (PERMS_DELETE, etc.) to
* remove.
* @param boolean $update Whether to automatically update the
* backend.
*/
function removeDefaultPermission($permission, $update = true)
{
if (!isset($this->data['default'])) {
return;
}
if ($this->get('type') == 'matrix') {
$this->data['default'] &= ~$permission;
if ($update) {
$this->save();
}
} else {
unset($this->data['default']);
if ($update) {
$this->save();
}
}
}
/**
* Removes a permission that a group currently has on this object.
*
* @param integer $groupId The id of the group to remove the
* permission from.
* @param constant $permission The permission (PERMS_DELETE, etc.) to
* remove.
* @param boolean $update Whether to automatically update the
* backend.
*/
function removeGroupPermission($groupId, $permission, $update = true)
{
if (empty($groupId) || !isset($this->data['groups'][$groupId])) {
return;
}
if ($this->get('type') == 'matrix') {
$this->data['groups'][$groupId] &= ~$permission;
if (empty($this->data['groups'][$groupId])) {
unset($this->data['groups'][$groupId]);
}
if ($update) {
$this->save();
}
} else {
unset($this->data['groups'][$groupId]);
if ($update) {
$this->save();
}
}
}
/**
* Returns an array of all user permissions on this object.
*
* @param integer $perm List only users with this permission level.
* Defaults to all users.
*
* @return array All user permissions for this object, indexed by user.
*/
function getUserPermissions($perm = null)
{
if (!isset($this->data['users']) || !is_array($this->data['users'])) {
return array();
} elseif (!$perm) {
return $this->data['users'];
} else {
$users = array();
foreach ($this->data['users'] as $user => $uperm) {
if ($uperm & $perm) {
$users[$user] = $uperm;
}
}
return $users;
}
}
/**
* Returns the guest permissions on this object.
*
* @return integer The guest permissions on this object.
*/
function getGuestPermissions()
{
return !empty($this->data['guest']) ?
$this->data['guest'] :
null;
}
/**
* Returns the creator permissions on this object.
*
* @return integer The creator permissions on this object.
*/
function getCreatorPermissions()
{
return !empty($this->data['creator']) ?
$this->data['creator'] :
null;
}
/**
* Returns the default permissions on this object.
*
* @return integer The default permissions on this object.
*/
function getDefaultPermissions()
{
return !empty($this->data['default']) ?
$this->data['default'] :
null;
}
/**
* Returns an array of all group permissions on this object.
*
* @param integer $perm List only users with this permission level.
* Defaults to all users.
*
* @return array All group permissions for this object, indexed by group.
*/
function getGroupPermissions($perm = null)
{
if (!isset($this->data['groups']) ||
!is_array($this->data['groups'])) {
return array();
} elseif (!$perm) {
return $this->data['groups'];
} else {
$groups = array();
foreach ($this->data['groups'] as $group => $gperm) {
if ($gperm & $perm) {
$groups[$group] = $gperm;
}
}
return $groups;
}
}
}
|