diff --git a/app/Admin/Controllers/System/AdminUserController.php b/app/Admin/Controllers/System/AdminUserController.php index 158960b..09a510c 100644 --- a/app/Admin/Controllers/System/AdminUserController.php +++ b/app/Admin/Controllers/System/AdminUserController.php @@ -5,7 +5,6 @@ namespace App\Admin\Controllers\System; use App\Admin\Controllers\AdminController; use App\Admin\Requests\System\AdminUserChangePasswordRequest; use App\Admin\Services\AdminUserService; -use Illuminate\Support\Facades\Hash; use Slowlyo\OwlAdmin\Admin; use Slowlyo\OwlAdmin\Renderers\Form; use Slowlyo\OwlAdmin\Renderers\Operation; @@ -87,12 +86,7 @@ class AdminUserController extends AdminController public function changePassword($id, AdminUserChangePasswordRequest $request) { - $model = Admin::adminUserModel(); - - $user = $model->findOrFail($id); - $user->update([ - 'password' => Hash::make($request->input('password')), - ]); + $this->service->update($id, $request->only(['password'])); return $this->autoResponse('success', __('admin.save')); } diff --git a/app/Admin/Services/AdminUserService.php b/app/Admin/Services/AdminUserService.php index 2dc671e..24f4d1e 100644 --- a/app/Admin/Services/AdminUserService.php +++ b/app/Admin/Services/AdminUserService.php @@ -41,32 +41,46 @@ class AdminUserService extends BaseService return $this->setError(__('admin.required', ['attribute' => __('admin.password')])); } - if (! $this->passwordHandler($data)) { - return false; + if (array_key_exists('confirm_password', $data)) { + if ($data['password'] !== $data['confirm_password']) { + return $this->setError(__('admin.admin_user.password_confirmation')); + } + unset($data['confirm_password']); } - $columns = $this->getTableColumns(); + if (Hash::needsRehash($data['password'])) { + $data['password'] = Hash::make($data['password']); + } $model = $this->getModel(); + $user = $model::create(Arr::except($data, ['roles'])); - return $this->saveData($data, $columns, $model); + if (isset($data['roles'])) { + $user->roles()->attach($data['roles']); + } + + return true; } public function update($primaryKey, $data): bool { - if ($this->checkUsernameUnique($data['username'], $primaryKey)) { + if (isset($data['username']) && $this->checkUsernameUnique($data['username'], $primaryKey)) { return $this->setError(__('admin.admin_user.username_already_exists')); } - if (! $this->passwordHandler($data)) { - return false; + if (isset($data['password']) && Hash::needsRehash($data['password'])) { + $data['password'] = Hash::make($data['password']); } - $columns = $this->getTableColumns(); + $user = $this->query()->whereKey($primaryKey)->first(); + $user->update(Arr::except($data, ['roles'])); - $model = $this->query()->whereKey($primaryKey)->first(); + if (isset($data['roles'])) { + $user->roles()->detach(); + $user->roles()->attach($data['roles']); + } - return $this->saveData($data, $columns, $model); + return true; } public function checkUsernameUnique($username, $id = 0): bool @@ -135,25 +149,4 @@ class AdminUserService extends BaseService return compact('items', 'total'); } - - protected function saveData($data, array $columns, AdminUser $model): bool - { - $roles = Arr::pull($data, 'roles'); - - foreach ($data as $k => $v) { - if (! in_array($k, $columns)) { - continue; - } - - $model->setAttribute($k, $v); - } - - if ($model->save()) { - $model->roles()->sync(Arr::has($roles, '0.id') ? Arr::pluck($roles, 'id') : $roles); - - return true; - } - - return false; - } }