123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace App\Console\Commands;
- use App\Repositories\Enums\PermissionEnum;
- use App\Repositories\Models\Base\User;
- use Spatie\Permission\Models\Role;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Spatie\Permission\Contracts\Permission as PermissionContract;
- use Spatie\Permission\Contracts\Role as RoleContract;
- use Spatie\Permission\Models\Permission;
- use Spatie\Permission\PermissionRegistrar;
- /**
- * Class PresenterCommand
- * @package Prettus\Repository\Generators\Commands
- * @author Anderson Andrade <contato@andersonandra.de>
- */
- class InitPermissionCommand extends Command
- {
- /**
- * The name of command.
- *
- * @var string
- */
- protected $name = 'init:permission';
- /**
- * The description of command.
- *
- * @var string
- */
- protected $description = 'Init a new permission.';
- /**
- * The type of class being generated.
- *
- * @var string
- */
- protected $type = 'permission';
- /**
- * Execute the command.
- *
- * @return void
- * @see fire()
- */
- public function handle()
- {
- // app(PermissionRegistrar::class)->forgetCachedPermissions();
- // $permissionClass = app(PermissionContract::class);
- //// $roleClass = app(RoleContract::class);
- //
- // DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束
- //
- //// DB::table($roleClass->getTable())->truncate();
- // DB::table($permissionClass->getTable())->truncate();
- //
- // DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束
- //
- // $permissions = PermissionEnum::makePermissions();
- // foreach ($permissions as $item) {
- // $permissionClass::create($item);
- // }
- $this->initOrganization();
- $this->initUserRole();
- $this->line('ok');
- }
- public function initOrganization()
- {
- $role = Role::query()->where('name', '组织管理员')->first();
- $permissions = Permission::query()->whereIn('name', [
- //分类
- PermissionEnum::ROUTE_CATEGORY()->name,
- //站点
- PermissionEnum::ROUTE_WEBSITE()->name,
- ])->get();
- $role->givePermissionTo($permissions);
- }
- public function initUserRole()
- {
- $user = User::query()->where('id', 1)->first();
- $role = Role::query()->where('name', '组织管理员')->first();
- $user->assignRole($role);
- }
- }
|