Manager.php 14.6 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

use Yii;
use yii\base\Component;
12
use yii\base\InvalidParamException;
13 14

/**
15 16 17 18 19 20 21 22 23
 * Manager is the base class for authorization manager classes.
 *
 * Manager extends [[Component]] and implements some methods
 * that are common among authorization manager classes.
 *
 * Manager together with its concrete child classes implement the Role-Based
 * Access Control (RBAC).
 *
 * The main idea is that permissions are organized as a hierarchy of
Qiang Xue committed
24
 * [[Item]] authorization items. Items on higher level inherit the permissions
25 26 27 28 29 30 31
 * represented by items on lower level. And roles are simply top-level authorization items
 * that may be assigned to individual users. A user is said to have a permission
 * to do something if the corresponding authorization item is inherited by one of his roles.
 *
 * Using authorization manager consists of two aspects. First, the authorization hierarchy
 * and assignments have to be established. Manager and its child classes
 * provides APIs to accomplish this task. Developers may need to develop some GUI
32
 * so that it is more intuitive to end-users. Second, developers call [[Manager::checkAccess()]]
33 34 35
 * at appropriate places in the application code to check if the current user
 * has the needed permission for an operation.
 *
36 37 38
 * @property Item[] $operations Operations (name => AuthItem). This property is read-only.
 * @property Item[] $roles Roles (name => AuthItem). This property is read-only.
 * @property Item[] $tasks Tasks (name => AuthItem). This property is read-only.
39 40 41 42 43
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @author Alexander Kochetov <creocoder@gmail.com>
 * @since 2.0
 */
