From f4f6170e1e9bf21892e9ef9c93484d3b95b5b946 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Wed, 3 Jul 2019 16:30:24 +0200 Subject: [PATCH] FEAT #11183 TIME 0:30 Update user substitute route + remove user enabled --- rest/index.php | 1 + sql/structure.sql | 1 - .../user/controllers/SignatureController.php | 2 +- src/app/user/controllers/UserController.php | 75 +++++++++++++------ src/core/models/AuthenticationModel.php | 4 +- .../app/signatures/signatures.component.ts | 4 +- 6 files changed, 60 insertions(+), 27 deletions(-) diff --git a/rest/index.php b/rest/index.php index 1eef5ff900..2058298f95 100755 --- a/rest/index.php +++ b/rest/index.php @@ -100,6 +100,7 @@ $app->put('/users/{id}', \User\controllers\UserController::class . ':update'); $app->delete('/users/{id}', \User\controllers\UserController::class . ':delete'); $app->get('/users/{id}/picture', \User\controllers\UserController::class . ':getPictureById'); $app->put('/users/{id}/preferences', \User\controllers\UserController::class . ':updatePreferences'); +$app->put('/users/{id}/substitute', \User\controllers\UserController::class . ':updateSubstitute'); $app->put('/users/{id}/password', \User\controllers\UserController::class . ':updatePassword'); $app->get('/users/{id}/history', \History\controllers\HistoryController::class . ':getByUserId'); $app->post('/password', \User\controllers\UserController::class . ':forgotPassword'); diff --git a/sql/structure.sql b/sql/structure.sql index 6011f0e635..676ce32c92 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -197,7 +197,6 @@ CREATE TABLE users firstname character varying(128) NOT NULL, lastname character varying(128) NOT NULL, picture text, - enabled boolean DEFAULT TRUE NOT NULL, "isRest" boolean DEFAULT FALSE NOT NULL, preferences jsonb NOT NULL DEFAULT '{"lang" : "fr", "writingMode" : "direct", "writingSize" : 1, "writingColor" : "#000000", "notifications" : true}', substitute INTEGER DEFAULT NULL, diff --git a/src/app/user/controllers/SignatureController.php b/src/app/user/controllers/SignatureController.php index ea86b4a7f7..991b5feb0f 100755 --- a/src/app/user/controllers/SignatureController.php +++ b/src/app/user/controllers/SignatureController.php @@ -91,7 +91,7 @@ class SignatureController if ($type[0] != 'image') { return $response->withStatus(400)->withJson(['errors' => 'Signature is not an image']); } elseif ($size > 1000000) { - return $response->withStatus(400)->withJson(['errors' => 'Max file size reached (1 MB)', 'lang' => 'maxFileSizeReached']); + return $response->withStatus(400)->withJson(['errors' => 'Max file size reached (1 MB)']); } $storeInfos = DocserverController::storeResourceOnDocServer([ diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php index ab3444a073..c51632c9b8 100755 --- a/src/app/user/controllers/UserController.php +++ b/src/app/user/controllers/UserController.php @@ -172,8 +172,7 @@ class UserController $set = [ 'firstname' => $body['firstname'], 'lastname' => $body['lastname'], - 'email' => $body['email'], - 'substitute' => null + 'email' => $body['email'] ]; if ($GLOBALS['id'] == $args['id'] && !empty($body['picture'])) { @@ -200,25 +199,6 @@ class UserController $set['picture'] = $infoContent . $body['picture']; } - if (!empty($body['substitute']) && $args['id'] != $body['substitute']) { - $existingUser = UserModel::getById(['id' => $body['substitute'], 'select' => ['substitute']]); - if (empty($existingUser)) { - return $response->withStatus(400)->withJson(['errors' => 'Substitute user does not exist']); - } elseif (!empty($existingUser['substitute'])) { - return $response->withStatus(400)->withJson(['errors' => 'Substitute user has already substituted']); - } - - $substitutedUsers = UserModel::get(['select' => ['id'], 'where' => ['substitute = ?'], 'data' => [$args['id']]]); - foreach ($substitutedUsers as $user) { - UserModel::update([ - 'set' => ['substitute' => $body['substitute']], - 'where' => ['id = ?'], - 'data' => [$user['id']] - ]); - } - $set['substitute'] = $body['substitute']; - } - UserModel::update([ 'set' => $set, 'where' => ['id = ?'], @@ -357,6 +337,59 @@ class UserController return $response->withStatus(204); } + public function updateSubstitute(Request $request, Response $response, array $args) + { + if ($GLOBALS['id'] != $args['id']) { + return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']); + } + + $body = $request->getParsedBody(); + + $user = UserModel::getById(['id' => $args['id'], 'select' => ['firstname', 'lastname']]); + if (empty($user)) { + return $response->withStatus(400)->withJson(['errors' => 'User does not exist']); + } + + $set = [ + 'substitute' => null + ]; + + if (!empty($body['substitute']) && $args['id'] != $body['substitute']) { + $existingUser = UserModel::getById(['id' => $body['substitute'], 'select' => ['substitute']]); + if (empty($existingUser)) { + return $response->withStatus(400)->withJson(['errors' => 'Substitute user does not exist']); + } elseif (!empty($existingUser['substitute'])) { + return $response->withStatus(400)->withJson(['errors' => 'Substitute user has already substituted']); + } + + $substitutedUsers = UserModel::get(['select' => ['id'], 'where' => ['substitute = ?'], 'data' => [$args['id']]]); + foreach ($substitutedUsers as $user) { + UserModel::update([ + 'set' => ['substitute' => $body['substitute']], + 'where' => ['id = ?'], + 'data' => [$user['id']] + ]); + } + $set['substitute'] = $body['substitute']; + } + + UserModel::update([ + 'set' => $set, + 'where' => ['id = ?'], + 'data' => [$args['id']] + ]); + + HistoryController::add([ + 'code' => 'OK', + 'objectType' => 'users', + 'objectId' => $args['id'], + 'type' => 'MODIFICATION', + 'message' => "{userUpdated} : {$user['firstname']} {$user['lastname']}" + ]); + + return $response->withStatus(204); + } + public function updatePassword(Request $request, Response $response, array $args) { if (!Validator::intVal()->notEmpty()->validate($args['id'])) { diff --git a/src/core/models/AuthenticationModel.php b/src/core/models/AuthenticationModel.php index 98ac929d61..666162fe35 100755 --- a/src/core/models/AuthenticationModel.php +++ b/src/core/models/AuthenticationModel.php @@ -29,8 +29,8 @@ class AuthenticationModel $aReturn = DatabaseModel::select([ 'select' => ['password'], 'table' => ['users'], - 'where' => ['login = ?', 'enabled = ?'], - 'data' => [$args['login'], 'true'] + 'where' => ['login = ?'], + 'data' => [$args['login']] ]); if (empty($aReturn[0])) { diff --git a/src/frontend/app/signatures/signatures.component.ts b/src/frontend/app/signatures/signatures.component.ts index 21e6b4374b..fce3f739aa 100755 --- a/src/frontend/app/signatures/signatures.component.ts +++ b/src/frontend/app/signatures/signatures.component.ts @@ -122,7 +122,7 @@ export class SignaturesComponent implements OnInit { handleFileInput(files: FileList) { const fileToUpload = files.item(0); - if (fileToUpload.size <= 2000000) { + if (fileToUpload.size <= 1000000) { if (['image/png', 'image/jpg', 'image/jpeg', 'image/gif'].indexOf(fileToUpload.type) !== -1) { const myReader: FileReader = new FileReader(); myReader.onloadend = (e) => { @@ -155,7 +155,7 @@ export class SignaturesComponent implements OnInit { this.notificationService.error('lang.notAnImage'); } } else { - this.notificationService.error('lang.imageTooBig'); + this.notificationService.error('lang.maxFileSizeReached'); } } } -- GitLab