itstructure /
laravel-rbac
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Itstructure\LaRbac\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Foundation\Auth\User as ParentUser; |
||
| 6 | use Illuminate\Console\Command; |
||
| 7 | use Itstructure\LaRbac\Models\Role; |
||
| 8 | use Itstructure\LaRbac\Helpers\Helper; |
||
| 9 | use Itstructure\LaRbac\Interfaces\RbacUserInterface; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Class AdminCommand |
||
| 13 | * |
||
| 14 | * @package Itstructure\LaRbac\Commands |
||
| 15 | * |
||
| 16 | * @author Andrey Girnik <[email protected]> |
||
| 17 | */ |
||
| 18 | class AdminCommand extends Command |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The name and signature of the console command. |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $signature = 'rbac:admin'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The console command description. |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | protected $description = 'Assign Admin role for existing special user.'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var RbacUserInterface|ParentUser |
||
| 34 | */ |
||
| 35 | protected $userModelClass; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $adminUserId; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * AdminCommand constructor. |
||
| 44 | */ |
||
| 45 | public function __construct() |
||
| 46 | { |
||
| 47 | parent::__construct(); |
||
| 48 | |||
| 49 | $this->userModelClass = config('rbac.userModelClass'); |
||
| 50 | $this->adminUserId = config('rbac.adminUserId'); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Execute the console command. |
||
| 55 | */ |
||
| 56 | public function handle() |
||
| 57 | { |
||
| 58 | try { |
||
| 59 | $this->info('Start assign admin role for existing special user.'); |
||
| 60 | |||
| 61 | Helper::checkUserModel($this->userModelClass); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 62 | |||
| 63 | Helper::checkAdminUserId($this->adminUserId); |
||
| 64 | |||
| 65 | $adminUser = Helper::retrieveUserModel($this->userModelClass, $this->adminUserId); |
||
| 66 | |||
| 67 | $adminRoleId = Role::where('slug', Role::ADMIN_ROLE)->firstOrFail()->id; |
||
| 68 | |||
| 69 | /** @var RbacUserInterface $adminUser */ |
||
| 70 | $adminUser->roles()->attach([ |
||
| 71 | $adminRoleId |
||
| 72 | ]); |
||
| 73 | |||
| 74 | $this->info('Admin role is assigned successfully.'); |
||
| 75 | |||
| 76 | } catch (\Exception $e) { |
||
| 77 | $this->error('Failed!'); |
||
| 78 | $this->error($e->getMessage()); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } |
||
| 82 |