From cf29a4a319459186d66c03ec00b1e9526a2564fe Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Mon, 27 Jun 2016 22:47:21 -0400 Subject: [PATCH 1/2] Extract common data from UserController postCreate and postEdit into a helper method. Use this method to store data about user. Fixes #2200 --- app/Http/Controllers/UsersController.php | 103 ++++++++++++----------- 1 file changed, 55 insertions(+), 48 deletions(-) diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 9233fbd30a..7ca6451ccb 100755 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -102,19 +102,15 @@ class UsersController extends Controller { $user = new User; - $user->first_name = $data['first_name']= e($request->input('first_name')); - $user->last_name = e($request->input('last_name')); + //Username, email, and password need to be handled specially because the need to respect config values on an edit. $user->email = $data['email'] = e($request->input('email')); - $user->activated = 1; - $user->locale = e($request->input('locale')); $user->username = $data['username'] = e($request->input('username')); - $user->permissions = json_encode($request->input('permission')); - if ($request->has('password')) { $user->password = bcrypt($request->input('password')); $data['password'] = $request->input('password'); } - + //populate all generic data. + $user = $this->extractUserDataFromRequest($user, $request); if ($user->save()) { @@ -279,57 +275,26 @@ class UsersController extends Controller return redirect()->route('users')->with('error', $error); } - // Update the user - $user->first_name = e($request->input('first_name')); - $user->last_name = e($request->input('last_name')); - $user->locale = e($request->input('locale')); - if (Input::has('username')) { - $user->username = e($request->input('username')); - } - - $user->email = e($request->input('email')); - $user->employee_num = e($request->input('employee_num')); - $user->activated = e($request->input('activated', $user->activated)); - $user->jobtitle = e($request->input('jobtitle')); - $user->phone = e($request->input('phone')); - $user->location_id = e($request->input('location_id')); - $user->company_id = e(Company::getIdForUser($request->input('company_id'))); - $user->manager_id = e($request->input('manager_id')); - $user->notes = e($request->input('notes')); - $user->permissions = json_encode($request->input('permission')); - - - - - if ($user->manager_id == "") { - $user->manager_id = null; - } - - if ($user->location_id == "") { - $user->location_id = null; - } - + // First handle anything exclusive to editing. if ($request->has('groups')) { $user->groups()->sync($request->input('groups')); } else { $user->groups()->sync(array()); } - + // If lock passwords is set, the username, email, and password cannot be changed. + if(!config('app.lock_passwords')) { // Do we want to update the user password? - if (($request->has('password')) && (!config('app.lock_passwords'))) { - $user->password = bcrypt($request->input('password')); - } - - // Do we want to update the user email? - if (!config('app.lock_passwords')) { + if ($request->has('password')) { + $user->password = bcrypt($request->input('password')); + } + if ( $request->has('username')) { + $user->username = e($request->input('username')); + } $user->email = e($request->input('email')); - } - - - if (!config('app.lock_passwords')) { } + $user = $this->extractUserDataFromRequest($user, $request); // Was the user updated? if ($user->save()) { @@ -346,6 +311,48 @@ class UsersController extends Controller } + /** + * Maps Request Information to a User object + * + * @auther [Daniel Meltzer] [] + * @since [v3.0] + * @param User $user + * @param Request $request + * @return User + */ + private function extractUserDataFromRequest(User $user, Request $request) + { + // Update the user + $user->first_name = e($request->input('first_name')); + $user->last_name = e($request->input('last_name')); + $user->locale = e($request->input('locale')); + $user->employee_num = e($request->input('employee_num')); + $user->activated = e($request->input('activated', $user->activated)); + $user->jobtitle = e($request->input('jobtitle')); + $user->phone = e($request->input('phone')); + $user->location_id = e($request->input('location_id')); + $user->company_id = e(Company::getIdForUser($request->input('company_id'))); + $user->manager_id = e($request->input('manager_id')); + $user->notes = e($request->input('notes')); + $user->permissions = json_encode($request->input('permission')); + + + if ($user->manager_id == "") { + $user->manager_id = null; + } + + if ($user->location_id == "") { + $user->location_id = null; + } + + if ($user->company_id == "") { + $user->company_id = null; + } + + + return $user; + } + /** * Delete a user * From b1c28d796578314391a1acd7974caddf1774facb Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Tue, 28 Jun 2016 00:11:59 -0400 Subject: [PATCH 2/2] Move checks back into methods instead of having an extra helper method. Also remove unnecessary lock_passwords checks because there is a check at the top of the method that does this already. --- app/Http/Controllers/UsersController.php | 85 +++++++++++++----------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 7ca6451ccb..2e5bd3c353 100755 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -109,8 +109,32 @@ class UsersController extends Controller $user->password = bcrypt($request->input('password')); $data['password'] = $request->input('password'); } - //populate all generic data. - $user = $this->extractUserDataFromRequest($user, $request); + // Update the user + $user->first_name = e($request->input('first_name')); + $user->last_name = e($request->input('last_name')); + $user->locale = e($request->input('locale')); + $user->employee_num = e($request->input('employee_num')); + $user->activated = e($request->input('activated', $user->activated)); + $user->jobtitle = e($request->input('jobtitle')); + $user->phone = e($request->input('phone')); + $user->location_id = e($request->input('location_id')); + $user->company_id = e(Company::getIdForUser($request->input('company_id'))); + $user->manager_id = e($request->input('manager_id')); + $user->notes = e($request->input('notes')); + $user->permissions = json_encode($request->input('permission')); + + + if ($user->manager_id == "") { + $user->manager_id = null; + } + + if ($user->location_id == "") { + $user->location_id = null; + } + + if ($user->company_id == "") { + $user->company_id = null; + } if ($user->save()) { @@ -281,47 +305,16 @@ class UsersController extends Controller } else { $user->groups()->sync(array()); } - // If lock passwords is set, the username, email, and password cannot be changed. - if(!config('app.lock_passwords')) { - - // Do we want to update the user password? - if ($request->has('password')) { - $user->password = bcrypt($request->input('password')); - } - if ( $request->has('username')) { - $user->username = e($request->input('username')); - } - $user->email = e($request->input('email')); - + // Do we want to update the user password? + if ($request->has('password')) { + $user->password = bcrypt($request->input('password')); } - $user = $this->extractUserDataFromRequest($user, $request); - - // Was the user updated? - if ($user->save()) { - - - // Prepare the success message - $success = trans('admin/users/message.success.update'); - - // Redirect to the user page - return redirect()->route('users')->with('success', $success); + if ( $request->has('username')) { + $user->username = e($request->input('username')); } + $user->email = e($request->input('email')); - return redirect()->back()->withInput()->withErrors($user->getErrors()); - } - - /** - * Maps Request Information to a User object - * - * @auther [Daniel Meltzer] [] - * @since [v3.0] - * @param User $user - * @param Request $request - * @return User - */ - private function extractUserDataFromRequest(User $user, Request $request) - { // Update the user $user->first_name = e($request->input('first_name')); $user->last_name = e($request->input('last_name')); @@ -350,7 +343,19 @@ class UsersController extends Controller } - return $user; + // Was the user updated? + if ($user->save()) { + + + // Prepare the success message + $success = trans('admin/users/message.success.update'); + + // Redirect to the user page + return redirect()->route('users')->with('success', $success); + } + + return redirect()->back()->withInput()->withErrors($user->getErrors()); + } /**