2024-04-22 11:14:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2024-05-10 16:56:58 +00:00
|
|
|
use App\PhyreConfig;
|
2024-04-22 11:14:05 +00:00
|
|
|
use App\UniversalDatabaseExecutor;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class DatabaseUser extends Model
|
|
|
|
{
|
|
|
|
use HasFactory;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'database_id',
|
|
|
|
'username',
|
|
|
|
'username_prefix',
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
public static function boot()
|
|
|
|
{
|
|
|
|
parent::boot();
|
|
|
|
|
|
|
|
static::creating(function ($model) {
|
|
|
|
|
|
|
|
$findDatabase = Database::where('id', $model->database_id)->first();
|
|
|
|
if (!$findDatabase) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$findHostingSubscription = HostingSubscription::where('id', $findDatabase->hosting_subscription_id)->first();
|
|
|
|
if (!$findHostingSubscription) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->username_prefix = $findHostingSubscription->system_username . '_';
|
|
|
|
$databaseUsername = $model->username_prefix . $model->username;
|
|
|
|
|
|
|
|
if ($findDatabase->is_remote_database_server) {
|
|
|
|
$findRemoteDatabaseServer = RemoteDatabaseServer::where('id', $findDatabase->remote_database_server_id)->first();
|
|
|
|
if (!$findRemoteDatabaseServer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$databaseManager = new UniversalDatabaseExecutor(
|
|
|
|
$findRemoteDatabaseServer->host,
|
|
|
|
$findRemoteDatabaseServer->port,
|
|
|
|
$findRemoteDatabaseServer->username,
|
|
|
|
$findRemoteDatabaseServer->password,
|
|
|
|
$findDatabase->database_name_prefix . $findDatabase->database_name
|
|
|
|
);
|
|
|
|
|
|
|
|
$createDatabaseUser = $databaseManager->createUser($databaseUsername, $model->password);
|
|
|
|
if (isset($createDatabaseUser['error'])) {
|
|
|
|
throw new \Exception($createDatabaseUser['message']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$universalDatabaseExecutor = new UniversalDatabaseExecutor(
|
2024-05-10 19:38:25 +00:00
|
|
|
PhyreConfig::get('MYSQL_HOST', '127.0.0.1'),
|
|
|
|
PhyreConfig::get('MYSQL_PORT', 3306),
|
2024-05-10 16:56:58 +00:00
|
|
|
PhyreConfig::get('MYSQL_ROOT_USERNAME'),
|
|
|
|
PhyreConfig::get('MYSQL_ROOT_PASSWORD'),
|
2024-04-22 11:14:05 +00:00
|
|
|
$findDatabase->database_name_prefix . $findDatabase->database_name
|
|
|
|
);
|
2024-09-13 09:25:57 +00:00
|
|
|
|
2024-04-22 11:14:05 +00:00
|
|
|
$createDatabase = $universalDatabaseExecutor->createUser($databaseUsername, $model->password);
|
|
|
|
if (isset($createDatabase['error'])) {
|
|
|
|
throw new \Exception($createDatabase['message']);
|
|
|
|
}
|
2024-09-13 09:25:57 +00:00
|
|
|
|
|
|
|
$universalDatabaseExecutor->userGrantPrivilegesToDatabase($databaseUsername, [
|
|
|
|
$findDatabase->database_name_prefix . $findDatabase->database_name
|
|
|
|
]);
|
2024-04-22 11:14:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function database()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Database::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|