64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Throwable;
|
|
|
|
class ApplicationReady extends Command
|
|
{
|
|
protected $signature = 'app:ready';
|
|
|
|
protected $description = '检查应用安装、数据库和管理员权限是否已经可用';
|
|
|
|
public function handle(): int
|
|
{
|
|
if (! config('app.installed')) {
|
|
return self::FAILURE;
|
|
}
|
|
|
|
try {
|
|
DB::selectOne('SELECT 1');
|
|
|
|
foreach ([
|
|
'users',
|
|
'companies',
|
|
'roles',
|
|
'permissions',
|
|
'role_permissions',
|
|
'user_roles',
|
|
'user_companies',
|
|
'system_database_connections',
|
|
] as $table) {
|
|
if (! Schema::hasTable($table)) {
|
|
return self::FAILURE;
|
|
}
|
|
}
|
|
|
|
$administratorCount = DB::table('users as u')
|
|
->join('user_roles as ur', 'ur.user_id', '=', 'u.id')
|
|
->join('roles as r', 'r.id', '=', 'ur.role_id')
|
|
->join('user_companies as uc', 'uc.user_id', '=', 'u.id')
|
|
->join('role_permissions as rp', 'rp.role_id', '=', 'r.id')
|
|
->join('permissions as p', 'p.id', '=', 'rp.permission_id')
|
|
->where('u.enabled', 1)
|
|
->whereIn('r.name', ['super_admin', 'admin'])
|
|
->where('p.name', 'read-admin-panel')
|
|
->distinct()
|
|
->count('u.id');
|
|
|
|
if ($administratorCount < 1) {
|
|
return self::FAILURE;
|
|
}
|
|
} catch (Throwable) {
|
|
return self::FAILURE;
|
|
}
|
|
|
|
$this->components->info('应用已就绪。');
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|