diff --git a/rest/index.php b/rest/index.php index 28b654dd65d14732b08b811d7b0cc686f573ccba..f934fd38fefb248b6b7ce50ca3db44d6983718b7 100755 --- a/rest/index.php +++ b/rest/index.php @@ -564,6 +564,8 @@ $app->get('/externalConnectionsEnabled', \SrcCore\controllers\CoreController::cl $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->put('/alfresco/accounts/{id}', \Alfresco\controllers\AlfrescoController::class . ':updateAccount'); +$app->delete('/alfresco/accounts/{id}', \Alfresco\controllers\AlfrescoController::class . ':deleteAccount'); $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/external/alfresco/controllers/AlfrescoController.php b/src/app/external/alfresco/controllers/AlfrescoController.php index 5c2b4f6e2aa53fdd46c3c9209bf1866e43618756..d7978d58931538d63dca4900d34dbf4bbe947ad9 100644 --- a/src/app/external/alfresco/controllers/AlfrescoController.php +++ b/src/app/external/alfresco/controllers/AlfrescoController.php @@ -79,6 +79,7 @@ class AlfrescoController 'id' => $alfresco['alfresco']['id'], 'label' => $alfresco['alfresco']['label'], 'login' => $alfresco['alfresco']['login'], + 'nodeId' => $alfresco['alfresco']['nodeId'], 'entities' => [] ]; @@ -109,6 +110,16 @@ class AlfrescoController return $response->withStatus(400)->withJson(['errors' => 'Body entities is empty or not an array']); } + foreach ($body['entities'] as $entity) { + if (!Validator::intVal()->notEmpty()->validate($entity)) { + return $response->withStatus(400)->withJson(['errors' => 'Body entities contains no integer values']); + } + } + $entities = EntityModel::get(['select' => ['id'], 'where' => ['id in (?)'], 'data' => [$body['entities']]]); + if (count($entities) != count($body['entities'])) { + return $response->withStatus(400)->withJson(['errors' => 'Some entities do not exist']); + } + $id = CoreConfigModel::uniqueId(); $account = [ 'id' => $id, @@ -128,6 +139,89 @@ class AlfrescoController return $response->withStatus(204); } + public function updateAccount(Request $request, Response $response, array $args) + { +// 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['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']); + } + + $accounts = EntityModel::get(['select' => ['external_id', 'id'], 'where' => ["external_id->'alfresco'->>'id' = ?"], 'data' => [$args['id']]]); + if (empty($accounts[0])) { + return $response->withStatus(400)->withJson(['errors' => 'Account not found']); + } + + foreach ($body['entities'] as $entity) { + if (!Validator::intVal()->notEmpty()->validate($entity)) { + return $response->withStatus(400)->withJson(['errors' => 'Body entities contains no integer values']); + } + } + $entities = EntityModel::get(['select' => ['id'], 'where' => ['id in (?)'], 'data' => [$body['entities']]]); + if (count($entities) != count($body['entities'])) { + return $response->withStatus(400)->withJson(['errors' => 'Some entities do not exist']); + } + + $alfresco = json_decode($accounts[0]['external_id'], true); + $account = [ + 'id' => $args['id'], + 'label' => $body['label'], + 'login' => $body['login'], + 'password' => empty($body['password']) ? $alfresco['alfresco']['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']] + ]); + + $previousEntities = array_column($accounts, 'id'); + $entitiesToRemove = array_diff($previousEntities, $body['entities']); + if (!empty($entitiesToRemove)) { + EntityModel::update([ + 'postSet' => ['external_id' => "external_id - 'alfresco'"], + 'where' => ['id in (?)'], + 'data' => [$entitiesToRemove] + ]); + } + + return $response->withStatus(204); + } + + public function deleteAccount(Request $request, Response $response, array $args) + { +// if (!PrivilegeController::hasPrivilege(['privilegeId' => 'admin_alfresco', 'userId' => $GLOBALS['id']])) { +// return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); +// } + + $accounts = EntityModel::get(['select' => ['external_id', 'id'], 'where' => ["external_id->'alfresco'->>'id' = ?"], 'data' => [$args['id']]]); + if (empty($accounts[0])) { + return $response->withStatus(400)->withJson(['errors' => 'Account not found']); + } + + $entitiesToRemove = array_column($accounts, 'id'); + EntityModel::update([ + 'postSet' => ['external_id' => "external_id - 'alfresco'"], + 'where' => ['id in (?)'], + 'data' => [$entitiesToRemove] + ]); + + return $response->withStatus(204); + } + public function getRootFolders(Request $request, Response $response) { $loadedXml = CoreConfigModel::getXmlLoaded(['path' => 'apps/maarch_entreprise/xml/alfrescoConfig.xml']);