/** * Execute the job. * * @return bool */ public function handle() { $this->user->name = $this->request->input('name'); $this->user->email = $this->request->input('email'); $password = $this->request->input('password'); // If the user doesn't have a set password mutator, // we'll encrypt the password. if (!$this->user->hasSetMutator('password')) { $password = bcrypt($password); } $this->user->password = $password; return $this->user->save(); }
public function postUpdate(Requests\Admin\UserRequest $request, $act, $id = 0) { $user = new User(); if ($act == 'edit') { $user = User::find($id); } $user->name = $request->input('name'); $user->phone = $request->input('phone'); $user->email = $request->input('email'); $user->password = $request->input('password') ?: $user->password; if ($user->save()) { $info = ['from' => 'update', 'status' => 'success']; j4flash($info); return redirect('admin/user'); } else { return redirect()->back()->withErrors(['err' => lang('submit failed')])->withInput(); } }
/** * Execute the job. * * @throws CannotRemoveRolesException * * @return bool */ public function handle() { $this->user->name = $this->request->input('name', $this->user->name); $this->user->email = $this->request->input('email'); $password = $this->request->input('password'); // Verify before changing the users password that it's not empty. if (!empty($password)) { // If the user doesn't have a set password mutator, // we'll encrypt the password. if (!$this->user->hasSetMutator('password')) { $password = bcrypt($password); } $this->user->password = $password; } // Retrieve the administrators name. $adminName = Role::getAdministratorName(); $roles = $this->request->input('roles', []); // Retrieve all administrator users. $administrators = $this->user->whereHas('roles', function (Builder $builder) use($adminName) { $builder->whereName($adminName); })->get(); // Retrieve the administrator role. $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 (count($roles) === 0 && $this->user->hasRole($admin) && $this->user->getKey() === auth()->user()->getKey() && count($administrators) === 1) { throw new CannotRemoveRolesException("Unable to remove the administrator role. You're the only administrator."); } if ($this->user->save()) { $this->user->roles()->sync($roles); return true; } return false; }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Requests\Admin\UserRequest $request, User $user) { $data = $request->all(); if ($request->password) { $data['password'] = bcrypt($data['password']); } else { unset($data['password']); } $user->update($data); $user->roles()->sync($request->input('role_list')); return redirect('/admin/user')->with('flash_success', 'Пользователь успешно обновлен.'); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update($id, ModelRequest $request) { try { $roles = $request->input('roles', []); $departments = $request->input('departments', []); $user_id = $request->input('user_id', null); $clear_picture = $request->input('clear_picture', false); $model = $this->getModel($id); try { DB::beginTransaction(); $model->user_id = $user_id; $model->update($request->all()); $model->roles()->sync($roles); $model->departments()->sync($departments); if ($clear_picture) { $picture = $model->picture; if ($picture) { try { File::delete(base_path() . $picture->filename); $picture->forcedelete(); } catch (Exception $e) { } } } else { if ($request->hasFile('photo')) { $uploaded_file = $request->file('photo'); if ($uploaded_file->isValid()) { $picture = $model->picture; if (!$picture) { $picture = new Picture(); } else { try { File::delete(base_path() . $picture->filename); } catch (Exception $e) { } } $picture->filename = $this->save_picture($model->name, $uploaded_file); $picture->mime_type = $uploaded_file->getMimeType(); $picture->extension = $uploaded_file->guessExtension(); if (!$picture->extension) { $picture->extension = $uploaded_file->getExtension(); } $picture->user_id = $model->id; $picture->save(); } } } DB::commit(); Flash::info(trans($this->resource_name . 'saved', ['model' => $this->model_name])); return redirect(route($this->show_route, [$model->id])); } catch (Exception $e) { DB::rollBack(); throw $e; } } catch (Exception $e) { $errors = []; if ($e->getCode() == 23000) { $errors['email'] = trans($this->resource_name . 'duplicated_email'); } else { Flash::error($e->getMessage()); } return $request->response($errors); } }