pratiksh404 /
adminetic
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Pratiksh\Adminetic\Console\Commands; |
||
| 4 | |||
| 5 | use Illuminate\Console\Command; |
||
| 6 | use Pratiksh\Adminetic\Models\Admin\Role; |
||
| 7 | use Pratiksh\Adminetic\Services\MakePermission; |
||
| 8 | |||
| 9 | class MakePermissionCommand extends Command |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The name and signature of the console command. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $signature = 'make:permission {name : Model Class (Singular), e.g role, Place, Car, Post} {role=0 : Role ID} {--all} {--onlyFlags : Generate flags only without files}'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The console command description. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $description = 'Make Permission Views and Flags for Model'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Create a new command instance. |
||
| 27 | * |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public function __construct() |
||
| 31 | { |
||
| 32 | parent::__construct(); |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Execute the console command. |
||
| 37 | * |
||
| 38 | * @return int |
||
| 39 | */ |
||
| 40 | public function handle() |
||
| 41 | { |
||
| 42 | $name = $this->argument('name'); |
||
| 43 | |||
| 44 | $role_id = $this->argument('role'); |
||
| 45 | |||
| 46 | $role = Role::find($role_id); |
||
| 47 | |||
| 48 | $only_flags = $this->option('onlyFlags') ?? 0; |
||
| 49 | |||
| 50 | if ($this->option('all')) { |
||
| 51 | $for_all = $this->option('all'); |
||
| 52 | } else { |
||
| 53 | $for_all = 0; |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($role_id == 0 && $for_all != 1) { |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 57 | $this->error('Role ID or --all flag must be given'); |
||
| 58 | } elseif ($role && $for_all) { |
||
| 59 | $this->error('Only one among role id aruguemwnt or --all flag option is accepted.'); |
||
| 60 | } else { |
||
| 61 | if (isset($role) && $role_id != 0 ? ($role->name == 'superadmin') : false) { |
||
|
0 ignored issues
–
show
|
|||
| 62 | $this->info('No permission is needed for Super Admin'); |
||
| 63 | } else { |
||
| 64 | MakePermission::makePermission($name, $role_id, $for_all, $only_flags); |
||
| 65 | $this->info('Permission created successfully'); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 |