Add database password change support and fix column name
This commit is contained in:
parent
0d61c50dcc
commit
5233d6e87b
11 changed files with 188 additions and 9 deletions
|
@ -60,7 +60,7 @@ class DatabaseRepository {
|
|||
try {
|
||||
$db = new Models\Database;
|
||||
$db->fill([
|
||||
'server' => $server->id,
|
||||
'server_id' => $server->id,
|
||||
'db_server' => $options['db_server'],
|
||||
'database' => $server->uuidShort . '_' . $options['database'],
|
||||
'username' => $server->uuidShort . '_' . str_random(7),
|
||||
|
@ -103,6 +103,54 @@ class DatabaseRepository {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the password for a given database.
|
||||
* @param int $database The ID of the database to modify.
|
||||
* @param string $password The new password to use for the database.
|
||||
* @return bool
|
||||
*/
|
||||
public function modifyPassword($database, $password)
|
||||
{
|
||||
$db = Models\Database::findOrFail($database);
|
||||
$dbr = Models\DatabaseServer::findOrFail($db->db_server);
|
||||
|
||||
DB::beginTransaction();
|
||||
try {
|
||||
|
||||
$db->password = Crypt::encrypt($password);
|
||||
$db->save();
|
||||
|
||||
$capsule = new Capsule;
|
||||
$capsule->addConnection([
|
||||
'driver' => 'mysql',
|
||||
'host' => $dbr->host,
|
||||
'port' => $dbr->port,
|
||||
'database' => 'mysql',
|
||||
'username' => $dbr->username,
|
||||
'password' => Crypt::decrypt($dbr->password),
|
||||
'charset' => 'utf8',
|
||||
'collation' => 'utf8_unicode_ci',
|
||||
'prefix' => '',
|
||||
'options' => [
|
||||
\PDO::ATTR_TIMEOUT => 3,
|
||||
]
|
||||
]);
|
||||
|
||||
$capsule->setAsGlobal();
|
||||
Capsule::statement(sprintf(
|
||||
'ALTER USER \'%s\'@\'%s\' IDENTIFIED BY \'%s\'',
|
||||
$db->username,
|
||||
$db->remote,
|
||||
$password
|
||||
));
|
||||
|
||||
DB::commit();
|
||||
} catch(\Exception $ex) {
|
||||
DB::rollback();
|
||||
throw $ex;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drops a database from the associated MySQL Server
|
||||
* @param int $database The ID of the database to drop.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue