diff --git a/lang/en.json b/lang/en.json index e9deb820576947f9538012d474189355dbfebc3d..2d131d5ad5c050a6845823a34ceea056fbfc6fa3 100755 --- a/lang/en.json +++ b/lang/en.json @@ -188,6 +188,7 @@ "warnPrivateKey": "This cause security issues", "ok": "Ok", "groupAdded" : "Group added", - "groupdeleted" : "Group deleted" + "groupdeleted" : "Group deleted", + "groupUpdated" : "Group updated" } } diff --git a/lang/fr.json b/lang/fr.json index 576517331e6fbac9e84d22dd348351e16553ae2a..38b1c5ee446251981c3840072c7715645532d092 100755 --- a/lang/fr.json +++ b/lang/fr.json @@ -190,7 +190,8 @@ "warnPrivateKey": "Cela compromet la sécurité de l'application.", "ok": "J'ai compris", "groupAdded" : "Groupe ajouté", - "groupdeleted" : "Groupe supprimé" + "groupdeleted" : "Groupe supprimé", + "groupUpdated" : "Groupe mis à jour" } } diff --git a/rest/index.php b/rest/index.php index d0dab6fd5e708b4943241d6d1aa9dbf39fa7fabc..ea489dcf838c2d14b7b5fcd5ef0643bebf08bca7 100755 --- a/rest/index.php +++ b/rest/index.php @@ -85,6 +85,7 @@ $app->post('/groups', \Group\controllers\GroupController::class . ':create'); $app->get('/groups', \Group\controllers\GroupController::class . ':get'); $app->get('/groups/{id}', \Group\controllers\GroupController::class . ':getById'); $app->delete('/groups/{id}', \Group\controllers\GroupController::class . ':delete'); +$app->put('/groups/{id}', \Group\controllers\GroupController::class . ':update'); //Users $app->post('/users', \User\controllers\UserController::class . ':create'); diff --git a/src/app/group/controllers/GroupController.php b/src/app/group/controllers/GroupController.php index b85811f519e204c6d15f489936162e702646930e..9028964efd29e85cc8abab2ce1c721ff95d325ef 100755 --- a/src/app/group/controllers/GroupController.php +++ b/src/app/group/controllers/GroupController.php @@ -63,6 +63,42 @@ class GroupController return $response->withJson(['id' => $id]); } + public function update(Request $request, Response $response, $aArgs) + { + if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_groups'])) { + return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']); + } + + $body = $request->getParsedBody(); + + $group = GroupModel::getById(['id' => $aArgs['id']]); + if (empty($group)) { + return $response->withStatus(400)->withJson(['errors' => 'Group not found']); + } + + if (empty($body)) { + return $response->withStatus(400)->withJson(['errors' => 'Body is not set or empty']); + } elseif (!Validator::stringType()->notEmpty()->length(1, 128)->validate($body['label'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string or longer than 128 caracteres']); + } + + GroupModel::update([ + 'set' => ['label' => $body['label']], + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + + HistoryController::add([ + 'code' => 'OK', + 'objectType' => 'groups', + 'objectId' => $aArgs['id'], + 'type' => 'UPDATE', + 'message' => "{groupUpdated} : {$body['label']}" + ]); + + return $response->withStatus(204); + } + public function delete(Request $request, Response $response, $aArgs) { if (!PrivilegeController::hasPrivilege(['userId' => $GLOBALS['id'], 'privilege' => 'manage_groups'])) { diff --git a/src/app/group/models/GroupModel.php b/src/app/group/models/GroupModel.php index e979b2d1a734ac55935fff5788ea13e569682aa0..094c300a1de0a401010c119150cf4912adca95c5 100755 --- a/src/app/group/models/GroupModel.php +++ b/src/app/group/models/GroupModel.php @@ -72,6 +72,21 @@ class GroupModel return $nextSequenceId; } + public static function update(array $args) + { + ValidatorModel::notEmpty($args, ['set', 'where', 'data']); + ValidatorModel::arrayType($args, ['set', 'where', 'data']); + + DatabaseModel::update([ + 'table' => 'groups', + 'set' => $args['set'], + 'where' => $args['where'], + 'data' => $args['data'] + ]); + + return true; + } + public static function delete(array $args) { ValidatorModel::notEmpty($args, ['where', 'data']);