Item.php 5.44 KB
Newer Older
1 2 3 4 5 6 7
<?php
/**
 * @link http://www.yiiframework.com/
 * @copyright Copyright (c) 2008 Yii Software LLC
 * @license http://www.yiiframework.com/license/
 */

8
namespace yii\rbac;
9 10 11 12 13

use Yii;
use yii\base\Object;

/**
14 15 16 17 18 19 20
 * 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.
 *
21 22 23 24
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
25
class Item extends Object
26 27 28 29 30
{
	const TYPE_OPERATION = 0;
	const TYPE_TASK = 1;
	const TYPE_ROLE = 2;

Qiang Xue committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * @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;

52
	private $_name;
53
	private $_oldName;
54 55 56 57 58


	/**
	 * 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
59
	 * of the [[Manager::checkAccess()]].
60 61 62 63 64 65 66
	 * @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__);
Qiang Xue committed
67
		if ($this->manager->executeBizRule($this->bizRule, $params, $this->data)) {
68 69 70
			if ($this->_name == $itemName) {
				return true;
			}
Qiang Xue committed
71
			foreach ($this->manager->getItemChildren($this->_name) as $item) {
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
				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) {
94
			$this->_oldName = $this->_name;
95 96 97 98 99 100 101 102 103
			$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.
104
	 * @see Manager::addItemChild
105 106 107
	 */
	public function addChild($name)
	{
Qiang Xue committed
108
		return $this->manager->addItemChild($this->_name, $name);
109 110 111 112 113 114 115
	}

	/**
	 * 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
116
	 * @see Manager::removeItemChild
117 118 119
	 */
	public function removeChild($name)
	{
Qiang Xue committed
120
		return $this->manager->removeItemChild($this->_name, $name);
121 122 123 124 125 126
	}

	/**
	 * Returns a value indicating whether a child exists
	 * @param string $name the child item name
	 * @return boolean whether the child exists
127
	 * @see Manager::hasItemChild
128 129 130
	 */
	public function hasChild($name)
	{
Qiang Xue committed
131
		return $this->manager->hasItemChild($this->_name, $name);
132 133 134 135
	}

	/**
	 * Returns the children of this item.
136
	 * @return Item[] all child items of this item.
137
	 * @see Manager::getItemChildren
138 139 140
	 */
	public function getChildren()
	{
Qiang Xue committed
141
		return $this->manager->getItemChildren($this->_name);
142 143 144 145 146 147 148 149
	}

	/**
	 * 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
150
	 * @return Assignment the authorization assignment information.
151
	 * @throws \yii\base\Exception if the item has already been assigned to the user
152
	 * @see Manager::assign
153 154 155
	 */
	public function assign($userId, $bizRule = null, $data = null)
	{
Qiang Xue committed
156
		return $this->manager->assign($userId, $this->_name, $bizRule, $data);
157 158 159 160 161 162
	}

	/**
	 * Revokes an authorization assignment from a user.
	 * @param mixed $userId the user ID (see [[User::id]])
	 * @return boolean whether removal is successful
163
	 * @see Manager::revoke
164 165 166
	 */
	public function revoke($userId)
	{
Qiang Xue committed
167
		return $this->manager->revoke($userId, $this->_name);
168 169 170 171 172 173
	}

	/**
	 * 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.
174
	 * @see Manager::isAssigned
175 176 177
	 */
	public function isAssigned($userId)
	{
Qiang Xue committed
178
		return $this->manager->isAssigned($userId, $this->_name);
179 180 181 182 183
	}

	/**
	 * Returns the item assignment information.
	 * @param mixed $userId the user ID (see [[User::id]])
184
	 * @return Assignment the item assignment information. Null is returned if
185
	 * this item is not assigned to the user.
186
	 * @see Manager::getAssignment
187 188 189
	 */
	public function getAssignment($userId)
	{
Qiang Xue committed
190
		return $this->manager->getAssignment($userId, $this->_name);
191
	}
192 193 194 195 196 197

	/**
	 * Saves an authorization item to persistent storage.
	 */
	public function save()
	{
Qiang Xue committed
198
		$this->manager->saveItem($this, $this->_oldName);
199 200
		unset($this->_oldName);
	}
201
}