Skip to content

Commit

Permalink
Merge pull request #753 from rjvandoesburg/master
Browse files Browse the repository at this point in the history
Pass credentials as parameters
  • Loading branch information
jenssegers committed Feb 25, 2016
2 parents ee7a5c6 + e24519a commit 9baab58
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/Jenssegers/Mongodb/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ protected function createConnection($dsn, array $config, array $options)
$driverOptions = $config['driver_options'];
}

// Check if the credentials are not already set in the options
if (!isset($options['username']) && isset($config['username'])) {
$options['username'] = $config['username'];
}
if (!isset($options['password']) && isset($config['password'])) {
$options['password'] = $config['password'];
}

return new Client($dsn, $options, $driverOptions);
}

Expand Down Expand Up @@ -175,20 +183,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}";
}

/**
Expand Down

0 comments on commit 9baab58

Please sign in to comment.