diff --git a/migration/19.12/1912.sql b/migration/19.12/1912.sql index 0b59000b538ae2a747f5aff871417e8f804650bd..d9bb2fd892a016e777d234e2e1939992e1a6b411 100644 --- a/migration/19.12/1912.sql +++ b/migration/19.12/1912.sql @@ -431,6 +431,10 @@ CREATE TABLE contacts_parameters ) WITH (OIDS=FALSE); +/* USERS */ +ALTER TABLE users DROP COLUMN IF EXISTS preferences; +ALTER TABLE users ADD COLUMN preferences jsonb NOT NULL DEFAULT '{"documentEdition" : "java"}'; + ALTER TABLE acknowledgement_receipts DROP COLUMN IF EXISTS contact_id; ALTER TABLE acknowledgement_receipts ADD COLUMN contact_id integer; ALTER TABLE contacts_groups_lists DROP COLUMN IF EXISTS contact_id; diff --git a/sql/structure.sql b/sql/structure.sql index 7ff189a4a417064162b7cf92ed14aec4bc0c4f87..0b2833882b5c2a40dd06b46e9de35c9acd65a0fb 100755 --- a/sql/structure.sql +++ b/sql/structure.sql @@ -215,6 +215,7 @@ CREATE TABLE users phone character varying(32) DEFAULT NULL::character varying, mail character varying(255) DEFAULT NULL::character varying, initials character varying(32) DEFAULT NULL::character varying, + preferences jsonb NOT NULL DEFAULT '{"documentEdition" : "java"}', status character varying(10) NOT NULL DEFAULT 'OK'::character varying, password_modification_date timestamp without time zone DEFAULT CURRENT_TIMESTAMP, loginmode character varying(50) DEFAULT NULL::character varying, diff --git a/src/app/user/controllers/UserController.php b/src/app/user/controllers/UserController.php index 002b6f6aadece829a41b513af0d5741d31ac884d..c5d5ad01d5f54ddc51a019282686eb9b4136fd98 100755 --- a/src/app/user/controllers/UserController.php +++ b/src/app/user/controllers/UserController.php @@ -53,6 +53,7 @@ use User\models\UserSignatureModel; class UserController { const ALTERNATIVES_CONNECTIONS_METHODS = ['sso', 'cas', 'ldap', 'ozwillo', 'shibboleth']; + const DOCUMENT_EDITION_METHODS = ['java', 'onlyOffice']; public function get(Request $request, Response $response) { @@ -491,8 +492,9 @@ class UserController public function getProfile(Request $request, Response $response) { - $user = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id', 'user_id', 'firstname', 'lastname', 'phone', 'mail', 'initials', 'external_id']]); + $user = UserModel::getById(['id' => $GLOBALS['id'], 'select' => ['id', 'user_id', 'firstname', 'lastname', 'phone', 'mail', 'initials', 'preferences', 'external_id']]); $user['external_id'] = json_decode($user['external_id'], true); + $user['preferences'] = json_decode($user['preferences'], true); $user['signatures'] = UserSignatureModel::getByUserSerialId(['userSerialid' => $user['id']]); $user['emailSignatures'] = UserModel::getEmailSignaturesById(['userId' => $user['user_id']]); $user['groups'] = UserModel::getGroupsByLogin(['login' => $user['user_id']]); @@ -522,31 +524,37 @@ class UserController public function updateProfile(Request $request, Response $response) { - $user = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); - - $data = $request->getParams(); + $body = $request->getParsedBody(); - $check = Validator::stringType()->notEmpty()->validate($data['firstname']); - $check = $check && Validator::stringType()->notEmpty()->validate($data['lastname']); - $check = $check && (empty($data['mail']) || filter_var($data['mail'], FILTER_VALIDATE_EMAIL)); - $check = $check && (empty($data['phone']) || preg_match("/\+?((|\ |\.|\(|\)|\-)?(\d)*)*\d/", $data['phone'])); - if (!$check) { - return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); + if (!Validator::stringType()->notEmpty()->validate($body['firstname'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body firstname is empty or not a string']); + } elseif (!Validator::stringType()->notEmpty()->validate($body['lastname'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body lastname is empty or not a string']); + } elseif (!Validator::stringType()->notEmpty()->validate($body['mail']) || !filter_var($body['mail'], FILTER_VALIDATE_EMAIL)) { + return $response->withStatus(400)->withJson(['errors' => 'Body mail is empty or not a valid email']); + } elseif (!empty($body['phone']) && !preg_match("/\+?((|\ |\.|\(|\)|\-)?(\d)*)*\d/", $body['phone'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body phone is not a valid phone number']); + } elseif (!Validator::arrayType()->notEmpty()->validate($body['preferences'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body preferences is empty or not an array']); + } + if (!in_array($body['preferences']['documentEdition'], UserController::DOCUMENT_EDITION_METHODS)) { + return $response->withStatus(400)->withJson(['errors' => 'Body preferences[documentEdition] is not allowed']); } UserModel::update([ 'set' => [ - 'firstname' => $data['firstname'], - 'lastname' => $data['lastname'], - 'mail' => $data['mail'], - 'phone' => $data['phone'], - 'initials' => $data['initials'] + 'firstname' => $body['firstname'], + 'lastname' => $body['lastname'], + 'mail' => $body['mail'], + 'phone' => $body['phone'], + 'initials' => $body['initials'], + 'preferences' => json_encode($body['preferences']) ], 'where' => ['id = ?'], - 'data' => [$user['id']] + 'data' => [$GLOBALS['id']] ]); - return $response->withJson(['success' => 'success']); + return $response->withStatus(204); } public function updatePassword(Request $request, Response $response, array $aArgs) diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php index 553621f1bf2cb9158a6b368c0a477b9113eb8fd5..49bda96f94bc5e3312b0eaea9b151606cf2893bc 100755 --- a/src/app/user/models/UserModelAbstract.php +++ b/src/app/user/models/UserModelAbstract.php @@ -109,25 +109,25 @@ abstract class UserModelAbstract return $nextSequenceId; } - public static function update(array $aArgs) + public static function update(array $args) { - ValidatorModel::notEmpty($aArgs, ['set', 'where', 'data']); - ValidatorModel::arrayType($aArgs, ['set', 'where', 'data']); + ValidatorModel::notEmpty($args, ['set', 'where', 'data']); + ValidatorModel::arrayType($args, ['set', 'where', 'data']); DatabaseModel::update([ 'table' => 'users', - 'set' => $aArgs['set'], - 'where' => $aArgs['where'], - 'data' => $aArgs['data'] + 'set' => $args['set'], + 'where' => $args['where'], + 'data' => $args['data'] ]); return true; } - public static function delete(array $aArgs) + public static function delete(array $args) { - ValidatorModel::notEmpty($aArgs, ['id']); - ValidatorModel::intVal($aArgs, ['id']); + ValidatorModel::notEmpty($args, ['id']); + ValidatorModel::intVal($args, ['id']); DatabaseModel::update([ 'table' => 'users', @@ -138,7 +138,7 @@ abstract class UserModelAbstract 'external_id' => 'external_id - \'maarchParapheur\'' ], 'where' => ['id = ?'], - 'data' => [$aArgs['id']] + 'data' => [$args['id']] ]); return true; diff --git a/test/unitTests/app/user/UserControllerTest.php b/test/unitTests/app/user/UserControllerTest.php index c0e3af6231dbd19b3e660204062397adcbac13ad..b82ff205c262e1c6bc9669f2523eeff51774243a 100755 --- a/test/unitTests/app/user/UserControllerTest.php +++ b/test/unitTests/app/user/UserControllerTest.php @@ -704,17 +704,17 @@ class UserControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); $aArgs = [ - 'firstname' => 'Wonder', - 'lastname' => 'User', - 'mail' => 'dev@maarch.org', - 'initials' => 'SU' + 'firstname' => 'Wonder', + 'lastname' => 'User', + 'mail' => 'dev@maarch.org', + 'initials' => 'SU', + 'preferences' => ['documentEdition' => 'java'] ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $userController->updateProfile($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(204, $response->getStatusCode()); - $this->assertSame('success', $responseBody->success); // READ $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); @@ -728,21 +728,22 @@ class UserControllerTest extends TestCase $this->assertSame('dev@maarch.org', $responseBody->mail); $this->assertSame('SU', $responseBody->initials); + // UPDATE $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); $aArgs = [ - 'firstname' => 'Super', - 'lastname' => 'Admin', - 'mail' => 'dev@maarch.org', - 'initials' => 'SU' + 'firstname' => 'Super', + 'lastname' => 'ADMIN', + 'mail' => 'dev@maarch.org', + 'initials' => 'SU', + 'preferences' => ['documentEdition' => 'java'] ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $userController->updateProfile($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(204, $response->getStatusCode()); - $this->assertSame('success', $responseBody->success); // READ $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); @@ -755,22 +756,6 @@ class UserControllerTest extends TestCase $this->assertSame('Admin', $responseBody->lastname); $this->assertSame('dev@maarch.org', $responseBody->mail); $this->assertSame('SU', $responseBody->initials); - - // CORRECT UPDATE - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); - $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ - 'firstname' => 'Super', - 'lastname' => 'ADMIN', - 'mail' => 'dev@maarch.org', - 'initials' => 'SU' - ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); - - $response = $userController->updateProfile($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); - - $this->assertSame('success', $responseBody->success); } public function testSetRedirectedBasket()