mirror of
https://git.code.sf.net/p/seeddms/code
synced 2025-02-11 09:35:00 +00:00
add new endpoints for managing roles
This commit is contained in:
parent
4275072338
commit
0da2308ee3
|
@ -170,7 +170,7 @@ class RestapiController { /* {{{ */
|
|||
'email'=>$u->getEmail(),
|
||||
'language' => $u->getLanguage(),
|
||||
'theme' => $u->getTheme(),
|
||||
'role' => array('id'=>(int)$u->getRole()->getId(), 'name'=>$u->getRole()->getName()),
|
||||
'role' => $this->__getRoleData($u->getRole()), //array('id'=>(int)$u->getRole()->getId(), 'name'=>$u->getRole()->getName()),
|
||||
'hidden'=>$u->isHidden() ? true : false,
|
||||
'disabled'=>$u->isDisabled() ? true : false,
|
||||
'isguest' => $u->isGuest() ? true : false,
|
||||
|
@ -189,6 +189,16 @@ class RestapiController { /* {{{ */
|
|||
return $data;
|
||||
} /* }}} */
|
||||
|
||||
protected function __getRoleData($r) { /* {{{ */
|
||||
$data = array(
|
||||
'type'=>'role',
|
||||
'id'=>(int)$r->getID(),
|
||||
'name'=>$r->getName(),
|
||||
'role'=>$r->getRole()
|
||||
);
|
||||
return $data;
|
||||
} /* }}} */
|
||||
|
||||
protected function __getAttributeDefinitionData($attrdef) { /* {{{ */
|
||||
$data = [
|
||||
'id' => (int)$attrdef->getId(),
|
||||
|
@ -1192,7 +1202,7 @@ class RestapiController { /* {{{ */
|
|||
->withHeader('Content-Description', 'File Transfer')
|
||||
->withHeader('Content-Transfer-Encoding', 'binary')
|
||||
->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"')
|
||||
->withHeader('Content-Length', filesize($dms->contentDir . $lc->getPath()))
|
||||
->withAddedHeader('Content-Length', filesize($dms->contentDir . $lc->getPath()))
|
||||
->withHeader('Expires', '0')
|
||||
->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
|
||||
->withHeader('Pragma', 'no-cache')
|
||||
|
@ -2055,6 +2065,9 @@ class RestapiController { /* {{{ */
|
|||
$comment = isset($params['comment']) ? $params['comment'] : '';
|
||||
$role = isset($params['role']) ? $params['role'] : 3;
|
||||
$roleobj = $role == 'admin' ? SeedDMS_Core_Role::getInstance(1, $dms) : ($role == 'guest' ? SeedDMS_Core_Role::getInstance(2, $dms) : SeedDMS_Core_Role::getInstance($role, $dms));
|
||||
if(!$roleobj) {
|
||||
return $response->withJson(array('success'=>false, 'message'=>'Missing role', 'data'=>''), 400);
|
||||
}
|
||||
|
||||
$newAccount = $dms->addUser($userName, seed_pass_hash($password), $fullname, $email, $language, $theme, $comment, $roleobj);
|
||||
if ($newAccount === false) {
|
||||
|
@ -2085,7 +2098,8 @@ class RestapiController { /* {{{ */
|
|||
} /* }}} */
|
||||
|
||||
/**
|
||||
* Updates the password of an existing Account, the password must be PUT as a md5 string
|
||||
* Updates the password of an existing Account, the password
|
||||
* will be hashed by this method
|
||||
*
|
||||
* @param <type> $id The user name or numerical identifier
|
||||
*/
|
||||
|
@ -2118,7 +2132,7 @@ class RestapiController { /* {{{ */
|
|||
return;
|
||||
}
|
||||
|
||||
$operation = $account->setPwd($newPassword);
|
||||
$operation = $account->setPwd(seed_pass_hash($newPassword));
|
||||
|
||||
if (!$operation){
|
||||
return $response->withJson(array('success'=>false, 'message'=>'', 'data'=>'Could not change password.'), 404);
|
||||
|
@ -2180,6 +2194,69 @@ class RestapiController { /* {{{ */
|
|||
}
|
||||
} /* }}} */
|
||||
|
||||
function getRoles($request, $response) { /* {{{ */
|
||||
$dms = $this->container->dms;
|
||||
$userobj = $this->container->userobj;
|
||||
$check = $this->checkIfAdmin($request, $response);
|
||||
if($check !== true)
|
||||
return $check;
|
||||
|
||||
$roles = $dms->getAllRoles();
|
||||
$data = [];
|
||||
foreach($roles as $r)
|
||||
$data[] = $this->__getRoleData($r);
|
||||
|
||||
return $response->withJson(array('success'=>true, 'message'=>'', 'data'=>$data), 200);
|
||||
} /* }}} */
|
||||
|
||||
function createRole($request, $response) { /* {{{ */
|
||||
$dms = $this->container->dms;
|
||||
$userobj = $this->container->userobj;
|
||||
|
||||
$check = $this->checkIfAdmin($request, $response);
|
||||
if($check !== true)
|
||||
return $check;
|
||||
$params = $request->getParsedBody();
|
||||
if (empty($params['name'])) {
|
||||
return $response->withJson(array('success'=>false, 'message'=>'Need a role name.', 'data'=>''), 400);
|
||||
}
|
||||
|
||||
$roleName = $params['name'];
|
||||
$roleType = $params['role'];
|
||||
|
||||
$newRole = $dms->addRole($roleName, $roleType);
|
||||
if ($newRole === false) {
|
||||
return $response->withJson(array('success'=>false, 'message'=>'Role could not be created, maybe it already exists', 'data'=>''), 500);
|
||||
}
|
||||
|
||||
// $result = array('id'=>(int)$newGroup->getID());
|
||||
return $response->withJson(array('success'=>true, 'message'=>'', 'data'=>$this->__getRoleData($newRole)), 201);
|
||||
} /* }}} */
|
||||
|
||||
function getRole($request, $response, $args) { /* {{{ */
|
||||
$dms = $this->container->dms;
|
||||
$userobj = $this->container->userobj;
|
||||
|
||||
$check = $this->checkIfAdmin($request, $response);
|
||||
if($check !== true)
|
||||
return $check;
|
||||
if(ctype_digit($args['id']))
|
||||
$role = $dms->getRole($args['id']);
|
||||
else {
|
||||
$role = $dms->getRoleByName($args['id']);
|
||||
}
|
||||
if($role) {
|
||||
$data = $this->__getRoleData($role);
|
||||
$data['users'] = array();
|
||||
foreach ($role->getUsers() as $user) {
|
||||
$data['users'][] = array('id' => (int)$user->getID(), 'login' => $user->getLogin());
|
||||
}
|
||||
return $response->withJson(array('success'=>true, 'message'=>'', 'data'=>$data), 200);
|
||||
} else {
|
||||
return $response->withJson(array('success'=>false, 'message'=>'No such role', 'data'=>''), 404);
|
||||
}
|
||||
} /* }}} */
|
||||
|
||||
function getGroups($request, $response) { /* {{{ */
|
||||
$dms = $this->container->dms;
|
||||
$userobj = $this->container->userobj;
|
||||
|
@ -2905,6 +2982,9 @@ $app->post('/users', \RestapiController::class.':createUser');
|
|||
$app->get('/users/{id}', \RestapiController::class.':getUserById');
|
||||
$app->put('/users/{id}/disable', \RestapiController::class.':setDisabledUser');
|
||||
$app->put('/users/{id}/password', \RestapiController::class.':changeUserPassword');
|
||||
$app->get('/roles', \RestapiController::class.':getRoles');
|
||||
$app->post('/roles', \RestapiController::class.':createRole');
|
||||
$app->get('/roles/{id}', \RestapiController::class.':getRole');
|
||||
$app->post('/groups', \RestapiController::class.':createGroup');
|
||||
$app->get('/groups', \RestapiController::class.':getGroups');
|
||||
$app->delete('/groups/{id}', \RestapiController::class.':deleteGroup');
|
||||
|
|
Loading…
Reference in New Issue
Block a user