public function fire() { $name = $this->argument('name'); $permission = Permission::where('name', '=', $name)->first(); if (is_null($permission)) { $this->comment("{$name} is not a valid permission."); exit; } if ($username = $this->option('user')) { $user = User::where('username', '=', $username)->first(); if (is_null($user)) { $this->comment("{$username} is not a valid user."); exit; } $already_granted = $permission->users()->where('username', '=', $username)->first(); if (is_null($already_granted)) { try { $permission->user()->attach($user); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->info("{$name} granted to {$username}."); } else { $this->comment("{$name} is already granted to {$username}."); } return; } if ($groupname = $this->option('group')) { $group = Group::where('name', '=', $groupname)->first(); if (is_null($group)) { $this->comment("{$groupname} is not a valid group."); exit; } $already_granted = $permission->groups()->where('name', '=', $groupname)->first(); if (is_null($already_granted)) { try { $permission->groups()->attach($group); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->info("{$name} granted to {$groupname}."); } else { $this->comment("{$name} is already granted to {$groupname}."); } return; } $this->comment('Please use the group or user option.'); }
public function fire() { $name = $this->argument('name'); $permission = Permission::where('name', '=', $name)->first(); if (!is_null($permission)) { try { $permission->users()->detach(); $permission->groups()->detach(); $permission->delete(); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->info("{$name} permission removed."); } else { $this->comment("{$name} permission does not exist."); } }
public function fire() { $name = $this->argument('name'); $permission = Permission::where('name', '=', $name)->first(); if (is_null($permission)) { $permission = new Permission(); $permission->name = $name; try { $permission->save(); } catch (\Exception $e) { $this->error($e->getMessage()); } $this->info("{$name} permission added."); } else { $this->comment("{$name} permission exists."); } }