PermissionController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 /**
19  * PermissionController checks, gets and sets permissions.
20  *
21  * The controller supports the following actions:
22  *
23  * <div class="controller-action">
24  * <div> __Action__ checkPermissions </div>
25  * <div>
26  * Check permissions of a set of operations for the current user.
27  * | Parameter | Description
28  * |------------------------|-------------------------
29  * | _in_ `operations` | Array of resource/context/action triples in the form _resource?context?action_
30  * | _out_ `result` | Associative array with the operations as keys and boolean values indicating if permissions are given or not
31  * | __Response Actions__ | |
32  * | `ok` | In all cases
33  * </div>
34  * </div>
35  *
36  * <div class="controller-action">
37  * <div> __Action__ checkPermissionsOfUser </div>
38  * <div>
39  * Check permissions of a set of operations for the given user.
40  * | Parameter | Description
41  * |------------------------|-------------------------
42  * | _in_ `operations` | Array of resource/context/action triples in the form _resource?context?action_
43  * | _in_ `user` | Username to check permissions for (optional, default: the authenticated user)
44  * | _out_ `result` | Associative array with the operations as keys and boolean values indicating if permissions are given or not
45  * | __Response Actions__ | |
46  * | `ok` | In all cases
47  * </div>
48  * </div>
49  *
50  * <div class="controller-action">
51  * <div> __Action__ getPermissions </div>
52  * <div>
53  * Get the permissions on an operation.
54  * | Parameter | Description
55  * |-----------------------|-------------------------
56  * | _in_ `operation` | A resource/context/action triple in the form _resource?context?action_
57  * | _out_ `result` | Assoziative array with keys 'default' (boolean), 'allow', 'deny' (arrays of role names) or null, if no permissions are defined.
58  * </div>
59  * </div>
60  *
61  * <div class="controller-action">
62  * <div> __Action__ setPermissions </div>
63  * <div>
64  * Set the permissions on an operation.
65  * | Parameter | Description
66  * |-----------------------|-------------------------
67  * | _in_ `operation` | A resource/context/action triple in the form _resource?context?action_
68  * | _in_ `permissions` | Assoziative array with keys 'default' (boolean), 'allow', 'deny' (arrays of role names).
69  * </div>
70  * </div>
71  *
72  * <div class="controller-action">
73  * <div> __Action__ createPermission </div>
74  * <div>
75  * Create/Change a permission for a role on an operation.
76  * | Parameter | Description
77  * |-----------------------|-------------------------
78  * | _in_ `operation` | A resource/context/action triple in the form _resource?context?action_
79  * | _in_ `role` | The role to add.
80  * | _in_ `modifier` | _+_ or _-_ whether to allow or disallow the action for the role.
81  * </div>
82  * </div>
83  *
84  * <div class="controller-action">
85  * <div> __Action__ removePermission </div>
86  * <div>
87  * Remove a role from a permission on an operation.
88  * | Parameter | Description
89  * |-----------------------|-------------------------
90  * | _in_ `operation` | A resource/context/action triple in the form _resource?context?action_
91  * | _in_ `role` | The role to remove.
92  * </div>
93  * </div>
94  *
95  * @author ingo herwig <ingo@wemove.com>
96  */
98 
99  /**
100  * @see Controller::validate()
101  */
102  protected function validate() {
103  $request = $this->getRequest();
104  $response = $this->getResponse();
105  $invalidParameters = [];
106  if ($request->getAction() == 'createPermission' || $request->getAction() == 'removePermission' ||
107  $request->getAction() == 'getPermissions' || $request->getAction() == 'setPermissions') {
108  foreach (['operation'] as $param) {
109  if(!$request->hasValue($param)) {
110  $invalidParameters[] = $param;
111  }
112  }
113  }
114  if ($request->getAction() == 'createPermission') {
115  $permissions = $request->getValue('permissions');
116  if (!isset($permissions['allow']) || !isset($permissions['deny'])) {
117  $invalidParameters[] = 'permissions';
118  }
119  }
120  if ($request->getAction() == 'createPermission') {
121  $modifier = $request->getValue('modifier');
124  $invalidParameters[] = 'modifier';
125  }
126  }
127 
128  if (sizeof($invalidParameters) > 0) {
129  $response->addError(ApplicationError::get('PARAMETER_INVALID',
130  ['invalidParameters' => $invalidParameters]));
131  return false;
132  }
133  return true;
134  }
135 
136  /**
137  * @see Controller::doExecute()
138  */
139  protected function doExecute($method=null) {
140  $request = $this->getRequest();
141  $response = $this->getResponse();
142  $permissionManager = $this->getPermissionManager();
143  $action = $request->getAction();
144 
145  // process actions
146  if (strpos($action, 'check') === 0) {
147  $result = [];
148  $operations = $request->hasValue('operations') ? $request->getValue('operations') : [];
149  $user = $action == 'checkPermissionsOfUser' ? $request->getValue('user') : null;
150 
151  foreach($operations as $operation) {
152  $opParts = ActionKey::parseKey($operation);
153  $result[$operation] = $permissionManager->authorize($opParts['resource'], $opParts['context'], $opParts['action'],
154  $user);
155  }
156  $response->setValue('result', $result);
157  }
158  else {
159  $operation = $request->getValue('operation');
160  $opParts = ActionKey::parseKey($operation);
161  $opResource = $opParts['resource'];
162  $opContext = $opParts['context'];
163  $opAction = $opParts['action'];
164 
165  if ($action == 'getPermissions') {
166  $result = $permissionManager->getPermissions($opResource, $opContext, $opAction);
167  $response->setValue('result', $result);
168  }
169  elseif ($action == 'setPermissions') {
170  $this->requireTransaction();
171  $permissions = $request->getValue('permissions');
172  $permissionManager->setPermissions($opResource, $opContext, $opAction, $permissions);
173  }
174  elseif ($action == 'createPermission') {
175  $this->requireTransaction();
176  $role = $request->getValue('role');
177  $modifier = $request->getValue('modifier');
178  $permissionManager->createPermission($opResource, $opContext, $opAction, $role, $modifier);
179  }
180  elseif ($action == 'removePermission') {
181  $this->requireTransaction();
182  $role = $request->getValue('role');
183  $permissionManager->removePermission($opResource, $opContext, $opAction, $role);
184  }
185  }
186  $response->setAction('ok');
187  }
188 }
189 ?>
getPermissionManager()
Get the PermissionManager instance.
Definition: Controller.php:291
requireTransaction()
Start or join a transaction that will be committed at the end of execution.
Definition: Controller.php:334
static parseKey($actionKey)
Parse an action.
Definition: ActionKey.php:42
ApplicationError is used to signal errors that occur while processing a request.
static get($code, $data=null)
Factory method for retrieving a predefined error instance.
getRequest()
Get the Request instance.
Definition: Controller.php:251
PermissionController checks, gets and sets permissions.
Application controllers.
Definition: namespaces.php:3
An action key is a combination of a resource, context and action that is represented as a string.
Definition: ActionKey.php:22
Controller is the base class of all controllers.
Definition: Controller.php:49
getResponse()
Get the Response instance.
Definition: Controller.php:259
PermissionManager implementations are used to handle all authorization requests.