From f2dcdd1caab80b8cbe2b146e6a7d575674f9a533 Mon Sep 17 00:00:00 2001 From: zorn Date: Wed, 13 May 2020 19:12:33 +1000 Subject: [PATCH] putSecurityDocument method --- lib/Doctrine/CouchDB/CouchDBClient.php | 15 ++++++++ lib/Doctrine/CouchDB/SecurityDocument.php | 44 +++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 lib/Doctrine/CouchDB/SecurityDocument.php diff --git a/lib/Doctrine/CouchDB/CouchDBClient.php b/lib/Doctrine/CouchDB/CouchDBClient.php index 91fa53c..9e2bf67 100644 --- a/lib/Doctrine/CouchDB/CouchDBClient.php +++ b/lib/Doctrine/CouchDB/CouchDBClient.php @@ -576,6 +576,21 @@ public function createDesignDocument($designDocName, DesignDocument $designDoc) ); } + /** + * https://docs.couchdb.org/en/stable/api/database/security.html + */ + public function putSecurityDocument(SecurityDocument $securityDoc) + { + $data = $securityDoc->getData(); + + $path = '/' . $this->databaseName . '/_security'; + $response = $this->httpClient->request('PUT', $path, json_encode($data)); + + if ($response->status != 200 || !$response->body['ok']) { + throw HTTPException::fromResponse($path, $response); + } + } + /** * GET /db/_compact. * diff --git a/lib/Doctrine/CouchDB/SecurityDocument.php b/lib/Doctrine/CouchDB/SecurityDocument.php new file mode 100644 index 0000000..07f4237 --- /dev/null +++ b/lib/Doctrine/CouchDB/SecurityDocument.php @@ -0,0 +1,44 @@ +data; + } + + public function addAdminName($name) + { + $this->addEntry('admins', 'names', $name); + } + + public function addAdminRole($role) + { + $this->addEntry('admins', 'roles', $name); + } + + public function addMemberName($name) + { + $this->addEntry('members', 'names', $name); + } + + public function addMemberRole($role) + { + $this->addEntry('members', 'roles', $name); + } + + private function addEntry($role, $type, $val) + { + if (!isset($this->data[$role][$type])) { + $this->data[$role][$type] = []; + } + $this->data[$role][$type][] = $val; + } +}