Skip to content

Commit 966f14b

Browse files
committed
feat: add password change functionality with validation and API route
1 parent 1c2edfa commit 966f14b

File tree

5 files changed

+67
-6
lines changed

5 files changed

+67
-6
lines changed

contexts/Authorization/Application/Coordinators/AuthorizationCoordinator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,12 @@ public function deleteUser(int $id)
8282

8383
return $user;
8484
}
85+
86+
public function changePassword(int $userId, string $newPassword)
87+
{
88+
$user = $this->repository->getById(UserId::fromInt($userId));
89+
$user->changePassword($newPassword);
90+
91+
$this->repository->changePassword($user);
92+
}
8593
}

contexts/Authorization/Infrastructure/Routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
Route::get('{id}', 'getUser')->name('getUser');
1111
Route::get('', 'getUserList')->name('getUserList');
1212
Route::post('', 'createUser')->name('createUser');
13+
Route::patch('{id}/password', 'changePassword')->name('changePassword');
1314
Route::put('{id}/subspend', 'subspendUser')->name('subspendUser');
1415
Route::put('{id}', 'updateUser')->name('updateUser');
1516
Route::delete('{id}', 'deleteUser')->name('deleteUser');

contexts/Authorization/Presentation/Controllers/AuthorizationController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Contexts\Authorization\Application\DTOs\CreateUserDTO;
1010
use Contexts\Authorization\Application\DTOs\GetUserListDTO;
1111
use Contexts\Authorization\Application\DTOs\UpdateUserDTO;
12+
use Contexts\Authorization\Presentation\Requests\ChangePasswordRequest;
1213
use Contexts\Authorization\Presentation\Requests\CreateUserRequest;
1314
use Contexts\Authorization\Presentation\Requests\GetUserListRequest;
1415
use Contexts\Authorization\Presentation\Requests\UpdateUserRequest;
@@ -77,4 +78,16 @@ public function deleteUser(UserIdRequest $request)
7778
->message('User deleted successfully')
7879
->send();
7980
}
81+
82+
public function changePassword(ChangePasswordRequest $request)
83+
{
84+
$data = $request->validated();
85+
$userId = (int) ($data['id']);
86+
$newPassword = $data['new_password'];
87+
app(AuthorizationCoordinator::class)->changePassword($userId, $newPassword);
88+
89+
return $this->success()
90+
->message('Password changed successfully')
91+
->send();
92+
}
8093
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Contexts\Authorization\Presentation\Requests;
6+
7+
use App\Http\Requests\BaseFormRequest;
8+
9+
class ChangePasswordRequest extends BaseFormRequest
10+
{
11+
public function rules(): array
12+
{
13+
return [
14+
'id' => $this->idRule(),
15+
'new_password' => ['required', 'string', 'min:8', 'max:255', 'confirmed'],
16+
'new_password_confirmation' => ['required', 'string', 'min:8', 'max:255'],
17+
];
18+
}
19+
}

contexts/Authorization/Tests/Feature/UserPublishingTest.php renamed to contexts/Authorization/Tests/Feature/UserTest.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
$response->assertStatus(201);
1414
});
1515

16-
it('can get a user', function () {
16+
it('can get a user via api', function () {
1717
$response = $this->postJson('users', [
1818
'email' => 'test@email.com',
1919
'password' => 'password123',
@@ -38,13 +38,13 @@
3838
]);
3939
});
4040

41-
it('can not get a user that does not exist', function () {
41+
it('can not get a user that does not exist via api', function () {
4242
$response = $this->get('users/1');
4343

4444
$response->assertStatus(404);
4545
});
4646

47-
it('can get a list of users', function () {
47+
it('can get a list of users via api', function () {
4848
$response = $this->postJson('users', [
4949
'email' => 'test@email.com',
5050
'password' => 'password123',
@@ -59,7 +59,7 @@
5959
$response->assertStatus(200);
6060
});
6161

62-
it('can update a user', function () {
62+
it('can update a user via api', function () {
6363
$response = $this->postJson('users', [
6464
'email' => 'test@email.com',
6565
'password' => 'password123',
@@ -85,7 +85,7 @@
8585
]);
8686
});
8787

88-
it('can subspend a user', function () {
88+
it('can subspend a user via api', function () {
8989
$response = $this->postJson('users', [
9090
'email' => 'test@email.com',
9191
'password' => 'password123',
@@ -102,7 +102,7 @@
102102
$response->assertStatus(200);
103103
});
104104

105-
it('can delete a user', function () {
105+
it('can delete a user via api', function () {
106106
$response = $this->postJson('users', [
107107
'email' => 'test@email.com',
108108
'password' => 'password123',
@@ -122,3 +122,23 @@
122122

123123
$response->assertStatus(404);
124124
});
125+
126+
it('can change a user password via api', function () {
127+
$response = $this->postJson('users', [
128+
'email' => 'test@email.com',
129+
'password' => 'password123',
130+
'display_name' => 'My User',
131+
'status' => 'active',
132+
]);
133+
134+
$response->assertStatus(201);
135+
136+
$id = (int) $response->json('data.id');
137+
138+
$response = $this->patchJson("users/{$id}/password", [
139+
'new_password' => 'newpassword123',
140+
'new_password_confirmation' => 'newpassword123',
141+
]);
142+
143+
$response->assertStatus(200);
144+
});

0 commit comments

Comments
 (0)