You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					202 lines
				
				5.4 KiB
			
		
		
			
		
	
	
					202 lines
				
				5.4 KiB
			| 
											13 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * @link http://www.yiiframework.com/
 | ||
|  |  * @copyright Copyright (c) 2008 Yii Software LLC
 | ||
|  |  * @license http://www.yiiframework.com/license/
 | ||
|  |  */
 | ||
|  | 
 | ||
| 
											13 years ago
										 | namespace yii\rbac;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | use Yii;
 | ||
|  | use yii\base\Object;
 | ||
|  | 
 | ||
|  | /**
 | ||
| 
											13 years ago
										 |  * Item represents an authorization item.
 | ||
|  |  * An authorization item can be an operation, a task or a role.
 | ||
|  |  * They form an authorization hierarchy. Items on higher levels of the hierarchy
 | ||
|  |  * inherit the permissions represented by items on lower levels.
 | ||
|  |  * A user may be assigned one or several authorization items (called [[Assignment]] assignments).
 | ||
|  |  * He can perform an operation only when it is among his assigned items.
 | ||
|  |  *
 | ||
| 
											13 years ago
										 |  * @author Qiang Xue <qiang.xue@gmail.com>
 | ||
|  |  * @author Alexander Kochetov <creocoder@gmail.com>
 | ||
|  |  * @since 2.0
 | ||
|  |  */
 | ||
| 
											13 years ago
										 | class Item extends Object
 | ||
| 
											13 years ago
										 | {
 | ||
|  | 	const TYPE_OPERATION = 0;
 | ||
|  | 	const TYPE_TASK = 1;
 | ||
|  | 	const TYPE_ROLE = 2;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	/**
 | ||
|  | 	 * @var Manager the auth manager of this item
 | ||
|  | 	 */
 | ||
|  | 	public $manager;
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the item description
 | ||
|  | 	 */
 | ||
|  | 	public $description;
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the business rule associated with this item
 | ||
|  | 	 */
 | ||
|  | 	public $bizRule;
 | ||
|  | 	/**
 | ||
|  | 	 * @var mixed the additional data associated with this item
 | ||
|  | 	 */
 | ||
|  | 	public $data;
 | ||
|  | 	/**
 | ||
|  | 	 * @var integer the authorization item type. This could be 0 (operation), 1 (task) or 2 (role).
 | ||
|  | 	 */
 | ||
|  | 	public $type;
 | ||
|  | 
 | ||
| 
											13 years ago
										 | 	private $_name;
 | ||
| 
											13 years ago
										 | 	private $_oldName;
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Checks to see if the specified item is within the hierarchy starting from this item.
 | ||
|  | 	 * This method is expected to be internally used by the actual implementations
 | ||
| 
											13 years ago
										 | 	 * of the [[Manager::checkAccess()]].
 | ||
| 
											13 years ago
										 | 	 * @param string $itemName the name of the item to be checked
 | ||
|  | 	 * @param array $params the parameters to be passed to business rule evaluation
 | ||
|  | 	 * @return boolean whether the specified item is within the hierarchy starting from this item.
 | ||
|  | 	 */
 | ||
|  | 	public function checkAccess($itemName, $params = array())
 | ||
|  | 	{
 | ||
|  | 		Yii::trace('Checking permission: ' . $this->_name, __METHOD__);
 | ||
| 
											13 years ago
										 | 		if ($this->manager->executeBizRule($this->bizRule, $params, $this->data)) {
 | ||
| 
											13 years ago
										 | 			if ($this->_name == $itemName) {
 | ||
|  | 				return true;
 | ||
|  | 			}
 | ||
| 
											13 years ago
										 | 			foreach ($this->manager->getItemChildren($this->_name) as $item) {
 | ||
| 
											13 years ago
										 | 				if ($item->checkAccess($itemName, $params)) {
 | ||
|  | 					return true;
 | ||
|  | 				}
 | ||
|  | 			}
 | ||
|  | 		}
 | ||
|  | 		return false;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @return string the item name
 | ||
|  | 	 */
 | ||
|  | 	public function getName()
 | ||
|  | 	{
 | ||
|  | 		return $this->_name;
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * @param string $value the item name
 | ||
|  | 	 */
 | ||
|  | 	public function setName($value)
 | ||
|  | 	{
 | ||
|  | 		if ($this->_name !== $value) {
 | ||
| 
											13 years ago
										 | 			$this->_oldName = $this->_name;
 | ||
| 
											13 years ago
										 | 			$this->_name = $value;
 | ||
|  | 		}
 | ||
|  | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Adds a child item.
 | ||
|  | 	 * @param string $name the name of the child item
 | ||
|  | 	 * @return boolean whether the item is added successfully
 | ||
|  | 	 * @throws \yii\base\Exception if either parent or child doesn't exist or if a loop has been detected.
 | ||
| 
											13 years ago
										 | 	 * @see Manager::addItemChild
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function addChild($name)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->addItemChild($this->_name, $name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Removes a child item.
 | ||
|  | 	 * Note, the child item is not deleted. Only the parent-child relationship is removed.
 | ||
|  | 	 * @param string $name the child item name
 | ||
|  | 	 * @return boolean whether the removal is successful
 | ||
| 
											13 years ago
										 | 	 * @see Manager::removeItemChild
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function removeChild($name)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->removeItemChild($this->_name, $name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns a value indicating whether a child exists
 | ||
|  | 	 * @param string $name the child item name
 | ||
|  | 	 * @return boolean whether the child exists
 | ||
| 
											13 years ago
										 | 	 * @see Manager::hasItemChild
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function hasChild($name)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->hasItemChild($this->_name, $name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the children of this item.
 | ||
| 
											13 years ago
										 | 	 * @return Item[] all child items of this item.
 | ||
| 
											13 years ago
										 | 	 * @see Manager::getItemChildren
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function getChildren()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->getItemChildren($this->_name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Assigns this item to a user.
 | ||
|  | 	 * @param mixed $userId the user ID (see [[User::id]])
 | ||
|  | 	 * @param string $bizRule the business rule to be executed when [[checkAccess()]] is called
 | ||
|  | 	 * for this particular authorization item.
 | ||
|  | 	 * @param mixed $data additional data associated with this assignment
 | ||
| 
											13 years ago
										 | 	 * @return Assignment the authorization assignment information.
 | ||
| 
											13 years ago
										 | 	 * @throws \yii\base\Exception if the item has already been assigned to the user
 | ||
| 
											13 years ago
										 | 	 * @see Manager::assign
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function assign($userId, $bizRule = null, $data = null)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->assign($userId, $this->_name, $bizRule, $data);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Revokes an authorization assignment from a user.
 | ||
|  | 	 * @param mixed $userId the user ID (see [[User::id]])
 | ||
|  | 	 * @return boolean whether removal is successful
 | ||
| 
											13 years ago
										 | 	 * @see Manager::revoke
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function revoke($userId)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->revoke($userId, $this->_name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns a value indicating whether this item has been assigned to the user.
 | ||
|  | 	 * @param mixed $userId the user ID (see [[User::id]])
 | ||
|  | 	 * @return boolean whether the item has been assigned to the user.
 | ||
| 
											13 years ago
										 | 	 * @see Manager::isAssigned
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function isAssigned($userId)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->isAssigned($userId, $this->_name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
|  | 
 | ||
|  | 	/**
 | ||
|  | 	 * Returns the item assignment information.
 | ||
|  | 	 * @param mixed $userId the user ID (see [[User::id]])
 | ||
| 
											13 years ago
										 | 	 * @return Assignment the item assignment information. Null is returned if
 | ||
| 
											13 years ago
										 | 	 * this item is not assigned to the user.
 | ||
| 
											13 years ago
										 | 	 * @see Manager::getAssignment
 | ||
| 
											13 years ago
										 | 	 */
 | ||
|  | 	public function getAssignment($userId)
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		return $this->manager->getAssignment($userId, $this->_name);
 | ||
| 
											13 years ago
										 | 	}
 | ||
| 
											13 years ago
										 | 
 | ||
|  | 	/**
 | ||
|  | 	 * Saves an authorization item to persistent storage.
 | ||
|  | 	 */
 | ||
|  | 	public function save()
 | ||
|  | 	{
 | ||
| 
											13 years ago
										 | 		$this->manager->saveItem($this, $this->_oldName);
 | ||
| 
											13 years ago
										 | 		unset($this->_oldName);
 | ||
|  | 	}
 | ||
| 
											13 years ago
										 | }
 |