fix: Added constants to role permissions for the API

- Permission now accepts a constant [read|manage_own|manage_org|publish] in addition to a numeric value [0|1|2|3]
- Querying a role via the API returns the constant additionally to the numeric value in the permission_description field

- Added /roles/view/{id} to the API
pull/2379/merge
iglocska 2017-08-01 11:24:29 +02:00
parent 0097e040b1
commit e0caa7a59e
2 changed files with 27 additions and 4 deletions

View File

@ -31,9 +31,13 @@ class RolesController extends AppController {
if (!$this->Role->exists()) {
throw new NotFoundException(__('Invalid role'));
}
$this->set('premissionLevelName', $this->Role->premissionLevelName);
$this->set('role', $this->Role->read(null, $id));
$this->set('id', $id);
if ($this->_isRest()) {
return $this->RestResponse->viewData($this->Role->read(null, $id), $this->response->type());
} else {
$this->set('premissionLevelName', $this->Role->premissionLevelName);
$this->set('role', $this->Role->read(null, $id));
$this->set('id', $id);
}
}
public function admin_add() {

View File

@ -47,6 +47,13 @@ class Role extends AppModel {
'permission' => "CASE WHEN (Role.perm_add + Role.perm_modify + Role.perm_publish = 3) THEN '3' WHEN (Role.perm_add + Role.perm_modify_org = 2) THEN '2' WHEN (Role.perm_add = 1) THEN '1' ELSE '0' END",
);
public $permissionConstants = array(
'read_only' => 0,
'manage_own' => 1,
'manage_org' => 2,
'publish' => 3
);
public $permFlags = array(
'perm_admin' => array('id' => 'RolePermAdmin', 'text' => 'Admin', 'readonlyenabled' => false),
'perm_site_admin' => array('id' => 'RolePermSiteAdmin', 'text' => 'Site Admin', 'readonlyenabled' => false),
@ -68,6 +75,14 @@ class Role extends AppModel {
//Conversion from the named data access permission levels
if (empty($this->data['Role']['permission'])) {
$this->data['Role']['permission'] = 0;
} else if (!is_numeric($this->data['Role']['permission'])) {
// If a constant was passed via the API, convert it to the numeric value
// For invalid entries, choose permission level 0
if (isset($this->permissionConstants[$this->data['Role']['permission']])) {
$this->data['Role']['permission'] = $this->permissionConstants[$this->data['Role']['permission']];
} else {
$this->data['Role']['permission'] = 0;
}
}
switch ($this->data['Role']['permission']) {
case '0':
@ -109,7 +124,11 @@ class Role extends AppModel {
public function afterFind($results, $primary = false) {
foreach ($results as $key => $val) {
unset($results[$key]['Role']['perm_full']);
if (isset($results[$key]['Role'])) {
unset($results[$key]['Role']['perm_full']);
$results[$key]['Role']['permission_description'] =
array_flip($this->permissionConstants)[$results[$key]['Role']['permission']];
}
}
return $results;
}