mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-02 14:36:17 +00:00
init
This commit is contained in:
58
app/Actions/Database/CreateDatabase.php
Executable file
58
app/Actions/Database/CreateDatabase.php
Executable file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Database;
|
||||
|
||||
use App\Models\Database;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateDatabase
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Server $server, array $input): Database
|
||||
{
|
||||
$this->validate($server, $input);
|
||||
|
||||
$database = new Database([
|
||||
'server_id' => $server->id,
|
||||
'name' => $input['name'],
|
||||
]);
|
||||
$database->save();
|
||||
$database->createOnServer();
|
||||
|
||||
return $database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(Server $server, array $input): void
|
||||
{
|
||||
$rules = [
|
||||
'name' => [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
Rule::unique('databases', 'name')->where('server_id', $server->id),
|
||||
],
|
||||
];
|
||||
if (isset($input['user']) && $input['user']) {
|
||||
$rules['username'] = [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
Rule::unique('database_users', 'username')->where('server_id', $server->id),
|
||||
];
|
||||
$rules['password'] = [
|
||||
'required',
|
||||
'min:6',
|
||||
];
|
||||
}
|
||||
if (isset($input['remote']) && $input['remote']) {
|
||||
$rules['host'] = 'required';
|
||||
}
|
||||
Validator::make($input, $rules)->validate();
|
||||
}
|
||||
}
|
53
app/Actions/Database/CreateDatabaseUser.php
Executable file
53
app/Actions/Database/CreateDatabaseUser.php
Executable file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Database;
|
||||
|
||||
use App\Models\DatabaseUser;
|
||||
use App\Models\Server;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class CreateDatabaseUser
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Server $server, array $input): DatabaseUser
|
||||
{
|
||||
$this->validate($server, $input);
|
||||
|
||||
$databaseUser = new DatabaseUser([
|
||||
'server_id' => $server->id,
|
||||
'username' => $input['username'],
|
||||
'password' => $input['password'],
|
||||
'host' => isset($input['remote']) && $input['remote'] ? $input['host'] : 'localhost',
|
||||
]);
|
||||
$databaseUser->save();
|
||||
$databaseUser->createOnServer();
|
||||
|
||||
return $databaseUser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(Server $server, array $input): void
|
||||
{
|
||||
$rules = [
|
||||
'username' => [
|
||||
'required',
|
||||
'alpha_dash',
|
||||
Rule::unique('database_users', 'username')->where('server_id', $server->id),
|
||||
],
|
||||
'password' => [
|
||||
'required',
|
||||
'min:6',
|
||||
],
|
||||
];
|
||||
if (isset($input['remote']) && $input['remote']) {
|
||||
$rules['host'] = 'required';
|
||||
}
|
||||
Validator::make($input, $rules)->validate();
|
||||
}
|
||||
}
|
55
app/Actions/Database/InstallPHPMyAdmin.php
Normal file
55
app/Actions/Database/InstallPHPMyAdmin.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Database;
|
||||
|
||||
use App\Models\Server;
|
||||
use App\Models\Service;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class InstallPHPMyAdmin
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function install(Server $server, array $input): Service
|
||||
{
|
||||
$this->validate($input);
|
||||
|
||||
$phpMyAdmin = $server->defaultService('phpmyadmin');
|
||||
if ($phpMyAdmin) {
|
||||
if ($phpMyAdmin->status === 'ready') {
|
||||
throw ValidationException::withMessages([
|
||||
'install' => __('Already installed'),
|
||||
])->errorBag('installPHPMyAdmin');
|
||||
}
|
||||
$phpMyAdmin->delete();
|
||||
}
|
||||
$phpMyAdmin = new Service([
|
||||
'server_id' => $server->id,
|
||||
'type' => 'phpmyadmin',
|
||||
'type_data' => [
|
||||
'allowed_ip' => $input['allowed_ip'],
|
||||
'php' => $server->defaultService('php')->version,
|
||||
],
|
||||
'name' => 'phpmyadmin',
|
||||
'version' => '5.1.2',
|
||||
'status' => 'installing',
|
||||
'is_default' => 1,
|
||||
]);
|
||||
$phpMyAdmin->save();
|
||||
$phpMyAdmin->install();
|
||||
|
||||
return $phpMyAdmin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
private function validate(array $input): void
|
||||
{
|
||||
Validator::make($input, [
|
||||
'allowed_ip' => 'required',
|
||||
])->validateWithBag('installPHPMyAdmin');
|
||||
}
|
||||
}
|
30
app/Actions/Database/LinkUser.php
Executable file
30
app/Actions/Database/LinkUser.php
Executable file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Actions\Database;
|
||||
|
||||
use App\Models\Database;
|
||||
use App\Models\DatabaseUser;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class LinkUser
|
||||
{
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function link(DatabaseUser $databaseUser, array $databases): void
|
||||
{
|
||||
$dbs = Database::query()
|
||||
->where('server_id', $databaseUser->server_id)
|
||||
->whereIn('name', $databases)
|
||||
->count();
|
||||
if (count($databases) !== $dbs) {
|
||||
throw ValidationException::withMessages(['databases' => __('Databases not found!')])
|
||||
->errorBag('linkUser');
|
||||
}
|
||||
|
||||
$databaseUser->databases = $databases;
|
||||
$databaseUser->unlinkUser();
|
||||
$databaseUser->linkUser();
|
||||
$databaseUser->save();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user