feat: /api/users/private/edit/{id}

This commit is contained in:
b1ek 2024-08-30 11:31:22 +10:00
parent 6f77d8100f
commit 9e665234c8
Signed by: blek
GPG Key ID: 14546221E3595D0C
8 changed files with 94 additions and 10 deletions

View File

@ -3,9 +3,8 @@
namespace App\Http\Controllers;
use App\Http\Requests\AuthorizedRequest;
use App\Http\Requests\UserEditRequest;
use App\Services\UserService;
use Illuminate\Http\Request;
use Validator;
class PrivateUserController extends Controller
{
@ -22,4 +21,12 @@ class PrivateUserController extends Controller
{
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);
}
}
}

View File

@ -27,6 +27,6 @@ class PublicUserController extends Controller
public function reset(LoginRequest $request)
{
$this->userService->reset($request->all());
$this->userService->setPassword($request->all());
}
}

View File

@ -2,6 +2,7 @@
namespace App\Http\Requests;
use App\Models\User;
use App\Rules\ZxcvbnRule;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rules\Password;
@ -28,7 +29,7 @@ class RegisterRequest extends RestRequest
'name' => ['required', 'string'],
'middle_name' => ['required', 'string'],
'email' => ['required', 'email'],
'phone' => ['required', 'regex:/^\+\d+$/'],
'phone' => ['required', 'regex:' . User::PHONE_REGEX ],
'password' => ['required', Password::min(1)->rules([ new ZxcvbnRule ])],
];
}

View File

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

View File

@ -13,6 +13,8 @@ class User extends Authenticatable
{
use HasFactory, Notifiable, UuidId, HasHistory;
public const PHONE_REGEX = '/^\+\d+$/';
/**
* The attributes that are mass assignable.
*

View File

@ -43,7 +43,7 @@ class UserService
return false;
}
public function reset($data)
public function setPassword($data)
{
$user = User::where([ 'email' => $data['email'] ])->first();
if ($user === null) {
@ -63,4 +63,23 @@ class UserService
{
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;
}
}

View File

@ -202,21 +202,39 @@ paths:
description: Auth failed
403:
description: Auth failed
404:
description: User not found
requestBody:
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:
application/json:
schema:
type: object
properties:
user:
$ref: '#/components/schemas/User'
new_pass:
type: object
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
example: 'very_strong_password123456'
/api/users/private/trash/group:

View File

@ -17,6 +17,7 @@ Route::prefix('/api')->group(function() {
Route::controller(PrivateUserController::class)->prefix('/users/private')->group(function () {
Route::get('/list', 'list');
Route::get('/get/{id}', 'get')->whereUuid('id');
Route::put('/edit/{id}', 'edit')->whereUuid('id');
});
});