[ Index ]
 

Code source de Horde 3.1.3

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/Horde/ -> Share.php (source)

   1  <?php
   2  
   3  require_once 'Horde/DataTree.php';
   4  
   5  /**
   6   * Horde_Share:: provides an interface to all shares a user might have.  Its
   7   * methods take care of any site-specific restrictions configured in in the
   8   * application's prefs.php and conf.php files.
   9   *
  10   * $Horde: framework/Share/Share.php,v 1.111.2.20 2006/05/04 13:25:29 jan Exp $
  11   *
  12   * Copyright 2002-2006 Joel Vandal <jvandal@infoteck.qc.ca>
  13   * Copyright 2002-2006 Infoteck Internet <webmaster@infoteck.qc.ca>
  14   * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org>
  15   *
  16   * See the enclosed file COPYING for license information (LGPL). If you did
  17   * not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
  18   *
  19   * @author  Joel Vandal <jvandal@infoteck.qc.ca>
  20   * @author  Mike Cochrame <mike@graftonhall.co.nz>
  21   * @author  Chuck Hagenbuch <chuck@horde.org>
  22   * @since   Horde 3.0
  23   * @package Horde_Share
  24   */
  25  class Horde_Share {
  26  
  27      /**
  28       * Pointer to a DataTree instance to manage/store shares
  29       *
  30       * @var DataTree
  31       */
  32      var $_datatree;
  33  
  34      /**
  35       * The application we're managing shares for.
  36       *
  37       * @var string
  38       */
  39      var $_app;
  40  
  41      /**
  42       * The subclass of DataTreeObject to instantiate shares as.
  43       *
  44       * @var string
  45       */
  46      var $_shareObject = 'DataTreeObject_Share';
  47  
  48      /**
  49       * A cache of all shares that have been retrieved, so we don't hit the
  50       * backend again and again for them.
  51       *
  52       * @var array
  53       */
  54      var $_cache = array();
  55  
  56      /**
  57       * Cache used for listShares().
  58       *
  59       * @var array
  60       */
  61      var $_listcache = array();
  62  
  63      /**
  64       * A list of objects that we're currently sorting, for reference during the
  65       * sorting algorithm.
  66       *
  67       * @var array
  68       */
  69      var $_sortList;
  70  
  71      /**
  72       * Attempts to return a reference to a concrete Horde_Share instance. It
  73       * will only create a new instance if no Horde_Share instance currently
  74       * exists.
  75       *
  76       * This method must be invoked as:
  77       *   <code>$var = &Horde_Share::singleton($app);</code>
  78       *
  79       * @param string $app  The applications that the shares relates to.
  80       *
  81       * @return Horde_Share  The concrete Share reference, or false on an error.
  82       */
  83      function &singleton($app)
  84      {
  85          static $shares;
  86  
  87          if (!isset($shares[$app])) {
  88              $shares[$app] = new Horde_Share($app);
  89          }
  90  
  91          return $shares[$app];
  92      }
  93  
  94      /**
  95       * Constructor.
  96       *
  97       * @param string $app  The application that the shares belong to.
  98       */
  99      function Horde_Share($app)
 100      {
 101          global $conf, $registry;
 102  
 103          if (empty($conf['datatree']['driver'])) {
 104              Horde::fatal('You must configure a DataTree backend to use Shares.', __FILE__, __LINE__);
 105          }
 106  
 107          $driver = $conf['datatree']['driver'];
 108          $this->_app = $app;
 109          $this->_datatree = &DataTree::singleton(
 110              $driver,
 111              array_merge(Horde::getDriverConfig('datatree', $driver), array('group' => 'horde.shares.' . $app))
 112          );
 113  
 114          Horde::callHook('_horde_hook_share_init', array($this, $app));
 115      }
 116  
 117      /**
 118       * Returns the DataTree instance used to manage this share.
 119       *
 120       * @return DataTree  This share's DataTree instance.
 121       */
 122      function &getDataTree()
 123      {
 124          return $this->_datatree;
 125      }
 126  
 127      /**
 128       * Returns a DataTreeObject_Share object corresponding to the given share
 129       * name, with the details retrieved appropriately.
 130       *
 131       * @param string $name  The name of the share to retrieve.
 132       *
 133       * @return DataTreeObject_Share  The requested share.
 134       */
 135      function &getShare($name)
 136      {
 137          if (isset($this->_cache[$name])) {
 138              return $this->_cache[$name];
 139          }
 140  
 141          $this->_cache[$name] = &$this->_datatree->getObject($name, $this->_shareObject);
 142          if (!is_a($this->_cache[$name], 'PEAR_Error')) {
 143              $this->_cache[$name]->setShareOb($this);
 144          }
 145  
 146          return $this->_cache[$name];
 147      }
 148  
 149      /**
 150       * Returns a DataTreeObject_Share object corresponding to the given unique
 151       * ID, with the details retrieved appropriately.
 152       *
 153       * @param string $cid  The id of the share to retrieve.
 154       *
 155       * @return DataTreeObject_Share  The requested share.
 156       */
 157      function &getShareById($cid)
 158      {
 159          $share = $this->_datatree->getObjectById($cid, $this->_shareObject);
 160          if (!is_a($share, 'PEAR_Error')) {
 161              $share->setShareOb($this);
 162          }
 163          return $share;
 164      }
 165  
 166      /**
 167       * Returns an array of DataTreeObject_Share objects corresponding to the
 168       * given set of unique IDs, with the details retrieved appropriately.
 169       *
 170       * @param array $cids  The array of ids to retrieve.
 171       *
 172       * @return array  The requested shares.
 173       */
 174      function &getShares($cids)
 175      {
 176          $shares = $this->_datatree->getObjects($cids, $this->_shareObject);
 177          if (is_a($shares, 'PEAR_Error')) {
 178              return $shares;
 179          }
 180  
 181          $keys = array_keys($shares);
 182          foreach ($keys as $key) {
 183              if (is_a($shares[$key], 'PEAR_Error')) {
 184                  return $shares[$key];
 185              }
 186  
 187              $this->_cache[$key] = &$shares[$key];
 188              $shares[$key]->setShareOb($this);
 189          }
 190  
 191          return $shares;
 192      }
 193  
 194      /**
 195       * Returns a new share object.
 196       *
 197       * @param string $name  The share's name.
 198       *
 199       * @return DataTreeObject_Share  A new share object.
 200       */
 201      function &newShare($name)
 202      {
 203          if (empty($name)) {
 204              return PEAR::raiseError('Share names must be non-empty');
 205          }
 206          $share = &new $this->_shareObject($name);
 207          $share->setDataTree($this->_datatree);
 208          $share->setShareOb($this);
 209          $share->set('owner', Auth::getAuth());
 210  
 211          return $share;
 212      }
 213  
 214      /**
 215       * Adds a share to the shares system. The share must first be created with
 216       * Horde_Share::newShare(), and have any initial details added to it,
 217       * before this function is called.
 218       *
 219       * @param DataTreeObject_Share $share  The new share object.
 220       *
 221       * @return boolean|PEAR_Error  PEAR_Error on failure.
 222       */
 223      function addShare($share)
 224      {
 225          if (!is_a($share, 'DataTreeObject_Share')) {
 226              return PEAR::raiseError('Shares must be DataTreeObject_Share objects or extend that class.');
 227          }
 228  
 229          $perm = &$GLOBALS['perms']->newPermission($share->getName());
 230          if (is_a($perm, 'PEAR_Error')) {
 231              return $perm;
 232          }
 233  
 234          /* Give the owner full access */
 235          $perm->addUserPermission($share->get('owner'), PERMS_SHOW, false);
 236          $perm->addUserPermission($share->get('owner'), PERMS_READ, false);
 237          $perm->addUserPermission($share->get('owner'), PERMS_EDIT, false);
 238          $perm->addUserPermission($share->get('owner'), PERMS_DELETE, false);
 239  
 240          $share->setPermission($perm, false);
 241  
 242          $result = Horde::callHook('_horde_hook_share_add', array($share),
 243                                    'horde', false);
 244          if (is_a($result, 'PEAR_Error')) {
 245              return $result;
 246          }
 247  
 248          return $this->_datatree->add($share);
 249      }
 250  
 251      /**
 252       * Removes a share from the shares system permanently.
 253       *
 254       * @param DataTreeObject_Share $share  The share to remove.
 255       *
 256       * @return boolean|PEAR_Error  PEAR_Error on failure.
 257       */
 258      function removeShare($share)
 259      {
 260          if (!is_a($share, 'DataTreeObject_Share')) {
 261              return PEAR::raiseError('Shares must be DataTreeObject_Share objects or extend that class.');
 262          }
 263  
 264          $result = Horde::callHook('_horde_hook_share_remove', array($share),
 265                                    'horde', false);
 266          if (is_a($result, 'PEAR_Error')) {
 267              return $result;
 268          }
 269  
 270          return $this->_datatree->remove($share);
 271      }
 272  
 273      /**
 274       * Checks to see if a share has any child shares.
 275       *
 276       * @param DataTreeObject_Share $share  The share to check for children.
 277       *
 278       * @return boolean  True if the specified share has child shares.
 279       */
 280      function hasChildren($share)
 281      {
 282          if (!is_a($share, 'DataTreeObject_Share')) {
 283              return PEAR::raiseError('Shares must be DataTreeObject_Share objects or extend that class.');
 284          }
 285  
 286          return (bool)$this->_datatree->getNumberOfChildren($share);
 287      }
 288  
 289      /**
 290       * Returns a share's direct parent object.
 291       *
 292       * @param string $share  Get the parent of this share.
 293       *
 294       * @return DataTreeObject_Share  The parent share, if it exists.
 295       */
 296      function &getParent($child)
 297      {
 298          $id = $this->_datatree->getParent($child);
 299          if (is_a($id, 'PEAR_Error')) {
 300              return $id;
 301          }
 302  
 303          if (!$id || ($id == DATATREE_ROOT)) {
 304              $error = PEAR::raiseError('Parent does not exist.');
 305              return $error;
 306          }
 307  
 308          return $this->getShareById($id);
 309      }
 310  
 311      /**
 312       * Returns the ID of a share.
 313       *
 314       * @param DataTreeObject_Share  The share to return the ID of.
 315       *
 316       * @return integer  The share's ID or PEAR_Error on failure.
 317       */
 318      function getShareId($share)
 319      {
 320          return $this->_datatree->getId($share->getName());
 321      }
 322  
 323      /**
 324       * Utility function to be used with uasort() for sorting arrays of
 325       * Horde_Share objects.
 326       * Example:<code>
 327       * uasort($list, array('Horde_Share', '_sortShares'));
 328       * </code>
 329       *
 330       * @access private
 331       */
 332      function _sortShares($a, $b)
 333      {
 334          $aParts = explode(':', $a->getName());
 335          $bParts = explode(':', $b->getName());
 336  
 337          $min = min(count($aParts), count($bParts));
 338          $idA = '';
 339          $idB = '';
 340          for ($i = 0; $i < $min; $i++) {
 341              if ($idA) {
 342                  $idA .= ':';
 343                  $idB .= ':';
 344              }
 345              $idA .= $aParts[$i];
 346              $idB .= $bParts[$i];
 347  
 348              if ($idA != $idB) {
 349                  $curA = isset($this->_sortList[$idA]) ? $this->_sortList[$idA]->get('name') : '';
 350                  $curB = isset($this->_sortList[$idB]) ? $this->_sortList[$idB]->get('name') : '';
 351                  return strnatcasecmp($curA, $curB);
 352              }
 353          }
 354  
 355          return count($aParts) > count($bParts);
 356      }
 357  
 358      /**
 359       * Checks if a share exists in the system.
 360       *
 361       * @param string $share  The share to check.
 362       *
 363       * @return boolean  True if the share exists, false otherwise.
 364       */
 365      function exists($share)
 366      {
 367          return $this->_datatree->exists($share);
 368      }
 369  
 370      /**
 371       * Returns the count of all shares that $userid has access to.
 372       *
 373       * @param string  $userid      The userid of the user to check access for.
 374       * @param integer $perm        The level of permissions required.
 375       * @param mixed   $attributes  Restrict the shares counted to those
 376       *                             matching $attributes. An array of
 377       *                             attribute/values pairs or a share owner
 378       *                             username.
 379       * @param string  $parent      The parent share to start searching at.
 380       * @param boolean $allLevels   Return all levels, or just the direct
 381       *                             children of $parent? Defaults to all levels.
 382       *
 383       * @return integer  Number of shares the user has access to.
 384       */
 385      function countShares($userid, $perm = PERMS_SHOW, $attributes = null,
 386                           $parent = DATATREE_ROOT, $allLevels = true)
 387      {
 388          $key = serialize(array($this->_app, $userid, $perm, $attributes, $parent, $allLevels, 'count'));
 389          if (empty($this->_listcache[$key])) {
 390              $criteria = $this->getShareCriteria($userid, $perm, $attributes);
 391              $this->_listcache[$key] = $this->_datatree->countByAttributes($criteria, $parent, $allLevels, 'id');
 392          }
 393  
 394          return $this->_listcache[$key];
 395      }
 396  
 397      /**
 398       * Returns an array of all shares that $userid has access to.
 399       *
 400       * @param string  $userid       The userid of the user to check access for.
 401       * @param integer $perm         The level of permissions required.
 402       * @param mixed   $attributes   Restrict the shares counted to those
 403       *                              matching $attributes. An array of
 404       *                              attribute/values pairs or a share owner
 405       *                              username.
 406       * @param string  $parent       The parent share to start searching at.
 407       * @param boolean $allLevels    Return all levels, or just the direct
 408       *                              children of $parent? Defaults to all
 409       *                              levels.
 410       * @param integer $from         The share to start listing at.
 411       * @param integer $count        The number of shares to return.
 412       * @param string  $sortby_name  Attribute name to use for sorting.
 413       * @param string  $sortby_key   Attribute key to use for sorting.
 414       * @param integer $direction    Sort direction:
 415       *                                0 - ascending
 416       *                                1 - descending
 417       *
 418       * @return array  The shares the user has access to.
 419       */
 420      function &listShares($userid, $perm = PERMS_SHOW, $attributes = null,
 421                           $parent = DATATREE_ROOT, $allLevels = true, $from = 0,
 422                           $count = 0, $sortby_name = null, $sortby_key = null,
 423                           $direction = 0)
 424      {
 425          $key = serialize(array($this->_app, $userid, $perm, $attributes,
 426                                 $parent, $allLevels, $from, $count,
 427                                 $sortby_name, $sortby_key, $direction));
 428          if (empty($this->_listcache[$key])) {
 429              $criteria = $this->getShareCriteria($userid, $perm, $attributes);
 430              $sharelist = $this->_datatree->getByAttributes(
 431                  $criteria, $parent, $allLevels, 'id', $from, $count,
 432                  $sortby_name, $sortby_key, $direction);
 433              if (is_a($sharelist, 'PEAR_Error') || !count($sharelist)) {
 434                  /* If we got back an error or an empty array, pass it back to
 435                   * the caller. */
 436                  return $sharelist;
 437              }
 438  
 439              /* Make sure getShares() didn't return an error. */
 440              $shares = &$this->getShares(array_keys($sharelist));
 441              if (is_a($shares, 'PEAR_Error')) {
 442                  return $shares;
 443              }
 444  
 445              $this->_listcache[$key] = &$shares;
 446              $this->_sortList = $this->_listcache[$key];
 447              uasort($this->_listcache[$key], array($this, '_sortShares'));
 448              $this->_sortList = null;
 449          }
 450  
 451          $result = Horde::callHook('_horde_hook_share_list', array($userid,
 452                                    $perm, $attributes, $this->_listcache[$key]),
 453                                    'horde', false);
 454          if (is_a($result, 'PEAR_Error')) {
 455              return $result;
 456          }
 457  
 458          return $this->_listcache[$key];
 459      }
 460  
 461      /**
 462       * Lists *all* shares for the current app/share, regardless of
 463       * permissions. This is for admin functionality and scripting tools, and
 464       * shouldn't be called from user-level code!
 465       *
 466       * @param boolean $parent  Start the listing at a certain point in the
 467       *                         tree. Defaults to DATATREE_ROOT, the root.
 468       *
 469       * @return array  All shares for the current app/share.
 470       */
 471      function listAllShares($parent = DATATREE_ROOT)
 472      {
 473          $sharelist = $this->_datatree->get(DATATREE_FORMAT_FLAT, $parent, true);
 474          if (is_a($sharelist, 'PEAR_Error') || !count($sharelist)) {
 475              // If we got back an error or an empty array, just return it.
 476              return $sharelist;
 477          }
 478          unset($sharelist[$parent]);
 479  
 480          $shares = &$this->getShares(array_keys($sharelist));
 481          if (is_a($shares, 'PEAR_Error')) {
 482              return $shares;
 483          }
 484  
 485          $this->_sortList = $shares;
 486          uasort($shares, array($this, '_sortShares'));
 487          $this->_sortList = null;
 488  
 489          return $shares;
 490      }
 491  
 492      /**
 493       * Returns an array of criteria for querying shares.
 494       *
 495       * @param string  $userid      The userid of the user to check access for.
 496       * @param integer $perm        The level of permissions required.
 497       * @param mixed   $attributes  Restrict the shares returned to those who
 498       *                             have these attribute values.
 499       *
 500       * @return array  The criteria tree for fetching this user's shares.
 501       */
 502      function getShareCriteria($userid, $perm = PERMS_SHOW, $attributes = null)
 503      {
 504          if (!empty($userid)) {
 505              $criteria = array(
 506                  'OR' => array(
 507                      // (owner == $userid)
 508                      array(
 509                          'AND' => array(
 510                              array('field' => 'name', 'op' => '=', 'test' => 'owner'),
 511                              array('field' => 'value', 'op' => '=', 'test' => $userid))),
 512  
 513                      // (name == perm_users and key == $userid and val & $perm)
 514                      array(
 515                          'AND' => array(
 516                              array('field' => 'name', 'op' => '=', 'test' => 'perm_users'),
 517                              array('field' => 'key', 'op' => '=', 'test' => $userid),
 518                              array('field' => 'value', 'op' => '&', 'test' => $perm))),
 519  
 520                      // (name == perm_creator and val & $perm)
 521                      array(
 522                          'AND' => array(
 523                              array('field' => 'name', 'op' => '=', 'test' => 'perm_creator'),
 524                              array('field' => 'value', 'op' => '&', 'test' => $perm))),
 525  
 526                      // (name == perm_default and val & $perm)
 527                      array(
 528                          'AND' => array(
 529                              array('field' => 'name', 'op' => '=', 'test' => 'perm_default'),
 530                              array('field' => 'value', 'op' => '&', 'test' => $perm)))));
 531  
 532              // If the user has any group memberships, check for those also.
 533              require_once  'Horde/Group.php';
 534              $group = &Group::singleton();
 535              $groups = $group->getGroupMemberships($userid, true);
 536              if (!is_a($groups, 'PEAR_Error') && count($groups)) {
 537                  // (name == perm_groups and key in ($groups) and val & $perm)
 538                  $criteria['OR'][] = array(
 539                      'AND' => array(
 540                          array('field' => 'name', 'op' => '=', 'test' => 'perm_groups'),
 541                          array('field' => 'key', 'op' => 'IN', 'test' => array_keys($groups)),
 542                          array('field' => 'value', 'op' => '&', 'test' => $perm)));
 543              }
 544          } else {
 545              $criteria = array(
 546                  'AND' => array(
 547                       array('field' => 'name', 'op' => '=', 'test' => 'perm_guest'),
 548                       array('field' => 'value', 'op' => '&', 'test' => $perm)));
 549          }
 550  
 551          if (is_array($attributes)) {
 552              // Build attribute/key filter.
 553              foreach ($attributes as $key => $value) {
 554                  $criteria = array(
 555                      'AND' => array(
 556                          $criteria,
 557                          array(
 558                              'JOIN' => array(
 559                                  'AND' => array(
 560                                      array('field' => 'name', 'op' => '=', 'test' => $key),
 561                                      array('field' => 'value', 'op' => '=', 'test' => $value))))));
 562              }
 563          } elseif (!empty($attributes)) {
 564              // Restrict to shares owned by the user specified in the
 565              // $attributes string.
 566              $criteria = array(
 567                  'AND' => array(
 568                      $criteria,
 569                      array(
 570                          'JOIN' => array(
 571                              array('field' => 'name', 'op' => '=', 'test' => 'owner'),
 572                              array('field' => 'value', 'op' => '=', 'test' => $attributes)))));
 573          }
 574  
 575          return $criteria;
 576      }
 577  
 578      /**
 579       * TODO
 580       *
 581       * @see Perms::getPermissions
 582       *
 583       * @param TODO
 584       * @param TODO
 585       *
 586       * @return TODO
 587       */
 588      function getPermissions($share, $user = null)
 589      {
 590          if (!is_a($share, 'DataTreeObject_Share')) {
 591              $share = &$this->getShare($share);
 592          }
 593  
 594          $perm = &$share->getPermission();
 595          return $GLOBALS['perms']->getPermissions($perm, $user);
 596      }
 597  
 598      /**
 599       * Returns the Identity for a particular share owner.
 600       *
 601       * @param mixed $share  The share to fetch the Identity for - either the
 602       *                      string name, or the DataTreeObject_Share object.
 603       *
 604       * @return string  The preference's value.
 605       */
 606      function &getIdentityByShare($share)
 607      {
 608          if (!is_a($share, 'DataTreeObject_Share')) {
 609              $share = $this->getShare($share);
 610              if (is_a($share, 'PEAR_Error')) {
 611                  return null;
 612              }
 613          }
 614  
 615          require_once 'Horde/Identity.php';
 616          $owner = $share->get('owner');
 617          return $ret = &Identity::singleton('none', $owner);
 618      }
 619  
 620  }
 621  
 622  /**
 623   * Extension of the DataTreeObject class for storing Share information in the
 624   * DataTree driver. If you want to store specialized Share information, you
 625   * should extend this class instead of extending DataTreeObject directly.
 626   *
 627   * @author  Mike Cochrane <mike@graftonhall.co.nz>
 628   * @since   Horde 3.0
 629   * @package Horde_Share
 630   */
 631  class DataTreeObject_Share extends DataTreeObject {
 632  
 633      /**
 634       * The Horde_Share object which this share came from - needed for
 635       * updating data in the backend to make changes stick, etc.
 636       *
 637       * @var Horde_Share
 638       */
 639      var $_shareOb;
 640  
 641      /**
 642       * The DataTreeObject_Share constructor. Just makes sure to call the parent
 643       * constructor so that the share's name is set properly.
 644       *
 645       * @param string $id  The id of the share.
 646       */
 647      function DataTreeObject_Share($id)
 648      {
 649          parent::DataTreeObject($id);
 650          if (is_null($this->data)) {
 651              $this->data = array();
 652          }
 653      }
 654  
 655      /**
 656       * Associates a Share object with this share.
 657       *
 658       * @param Horde_Share $shareOb  The Share object.
 659       */
 660      function setShareOb(&$shareOb)
 661      {
 662          $this->_shareOb = &$shareOb;
 663      }
 664  
 665      /**
 666       * Returns this share's parent object.
 667       *
 668       * @return DataTreeObject_Share  The parent share, if it exists.
 669       */
 670      function &getParent()
 671      {
 672          $parent = $this->_shareOb->getParent($this);
 673          return $parent;
 674      }
 675  
 676      /**
 677       * Gives a user a certain privilege for this share.
 678       *
 679       * @param string $userid       The userid of the user.
 680       * @param integer $permission  A PERMS_* constant.
 681       */
 682      function addUserPermission($userid, $permission)
 683      {
 684          $perm = &$this->getPermission();
 685          $perm->addUserPermission($userid, $permission, false);
 686          $this->setPermission($perm);
 687      }
 688  
 689      /**
 690       * Removes a certain privilege for a user from this share.
 691       *
 692       * @param string $userid       The userid of the user.
 693       * @param integer $permission  A PERMS_* constant.
 694       */
 695      function removeUserPermission($userid, $permission)
 696      {
 697          $perm = &$this->getPermission();
 698          $perm->removeUserPermission($userid, $permission, false);
 699          $this->setPermission($perm);
 700      }
 701  
 702      /**
 703       * Gives a group certain privileges for this share.
 704       *
 705       * @param string $group        The group to add permissions for.
 706       * @param integer $permission  A PERMS_* constant.
 707       */
 708      function addGroupPermission($group, $permission)
 709      {
 710          $perm = &$this->getPermission();
 711          $perm->addGroupPermission($group, $permission, false);
 712          $this->setPermission($perm);
 713      }
 714  
 715      /**
 716       * Removes a certain privilege from a group.
 717       *
 718       * @param string $group         The group to remove permissions from.
 719       * @param constant $permission  A PERMS_* constant.
 720       */
 721      function removeGroupPermission($group, $permission)
 722      {
 723          $perm = &$this->getPermission();
 724          $perm->removeGroupPermission($group, $permission, false);
 725          $this->setPermission($perm);
 726      }
 727  
 728      /**
 729       * Checks to see if a user has a given permission.
 730       *
 731       * @param string $userid       The userid of the user.
 732       * @param integer $permission  A PERMS_* constant to test for.
 733       * @param string $creator      The creator of the event.
 734       *
 735       * @return boolean  Whether or not $userid has $permission.
 736       */
 737      function hasPermission($userid, $permission, $creator = null)
 738      {
 739          if ($userid == $this->get('owner')) {
 740              return true;
 741          }
 742  
 743          return $GLOBALS['perms']->hasPermission($this->getPermission(), $userid, $permission, $creator);
 744      }
 745  
 746      /**
 747       * Removes a user from this share.
 748       *
 749       * @param string $userid  The userid of the user to remove.
 750       */
 751      function removeUser($userid)
 752      {
 753          /* Remove all $userid's permissions. */
 754          $perm = &$this->getPermission();
 755          $perm->removeUserPermission($userid, PERMS_SHOW, false);
 756          $perm->removeUserPermission($userid, PERMS_READ, false);
 757          $perm->removeUserPermission($userid, PERMS_EDIT, false);
 758          $perm->removeUserPermission($userid, PERMS_DELETE, false);
 759          return $this->setPermission($perm);
 760      }
 761  
 762      /**
 763       * Removes a group from this share.
 764       *
 765       * @param integer $groupId  The group to remove.
 766       */
 767      function removeGroup($groupId)
 768      {
 769          /* Remove all $groupId's permissions. */
 770          $perm = &$this->getPermission();
 771          $perm->removeGroupPermission($groupId, PERMS_SHOW, false);
 772          $perm->removeGroupPermission($groupId, PERMS_READ, false);
 773          $perm->removeGroupPermission($groupId, PERMS_EDIT, false);
 774          $perm->removeGroupPermission($groupId, PERMS_DELETE, false);
 775          return $this->setPermission($perm);
 776      }
 777  
 778      /**
 779       * Returns an array containing all the userids of the users with access to
 780       * this share.
 781       *
 782       * @param integer $perm_level  List only users with this permission level.
 783       *                             Defaults to all users.
 784       *
 785       * @return array  The users with access to this share.
 786       */
 787      function listUsers($perm_level = null)
 788      {
 789          $perm = &$this->getPermission();
 790          return array_keys($perm->getUserPermissions($perm_level));
 791      }
 792  
 793      /**
 794       * Returns an array containing all the groupids of the groups with access
 795       * to this share.
 796       *
 797       * @param integer $perm_level  List only users with this permission level.
 798       *                             Defaults to all users.
 799       *
 800       * @return array  The IDs of the groups with access to this share.
 801       */
 802      function listGroups($perm_level = null)
 803      {
 804          $perm = &$this->getPermission();
 805          return array_keys($perm->getGroupPermissions($perm_level));
 806      }
 807  
 808      /**
 809       * TODO
 810       *
 811       * @param TODO
 812       * @param boolean $update  TODO
 813       *
 814       * @return TODO
 815       */
 816      function setPermission(&$perm, $update = true)
 817      {
 818          $this->data['perm'] = $perm->getData();
 819          if ($update) {
 820              return $this->save();
 821          }
 822          return true;
 823      }
 824  
 825      /**
 826       * TODO
 827       *
 828       * @return TODO
 829       */
 830      function &getPermission()
 831      {
 832          $perm = &new DataTreeObject_Permission($this->getName());
 833          $perm->data = isset($this->data['perm']) ? $this->data['perm'] : array();
 834  
 835          return $perm;
 836      }
 837  
 838      /**
 839       * Forces all children of this share to inherit the permissions set on this
 840       * share.
 841       *
 842       * @return TODO
 843       */
 844      function inheritPermissions()
 845      {
 846          $perm = &$this->getPermission();
 847          $children = $this->_shareOb->listAllShares($this->getName());
 848          if (is_a($children, 'PEAR_Error')) {
 849              return $children;
 850          }
 851  
 852          foreach ($children as $child) {
 853              $child->setPermission($perm);
 854          }
 855  
 856          return true;
 857      }
 858  
 859      /**
 860       * Saves any changes to this object to the backend permanently. New objects
 861       * are added instead.
 862       *
 863       * @return boolean | PEAR_Error  PEAR_Error on failure.
 864       */
 865      function save()
 866      {
 867          $result = Horde::callHook('_horde_hook_share_modify', array($this),
 868                                    'horde', false);
 869          if (is_a($result, 'PEAR_Error')) {
 870              return $result;
 871          }
 872  
 873          return parent::save();
 874      }
 875  
 876      /**
 877       * Maps this object's attributes from the data array into a format that we
 878       * can store in the attributes storage backend.
 879       *
 880       * @access protected
 881       *
 882       * @param boolean $permsonly  Only process permissions? Lets subclasses
 883       *                            override part of this method while handling
 884       *                            their additional attributes seperately.
 885       *
 886       * @return array  The attributes array.
 887       */
 888      function _toAttributes($permsonly = false)
 889      {
 890          // Default to no attributes.
 891          $attributes = array();
 892  
 893          foreach ($this->data as $key => $value) {
 894              if ($key == 'perm') {
 895                  foreach ($value as $type => $perms) {
 896                      if (is_array($perms)) {
 897                          foreach ($perms as $member => $perm) {
 898                              $attributes[] = array('name' => 'perm_' . $type,
 899                                                    'key' => $member,
 900                                                    'value' => $perm);
 901                          }
 902                      } else {
 903                          $attributes[] = array('name' => 'perm_' . $type,
 904                                                'key' => '',
 905                                                'value' => $perms);
 906                      }
 907                  }
 908              } elseif (!$permsonly) {
 909                  $attributes[] = array('name' => $key,
 910                                        'key' => '',
 911                                        'value' => $value);
 912              }
 913          }
 914  
 915          return $attributes;
 916      }
 917  
 918      /**
 919       * Takes in a list of attributes from the backend and maps it to our
 920       * internal data array.
 921       *
 922       * @access protected
 923       *
 924       * @param array $attributes   The list of attributes from the backend
 925       *                            (attribute name, key, and value).
 926       * @param boolean $permsonly  Only process permissions? Lets subclasses
 927       *                            override part of this method while handling
 928       *                            their additional attributes seperately.
 929       */
 930      function _fromAttributes($attributes, $permsonly = false)
 931      {
 932          // Initialize data array.
 933          $this->data['perm'] = array();
 934  
 935          foreach ($attributes as $attr) {
 936              if (substr($attr['name'], 0, 4) == 'perm') {
 937                  if (!empty($attr['key'])) {
 938                      $this->data['perm'][substr($attr['name'], 5)][$attr['key']] = $attr['value'];
 939                  } else {
 940                      $this->data['perm'][substr($attr['name'], 5)] = $attr['value'];
 941                  }
 942              } elseif (!$permsonly) {
 943                  $this->data[$attr['name']] = $attr['value'];
 944              }
 945          }
 946      }
 947  
 948  }


Généré le : Sun Feb 25 18:01:28 2007 par Balluche grâce à PHPXref 0.7