Compare commits

..

2 Commits

Author SHA1 Message Date
b1ek 01269f44cf
refactor: remove excessive spaces 2024-08-30 11:31:32 +10:00
b1ek 9e665234c8
feat: /api/users/private/edit/{id} 2024-08-30 11:31:22 +10:00
9 changed files with 95 additions and 11 deletions

View File

@ -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);
}
}
} }

View File

@ -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());
} }
} }

View File

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

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; use HasFactory, Notifiable, UuidId, HasHistory;
public const PHONE_REGEX = '/^\+\d+$/';
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View File

@ -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;
}
} }

View File

@ -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:

View File

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