Compare commits
2 Commits
6f77d8100f
...
01269f44cf
Author | SHA1 | Date |
---|---|---|
b1ek | 01269f44cf | |
b1ek | 9e665234c8 |
|
@ -3,9 +3,8 @@
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Http\Requests\AuthorizedRequest;
|
use App\Http\Requests\AuthorizedRequest;
|
||||||
|
use App\Http\Requests\UserEditRequest;
|
||||||
use App\Services\UserService;
|
use App\Services\UserService;
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Validator;
|
|
||||||
|
|
||||||
class PrivateUserController extends Controller
|
class PrivateUserController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -22,4 +21,12 @@ class PrivateUserController extends Controller
|
||||||
{
|
{
|
||||||
return $this->userService->getOneById($id);
|
return $this->userService->getOneById($id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function edit(UserEditRequest $request, string $id)
|
||||||
|
{
|
||||||
|
$user = $this->userService->editUser($request->all(), $id);
|
||||||
|
if ($user === null) {
|
||||||
|
return response('', 404);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,6 +27,6 @@ class PublicUserController extends Controller
|
||||||
|
|
||||||
public function reset(LoginRequest $request)
|
public function reset(LoginRequest $request)
|
||||||
{
|
{
|
||||||
$this->userService->reset($request->all());
|
$this->userService->setPassword($request->all());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http\Requests;
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
use App\Rules\ZxcvbnRule;
|
use App\Rules\ZxcvbnRule;
|
||||||
use Illuminate\Foundation\Http\FormRequest;
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
use Illuminate\Validation\Rules\Password;
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
@ -28,7 +29,7 @@ class RegisterRequest extends RestRequest
|
||||||
'name' => ['required', 'string'],
|
'name' => ['required', 'string'],
|
||||||
'middle_name' => ['required', 'string'],
|
'middle_name' => ['required', 'string'],
|
||||||
'email' => ['required', 'email'],
|
'email' => ['required', 'email'],
|
||||||
'phone' => ['required', 'regex:/^\+\d+$/'],
|
'phone' => ['required', 'regex:' . User::PHONE_REGEX ],
|
||||||
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
|
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Requests;
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Rules\ZxcvbnRule;
|
||||||
|
use Illuminate\Foundation\Http\FormRequest;
|
||||||
|
use Illuminate\Validation\Rules\Password;
|
||||||
|
|
||||||
|
class UserEditRequest extends AuthorizedRequest
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the validation rules that apply to the request.
|
||||||
|
*
|
||||||
|
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
|
||||||
|
*/
|
||||||
|
public function rules(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'user.last_name' => [ 'string' ],
|
||||||
|
'user.name' => [ 'string' ],
|
||||||
|
'user.middle_name' => [ 'string' ],
|
||||||
|
'user.email' => [ 'email' ],
|
||||||
|
'user.phone' => [ 'string', 'regex:' . User::PHONE_REGEX ],
|
||||||
|
'user' => 'required',
|
||||||
|
'password' => [ Password::min(1)->rules([ new ZxcvbnRule ]) ],
|
||||||
|
|
||||||
|
'user.email_verified_at' => 'prohibited',
|
||||||
|
'user.created_at' => 'prohibited',
|
||||||
|
'user.updated_at' => 'prohibited',
|
||||||
|
'user.deleted_at' => 'prohibited',
|
||||||
|
'user.id' => 'prohibited',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,8 @@ class User extends Authenticatable
|
||||||
{
|
{
|
||||||
use HasFactory, Notifiable, UuidId, HasHistory;
|
use HasFactory, Notifiable, UuidId, HasHistory;
|
||||||
|
|
||||||
|
public const PHONE_REGEX = '/^\+\d+$/';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The attributes that are mass assignable.
|
* The attributes that are mass assignable.
|
||||||
*
|
*
|
||||||
|
|
|
@ -43,7 +43,7 @@ class UserService
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reset($data)
|
public function setPassword($data)
|
||||||
{
|
{
|
||||||
$user = User::where([ 'email' => $data['email'] ])->first();
|
$user = User::where([ 'email' => $data['email'] ])->first();
|
||||||
if ($user === null) {
|
if ($user === null) {
|
||||||
|
@ -63,4 +63,23 @@ class UserService
|
||||||
{
|
{
|
||||||
return User::where([ 'id' => $id ])->first();
|
return User::where([ 'id' => $id ])->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Will return `null` if failed
|
||||||
|
*/
|
||||||
|
public function editUser(array $data, string $id): User | null
|
||||||
|
{
|
||||||
|
$user = $this->getOneById($id);
|
||||||
|
if ($user === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (array_key_exists('password', $data)) {
|
||||||
|
$this->setPassword([ 'email' => $user['email'], 'password' => $data['password'] ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$user->fill($data['user']);
|
||||||
|
$user->save();
|
||||||
|
return $user;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -202,21 +202,39 @@ paths:
|
||||||
description: Auth failed
|
description: Auth failed
|
||||||
403:
|
403:
|
||||||
description: Auth failed
|
description: Auth failed
|
||||||
|
404:
|
||||||
|
description: User not found
|
||||||
requestBody:
|
requestBody:
|
||||||
description: |-
|
description: |-
|
||||||
All fields of `user` are required. The whole record will be updated with exactly what you provide here. It is assumed that you already have all information about the user beforehand
|
All fields of `user` are optional. If a field is specified, the database record will change to the field's value.
|
||||||
|
|
||||||
`new_pass` is optional, only if you want to update the password.
|
`password` is optional, only if you want to update the password.
|
||||||
|
|
||||||
Note: updating password will revoke all current sessions of the user
|
Note: updating password will not revoke all current sessions of the user
|
||||||
content:
|
content:
|
||||||
application/json:
|
application/json:
|
||||||
schema:
|
schema:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
user:
|
user:
|
||||||
$ref: '#/components/schemas/User'
|
type: object
|
||||||
new_pass:
|
properties:
|
||||||
|
last_name:
|
||||||
|
type: string
|
||||||
|
example: doe
|
||||||
|
name:
|
||||||
|
type: string
|
||||||
|
example: jade
|
||||||
|
middle_name:
|
||||||
|
type: string
|
||||||
|
example: john
|
||||||
|
email:
|
||||||
|
type: string
|
||||||
|
example: jdoe@example.com
|
||||||
|
phone:
|
||||||
|
type: string
|
||||||
|
example: '+000000'
|
||||||
|
password:
|
||||||
type: string
|
type: string
|
||||||
example: 'very_strong_password123456'
|
example: 'very_strong_password123456'
|
||||||
/api/users/private/trash/group:
|
/api/users/private/trash/group:
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
<link rel="stylesheet" href="//unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css" crossorigin="anonymous" integrity="sha384-wxLW6kwyHktdDGr6Pv1zgm/VGJh99lfUbzSn6HNHBENZlCN7W602k9VkGdxuFvPn">
|
<link rel="stylesheet" href="//unpkg.com/swagger-ui-dist@5.17.14/swagger-ui.css" crossorigin="anonymous" integrity="sha384-wxLW6kwyHktdDGr6Pv1zgm/VGJh99lfUbzSn6HNHBENZlCN7W602k9VkGdxuFvPn">
|
||||||
</head>
|
</head>
|
||||||
<body class="font-sans antialiased dark:bg-black dark:text-white/50">
|
<body class="font-sans antialiased dark:bg-black dark:text-white/50">
|
||||||
<div id="swagger-ui"></div>
|
<div id="swagger-ui"></div>
|
||||||
|
|
||||||
<script src="//unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js" crossorigin='anonymous' integrity="sha384-wmyclcVGX/WhUkdkATwhaK1X1JtiNrr2EoYJ+diV3vj4v6OC5yCeSu+yW13SYJep"></script>
|
<script src="//unpkg.com/swagger-ui-dist@5.17.14/swagger-ui-bundle.js" crossorigin='anonymous' integrity="sha384-wmyclcVGX/WhUkdkATwhaK1X1JtiNrr2EoYJ+diV3vj4v6OC5yCeSu+yW13SYJep"></script>
|
||||||
<script>
|
<script>
|
||||||
|
|
|
@ -17,6 +17,7 @@ Route::prefix('/api')->group(function() {
|
||||||
Route::controller(PrivateUserController::class)->prefix('/users/private')->group(function () {
|
Route::controller(PrivateUserController::class)->prefix('/users/private')->group(function () {
|
||||||
Route::get('/list', 'list');
|
Route::get('/list', 'list');
|
||||||
Route::get('/get/{id}', 'get')->whereUuid('id');
|
Route::get('/get/{id}', 'get')->whereUuid('id');
|
||||||
|
Route::put('/edit/{id}', 'edit')->whereUuid('id');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue