From 2948b7148068553593d61e9ab3dd465447042d92 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 13:38:08 +0100 Subject: [PATCH 1/7] Add credentials to parameters - Adding the credentials via dsn causes problems when the password contains an @ symbol --- src/Jenssegers/Mongodb/Connection.php | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 634e8faab..583ec72f4 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -136,6 +136,9 @@ protected function createConnection($dsn, array $config, array $options) $driverOptions = $config['driver_options']; } + $options['username'] = $config['username']; + $options['password'] = $config['password']; + return new Client($dsn, $options, $driverOptions); } @@ -175,20 +178,7 @@ protected function getDsn(array $config) } } - // The database name needs to be in the connection string, otherwise it will - // authenticate to the admin database, which may result in permission errors. - $auth = ''; - if (! empty($username)) { - $auth .= $username; - } - if (! empty($password)) { - $auth .= ':' . urlencode($password); - } - if ($auth) { - $auth .= '@'; - } - - return "mongodb://" . $auth . implode(',', $hosts) . "/{$database}"; + return "mongodb://" . implode(',', $hosts) . "/{$database}"; } /** From f3c646e947a03ec798d32cdc770132303e114fb6 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 13:41:01 +0100 Subject: [PATCH 2/7] Add comment and check if parameters are set --- src/Jenssegers/Mongodb/Connection.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 583ec72f4..14ee4179b 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -135,9 +135,10 @@ protected function createConnection($dsn, array $config, array $options) if (isset($config['driver_options']) && is_array($config['driver_options'])) { $driverOptions = $config['driver_options']; } - - $options['username'] = $config['username']; - $options['password'] = $config['password']; + + // Get the credentials from the config and check if they are set + $options['username'] = isset($options['username']) ? $config['username'] : ''; + $options['password'] = isset($options['password']) ? $config['password'] : ''; return new Client($dsn, $options, $driverOptions); } From eaf2b8f1fdd297f2cd612c437cc2d18724746306 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 14:01:35 +0100 Subject: [PATCH 3/7] Add credentials to options array in config --- README.md | 6 +++--- src/Jenssegers/Mongodb/Connection.php | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d98eae651..ceb65624b 100644 --- a/README.md +++ b/README.md @@ -117,10 +117,10 @@ And add a new mongodb connection: 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE', ''), - 'username' => env('DB_USERNAME', ''), - 'password' => env('DB_PASSWORD', ''), 'options' => [ - 'db' => 'admin' // sets the authentication database required by mongo 3 + 'db' => 'admin', // sets the authentication database required by mongo 3 + 'username' => env('DB_USERNAME', ''), + 'password' => env('DB_PASSWORD', ''), ] ], ``` diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 14ee4179b..2be4fb0a0 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -135,10 +135,6 @@ protected function createConnection($dsn, array $config, array $options) if (isset($config['driver_options']) && is_array($config['driver_options'])) { $driverOptions = $config['driver_options']; } - - // Get the credentials from the config and check if they are set - $options['username'] = isset($options['username']) ? $config['username'] : ''; - $options['password'] = isset($options['password']) ? $config['password'] : ''; return new Client($dsn, $options, $driverOptions); } From 38c91036437b06369048d045d567e6c6859861f3 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 14:13:39 +0100 Subject: [PATCH 4/7] Update readme to conform to new options structure --- README.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ceb65624b..cb2bfe8bd 100644 --- a/README.md +++ b/README.md @@ -133,9 +133,12 @@ You can connect to multiple servers or replica sets with the following configura 'host' => ['server1', 'server2'], 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE', ''), - 'username' => env('DB_USERNAME', ''), - 'password' => env('DB_PASSWORD', ''), - 'options' => ['replicaSet' => 'replicaSetName'] + 'options' => [ + 'replicaSet' => 'replicaSetName', + 'db' => 'admin', // sets the authentication database required by mongo 3 + 'username' => env('DB_USERNAME', ''), + 'password' => env('DB_PASSWORD', ''), + ] ], ``` From 154ab12a70fdac5c2ae59ebc5d15ba273cbd8539 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 14:45:52 +0100 Subject: [PATCH 5/7] Add credentials to MongoDB constructor - Reverted the config changes - The credentials are now added to the MongoDB constructor if they are not already present - Also fixed failing test by changing method access level --- README.md | 15 ++++++--------- src/Jenssegers/Mongodb/Connection.php | 7 +++++++ src/Jenssegers/Mongodb/Schema/Blueprint.php | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index cb2bfe8bd..d98eae651 100644 --- a/README.md +++ b/README.md @@ -117,10 +117,10 @@ And add a new mongodb connection: 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE', ''), + 'username' => env('DB_USERNAME', ''), + 'password' => env('DB_PASSWORD', ''), 'options' => [ - 'db' => 'admin', // sets the authentication database required by mongo 3 - 'username' => env('DB_USERNAME', ''), - 'password' => env('DB_PASSWORD', ''), + 'db' => 'admin' // sets the authentication database required by mongo 3 ] ], ``` @@ -133,12 +133,9 @@ You can connect to multiple servers or replica sets with the following configura 'host' => ['server1', 'server2'], 'port' => env('DB_PORT', 27017), 'database' => env('DB_DATABASE', ''), - 'options' => [ - 'replicaSet' => 'replicaSetName', - 'db' => 'admin', // sets the authentication database required by mongo 3 - 'username' => env('DB_USERNAME', ''), - 'password' => env('DB_PASSWORD', ''), - ] + 'username' => env('DB_USERNAME', ''), + 'password' => env('DB_PASSWORD', ''), + 'options' => ['replicaSet' => 'replicaSetName'] ], ``` diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 2be4fb0a0..5ac8d6fa0 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -136,6 +136,13 @@ protected function createConnection($dsn, array $config, array $options) $driverOptions = $config['driver_options']; } + if (!isset($options['username'])){ + $options['username'] = isset($config['username']) ? $config['username'] : ''; + } + if (!isset($options['password'])){ + $options['password'] = isset($config['password']) ? $config['password'] : ''; + } + return new Client($dsn, $options, $driverOptions); } diff --git a/src/Jenssegers/Mongodb/Schema/Blueprint.php b/src/Jenssegers/Mongodb/Schema/Blueprint.php index 3b4053ca9..144675b39 100644 --- a/src/Jenssegers/Mongodb/Schema/Blueprint.php +++ b/src/Jenssegers/Mongodb/Schema/Blueprint.php @@ -209,7 +209,7 @@ public function drop() * @param array $parameters * @return Blueprint */ - protected function addColumn($type, $name, array $parameters = []) + public function addColumn($type, $name, array $parameters = []) { $this->fluent($name); From a1ed46f28a0053a927564b0598b62738e57c5697 Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 14:50:11 +0100 Subject: [PATCH 6/7] Fix coding style according to StyleCI --- src/Jenssegers/Mongodb/Connection.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 5ac8d6fa0..215152323 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -136,10 +136,11 @@ protected function createConnection($dsn, array $config, array $options) $driverOptions = $config['driver_options']; } - if (!isset($options['username'])){ + // Check if the credentials are not already set in the options + if (!isset($options['username'])) { $options['username'] = isset($config['username']) ? $config['username'] : ''; } - if (!isset($options['password'])){ + if (!isset($options['password'])) { $options['password'] = isset($config['password']) ? $config['password'] : ''; } From e24519a8c984694aaf820bcc9ba80d5fc828252f Mon Sep 17 00:00:00 2001 From: Robert-John van Doesburg Date: Thu, 25 Feb 2016 15:49:48 +0100 Subject: [PATCH 7/7] Allow anonymous connection - If no user was given an empy user was set resulting in an error --- src/Jenssegers/Mongodb/Connection.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Jenssegers/Mongodb/Connection.php b/src/Jenssegers/Mongodb/Connection.php index 215152323..f7eca2e5a 100644 --- a/src/Jenssegers/Mongodb/Connection.php +++ b/src/Jenssegers/Mongodb/Connection.php @@ -137,11 +137,11 @@ protected function createConnection($dsn, array $config, array $options) } // Check if the credentials are not already set in the options - if (!isset($options['username'])) { - $options['username'] = isset($config['username']) ? $config['username'] : ''; + if (!isset($options['username']) && isset($config['username'])) { + $options['username'] = $config['username']; } - if (!isset($options['password'])) { - $options['password'] = isset($config['password']) ? $config['password'] : ''; + if (!isset($options['password']) && isset($config['password'])) { + $options['password'] = $config['password']; } return new Client($dsn, $options, $driverOptions);