diff --git a/rest/index.php b/rest/index.php index 1cfe06fcaecbe48f022f4b31be42a41fbcc21b90..0106e12e5cc2d01216cbb8ab5f4716684e11d76a 100755 --- a/rest/index.php +++ b/rest/index.php @@ -254,7 +254,7 @@ $app->get('/res/{resId}/content', \Resource\controllers\ResController::class . ' $app->get('/res/{resId}/thumbnail', \Resource\controllers\ResController::class . ':getThumbnailContent'); $app->put('/res/resource/status', \Resource\controllers\ResController::class . ':updateStatus'); $app->post('/res/list', \Resource\controllers\ResController::class . ':getList'); -$app->get('/res/{resId}/lock', \Resource\controllers\ResController::class . ':isLock'); +$app->put('/resources/{resId}/lock', \Resource\controllers\ResController::class . ':lock'); $app->get('/res/{resId}/notes/count', \Resource\controllers\ResController::class . ':getNotesCountForCurrentUserById'); $app->put('/res/externalInfos', \Resource\controllers\ResController::class . ':updateExternalInfos'); $app->get('/categories', \Resource\controllers\ResController::class . ':getCategories'); diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index 6d97a03436b6619fc74f4abc067abd58f09bbc8c..177d3c2de87a12a1705d652edd51cbe2bfd4e990 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -519,6 +519,32 @@ class ResController return $response->withJson(['lock' => $lock, 'lockBy' => $lockBy]); } + public function lock(Request $request, Response $response, array $aArgs) + { + if (!ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) { + return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']); + } + + $currentUser = UserModel::getByLogin(['login' => $GLOBALS['userId'], 'select' => ['id']]); + $resource = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['locker_user_id', 'locker_time']]); + + $lock = true; + if (empty($resource['locker_user_id'] || empty($resource['locker_time']))) { + $lock = false; + } elseif ($resource['locker_user_id'] == $currentUser['id']) { + $lock = false; + } elseif (strtotime($resource['locker_time']) < time()) { + $lock = false; + } + + if ($lock) { + $user = UserModel::getLabelledUserById(['id' => $resource['locker_user_id']]); + return $response->withStatus(403)->withJson(['lockBy' => $user]); + } + + return $response->withStatus(204); + } + public function getNotesCountForCurrentUserById(Request $request, Response $response, array $aArgs) { return $response->withJson(NoteModel::countByResId(['resId' => $aArgs['resId'], 'login' => $GLOBALS['userId']])); diff --git a/src/frontend/app/signature-book.component.ts b/src/frontend/app/signature-book.component.ts index f65a8faa96f62781ef6a972f41b8fe6e5b2fc47c..c335543819aaf0dd6292f0c0de600bef9e653121 100755 --- a/src/frontend/app/signature-book.component.ts +++ b/src/frontend/app/signature-book.component.ts @@ -487,18 +487,20 @@ export class SignatureBookComponent implements OnInit { } changeLocation(resId: number, origin: string) { - this.http.get(this.coreUrl + 'rest/res/' + resId + '/lock') - .subscribe((data : any) => { - if (!data.lock) { - let path = "/groups/" + this.groupId + "/baskets/" + this.basketId + '/signatureBook/' + resId; - this.router.navigate([path]); - } else { + this.http.put(this.coreUrl + 'rest/resources/' + resId + '/lock', {}) + .subscribe(() => { + let path = "/groups/" + this.groupId + "/baskets/" + this.basketId + '/signatureBook/' + resId; + this.router.navigate([path]); + }, (err) => { + if (err.error.lockBy) { if (origin == "view") { - alert("Courrier verrouillé par " + data.lockBy); + alert("Courrier verrouillé par " + err.error.lockBy); } else if (origin == "action") { - alert("Courrier suivant verrouillé par " + data.lockBy); + alert("Courrier suivant verrouillé par " + err.error.lockBy); this.backToBasket(); } + } else { + this.notify.error(err.error.errors); } }); }