Merge pull request #1963 from Sir3lit/maxconn

Add Max Concurrent Connections for database users
This commit is contained in:
Dane Everitt 2020-04-25 11:01:01 -07:00 committed by GitHub
commit 72ecac5236
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 68 additions and 7 deletions

View file

@ -68,9 +68,10 @@ interface DatabaseRepositoryInterface extends RepositoryInterface
* @param string $username
* @param string $remote
* @param string $password
* @param $max_connections
* @return bool
*/
public function createUser(string $username, string $remote, string $password): bool;
public function createUser(string $username, string $remote, string $password, string $max_connections): bool;
/**
* Give a specific user access to a given database.

View file

@ -25,6 +25,7 @@ class StoreServerDatabaseRequest extends AdminFormRequest
$query->where('database_host_id', $this->input('database_host_id') ?? 0);
}),
],
'max_connections' => 'nullable',
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
'database_host_id' => 'required|integer|exists:database_hosts,id',
];

View file

@ -30,7 +30,7 @@ class Database extends Model
* @var array
*/
protected $fillable = [
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote',
'server_id', 'database_host_id', 'database', 'username', 'password', 'remote', 'max_connections',
];
/**
@ -41,6 +41,7 @@ class Database extends Model
protected $casts = [
'server_id' => 'integer',
'database_host_id' => 'integer',
'max_connections' => 'integer',
];
/**
@ -51,6 +52,7 @@ class Database extends Model
'database_host_id' => 'required|exists:database_hosts,id',
'database' => 'required|string|alpha_dash|between:3,100',
'username' => 'string|alpha_dash|between:3,100',
'max_connections' => 'nullable|integer',
'remote' => 'required|string|regex:/^[0-9%.]{1,15}$/',
'password' => 'string',
];

View file

@ -135,11 +135,16 @@ class DatabaseRepository extends EloquentRepository implements DatabaseRepositor
* @param string $username
* @param string $remote
* @param string $password
* @param $max_connections
* @return bool
*/
public function createUser(string $username, string $remote, string $password): bool
public function createUser(string $username, string $remote, string $password, $max_connections): bool
{
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
if (!$max_connections) {
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\'', $username, $remote, $password));
} else {
return $this->run(sprintf('CREATE USER `%s`@`%s` IDENTIFIED BY \'%s\' WITH MAX_USER_CONNECTIONS %s', $username, $remote, $password, $max_connections));
}
}
/**

View file

@ -84,7 +84,8 @@ class DatabaseManagementService
$this->repository->createUser(
$database->username,
$database->remote,
$this->encrypter->decrypt($database->password)
$this->encrypter->decrypt($database->password),
$database->max_connections
);
$this->repository->assignUserToDatabase(
$database->database,

View file

@ -71,7 +71,7 @@ class DatabasePasswordService
]);
$this->repository->dropUser($database->username, $database->remote);
$this->repository->createUser($database->username, $database->remote, $password);
$this->repository->createUser($database->username, $database->remote, $password, $database->max_connections);
$this->repository->assignUserToDatabase($database->database, $database->username, $database->remote);
$this->repository->flush();
});

View file

@ -56,6 +56,7 @@ class ServerDatabaseTransformer extends BaseTransformer
'database' => $model->database,
'username' => $model->username,
'remote' => $model->remote,
'max_connections' => $model->max_connections,
'created_at' => Chronos::createFromFormat(Chronos::DEFAULT_TO_STRING_FORMAT, $model->created_at)
->setTimezone(config('app.timezone'))
->toIso8601String(),

View file

@ -58,6 +58,7 @@ class DatabaseTransformer extends BaseClientTransformer
'name' => $model->database,
'username' => $model->username,
'connections_from' => $model->remote,
'max_connections' => $model->max_connections,
];
}