/** * Execute the console command. * * @return boolean */ public function handle() { if (empty($this->option('role')) && empty($this->option('user'))) { $this->error('Missing options for role and/or user(e-mail) to select'); return false; } /* * Detect the role->id and lookup the user if its thru a user assignment. */ $role = false; $user = false; if (!empty($this->option('role'))) { if (!is_object($role)) { $role = Role::where('name', $this->option('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->option('role')); } } if (!empty($this->option('user'))) { if (!is_object($user)) { $user = User::where('email', $this->option('user'))->first(); } if (!is_object($user)) { $user = Role::find($this->option('user')); } } if (!is_object($role) || !is_object($user)) { $this->error('Unable to find role with this criteria'); return false; } $RoleUser = new RoleUser(); $RoleUser->user_id = $user->id; $RoleUser->role_id = $role->id; $validation = Validator::make($RoleUser->toArray(), RoleUser::createRules($RoleUser)); if ($validation->fails()) { $this->warn('The role has already been granted this permission'); $this->error('Failed to create the permission due to validation warnings'); return false; } if (!$RoleUser->save()) { $this->error('Failed to save the permission into the database'); return false; } $this->info("The role {$role->name} has been granted to user {$user->email}"); return true; }
/** * Execute the console command. * * @return boolean */ public function handle() { if (empty($this->option('role')) && empty($this->option('user'))) { $this->error('Missing options for role and/or user(e-mail) to select'); return false; } /* * Detect the role->id and lookup the user if its thru a user assignment. */ $role = false; $user = false; if (!empty($this->option('role'))) { if (!is_object($role)) { $role = Role::where('name', $this->option('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->option('role')); } } if (!empty($this->option('user'))) { if (!is_object($user)) { $user = User::where('email', $this->option('user'))->first(); } if (!is_object($user)) { $user = Role::find($this->option('user')); } } if (!is_object($role) || !is_object($user)) { $this->error('Unable to find role with this criteria'); return false; } $roleUser = RoleUser::all()->where('role_id', $role->id)->where('user_id', $user->id)->first(); if (!is_object($roleUser)) { $this->error('Nothing to delete, this {$permission->name} permission is not linked to the role {$role->name}'); return false; } if (!$roleUser->delete()) { $this->error('Failed to remove the permission into the database'); return false; } $this->info("The role {$role->name} has been revoked from role {$user->email}"); return true; }