|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace VentureDrake\LaravelCrm\Console; |
| 4 | + |
| 5 | +use Carbon\Carbon; |
| 6 | +use DB; |
| 7 | +use Illuminate\Console\Command; |
| 8 | +use Illuminate\Support\Composer; |
| 9 | + |
| 10 | +class LaravelCrmPermissions extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * The name and signature of the console command. |
| 14 | + * |
| 15 | + * @var string |
| 16 | + */ |
| 17 | + protected $signature = 'laravelcrm:permissions'; |
| 18 | + |
| 19 | + /** |
| 20 | + * The console command description. |
| 21 | + * |
| 22 | + * @var string |
| 23 | + */ |
| 24 | + protected $description = 'Install Laravel CRM package'; |
| 25 | + |
| 26 | + /** |
| 27 | + * The Composer instance. |
| 28 | + * |
| 29 | + * @var \Illuminate\Foundation\Composer |
| 30 | + */ |
| 31 | + protected $composer; |
| 32 | + |
| 33 | + /** |
| 34 | + * Create a new command instance. |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function __construct(Composer $composer) |
| 39 | + { |
| 40 | + parent::__construct(); |
| 41 | + $this->composer = $composer; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Execute the console command. |
| 46 | + * |
| 47 | + * @return mixed |
| 48 | + */ |
| 49 | + public function handle() |
| 50 | + { |
| 51 | + $this->info('Updating LaravelCRM Permissions...'); |
| 52 | + |
| 53 | + $this->comment('Clearing permissions cache'); |
| 54 | + |
| 55 | + app()[\Spatie\Permission\PermissionRegistrar::class]->forgetCachedPermissions(); |
| 56 | + |
| 57 | + foreach (DB::table('teams')->get() as $team) { |
| 58 | + foreach (DB::table('roles') |
| 59 | + ->whereNull('team_id') |
| 60 | + ->get() as $role) { |
| 61 | + $this->info('Inserting role '.$role->name.' for team '.$team->name); |
| 62 | + |
| 63 | + DB::table('roles')->updateOrInsert([ |
| 64 | + 'name' => $role->name, |
| 65 | + 'guard_name' => $role->guard_name, |
| 66 | + 'description' => $role->description, |
| 67 | + 'crm_role' => $role->crm_role, |
| 68 | + 'team_id' => $team->id, |
| 69 | + ], [ |
| 70 | + 'created_at' => Carbon::now(), |
| 71 | + 'updated_at' => Carbon::now(), |
| 72 | + ]); |
| 73 | + |
| 74 | + if ($newRole = DB::table('roles')->where([ |
| 75 | + 'name' => $role->name, |
| 76 | + 'guard_name' => $role->guard_name, |
| 77 | + 'description' => $role->description, |
| 78 | + 'crm_role' => $role->crm_role, |
| 79 | + 'team_id' => $team->id, |
| 80 | + ])->first()) { |
| 81 | + foreach (DB::table('permissions') |
| 82 | + ->leftJoin('role_has_permissions', 'permissions.id', '=', 'role_has_permissions.permission_id') |
| 83 | + ->where('role_has_permissions.role_id', $role->id) |
| 84 | + ->get() as $permission) { |
| 85 | + $this->info('Inserting permission '.$permission->name.' for role '.$role->name); |
| 86 | + |
| 87 | + DB::table('permissions')->updateOrInsert([ |
| 88 | + 'name' => $permission->name, |
| 89 | + 'guard_name' => $permission->guard_name, |
| 90 | + 'description' => $permission->description, |
| 91 | + 'crm_permission' => $permission->crm_permission, |
| 92 | + 'team_id' => $team->id, |
| 93 | + ], [ |
| 94 | + 'created_at' => Carbon::now(), |
| 95 | + 'updated_at' => Carbon::now(), |
| 96 | + ]); |
| 97 | + |
| 98 | + if ($newPermission = DB::table('permissions')->where([ |
| 99 | + 'name' => $permission->name, |
| 100 | + 'guard_name' => $permission->guard_name, |
| 101 | + 'description' => $permission->description, |
| 102 | + 'crm_permission' => $permission->crm_permission, |
| 103 | + 'team_id' => $team->id, |
| 104 | + ])->first()) { |
| 105 | + DB::table('role_has_permissions')->updateOrInsert([ |
| 106 | + 'permission_id' => $newPermission->id, |
| 107 | + 'role_id' => $newRole->id, |
| 108 | + ]); |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + $this->info('LaravelCRM Permissions Update Complete.'); |
| 116 | + } |
| 117 | +} |
0 commit comments