diff --git a/appinfo/routes.php b/appinfo/routes.php index c98468f3..782d0e03 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -41,6 +41,7 @@ return [ //Revisions ['name' => 'credential#getRevision', 'url' => '/api/v2/credentials/{credential_id}/revision', 'verb' => 'GET'], ['name' => 'credential#deleteRevision', 'url' => '/api/v2/credentials/{credential_id}/revision/{revision_id}', 'verb' => 'DELETE'], + ['name' => 'credential#updateRevision', 'url' => '/api/v2/credentials/{credential_id}/revision/{revision_id}', 'verb' => 'PATCH'], //File stuff ['name' => 'file#uploadFile', 'url' => '/api/v2/file', 'verb' => 'POST'], diff --git a/controller/credentialcontroller.php b/controller/credentialcontroller.php index 397b3793..24e6ed08 100644 --- a/controller/credentialcontroller.php +++ b/controller/credentialcontroller.php @@ -13,6 +13,7 @@ namespace OCA\Passman\Controller; use OCA\Files_External\NotFoundException; use OCA\Passman\Db\SharingACL; +use OCP\AppFramework\Db\DoesNotExistException; use OCP\AppFramework\Http; use OCP\AppFramework\Http\DataResponse; use OCP\IRequest; @@ -256,4 +257,19 @@ class CredentialController extends ApiController { $result = $this->credentialRevisionService->deleteRevision($revision_id, $this->userId); return new JSONResponse($result); } + + /** + * @NoAdminRequired + */ + public function updateRevision($credential_id, $revision_id, $credential_data){ + $revision = null; + try{ + $revision = $this->credentialRevisionService->getRevision($revision_id); + } catch(DoesNotExistException $exception){ + return new NotFoundResponse(); + } + + $revision->setCredentialData($credential_data); + $this->credentialRevisionService->updateRevision($revision); + } } \ No newline at end of file diff --git a/lib/Db/CredentialRevisionMapper.php b/lib/Db/CredentialRevisionMapper.php index 578d8100..cf2177b6 100644 --- a/lib/Db/CredentialRevisionMapper.php +++ b/lib/Db/CredentialRevisionMapper.php @@ -38,6 +38,22 @@ class CredentialRevisionMapper extends Mapper { return $this->findEntities($sql, $params); } + /** + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return CredentialRevision + */ + public function getRevision($revision_id, $user_id = null) { + $sql = 'SELECT * FROM `*PREFIX*passman_revisions` ' . + 'WHERE `id` = ?'; + $params = [$revision_id]; + if ($user_id !== null) { + $sql.= ' and `user_id` = ? '; + $params[] = $user_id; + } + return $this->findEntity($sql, $params); + } + public function create($credential, $userId, $credential_id, $edited_by) { $revision = new CredentialRevision(); $revision->setGuid($this->utils->GUID()); diff --git a/lib/Service/CredentialRevisionService.php b/lib/Service/CredentialRevisionService.php index b15eed76..57004417 100644 --- a/lib/Service/CredentialRevisionService.php +++ b/lib/Service/CredentialRevisionService.php @@ -11,6 +11,7 @@ namespace OCA\Passman\Service; +use OCA\Passman\Db\CredentialRevision; use OCP\IConfig; use OCP\AppFramework\Db\DoesNotExistException; @@ -33,7 +34,15 @@ class CredentialRevisionService { return $this->credentialRevisionMapper->getRevisions($credential_id, $user_id); } + public function getRevision($credential_id, $user_id = null){ + return $this->credentialRevisionMapper->getRevision($credential_id, $user_id); + } + public function deleteRevision($revision_id, $user_id){ return $this->credentialRevisionMapper->deleteRevision($revision_id, $user_id); } + + public function updateRevision(CredentialRevision $credentialRevision){ + return $this->credentialRevisionMapper->update($credentialRevision); + } } \ No newline at end of file