From dd4942f67cccf14c7c3fb56c80d6d18eaf0db651 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Wed, 22 Apr 2020 19:09:49 +0200 Subject: [PATCH] FEAT #13664 TIME 1:50 Get Alfresco accounts + create --- rest/index.php | 2 + src/app/entity/models/EntityModelAbstract.php | 20 ++--- .../controllers/AlfrescoController.php | 83 +++++++++++++++++-- .../controllers/PriorityController.php | 2 +- src/frontend/lang/lang-fr.ts | 2 +- 5 files changed, 90 insertions(+), 19 deletions(-) diff --git a/rest/index.php b/rest/index.php index 09a7b979199..28b654dd65d 100755 --- a/rest/index.php +++ b/rest/index.php @@ -562,6 +562,8 @@ $app->get('/externalConnectionsEnabled', \SrcCore\controllers\CoreController::cl //Alfresco $app->get('/alfresco/accounts', \Alfresco\controllers\AlfrescoController::class . ':getAccounts'); +$app->post('/alfresco/accounts', \Alfresco\controllers\AlfrescoController::class . ':createAccount'); +$app->get('/alfresco/accounts/{id}', \Alfresco\controllers\AlfrescoController::class . ':getAccountById'); $app->get('/alfresco/rootFolders', \Alfresco\controllers\AlfrescoController::class . ':getRootFolders'); $app->get('/alfresco/folders/{id}/children', \Alfresco\controllers\AlfrescoController::class . ':getChildrenFoldersById'); $app->get('/alfresco/autocomplete/folders', \Alfresco\controllers\AlfrescoController::class . ':getFolders'); diff --git a/src/app/entity/models/EntityModelAbstract.php b/src/app/entity/models/EntityModelAbstract.php index e995f802003..64eaae4d530 100755 --- a/src/app/entity/models/EntityModelAbstract.php +++ b/src/app/entity/models/EntityModelAbstract.php @@ -112,21 +112,17 @@ abstract class EntityModelAbstract return true; } - 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::stringType($aArgs['set'], [ - 'entity_label', 'short_label', 'entity_type', 'adrs_1', 'adrs_2', 'adrs_3', - 'zipcode', 'city', 'country', 'email', 'business_id', 'parent_entity_id', - 'ldap_id', 'transferring_agency', 'archival_agreement', 'archival_agency', 'entity_full_name' - ]); + ValidatorModel::notEmpty($args, ['where', 'data']); + ValidatorModel::arrayType($args, ['set', 'postSet', 'where', 'data']); DatabaseModel::update([ - 'table' => 'entities', - 'set' => $aArgs['set'], - 'where' => $aArgs['where'], - 'data' => $aArgs['data'] + 'table' => 'entities', + 'set' => $args['set'], + 'postSet' => $args['postSet'], + 'where' => $args['where'], + 'data' => $args['data'] ]); return true; diff --git a/src/app/external/alfresco/controllers/AlfrescoController.php b/src/app/external/alfresco/controllers/AlfrescoController.php index 31fc3794d11..5c2b4f6e2aa 100644 --- a/src/app/external/alfresco/controllers/AlfrescoController.php +++ b/src/app/external/alfresco/controllers/AlfrescoController.php @@ -37,24 +37,97 @@ class AlfrescoController // return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); // } - $entities = EntityModel::get(['select' => ['external_id'], 'where' => ["external_id->>'alfresco' is not null"]]); + $entities = EntityModel::get(['select' => ['external_id', 'short_label'], 'where' => ["external_id->>'alfresco' is not null"]]); $accounts = []; $alreadyAdded = []; foreach ($entities as $entity) { $alfresco = json_decode($entity['external_id'], true); - if (!in_array($alfresco['alfresco']['login'], $alreadyAdded)) { + if (!in_array($alfresco['alfresco']['id'], $alreadyAdded)) { $accounts[] = [ - 'label' => $alfresco['alfresco']['label'], - 'login' => $alfresco['alfresco']['login'] + 'id' => $alfresco['alfresco']['id'], + 'label' => $alfresco['alfresco']['label'], + 'login' => $alfresco['alfresco']['login'], + 'entitiesLabel' => [$entity['short_label']] ]; - $alreadyAdded[] = $alfresco['alfresco']['login']; + $alreadyAdded[] = $alfresco['alfresco']['id']; + } else { + foreach ($accounts as $key => $value) { + if ($value['id'] == $alfresco['alfresco']['id']) { + $accounts[$key]['entitiesLabel'][] = $entity['short_label']; + } + } } } return $response->withJson(['accounts' => $accounts]); } + public function getAccountById(Request $request, Response $response, array $args) + { +// if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_alfresco', 'userId' => $GLOBALS['id']])) { +// return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); +// } + + $entities = EntityModel::get(['select' => ['external_id', 'id'], 'where' => ["external_id->'alfresco'->>'id' = ?"], 'data' => [$args['id']]]); + if (empty($entities[0])) { + return $response->withStatus(400)->withJson(['errors' => 'Account not found']); + } + + $alfresco = json_decode($entities[0]['external_id'], true); + $account = [ + 'id' => $alfresco['alfresco']['id'], + 'label' => $alfresco['alfresco']['label'], + 'login' => $alfresco['alfresco']['login'], + 'entities' => [] + ]; + + foreach ($entities as $entity) { + $account['entities'][] = $entity['id']; + } + + return $response->withJson($account); + } + + public function createAccount(Request $request, Response $response) + { +// if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_alfresco', 'userId' => $GLOBALS['id']])) { +// return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); +// } + + $body = $request->getParsedBody(); + + if (!Validator::stringType()->notEmpty()->validate($body['label'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']); + } elseif (!Validator::stringType()->notEmpty()->validate($body['login'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body login is empty or not a string']); + } elseif (!Validator::stringType()->notEmpty()->validate($body['password'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body password is empty or not a string']); + } elseif (!Validator::stringType()->notEmpty()->validate($body['nodeId'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body nodeId is empty or not a string']); + } elseif (!Validator::arrayType()->notEmpty()->validate($body['entities'])) { + return $response->withStatus(400)->withJson(['errors' => 'Body entities is empty or not an array']); + } + + $id = CoreConfigModel::uniqueId(); + $account = [ + 'id' => $id, + 'label' => $body['label'], + 'login' => $body['login'], + 'password' => PasswordModel::encrypt(['password' => $body['password']]), + 'nodeId' => $body['nodeId'] + ]; + $account = json_encode($account); + + EntityModel::update([ + 'postSet' => ['external_id' => "jsonb_set(external_id, '{alfresco}', '{$account}')"], + 'where' => ['id in (?)'], + 'data' => [$body['entities']] + ]); + + return $response->withStatus(204); + } + public function getRootFolders(Request $request, Response $response) { $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/alfrescoConfig.xml']); diff --git a/src/app/priority/controllers/PriorityController.php b/src/app/priority/controllers/PriorityController.php index a8cc82cd13b..cb75da1dd79 100755 --- a/src/app/priority/controllers/PriorityController.php +++ b/src/app/priority/controllers/PriorityController.php @@ -45,7 +45,7 @@ class PriorityController return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } - $data = $request->getParams(); + $data = $request->getParsedBody(); $check = Validator::stringType()->notEmpty()->validate($data['label']); $check = $check && Validator::stringType()->notEmpty()->validate($data['color']); $check = $check && (Validator::intVal()->notEmpty()->validate($data['delays']) || $data['delays'] == 0); diff --git a/src/frontend/lang/lang-fr.ts b/src/frontend/lang/lang-fr.ts index 51a7565f754..1f173107d76 100755 --- a/src/frontend/lang/lang-fr.ts +++ b/src/frontend/lang/lang-fr.ts @@ -1673,7 +1673,7 @@ export const LANG_FR = { "accountSuspended": "Votre compte utilisateur a été suspendu", "accountLocked": "Nombre de tentatives de connexion dépassée. Réessayez dans", "modelUsedByResources": "Le modèle est utilisé par des courriers, vous ne pouvez pas le supprimer.", - "mustChangePassword": "Vous êtes invité à changer votre mot de passe.", + "mustChangePassword": "Vous êtes invités à changer votre mot de passe.", "linkedResources": "Pièces jointes (courriers liés)", "accessNotFound": "Accès introuvable", "moreOneCustom": "Cette url ne correspond à aucune instance configurée, veuillez vérifier l'adresse.", -- GitLab