InitPermissionCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\Enums\PermissionEnum;
  4. use App\Repositories\Models\Base\User;
  5. use Spatie\Permission\Models\Role;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Spatie\Permission\Contracts\Permission as PermissionContract;
  9. use Spatie\Permission\Contracts\Role as RoleContract;
  10. use Spatie\Permission\Models\Permission;
  11. use Spatie\Permission\PermissionRegistrar;
  12. /**
  13. * Class PresenterCommand
  14. * @package Prettus\Repository\Generators\Commands
  15. * @author Anderson Andrade <contato@andersonandra.de>
  16. */
  17. class InitPermissionCommand extends Command
  18. {
  19. /**
  20. * The name of command.
  21. *
  22. * @var string
  23. */
  24. protected $name = 'init:permission';
  25. /**
  26. * The description of command.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'Init a new permission.';
  31. /**
  32. * The type of class being generated.
  33. *
  34. * @var string
  35. */
  36. protected $type = 'permission';
  37. /**
  38. * Execute the command.
  39. *
  40. * @return void
  41. * @see fire()
  42. */
  43. public function handle()
  44. {
  45. // app(PermissionRegistrar::class)->forgetCachedPermissions();
  46. // $permissionClass = app(PermissionContract::class);
  47. //// $roleClass = app(RoleContract::class);
  48. //
  49. // DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束
  50. //
  51. //// DB::table($roleClass->getTable())->truncate();
  52. // DB::table($permissionClass->getTable())->truncate();
  53. //
  54. // DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束
  55. //
  56. // $permissions = PermissionEnum::makePermissions();
  57. // foreach ($permissions as $item) {
  58. // $permissionClass::create($item);
  59. // }
  60. $this->initOrganization();
  61. $this->initUserRole();
  62. $this->line('ok');
  63. }
  64. public function initOrganization()
  65. {
  66. $role = Role::query()->where('name', '组织管理员')->first();
  67. $permissions = Permission::query()->whereIn('name', [
  68. //分类
  69. PermissionEnum::ROUTE_CATEGORY()->name,
  70. //站点
  71. PermissionEnum::ROUTE_WEBSITE()->name,
  72. ])->get();
  73. $role->givePermissionTo($permissions);
  74. }
  75. public function initUserRole()
  76. {
  77. $user = User::query()->where('id', 1)->first();
  78. $role = Role::query()->where('name', '组织管理员')->first();
  79. $user->assignRole($role);
  80. }
  81. }