Skip to content

Commit 9baab58

Browse files
committed
Merge pull request #753 from rjvandoesburg/master
Pass credentials as parameters
2 parents ee7a5c6 + e24519a commit 9baab58

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

src/Jenssegers/Mongodb/Connection.php

+9-14
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ protected function createConnection($dsn, array $config, array $options)
136136
$driverOptions = $config['driver_options'];
137137
}
138138

139+
// Check if the credentials are not already set in the options
140+
if (!isset($options['username']) && isset($config['username'])) {
141+
$options['username'] = $config['username'];
142+
}
143+
if (!isset($options['password']) && isset($config['password'])) {
144+
$options['password'] = $config['password'];
145+
}
146+
139147
return new Client($dsn, $options, $driverOptions);
140148
}
141149

@@ -175,20 +183,7 @@ protected function getDsn(array $config)
175183
}
176184
}
177185

178-
// The database name needs to be in the connection string, otherwise it will
179-
// authenticate to the admin database, which may result in permission errors.
180-
$auth = '';
181-
if (! empty($username)) {
182-
$auth .= $username;
183-
}
184-
if (! empty($password)) {
185-
$auth .= ':' . urlencode($password);
186-
}
187-
if ($auth) {
188-
$auth .= '@';
189-
}
190-
191-
return "mongodb://" . $auth . implode(',', $hosts) . "/{$database}";
186+
return "mongodb://" . implode(',', $hosts) . "/{$database}";
192187
}
193188

194189
/**

0 commit comments

Comments
 (0)