44
abstract class Manager extends Component
45
{
46 47 48 49 50 51
    /**
     * @var array list of role names that are assigned to all users implicitly.
     * These roles do not need to be explicitly assigned to any user.
     * When calling [[checkAccess()]], these roles will be checked first.
     * For performance reason, you should minimize the number of such roles.
     * A typical usage of such roles is to define an 'authenticated' role and associate
52
     * it with a rule which checks if the current user is authenticated.
53 54 55 56
     * And then declare 'authenticated' in this property so that it can be applied to
     * every authenticated user.
     */
    public $defaultRoles = [];
57

58 59 60
    /**
     * Creates a role.
     * This is a shortcut method to [[Manager::createItem()]].
61
     *
62 63
     * @param string $name the item name
     * @param string $description the item description.
64
     * @param string $ruleName name of the rule associated with this item
65 66
     * @param mixed $data additional data to be passed when evaluating the business rule
     * @return Item the authorization item
67
     */
68
    public function createRole($name, $description = '', $ruleName = null, $data = null)
69
    {
70
        return $this->createItem($name, Item::TYPE_ROLE, $description, $ruleName, $data);
71
    }
72

73 74 75
    /**
     * Creates a task.
     * This is a shortcut method to [[Manager::createItem()]].
76
     *
77 78
     * @param string $name the item name
     * @param string $description the item description.
79
     * @param string $ruleName name of the rule associated with this item
80 81
     * @param mixed $data additional data to be passed when evaluating the business rule
     * @return Item the authorization item
82
     */
83
    public function createTask($name, $description = '', $ruleName = null, $data = null)
84
    {
85
        return $this->createItem($name, Item::TYPE_TASK, $description, $ruleName, $data);
86
    }
87

88 89 90
    /**
     * Creates an operation.
     * This is a shortcut method to [[Manager::createItem()]].
91
     *
92 93
     * @param string $name the item name
     * @param string $description the item description.
94
     * @param string $ruleName name of the rule associated with this item
95 96
     * @param mixed $data additional data to be passed when evaluating the business rule
     * @return Item the authorization item
97
     */
98
    public function createOperation($name, $description = '', $ruleName = null, $data = null)
99
    {
100
        return $this->createItem($name, Item::TYPE_OPERATION, $description, $ruleName, $data);
101
    }
102

103 104 105
    /**
     * Returns roles.
     * This is a shortcut method to [[Manager::getItems()]].
106
     *
107 108
     * @param mixed $userId the user ID. If not null, only the roles directly assigned to the user
     * will be returned. Otherwise, all roles will be returned.
109 110 111 112 113 114
     * @return Item[] roles (name => AuthItem)
     */
    public function getRoles($userId = null)
    {
        return $this->getItems($userId, Item::TYPE_ROLE);
    }
115

116 117 118
    /**
     * Returns tasks.
     * This is a shortcut method to [[Manager::getItems()]].
119
     *
120 121
     * @param mixed $userId the user ID. If not null, only the tasks directly assigned to the user
     * will be returned. Otherwise, all tasks will be returned.
122 123 124 125 126 127
     * @return Item[] tasks (name => AuthItem)
     */
    public function getTasks($userId = null)
    {
        return $this->getItems($userId, Item::TYPE_TASK);
    }
128

129 130 131
    /**
     * Returns operations.
     * This is a shortcut method to [[Manager::getItems()]].
132
     *
133 134
     * @param mixed $userId the user ID. If not null, only the operations directly assigned to the user
     * will be returned. Otherwise, all operations will be returned.
135 136 137 138 139 140
     * @return Item[] operations (name => AuthItem)
     */
    public function getOperations($userId = null)
    {
        return $this->getItems($userId, Item::TYPE_OPERATION);
    }
141

142
    /**
143 144 145
     * Executes the specified rule.
     *
     * @param string $ruleName name of the rule to be executed.
146 147
     * @param array $params parameters passed to [[Manager::checkAccess()]].
     * @param mixed $data additional data associated with the authorization item or assignment.
148 149
     * @return boolean whether the rule execution returns true.
     * If the rule is empty, it will still return true.
150
     */
151
    public function executeRule($ruleName, $params, $data)
152
    {
153 154 155 156 157
        $rule = $this->getRule($ruleName);
        if ($rule) {
            return $rule->execute($params, $data);
        }
        return true;
158
    }
159

160 161
    /**
     * Checks the item types to make sure a child can be added to a parent.
162 163
     * @param integer $parentType parent item type
     * @param integer $childType child item type
164 165 166 167 168 169 170 171 172
     * @throws InvalidParamException if the item cannot be added as a child due to its incompatible type.
     */
    protected function checkItemChildType($parentType, $childType)
    {
        static $types = ['operation', 'task', 'role'];
        if ($parentType < $childType) {
            throw new InvalidParamException("Cannot add an item of type '{$types[$childType]}' to an item of type '{$types[$parentType]}'.");
        }
    }
173

174 175
    /**
     * Performs access check for the specified user.
176 177 178
     * @param mixed $userId the user ID. This should be either an integer or a string representing
     * the unique identifier of a user. See [[\yii\web\User::id]].
     * @param string $itemName the name of the operation that we are checking access to
179
     * @param array $params name-value pairs that would be passed to rules associated
180
     * with the tasks and roles assigned to the user.
181 182 183
     * @return boolean whether the operations can be performed by the user.
     */
    abstract public function checkAccess($userId, $itemName, $params = []);
184

185 186 187 188 189 190
    /**
     * Creates an authorization item.
     * An authorization item represents an action permission (e.g. creating a post).
     * It has three types: operation, task and role.
     * Authorization items form a hierarchy. Higher level items inheirt permissions representing
     * by lower level items.
191
     *
192 193 194
     * @param string $name the item name. This must be a unique identifier.
     * @param integer $type the item type (0: operation, 1: task, 2: role).
     * @param string $description description of the item
195
     * @param string $ruleName name of the rule associated with the item.
196
     * @param mixed $data additional data associated with the item.
197
     * @throws \yii\base\Exception if an item with the same name already exists
198
     * @return Item the authorization item
199
     */
200 201
    abstract public function createItem($name, $type, $description = '', $ruleName = null, $data = null);

202 203
    /**
     * Removes the specified authorization item.
204
     * @param string $name the name of the item to be removed
205 206 207
     * @return boolean whether the item exists in the storage and has been removed
     */
    abstract public function removeItem($name);
208

209 210
    /**
     * Returns the authorization items of the specific type and user.
211 212 213 214 215
     * @param mixed $userId the user ID. Defaults to null, meaning returning all items even if
     * they are not assigned to a user.
     * @param integer $type the item type (0: operation, 1: task, 2: role). Defaults to null,
     * meaning returning all items regardless of their type.
     * @return Item[] the authorization items of the specific type.
216 217
     */
    abstract public function getItems($userId = null, $type = null);
218

219 220
    /**
     * Returns the authorization item with the specified name.
221 222
     * @param string $name the name of the item
     * @return Item the authorization item. Null if the item cannot be found.
223 224
     */
    abstract public function getItem($name);
225

226 227
    /**
     * Saves an authorization item to persistent storage.
228
     * @param Item $item the item to be saved.
229 230
     * @param string $oldName the old item name. If null, it means the item name is not changed.
     */
231
    abstract public function saveItem(Item $item, $oldName = null);
232

233 234
    /**
     * Adds an item as a child of another item.
235 236
     * @param string $itemName the parent item name
     * @param string $childName the child item name
237 238 239
     * @throws \yii\base\Exception if either parent or child doesn't exist or if a loop has been detected.
     */
    abstract public function addItemChild($itemName, $childName);
240

241 242 243
    /**
     * Removes a child from its parent.
     * Note, the child item is not deleted. Only the parent-child relationship is removed.
244 245
     * @param string $itemName the parent item name
     * @param string $childName the child item name
246 247 248
     * @return boolean whether the removal is successful
     */
    abstract public function removeItemChild($itemName, $childName);
249

250 251
    /**
     * Returns a value indicating whether a child exists within a parent.
252 253
     * @param string $itemName the parent item name
     * @param string $childName the child item name
254 255 256
     * @return boolean whether the child exists
     */
    abstract public function hasItemChild($itemName, $childName);
257

258 259
    /**
     * Returns the children of the specified item.
260 261
     * @param mixed $itemName the parent item name. This can be either a string or an array.
     * The latter represents a list of item names.
262 263 264
     * @return Item[] all child items of the parent
     */
    abstract public function getItemChildren($itemName);
265

266 267
    /**
     * Assigns an authorization item to a user.
268
     *
269 270
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
     * @param string $itemName the item name
271
     * @param string $ruleName name of the rule to be executed when [[checkAccess()]] is called
272 273 274
     * for this particular authorization item.
     * @param mixed $data additional data associated with this assignment
     * @return Assignment the authorization assignment information.
275 276
     * @throws \yii\base\Exception if the item does not exist or if the item has already been assigned to the user
     */
277 278
    abstract public function assign($userId, $itemName, $ruleName = null, $data = null);

279 280
    /**
     * Revokes an authorization assignment from a user.
281 282
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
     * @param string $itemName the item name
283 284 285
     * @return boolean whether removal is successful
     */
    abstract public function revoke($userId, $itemName);
286

287 288
    /**
     * Revokes all authorization assignments from a user.
289
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
290 291 292
     * @return boolean whether removal is successful
     */
    abstract public function revokeAll($userId);
293

294 295
    /**
     * Returns a value indicating whether the item has been assigned to the user.
296 297
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
     * @param string $itemName the item name
298 299 300
     * @return boolean whether the item has been assigned to the user.
     */
    abstract public function isAssigned($userId, $itemName);
301

302 303
    /**
     * Returns the item assignment information.
304 305
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
     * @param string $itemName the item name
306
     * @return Assignment the item assignment information. Null is returned if
307
     * the item is not assigned to the user.
308 309 310 311
     */
    abstract public function getAssignment($userId, $itemName);
    /**
     * Returns the item assignments for the specified user.
312
     * @param mixed $userId the user ID (see [[\yii\web\User::id]])
313
     * @return Item[] the item assignment information for the user. An empty array will be
314
     * returned if there is no item assigned to the user.
315 316
     */
    abstract public function getAssignments($userId);
317 318 319 320 321 322 323 324 325

    /**
     * Removes the specified rule.
     * @param string $name the name of the rule to be removed
     * @return boolean whether the rule exists in the storage and has been removed
     */
    abstract public function removeRule($name);

    /**
326
     * Inserts new rule.
327
     *
328
     * @param Rule $rule the rule that needs to be stored.
329
     */
330 331 332 333 334 335 336 337 338
    abstract public function insertRule(Rule $rule);

    /**
     * Updates existing rule.
     *
     * @param string $name the name of the rule to update
     * @param Rule $rule new rule
     */
    abstract public function updateRule($name, Rule $rule);
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354

    /**
     * Returns rule given its name.
     *
     * @param string $name name of the rule.
     * @return Rule
     */
    abstract public function getRule($name);

    /**
     * Returns all rules.
     *
     * @return Rule[]
     */
    abstract public function getRules();

355 356 357 358
    /**
     * Saves the changes to an authorization assignment.
     * @param Assignment $assignment the assignment that has been changed.
     */
359 360
    abstract public function saveAssignment(Assignment $assignment);

361 362 363 364
    /**
     * Removes all authorization data.
     */
    abstract public function clearAll();
365

366 367 368 369
    /**
     * Removes all authorization assignments.
     */
    abstract public function clearAssignments();
370

371 372 373 374 375 376
    /**
     * Saves authorization data into persistent storage.
     * If any change is made to the authorization data, please make
     * sure you call this method to save the changed data into persistent storage.
     */
    abstract public function save();
377
}