Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit 9325b43

Browse files
committed
finished PUT and DELETE endpoints for users
1 parent 1b0f0c6 commit 9325b43

File tree

8 files changed

+339
-5
lines changed

8 files changed

+339
-5
lines changed

app/Modules/Auth/Controllers/UserController.php

+75-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Illuminate\Database\Eloquent\ModelNotFoundException;
66
use Illuminate\Http\JsonResponse;
7+
use Illuminate\Http\Response;
78
use Illuminate\Support\Facades\Auth;
89
use LarAPI\Core\Http\BaseController;
910
use LarAPI\Modules\Auth\Requests\CreateUserRequest;
11+
use LarAPI\Modules\Auth\Requests\UpdateUserRequest;
1012
use LarAPI\Modules\Auth\Services\UserService;
1113
use LarAPI\Modules\Common\Requests\CommonTableRequest;
1214

@@ -197,11 +199,83 @@ public function show(string $uuid): JsonResponse
197199
return $this->apiSuccessResponse($this->service->getUser($uuid));
198200
}
199201

200-
public function update(string $uuid): JsonResponse
202+
/**
203+
* Updates the info of a specific user
204+
*
205+
* @OA\Put(
206+
* tags={"Users Management"},
207+
* path="/v1/users/{uuid}",
208+
* description="Updates the info of a specific user",
209+
* security={ "jwt": {} },
210+
*
211+
* @OA\RequestBody(description="Info needed to update the user info",
212+
* @OA\MediaType(mediaType="application/json",
213+
* @OA\Schema(type="object", required={"name"},
214+
* @OA\Property(property="name", type="string"),
215+
* @OA\Property(property="password", type="string", description="Must contain at least one lowercase letter, one uppercase letter, one number and one special character"),
216+
* @OA\Property(property="password_confirmation", type="string"),
217+
* @OA\Property(property="active", type="boolean"),
218+
* @OA\Property(property="role_id", type="integer"),
219+
* ),
220+
* ),
221+
* ),
222+
*
223+
* @OA\Response(response="200", description="The success response",
224+
* @OA\MediaType(mediaType="application/json",
225+
* @OA\Schema(type="object",
226+
* @OA\Property(property="success", type="boolean"),
227+
* ),
228+
* ),
229+
* ),
230+
* @OA\Response(response="400", description="Invalid Request"),
231+
* @OA\Response(response="401", description="Unauthorized"),
232+
* @OA\Response(response="403", description="Access Denied"),
233+
* @OA\Response(response="422", description="Data is invalid"),
234+
* )
235+
*
236+
* @param UpdateUserRequest $request
237+
* @param string $uuid
238+
* @return JsonResponse
239+
*/
240+
public function update(UpdateUserRequest $request, string $uuid): JsonResponse
201241
{
242+
$this->service->updateUser(Auth::user(), $uuid, $request->getDTO());
243+
return $this->apiSimpleSuccessResponse(Response::HTTP_OK);
202244
}
203245

246+
/**
247+
* Deletes a specific user
248+
*
249+
* @OA\Delete(
250+
* tags={"Users Management"},
251+
* path="/v1/users/{uuid}",
252+
* description="Deletes a specific user",
253+
* security={ "jwt": {} },
254+
*
255+
* @OA\Parameter(name="uuid", in="path", required=true,
256+
* @OA\Schema(type="string"),
257+
* ),
258+
*
259+
* @OA\Response(response="200", description="The success response",
260+
* @OA\MediaType(mediaType="application/json",
261+
* @OA\Schema(type="object",
262+
* @OA\Property(property="success", type="boolean"),
263+
* ),
264+
* ),
265+
* ),
266+
* @OA\Response(response="400", description="Invalid Request"),
267+
* @OA\Response(response="401", description="Unauthorized"),
268+
* @OA\Response(response="403", description="Access Denied"),
269+
* @OA\Response(response="404", description="Resource not found"),
270+
* )
271+
*
272+
* @param string $uuid
273+
* @return JsonResponse
274+
* @throws ModelNotFoundException
275+
*/
204276
public function delete(string $uuid): JsonResponse
205277
{
278+
$this->service->deleteUser($uuid);
279+
return $this->apiSimpleSuccessResponse(Response::HTTP_OK);
206280
}
207281
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
4+
namespace LarAPI\Modules\Auth\Requests;
5+
6+
use Illuminate\Support\Facades\Hash;
7+
use LarAPI\Core\Http\BaseRequest;
8+
use LarAPI\Modules\Auth\Support\DTOs\UpdateUserDTO;
9+
use LarAPI\Modules\Common\Support\DTOs\DTOInterface;
10+
11+
class UpdateUserRequest extends BaseRequest
12+
{
13+
/**
14+
* @return array
15+
*/
16+
public function rules(): array
17+
{
18+
return [
19+
UpdateUserDTO::NAME => ['required', 'string'],
20+
UpdateUserDTO::PASSWORD_CONFIRMATION => ['required_with:' . UpdateUserDTO::PASSWORD, 'string'],
21+
UpdateUserDTO::ACTIVE => ['sometimes', 'boolean'],
22+
UpdateUserDTO::ROLE => ['sometimes', 'integer'],
23+
UpdateUserDTO::PASSWORD => [
24+
'sometimes', 'nullable', 'string', 'min:8', 'confirmed',
25+
'regex:/[a-z]/', 'regex:/[A-Z]/',
26+
'regex:/[0-9]/', 'regex:/[@$!%*#?&]/'
27+
],
28+
];
29+
}
30+
31+
/**
32+
* @inheritDoc
33+
*/
34+
public function getDTO(): DTOInterface
35+
{
36+
$dto = new UpdateUserDTO();
37+
$password = $this->input($dto::PASSWORD);
38+
39+
return $dto->setName($this->input($dto::NAME))
40+
->setPassword(!is_null($password) ? Hash::make($password) : null)
41+
->setActive($this->input($dto::ACTIVE))
42+
->setRoleId($this->input($dto::ROLE));
43+
}
44+
}

