Do not update password when no new password is provided

This commit is contained in:
Christopher C. Wells 2021-04-21 09:08:07 -07:00
parent 1d1df92d8f
commit 104bbcd614
2 changed files with 27 additions and 1 deletions

View File

@ -52,7 +52,12 @@ class UserController extends Controller
{
$input = $request->validated();
$input['remember_token'] = Str::random(10);
$input['password'] = Hash::make($input['password']);
if (!empty($input['password'])) {
$input['password'] = Hash::make($input['password']);
}
else {
unset($input['password']);
}
$input['admin'] = $input['admin'] ?? false;
$user->fill($input)->save();

View File

@ -67,6 +67,27 @@ class UserControllerTest extends HttpControllerTestCase
$response->assertSessionHasNoErrors();
}
public function testCanChangeUserPassword(): void {
$user = $this->createInstance();
$user->password = 'password1';
$user->save();
$input = $user->toArray();
$input['password'] = 'password2';
$input['password_confirmation'] = 'password2';
$put_url = action([$this->class(), 'update'], [$this->routeKey() => $user]);
$response = $this->put($put_url, $input);
$response->assertSessionHasNoErrors();
$user->refresh();
$this->logout();
$response = $this-> post('/login', ['username' => $user->username, 'password' => 'password1']);
$response->assertSessionHasErrors();
$this->post('/login', ['username' => $user->username, 'password' => 'password2']);
$this->assertAuthenticatedAs($user);
}
public function testCanNotDeleteSelf(): void {
$user = User::first();
$edit_url = action([$this->class(), 'delete'], [$this->routeKey() => $user]);