From 5793310823a7e7cb7fceae665b8c8c9dd7c6d78a Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 15:39:40 -0400 Subject: [PATCH 1/9] Adding functions to create user and update user password --- src/PHPCouchDB/Server.php | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 0b8a67c..291b8e9 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -165,6 +165,48 @@ public function useDb($options) : Database ); } + public function createUser($username, $password, $roles = []) + { + $doc = [ + 'name' => $username, + 'password' => $password, + 'roles' => $roles, + 'type' => 'user', + ]; + $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + { + return $response_data['rev']; + } + return false; + } + + public function updateUser($username, $password, $rev = false) + { + $doc = [ + 'password' => $password, + ]; + if(!$rev) + { + $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + { + $rev = $response_data['rev']; + } + } + $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, [ + 'json' => $doc, + 'headers' => [ + 'If-Match' => $rev + ], + ]); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + { + return $response_data['rev']; + } + return false; + } + /** * If you need to make a request that isn't supported by this library, * use this method to get the client to use. Aimed at more advanced From 389e82082d01315712a40a4fcc2de6a7727b94da Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 17:38:43 -0400 Subject: [PATCH 2/9] forgot to implement try...catch --- src/PHPCouchDB/Server.php | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 291b8e9..491140f 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -173,11 +173,15 @@ public function createUser($username, $password, $roles = []) 'roles' => $roles, 'type' => 'user', ]; - $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); - if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) - { - return $response_data['rev']; - } + try { + $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + { + return $response_data['rev']; + } + } catch (\GuzzleHttp\Exception\ClientException $e) { + return false; + } return false; } @@ -194,17 +198,21 @@ public function updateUser($username, $password, $rev = false) $rev = $response_data['rev']; } } - $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, [ - 'json' => $doc, - 'headers' => [ - 'If-Match' => $rev - ], - ]); - if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) - { - return $response_data['rev']; + try{ + $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, [ + 'json' => $doc, + 'headers' => [ + 'If-Match' => $rev + ], + ]); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + { + return $response_data['rev']; + } + } catch (\GuzzleHttp\Exception\ClientException $e) { + return false; } - return false; + return false; } /** From 26a29dfeb61a78a2e7474782814ca056bff679f1 Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 18:25:28 -0400 Subject: [PATCH 3/9] documentation and final fixes --- src/PHPCouchDB/Server.php | 79 +++++++++++++++++++++++++++++++++------ 1 file changed, 67 insertions(+), 12 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 491140f..393159c 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -164,7 +164,16 @@ public function useDb($options) : Database 'Database doesn\'t exist, include "create_if_not_exists" parameter to create it' ); } - + /** + * Create a database user and return the revision of the user record (for later updating) + * + * @author David Baltusavich + * @param $username the new username + * @param $password the password to setup for the user + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @return string revision string + * @throws \PHPCouchDB\Exception\ServerException if there's a problem + */ public function createUser($username, $password, $roles = []) { $doc = [ @@ -179,26 +188,61 @@ public function createUser($username, $password, $roles = []) { return $response_data['rev']; } - } catch (\GuzzleHttp\Exception\ClientException $e) { + } + catch (\GuzzleHttp\Exception\ClientException $e) { return false; } - return false; + throw new Exception\ServerException( + 'Problem creating user' + ); + } - public function updateUser($username, $password, $rev = false) + /** + * Update a database user and return the revision of the user record (for further updating) + * + * @author David Baltusavich + * @param $username the new username + * @param $password the new password + * @param optional $rev the revision of the current record (saves a query if you can specify this) + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @return string revision string + * @throws \PHPCouchDB\Exception\ServerException if there's a problem + */ + public function updateUser($username, $password, $rev = false, $roles = []) { $doc = [ 'password' => $password, + 'type' => 'user', + 'name' => $username, + 'roles' => $roles, ]; if(!$rev) { - $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); - if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) + try { - $rev = $response_data['rev']; + $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); + if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) + { + $rev = $response_data['_rev']; + } + else + { + if($response->getStatusCode() == 404) + { + throw new Exception\ServerException("Your connection doesn't have privileges to confirm the user, or the user does not exist"); + } + throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); + } + } + catch (\GuzzleHttp\Exception\ClientException $e){ + throw new Exception\ServerException( + 'Could not retrieve the username provided' + ); } } - try{ + try + { $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, [ 'json' => $doc, 'headers' => [ @@ -207,12 +251,23 @@ public function updateUser($username, $password, $rev = false) ]); if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { - return $response_data['rev']; + return $response_data['rev']; } - } catch (\GuzzleHttp\Exception\ClientException $e) { - return false; + else + { + if($response->getStatusCode() == 409) + { + throw new Exception\ServerException("Your connection doesn't have privileges to update the user password"); + } + throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); + } + } + catch (\GuzzleHttp\Exception\ClientException $e) + { + throw new Exception\ServerException( + "Failing Updating User: " . $e->getMessage() . "REV: $rev" + ); } - return false; } /** From 282cc4441fab29f204aa59f8e98016fdc22fc940 Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 18:37:07 -0400 Subject: [PATCH 4/9] A few things got missed --- src/PHPCouchDB/Server.php | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 393159c..e36a9ae 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -186,16 +186,21 @@ public function createUser($username, $password, $roles = []) $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { - return $response_data['rev']; + return $response_data['_rev']; + } + else + { + throw new Exception\ServerException( + 'Problem creating user' + ); } } - catch (\GuzzleHttp\Exception\ClientException $e) { - return false; + catch (\GuzzleHttp\Exception\ClientException $e){ + throw new Exception\ServerException( + 'Connection or other Error:' . $e->getMessage() + ); } - throw new Exception\ServerException( - 'Problem creating user' - ); - + } /** @@ -222,7 +227,8 @@ public function updateUser($username, $password, $rev = false, $roles = []) try { $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); - if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) + if ($response->getStatusCode() == 200 + && $response_data = json_decode($response->getBody(), true)) { $rev = $response_data['_rev']; } @@ -230,7 +236,9 @@ public function updateUser($username, $password, $rev = false, $roles = []) { if($response->getStatusCode() == 404) { - throw new Exception\ServerException("Your connection doesn't have privileges to confirm the user, or the user does not exist"); + throw new Exception\ServerException( + "Your connection doesn't have privileges to confirm the user, or the user does not exist" + ); } throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); } @@ -251,7 +259,7 @@ public function updateUser($username, $password, $rev = false, $roles = []) ]); if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { - return $response_data['rev']; + return $response_data['_rev']; } else { From 15505b2ad509897eedba08b4fb4b57d68731fb58 Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 18:38:39 -0400 Subject: [PATCH 5/9] don't like breaking up the if statement --- src/PHPCouchDB/Server.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index e36a9ae..fd6e85c 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -227,8 +227,7 @@ public function updateUser($username, $password, $rev = false, $roles = []) try { $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); - if ($response->getStatusCode() == 200 - && $response_data = json_decode($response->getBody(), true)) + if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) { $rev = $response_data['_rev']; } From a060b4071167afdbc9edc732e4b38cda03aa7e8a Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 18:40:15 -0400 Subject: [PATCH 6/9] shorten message --- src/PHPCouchDB/Server.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index fd6e85c..6e65a1b 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -264,7 +264,7 @@ public function updateUser($username, $password, $rev = false, $roles = []) { if($response->getStatusCode() == 409) { - throw new Exception\ServerException("Your connection doesn't have privileges to update the user password"); + throw new Exception\ServerException("Bad permissions"); } throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); } From 06b56bdcef403e305cacb6539e87c6b5ec9be970 Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sat, 31 Mar 2018 18:51:15 -0400 Subject: [PATCH 7/9] Style changes (phpcbf) --- src/PHPCouchDB/Server.php | 65 +++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 6e65a1b..7ca81fb 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -28,9 +28,9 @@ class Server * * See also: \PHPCouchDB\Server:createFromURL() * - * @param array $options Supply either a string "url" parameter OR a - * \GuzzleHttp\ClientInterface "client" parameter if more configuration - * is required + * @param array $options Supply either a string "url" parameter OR a + * \GuzzleHttp\ClientInterface "client" parameter if more configuration + * is required * @throws \PHPCouchDB\Exception\ServerException if there's a problem * with parsing arguments or connecting to the database */ @@ -43,13 +43,16 @@ public function __construct(array $options) } if (isset($options[self::OPTION_CLIENT]) - && $options[self::OPTION_CLIENT] instanceof \GuzzleHttp\ClientInterface) { + && $options[self::OPTION_CLIENT] instanceof \GuzzleHttp\ClientInterface + ) { $client = $options[self::OPTION_CLIENT]; } elseif (isset($options[self::OPTION_URL])) { // set a descriptive user agent $user_agent = \GuzzleHttp\default_user_agent(); - $client = new \GuzzleHttp\Client(["base_uri" => $options[self::OPTION_URL], - "headers" => ["User-Agent" => "PHPCouchDB/" . VERSION . " " . $user_agent]]); + $client = new \GuzzleHttp\Client( + ["base_uri" => $options[self::OPTION_URL], + "headers" => ["User-Agent" => "PHPCouchDB/" . VERSION . " " . $user_agent]] + ); } else { throw new Exception\ServerException( 'Failed to parse $options, array should contain either a url or a client' @@ -113,7 +116,7 @@ public function getAllDbs() : array /** * Create and return a Database object to work with * - * @param $options Supply the "name" (required) and an optional boolean + * @param $options Supply the "name" (required) and an optional boolean * "create_if_not_exists" value (default is false) * @return \CouchDB\Database represents the named database * @throws \PHPCouchDB\Exception\ServerException if there's a problem @@ -168,9 +171,9 @@ public function useDb($options) : Database * Create a database user and return the revision of the user record (for later updating) * * @author David Baltusavich - * @param $username the new username - * @param $password the password to setup for the user - * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @param $username the new username + * @param $password the password to setup for the user + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] * @return string revision string * @throws \PHPCouchDB\Exception\ServerException if there's a problem */ @@ -184,8 +187,7 @@ public function createUser($username, $password, $roles = []) ]; try { $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); - if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) - { + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { return $response_data['_rev']; } else @@ -207,10 +209,10 @@ public function createUser($username, $password, $roles = []) * Update a database user and return the revision of the user record (for further updating) * * @author David Baltusavich - * @param $username the new username - * @param $password the new password - * @param optional $rev the revision of the current record (saves a query if you can specify this) - * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @param $username the new username + * @param $password the new password + * @param optional $rev the revision of the current record (saves a query if you can specify this) + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] * @return string revision string * @throws \PHPCouchDB\Exception\ServerException if there's a problem */ @@ -222,19 +224,16 @@ public function updateUser($username, $password, $rev = false, $roles = []) 'name' => $username, 'roles' => $roles, ]; - if(!$rev) - { + if(!$rev) { try { - $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); - if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) - { + $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); + if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) { $rev = $response_data['_rev']; } else { - if($response->getStatusCode() == 404) - { + if($response->getStatusCode() == 404) { throw new Exception\ServerException( "Your connection doesn't have privileges to confirm the user, or the user does not exist" ); @@ -243,27 +242,27 @@ public function updateUser($username, $password, $rev = false, $roles = []) } } catch (\GuzzleHttp\Exception\ClientException $e){ - throw new Exception\ServerException( + throw new Exception\ServerException( 'Could not retrieve the username provided' ); } } try { - $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, [ + $response = $this->client->request( + "PUT", "/_users/org.couchdb.user:" . $username, [ 'json' => $doc, 'headers' => [ 'If-Match' => $rev ], - ]); - if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) - { - return $response_data['_rev']; + ] + ); + if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { + return $response_data['_rev']; } else { - if($response->getStatusCode() == 409) - { + if($response->getStatusCode() == 409) { throw new Exception\ServerException("Bad permissions"); } throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); @@ -272,8 +271,8 @@ public function updateUser($username, $password, $rev = false, $roles = []) catch (\GuzzleHttp\Exception\ClientException $e) { throw new Exception\ServerException( - "Failing Updating User: " . $e->getMessage() . "REV: $rev" - ); + "Failing Updating User: " . $e->getMessage() . "REV: $rev" + ); } } From 7e58523c466003aa6d9fd9500ce51439aa44404e Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sun, 22 Apr 2018 14:44:26 +0000 Subject: [PATCH 8/9] Re-ran PHPCBF --- src/PHPCouchDB/Server.php | 45 +++++++++++++-------------------------- 1 file changed, 15 insertions(+), 30 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 7ca81fb..678367a 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -170,7 +170,6 @@ public function useDb($options) : Database /** * Create a database user and return the revision of the user record (for later updating) * - * @author David Baltusavich * @param $username the new username * @param $password the password to setup for the user * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] @@ -189,26 +188,19 @@ public function createUser($username, $password, $roles = []) $response = $this->client->request("PUT", "/_users/org.couchdb.user:" . $username, ['json' => $doc]); if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { return $response_data['_rev']; - } - else - { + } else { throw new Exception\ServerException( 'Problem creating user' ); } - } - catch (\GuzzleHttp\Exception\ClientException $e){ - throw new Exception\ServerException( - 'Connection or other Error:' . $e->getMessage() - ); + } catch (\GuzzleHttp\Exception\ClientException $e) { + return false; } - } /** * Update a database user and return the revision of the user record (for further updating) * - * @author David Baltusavich * @param $username the new username * @param $password the new password * @param optional $rev the revision of the current record (saves a query if you can specify this) @@ -224,33 +216,30 @@ public function updateUser($username, $password, $rev = false, $roles = []) 'name' => $username, 'roles' => $roles, ]; - if(!$rev) { - try - { + if (!$rev) { + try { $response = $this->client->request("GET", "/_users/org.couchdb.user:" . $username); if ($response->getStatusCode() == 200 && $response_data = json_decode($response->getBody(), true)) { $rev = $response_data['_rev']; - } - else - { - if($response->getStatusCode() == 404) { + } else { + if ($response->getStatusCode() == 404) { throw new Exception\ServerException( "Your connection doesn't have privileges to confirm the user, or the user does not exist" ); } throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); } - } - catch (\GuzzleHttp\Exception\ClientException $e){ + } catch (\GuzzleHttp\Exception\ClientException $e) { throw new Exception\ServerException( 'Could not retrieve the username provided' ); } } - try - { + try { $response = $this->client->request( - "PUT", "/_users/org.couchdb.user:" . $username, [ + "PUT", + "/_users/org.couchdb.user:" . $username, + [ 'json' => $doc, 'headers' => [ 'If-Match' => $rev @@ -259,17 +248,13 @@ public function updateUser($username, $password, $rev = false, $roles = []) ); if ($response->getStatusCode() == 201 && $response_data = json_decode($response->getBody(), true)) { return $response_data['_rev']; - } - else - { - if($response->getStatusCode() == 409) { + } else { + if ($response->getStatusCode() == 409) { throw new Exception\ServerException("Bad permissions"); } throw new Exception\ServerException("Something went wrong: " . $response->getStatusCode()); } - } - catch (\GuzzleHttp\Exception\ClientException $e) - { + } catch (\GuzzleHttp\Exception\ClientException $e) { throw new Exception\ServerException( "Failing Updating User: " . $e->getMessage() . "REV: $rev" ); From 3fa5fc0d8f3b9d0557090fb9c9c8548dcbc63fcc Mon Sep 17 00:00:00 2001 From: David Baltusavich Date: Sun, 22 Apr 2018 14:52:51 +0000 Subject: [PATCH 9/9] phpcbf --- src/PHPCouchDB/Server.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/PHPCouchDB/Server.php b/src/PHPCouchDB/Server.php index 678367a..756877b 100644 --- a/src/PHPCouchDB/Server.php +++ b/src/PHPCouchDB/Server.php @@ -172,7 +172,7 @@ public function useDb($options) : Database * * @param $username the new username * @param $password the password to setup for the user - * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] * @return string revision string * @throws \PHPCouchDB\Exception\ServerException if there's a problem */ @@ -203,8 +203,8 @@ public function createUser($username, $password, $roles = []) * * @param $username the new username * @param $password the new password - * @param optional $rev the revision of the current record (saves a query if you can specify this) - * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] + * @param optional $rev the revision of the current record (saves a query if you can specify this) + * @param optional $roles if you want to specify the roles on the server for the user. defaults to [] * @return string revision string * @throws \PHPCouchDB\Exception\ServerException if there's a problem */