app/Modules/Auth/Routing/v1.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
Route::prefix('{uuid}')->group(function () {
2727
Route::get('/', 'UserController@show');
2828
Route::put('/', 'UserController@update')->middleware('block_viewer');
29-
Route::delete('/', 'UserController@delete')->middleware('block_viewer');
29+
Route::delete('/', 'UserController@delete')->middleware('check_manager');
3030
});
3131
});
3232
});

app/Modules/Auth/Services/UserService.php

+31
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use LarAPI\Models\Auth\Role;
77
use LarAPI\Models\Auth\User;
88
use LarAPI\Modules\Auth\Support\DTOs\CreateUserDTO;
9+
use LarAPI\Modules\Auth\Support\DTOs\UpdateUserDTO;
910
use LarAPI\Modules\Common\Support\DTOs\CommonTableDTO;
1011
use LarAPI\Modules\Common\Support\Paginator;
1112
use LarAPI\Repositories\Auth\UserRepository;
@@ -63,4 +64,34 @@ public function getUser(string $userUuid): User
6364
{
6465
return $this->repository->getByOrFail('uuid', $userUuid);
6566
}
67+
68+
/**
69+
* @param User $user
70+
* @param string $userUuid
71+
* @param UpdateUserDTO $dto
72+
* @return int
73+
*/
74+
public function updateUser(User $user, string $userUuid, UpdateUserDTO $dto): int
75+
{
76+
if (!$user->is_admin && !is_null($dto->getRoleId())) {
77+
if ($dto->getRoleId() === Role::ROLE_ADMIN) {
78+
$dto->setRoleId(Role::ROLE_NORMAL);
79+
}
80+
if ($dto->getRoleId() === Role::ROLE_MANAGER && !$user->is_manager) {
81+
$dto->setRoleId(Role::ROLE_NORMAL);
82+
}
83+
}
84+
85+
$params = collect($dto->toArray())->filter()->toArray();
86+
return $this->repository->updateBy('uuid', $userUuid, $params);
87+
}
88+
89+
/**
90+
* @param string $userUuid
91+
* @return mixed
92+
*/
93+
public function deleteUser(string $userUuid)
94+
{
95+
return $this->repository->deleteBy('uuid', $userUuid);
96+
}
6697
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
namespace LarAPI\Modules\Auth\Support\DTOs;
4+
5+
use LarAPI\Modules\Common\Support\DTOs\DTOInterface;
6+
7+
class UpdateUserDTO implements DTOInterface
8+
{
9+
public const NAME = 'name';
10+
public const PASSWORD = 'password';
11+
public const PASSWORD_CONFIRMATION = 'password_confirmation';
12+
public const ACTIVE = 'active';
13+
public const ROLE = 'role_id';
14+
15+
private string $name;
16+
private ?string $password;
17+
private ?bool $active;
18+
private ?int $roleId;
19+
20+
/**
21+
* @return string
22+
*/
23+
public function getName(): string
24+
{
25+
return $this->name;
26+
}
27+
28+
/**
29+
* @param string $name
30+
* @return UpdateUserDTO
31+
*/
32+
public function setName(string $name): UpdateUserDTO
33+
{
34+
$this->name = $name;
35+
return $this;
36+
}
37+
38+
/**
39+
* @return string|null
40+
*/
41+
public function getPassword(): ?string
42+
{
43+
return $this->password;
44+
}
45+
46+
/**
47+
* @param string|null $password
48+
* @return UpdateUserDTO
49+
*/
50+
public function setPassword(?string $password): UpdateUserDTO
51+
{
52+
$this->password = $password;
53+
return $this;
54+
}
55+
56+
/**
57+
* @return bool|null
58+
*/
59+
public function getActive(): ?bool
60+
{
61+
return $this->active;
62+
}
63+
64+
/**
65+
* @param bool|null $active
66+
* @return UpdateUserDTO
67+
*/
68+
public function setActive(?bool $active): UpdateUserDTO
69+
{
70+
$this->active = $active;
71+
return $this;
72+
}
73+
74+
/**
75+
* @return int|null
76+
*/
77+
public function getRoleId(): ?int
78+
{
79+
return $this->roleId;
80+
}
81+
82+
/**
83+
* @param int|null $roleId
84+
* @return UpdateUserDTO
85+
*/
86+
public function setRoleId(?int $roleId): UpdateUserDTO
87+
{
88+
$this->roleId = $roleId;
89+
return $this;
90+
}
91+
92+
/**
93+
* @return array
94+
*/
95+
public function toArray(): array
96+
{
97+
return [
98+
self::NAME => $this->name,
99+
self::PASSWORD => $this->password,
100+
self::ACTIVE => $this->active,
101+
self::ROLE => $this->roleId,
102+
];
103+
}
104+
}

config/jwt.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@
101101
|
102102
*/
103103

104-
'ttl' => env('JWT_TTL', 60),
104+
'ttl' => env('JWT_TTL', 1440),
105105

106106
/*
107107
|--------------------------------------------------------------------------
@@ -120,7 +120,7 @@
120120
|
121121
*/
122122

123-
'refresh_ttl' => env('JWT_REFRESH_TTL', 20160),
123+
'refresh_ttl' => env('JWT_REFRESH_TTL', 40320),
124124

125125
/*
126126
|--------------------------------------------------------------------------

0 commit comments

Comments
 (0)