public function testEnabled() { $this->initDB(); $exitCode = Artisan::call('role:edit', ['id' => $this->role->id, '--name' => 'some bogus value']); $this->assertEquals($exitCode, 0); $this->assertContains('The role has been updated', Artisan::output()); $this->assertEquals(Role::find($this->role->id)->name, 'some bogus value'); }
/** * {@inheritdoc}. */ protected function getObjectByArguments() { $role = false; if (!is_object($role)) { $role = Role::where('name', $this->argument('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->argument('role')); } return $role; }
/** * Execute the console command. * * @return bool */ public function handle() { if (empty($this->option('role')) || empty($this->option('permission'))) { $this->error('Missing options for role and/or permission to select, try --help'); return false; } /* * Detect the role->id and lookup the user if its thru a user assignment. */ $role = false; if (!is_object($role)) { $role = Role::where('name', $this->option('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->option('role')); } if (!is_object($role)) { $this->error('Unable to find role with this criteria'); return false; } /* * Detect the permission->id and lookup the name if needed */ $permission = false; if (!is_object($permission)) { $permission = Permission::where('name', $this->option('permission'))->first(); } if (!is_object($permission)) { $permission = Permission::find($this->option('permission')); } if (!is_object($permission)) { $this->error('Unable to find permission with this criteria'); return false; } $permissionRole = new PermissionRole(); $permissionRole->permission_id = $permission->id; $permissionRole->role_id = $role->id; $validation = Validator::make($permissionRole->toArray(), PermissionRole::createRules($permissionRole)); 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 (!$permissionRole->save()) { $this->error('Failed to save the permission into the database'); return false; } $this->info("The permission {$permission->name} has been granted to role {$role->name}"); 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 = 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; }
/** * Execute the console command. * * @return boolean */ public function handle() { if (empty($this->option('role'))) { $this->warn('the required role argument was not passed, try --help'); return false; } $role = false; if (!is_object($role)) { $role = Role::where('name', $this->option('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->option('role')); } if (!is_object($role)) { $this->error('Unable to find role with this criteria'); return false; } // Apply changes to the role object if (!empty($this->option('name'))) { $role->name = $this->option('name'); } if (!empty($this->option('description'))) { $role->description = $this->option('description'); } $validation = Validator::make($role->toArray(), Role::updateRules($role)); if ($validation->fails()) { foreach ($validation->messages()->all() as $message) { $this->warn($message); } $this->error('Failed to create the role due to validation warnings'); return false; } // Save the object $role->save(); $this->info("Role has been successfully updated"); return true; }
/** * Execute the console command. * * @return boolean */ public function handle() { if (empty($this->option('role'))) { $this->warn('no email or id argument was passed, try --help'); return false; } $role = false; if (!is_object($role)) { $role = Role::where('name', $this->option('role'))->first(); } if (!is_object($role)) { $role = Role::find($this->option('role')); } if (!is_object($role)) { $this->error('Unable to find role with this criteria'); return false; } $permissions = ''; foreach ($role->permissions as $permission) { $permissions .= $permission->name . PHP_EOL; } $table = []; $counter = 0; foreach (array_combine($this->headers, $this->fields) as $header => $field) { $counter++; if ($header == 'Permissions') { $table[$counter][] = $header; $table[$counter][] = chop($permissions); } else { $table[$counter][] = $header; $table[$counter][] = (string) $role->{$field}; } } $this->table(['Role Setting', 'Role Value'], $table); return true; }
/** * {@inheritdoc}. */ protected function getModelFromRequest() { return Role::find($this->argument('id')); }