main
Jing Li 2024-03-25 17:43:52 +08:00
parent 084db9dae2
commit 4611f6ac6d
2 changed files with 25 additions and 38 deletions

View File

@ -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'));
}

View File

@ -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;
}
}