/**
  * Removes the specified user from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $userId
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($roleId, $userId)
 {
     $this->authorize('admin.roles.users.destroy');
     $role = $this->role->findOrFail($roleId);
     $user = $role->users()->findOrFail($userId);
     // Retrieve the administrators name.
     $adminName = Role::getAdministratorName();
     // Retrieve all administrators.
     $administrators = $this->user->whereHas('roles', function ($query) use($adminName) {
         $query->whereName($adminName);
     })->get();
     $admin = Role::whereName($adminName)->first();
     // We need to verify that if the user is trying to remove all roles on themselves,
     // and they are the only administrator, that we throw an exception notifying them
     // that they can't do that. Though we want to allow the user to remove the
     // administrator role if more than one administrator exists.
     if ($user->hasRole($admin) && $user->id === auth()->user()->id && count($administrators) === 1) {
         flash()->setTimer(null)->error('Error!', "Unable to remove the administrator role from this user. You're the only administrator.");
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     if ($role->users()->detach($user)) {
         flash()->success('Success!', 'Successfully removed user.');
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     flash()->error('Error!', 'There was an issue removing this user. Please try again.');
     return redirect()->route('admin.roles.show', [$roleId]);
 }
 /**
  * Removes the specified permission from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $permissionId
  *
  * @return int
  */
 public function destroy($roleId, $permissionId)
 {
     $this->authorize('admin.roles.permissions.destroy');
     $role = $this->role->findOrFail($roleId);
     $permission = $role->permissions()->findOrFail($permissionId);
     return $role->permissions()->detach($permission);
 }
 /**
  * Removes the specified permission from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $permissionId
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($roleId, $permissionId)
 {
     $this->authorize('admin.roles.permissions.destroy');
     $role = $this->role->findOrFail($roleId);
     $permission = $role->permissions()->findOrFail($permissionId);
     if ($role->permissions()->detach($permission)) {
         flash()->success('Success!', 'Successfully removed permission.');
         return redirect()->route('admin.roles.show', [$roleId]);
     }
     flash()->error('Error!', 'There was an issue removing this permission. Please try again.');
     return redirect()->route('admin.roles.show', [$roleId]);
 }
 /**
  * Store role
  *
  * @param array $roleData            
  * @throws NotFoundException, ValidationException
  * @return \App\Models\RoleModel
  */
 public function store($roleData)
 {
     try {
         if (array_get($roleData, 'id')) {
             $role = RoleModel::findOrFail((int) array_get($roleData, 'id'))->fill($roleData);
         } else {
             $role = new RoleModel();
             $role->fill($roleData);
         }
     } catch (Exception $e) {
         throw new NotFoundException(trans('app.notFound'));
     }
     if (!$role->validate()) {
         throw new ValidationException(trans('app.correctErrors'), $role->errors()->toArray());
     }
     try {
         $role->save();
         // associate permissions
         if (array_get($roleData, 'permission_id')) {
             $role->perms()->sync(array_get($roleData, 'permission_id'));
         }
     } catch (Exception $e) {
         throw $e;
     }
     return $role;
 }
Exemple #5
0
 /**
  * Update the specified resource in storage.
  *
  * @param Request $request
  * @param int $id
  *
  * @return mixed
  */
 public function update(Request $request, $id)
 {
     $this->validate($request, ['role' => 'required|unique:roles,role,' . $id]);
     $role = Role::findOrFail($id);
     $role->update($request->all());
     \Flash::success('Role updated!');
     return redirect('admin/data-management/roles');
 }
 public function revokeRole($id, $role_id)
 {
     $this->authorize('user_revoke_role');
     $user = User::find($id);
     $role = Role::findOrFail($role_id);
     $user->revokeRole($role);
     return redirect()->back();
 }
 /**
  * Deletes the specified role.
  *
  * @param int|string $id
  *
  * @throws CannotDeleteAdministratorRole
  *
  * @return bool
  */
 public function destroy($id)
 {
     $this->authorize('admin.roles.destroy');
     $role = $this->role->findOrFail($id);
     if ($role->isAdministrator()) {
         throw new CannotDeleteAdministratorRole("You can't delete the administrator role.");
     }
     return $role->delete();
 }
 public function getEdit($id)
 {
     //fetch the role here just to force a 404 if it doesnt exit, we fetch it via ajax for the display anyway.
     $role = Role::findOrFail($id);
     if ($role->name == 'administrator') {
         abort(404);
     }
     return view('admin.roles.edit')->with('page_title', trans('admin.roles_title'))->with('id', $id);
 }
 public function updateRole($request)
 {
     $id = $request->route('role');
     $model = Role::findOrFail($id);
     $model->fill(['name' => $request->name, 'label' => $request->label]);
     $permissions = array_flatten($request->permissions);
     $model->permissions()->sync($permissions);
     return $model->save();
 }
 /**
  * Removes the specified user from the specified role.
  *
  * @param int|string $roleId
  * @param int|string $userId
  *
  * @throws CannotRemoveRolesException
  *
  * @return int
  */
 public function destroy($roleId, $userId)
 {
     $this->authorize('admin.roles.users.destroy');
     $role = $this->role->findOrFail($roleId);
     $user = $role->users()->findOrFail($userId);
     // Retrieve the administrators name.
     $adminName = Role::getAdministratorName();
     // Retrieve all administrators.
     $administrators = $this->user->whereHas('roles', function (Builder $builder) use($adminName) {
         $builder->whereName($adminName);
     })->get();
     $admin = Role::whereName($adminName)->first();
     // We need to verify that if the user is trying to remove all roles on themselves,
     // and they are the only administrator, that we throw an exception notifying them
     // that they can't do that. Though we want to allow the user to remove the
     // administrator role if more than one administrator exists.
     if ($user->hasRole($admin) && $user->getKey() === auth()->user()->getKey() && count($administrators) === 1) {
         throw new CannotRemoveRolesException("Unable to remove the administrator role from this user. You're the only administrator.");
     }
     return $role->users()->detach($user);
 }
Exemple #11
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(Request $request, $id)
 {
     $data = $request->all();
     $validator = Validator::make($data, ['name' => 'max:255', 'display_name' => 'max:255']);
     if ($validator->fails()) {
         $this->throwValidationException($request, $validator);
     }
     $role = Role::findOrFail($id);
     $role->update($data);
     $msg = array('msg' => '已成功更新');
     return json_encode($msg);
 }
Exemple #12
0
 /**
  * Get the validation rules that apply to the request.
  *
  * @return array
  */
 public function rules()
 {
     switch ($this->method()) {
         case 'POST':
             return ['name' => 'required|min:3|max:255|unique:roles,name'];
         case 'PUT':
         case 'PATCH':
             $id = $this->route()->roles;
             $role = Role::findOrFail($id);
             return ['name' => 'required|min:3|max:255|unique:roles,name,' . $role->id];
         default:
             break;
     }
 }
Exemple #13
0
 /**
  * Deletes the specified role.
  *
  * @param int|string $id
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function destroy($id)
 {
     $this->authorize('admin.roles.destroy');
     $role = $this->role->findOrFail($id);
     if ($role->isAdministrator()) {
         flash()->setTimer(null)->error('Error!', "You can't delete the administrator role.");
         return redirect()->route('admin.roles.show', [$id]);
     }
     if ($role->delete()) {
         flash()->success('Success!', 'Successfully deleted role.');
         return redirect()->route('admin.roles.index');
     }
     flash()->error('Error!', 'There was an issue deleting this role. Please try again.');
     return redirect()->route('admin.roles.show', [$id]);
 }
 /**
  * Efface la ressource de la bd.
  *
  * @param  int  $id l'id du rôle à effacer
  * @return Response
  */
 public function destroy($id)
 {
     try {
         $role = Role::findOrFail($id);
         $role->delete();
     } catch (ModelNotFoundException $e) {
         App::abort(404);
     }
     return Redirect::action('RolesController@index');
 }
Exemple #15
0
 public function init()
 {
     return false;
     //分配权限
     $admin = Role::findOrFail(2);
     $user = User::where('name', '=', 'cd')->first();
     // role attach alias
     $user->attachRole($admin);
     // parameter can be an Role object, array, or id
     // or eloquent's original technique
     $user->roles()->attach($admin->id);
     // id only
     //添加权限
     $owner = Role::findOrFail(1);
     $admin = Role::findOrFail(2);
     $createPost = new Permission();
     $createPost->name = 'create-post';
     $createPost->display_name = 'Create Posts';
     // optional
     // Allow a user to...
     $createPost->description = 'create new blog posts';
     // optional
     $createPost->save();
     $editUser = new Permission();
     $editUser->name = 'edit-user';
     $editUser->display_name = 'Edit Users';
     // optional
     // Allow a user to...
     $editUser->description = 'edit existing users';
     // optional
     $editUser->save();
     $admin->attachPermission($createPost);
     // equivalent to $admin->perms()->sync(array($createPost->id));
     $owner->attachPermissions(array($createPost, $editUser));
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $role = \App\Models\Role::findOrFail($id);
     $this->authorize('destroy', $role);
     $role->delete();
     return redirect(route('role.index'));
 }
 public function deletePermissionsDestroyAll($id)
 {
     $role = Role::findOrFail($id);
     if ($role->name == 'administrator') {
         return response()->json(['status' => 'failed', 'message' => trans('api.resource_delete_failed_relationship', ['relationship' => trans('global.permission')]), 'errors' => ['name' => [trans('global.role_admin_update_error')]]])->setStatusCode(422);
     }
     $role->permissions()->sync([]);
     return response()->json(['status' => 'success', 'message' => trans('api.resource_deleted', ['resource' => trans('global.permission')])])->setStatusCode(200);
 }
Exemple #18
0
 public function editRoles(Request $request)
 {
     $action = $request->input('action');
     if ($action == 'ADD') {
         $role = new Role();
         $role->name = $request->input('rolename');
         $role->save();
     } elseif ($action == 'DELETE') {
         $roleId = $request->input('roleid');
         $role = Role::findOrFail($roleId);
         $role->delete();
     } elseif ($action == "EDITPERMS") {
         $role = Role::findOrFail($request->input('roleid'));
         $perms = $request->input('rolepermissions-' . $request->input('roleid'));
         $role->perms()->sync($perms);
     }
     return redirect('admin/roles');
 }
 public function edit($id)
 {
     $data = Role::findOrFail($id);
     return view('admin.permission.edit', $data);
 }
Exemple #20
0
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     $role = Role::findOrFail($id);
     $role->delete();
     return redirect()->route('role.manager.index');
 }
 public function deleteRolesDestroy($id, $roleid)
 {
     $user = $this->repo->findOrFail($id);
     $role = Role::findOrFail($roleid);
     if (!$user->hasRole($role->name)) {
         abort(404);
     }
     $user->roles()->detach($role->id);
     return response()->json(['status' => 'success', 'message' => trans('api.resource_deleted', ['resource' => trans('global.role')])])->setStatusCode(200);
 }