/** * @param $id * @param $input * @param $roles * @return bool * @throws EntityNotValidException * @throws \Exception */ public function update($id, $input, $roles) { $this->validatePermission($input); $permission = $this->findOrThrowException($id); $permission->name = $input['name']; $permission->display_name = $input['display_name']; $permission->system = isset($input['system']) ? 1 : 0; //See if this permission is tied directly to a user first if (count($permission->users) > 0) { throw new Exception('This permission is currently tied directly to one or more users and can not be assigned to a role.'); } $this->permissionMustContainRole($roles); if ($permission->save()) { //Detach permission from every role, then add the permission to the selected roles $currentRoles = $this->roles->getAllRoles(); foreach ($currentRoles as $role) { $role->detachPermission($permission); } if (count($roles['permission_roles']) > 0) { //For each role, load role, collect perms, add perm to perms, flush perms, read perms foreach ($roles['permission_roles'] as $role_id) { //Get the role, with permissions $role = $this->roles->findOrThrowException($role_id, true); //Get the roles permissions into an array $role_permissions = $role->permissions->lists('id'); if (count($role_permissions) >= 1) { //Role has permissions, gather them first //Add this new permission id to the role array_push($role_permissions, $permission->id); //For some reason the lists() casts as a string, convert all to int $role_permissions_temp = array(); foreach ($role_permissions as $rp) { array_push($role_permissions_temp, (int) $rp); } $role_permissions = $role_permissions_temp; //Sync the permissions to the role $role->permissions()->sync($role_permissions); } else { //Role has no permissions, add the 1 $role->permissions()->sync([$permission->id]); } } } return true; } throw new Exception("There was a problem updating this permission. Please try again."); }
/** * @param $id * @return mixed */ public function edit($id) { $user = $this->users->findOrThrowException($id, true); return view('vault::edit')->withUser($user)->withUserRoles($user->roles->lists('id')->all())->withRoles($this->roles->getAllRoles('id', 'asc', true))->withUserPermissions($user->permissions->lists('id')->all())->withPermissions($this->permissions->getPermissionsNotAssociatedWithRole()); }