PermissionController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 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 
24 
25 /**
26  * PermissionController checks permissions for a set of operations for
27  * the current user.
28  *
29  * The controller supports the following actions:
30  *
31  * <div class="controller-action">
32  * <div> __Action__ checkPermissions </div>
33  * <div>
34  * Check permissions of a set of operations for the current user.
35  * | Parameter | Description
36  * |------------------------|-------------------------
37  * | _in_ `operations` | Array of resource/context/action triples in the form _resource?context?action_
38  * | _out_ `result` | Associative array with the operations as keys and boolean values indicating if permissions are given or not
39  * | __Response Actions__ | |
40  * | `ok` | In all cases
41  * </div>
42  * </div>
43  *
44  * <div class="controller-action">
45  * <div> __Action__ checkPermissionsOfUser </div>
46  * <div>
47  * Check permissions of a set of operations for the given user.
48  * | Parameter | Description
49  * |------------------------|-------------------------
50  * | _in_ `operations` | Array of resource/context/action triples in the form _resource?context?action_
51  * | _in_ `user` | Username to check permissions for (optional, default: the authenticated user)
52  * | _out_ `result` | Associative array with the operations as keys and boolean values indicating if permissions are given or not
53  * | __Response Actions__ | |
54  * | `ok` | In all cases
55  * </div>
56  * </div>
57  *
58  * <div class="controller-action">
59  * <div> __Action__ getPermissions </div>
60  * <div>
61  * Get the permissions on a resource, context, action combination.
62  * | Parameter | Description
63  * |-----------------------|-------------------------
64  * | _in_ `resource` | The resource (e.g. class name of the Controller or ObjectId).
65  * | _in_ `context` | The context in which the action takes place (optional).
66  * | _in_ `action` | The action to process.
67  * | _out_ `result` | Assoziative array with keys 'default' (boolean), 'allow', 'deny' (arrays of role names) or null, if no permissions are defined.
68  * </div>
69  * </div>
70  *
71  * <div class="controller-action">
72  * <div> __Action__ setPermissions </div>
73  * <div>
74  * Set the permissions on a resource, context, action combination.
75  * | Parameter | Description
76  * |-----------------------|-------------------------
77  * | _in_ `resource` | The resource (e.g. class name of the Controller or ObjectId).
78  * | _in_ `context` | The context in which the action takes place (optional).
79  * | _in_ `action` | The action to process.
80  * | _in_ `permissions` | Assoziative array with keys 'default' (boolean), 'allow', 'deny' (arrays of role names).
81  * </div>
82  * </div>
83  *
84  * <div class="controller-action">
85  * <div> __Action__ createPermission </div>
86  * <div>
87  * Create/Change a permission for a role on a resource, context, action combination.
88  * | Parameter | Description
89  * |-----------------------|-------------------------
90  * | _in_ `resource` | The resource (e.g. class name of the Controller or ObjectId).
91  * | _in_ `context` | The context in which the action takes place (optional).
92  * | _in_ `action` | The action to process.
93  * | _in_ `role` | The role to add.
94  * | _in_ `modifier` | _+_ or _-_ whether to allow or disallow the action for the role.
95  * </div>
96  * </div>
97  *
98  * <div class="controller-action">
99  * <div> __Action__ removePermission </div>
100  * <div>
101  * Remove a role from a permission on a resource, context, action combination.
102  * | Parameter | Description
103  * |-----------------------|-------------------------
104  * | _in_ `resource` | The resource (e.g. class name of the Controller or ObjectId).
105  * | _in_ `context` | The context in which the action takes place (optional).
106  * | _in_ `action` | The action to process.
107  * | _in_ `role` | The role to remove.
108  * </div>
109  * </div>
110  *
111  * @author ingo herwig <ingo@wemove.com>
112  */
114 
115  private $_principalFactory = null;
116 
117  /**
118  * Constructor
119  * @param $session
120  * @param $persistenceFacade
121  * @param $permissionManager
122  * @param $actionMapper
123  * @param $localization
124  * @param $message
125  * @param $configuration
126  * @param $principalFactory
127  */
128  public function __construct(Session $session,
129  PersistenceFacade $persistenceFacade,
130  PermissionManager $permissionManager,
131  ActionMapper $actionMapper,
132  Localization $localization,
133  Message $message,
134  Configuration $configuration,
135  PrincipalFactory $principalFactory) {
136  parent::__construct($session, $persistenceFacade, $permissionManager,
137  $actionMapper, $localization, $message, $configuration);
138  $this->_principalFactory = $principalFactory;
139  }
140 
141  /**
142  * @see Controller::validate()
143  */
144  protected function validate() {
145  $request = $this->getRequest();
146  $response = $this->getResponse();
147  $invalidParameters = array();
148  if ($request->getAction() == 'createPermission' || $request->getAction() == 'removePermission' ||
149  $request->getAction() == 'getPermissions' || $request->getAction() == 'setPermissions') {
150  foreach (array('resource', 'context', 'action') as $param) {
151  if(!$request->hasValue($param)) {
152  $invalidParameters[] = $param;
153  }
154  }
155  }
156  if ($request->getAction() == 'createPermission') {
157  $permissions = $request->getValue('permissions');
158  if (!isset($permissions['allow']) || !isset($permissions['deny'])) {
159  $invalidParameters[] = 'permissions';
160  }
161  }
162  if ($request->getAction() == 'createPermission') {
163  $modifier = $request->getValue('modifier');
166  $invalidParameters[] = 'modifier';
167  }
168  }
169 
170  if (sizeof($invalidParameters) > 0) {
171  $response->addError(ApplicationError::get('PARAMETER_INVALID',
172  array('invalidParameters' => $invalidParameters)));
173  return false;
174  }
175  return true;
176  }
177 
178  /**
179  * @see Controller::doExecute()
180  */
181  protected function doExecute() {
182  $request = $this->getRequest();
183  $response = $this->getResponse();
184  $permissionManager = $this->getPermissionManager();
185  $transaction = $this->getPersistenceFacade()->getTransaction();
186 
187  $resource = $request->getValue('resource');
188  $context = $request->getValue('context');
189  $action = $request->getValue('action');
190 
191  // process actions
192  if ($request->getAction() == 'checkPermissions') {
193  $result = array();
194  $permissions = $request->hasValue('operations') ? $request->getValue('operations') : array();
195  foreach($permissions as $permission) {
196  $keyParts = ActionKey::parseKey($permission);
197  $result[$permission] = $permissionManager->authorize($keyParts['resource'], $keyParts['context'], $keyParts['action']);
198  }
199  $response->setValue('result', $result);
200  }
201  elseif ($request->getAction() == 'checkPermissionsOfUser') {
202  $result = array();
203  $permissions = $request->hasValue('operations') ? $request->getValue('operations') : array();
204  $user = $request->hasValue('user') ? $this->_principalFactory->getUser($request->getValue('user')) : null;
205  foreach($permissions as $permission) {
206  $keyParts = ActionKey::parseKey($permission);
207  $result[$permission] = $permissionManager->authorize($keyParts['resource'], $keyParts['context'], $keyParts['action'],
208  $user);
209  }
210  $response->setValue('result', $result);
211  }
212  elseif ($request->getAction() == 'getPermissions') {
213 
214  $result = $permissionManager->getPermissions($resource, $context, $action);
215  $response->setValue('result', $result);
216  }
217  elseif ($request->getAction() == 'setPermissions') {
218  $permissions = $request->getValue('permissions');
219 
220  $transaction->begin();
221  $permissionManager->setPermissions($resource, $context, $action, $permissions);
222  $transaction->commit();
223  }
224  elseif ($request->getAction() == 'createPermission') {
225  $role = $request->getValue('role');
226  $modifier = $request->getValue('modifier');
227 
228  $transaction->begin();
229  $permissionManager->createPermission($resource, $context, $action, $role, $modifier);
230  $transaction->commit();
231  }
232  elseif ($request->getAction() == 'removePermission') {
233  $role = $request->getValue('role');
234 
235  $transaction->begin();
236  $permissionManager->removePermission($resource, $context, $action, $role);
237  $transaction->commit();
238  }
239  $response->setAction('ok');
240  }
241 }
242 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
Localization defines the interface for storing localized entity instances and retrieving them back...
Controller is the base class of all controllers.
Definition: Controller.php:48
getPermissionManager()
Get the PermissionManager instance.
Definition: Controller.php:230
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
Session is the interface for session implementations and defines access to session variables...
Definition: Session.php:21
static parseKey($actionKey)
Parse an action.
Definition: ActionKey.php:42
PrincipalFactory implementations are used to retrieve User and Role instances.
PermissionManager implementations are used to handle all authorization requests.
Implementations of Configuration give access to the application configuration.
PermissionController checks permissions for a set of operations for the current user.
Application controllers.
Definition: namespaces.php:3
ActionMapper implementations are responsible for instantiating and executing Controllers based on the...
static get($code, $data=null)
Factory method for retrieving a predefind error instance.
PersistenceFacade defines the interface for PersistenceFacade implementations.
__construct(Session $session, PersistenceFacade $persistenceFacade, PermissionManager $permissionManager, ActionMapper $actionMapper, Localization $localization, Message $message, Configuration $configuration, PrincipalFactory $principalFactory)
Constructor.
getResponse()
Get the Response instance.
Definition: Controller.php:198
getPersistenceFacade()
Get the PersistenceFacade instance.
Definition: Controller.php:222