Skip to content
Snippets Groups Projects
Verified Commit ec55bc4a authored by Florian Azizian's avatar Florian Azizian
Browse files

FIX #10733 TIME 0:45 refactor admin group routes

parent 9891cda4
No related branches found
No related tags found
No related merge requests found
......@@ -92,8 +92,8 @@ $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');
$app->put('/groups/{id}/privilege/{privilegeId}', \Group\controllers\GroupController::class . ':updateGroupPrivilege');
$app->post('/groups/{id}/user/{userId}', \Group\controllers\GroupController::class . ':addUser');
$app->delete('/groups/{id}/user/{userId}', \Group\controllers\GroupController::class . ':removeUser');
$app->put('/groups/{id}/users', \Group\controllers\GroupController::class . ':addUser');
$app->delete('/groups/{id}/users/{userId}', \Group\controllers\GroupController::class . ':removeUser');
//Users
$app->post('/users', \User\controllers\UserController::class . ':create');
......
......@@ -211,26 +211,27 @@ class GroupController
return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
}
$body = $request->getParsedBody();
if (!Validator::intVal()->notEmpty()->validate($aArgs['id'])) {
return $response->withStatus(400)->withJson(['errors' => 'Id must be an integer']);
} elseif (!Validator::intVal()->notEmpty()->validate($aArgs['userId'])) {
} elseif (!Validator::intVal()->notEmpty()->validate($body['userId'])) {
return $response->withStatus(400)->withJson(['errors' => 'userId must be an integer']);
}
$group = GroupModel::getById(['id' => $aArgs['id']]);
$user = UserModel::getById(['id' => $aArgs['userId'], 'select' => ['firstname', 'lastname']]);
$user = UserModel::getById(['id' => $body['userId'], 'select' => ['firstname', 'lastname']]);
if (empty($group)) {
return $response->withStatus(400)->withJson(['errors' => 'Group not found']);
} elseif (empty($user)) {
return $response->withStatus(400)->withJson(['errors' => 'User not found']);
} elseif (UserGroupModel::hasGroup(['groupId' => $aArgs['id'], 'userId' => $aArgs['userId']])) {
} elseif (UserGroupModel::hasGroup(['groupId' => $aArgs['id'], 'userId' => $body['userId']])) {
return $response->withStatus(400)->withJson(['errors' => 'This user already has this group']);
}
UserGroupModel::addUser([
'groupId' => $aArgs['id'],
'userId' => $aArgs['userId']
'userId' => $body['userId']
]);
HistoryController::add([
......@@ -244,7 +245,7 @@ class GroupController
HistoryController::add([
'code' => 'OK',
'objectType' => 'users',
'objectId' => $aArgs['userId'],
'objectId' => $body['userId'],
'type' => 'MODIFICATION',
'message' => "{groupAdded} : {$group['label']}"
]);
......
......@@ -80,13 +80,25 @@ class GroupControllerTest extends TestCase
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $groupController->addUser($request, new \Slim\Http\Response(), ['id' => self::$groupId, 'userId' => 1]);
$aArgs = [
'userId' => 1
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $groupController->addUser($fullRequest, new \Slim\Http\Response(), ['id' => self::$groupId]);
$responseBody = json_decode((string)$response->getBody());
$this->assertEmpty($responseBody);
//Fail
$response = $groupController->addUser($request, new \Slim\Http\Response(), ['id' => self::$groupId, 'userId' => 12456789]);
$aArgs = [
'userId' => 12456789
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $groupController->addUser($fullRequest, new \Slim\Http\Response(), ['id' => self::$groupId]);
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('User not found', $responseBody->errors);
......@@ -125,7 +137,7 @@ class GroupControllerTest extends TestCase
$this->assertEmpty($responseBody);
//Fail
$response = $groupController->addUser($request, new \Slim\Http\Response(), ['id' => self::$groupId, 'userId' => 12456789]);
$response = $groupController->removeUser($request, new \Slim\Http\Response(), ['id' => self::$groupId, 'userId' => 12456789]);
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('User not found', $responseBody->errors);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment