Compare commits

..

No commits in common. "01269f44cf390cb969d264842593b5159d77fbe5" and "6f77d8100fc381f16fc9afe73e62b623fea2f7fb" have entirely different histories.

9 changed files with 11 additions and 95 deletions

View File

@ -3,8 +3,9 @@
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
{
@ -21,12 +22,4 @@ 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->setPassword($request->all());
$this->userService->reset($request->all());
}
}

View File

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

View File

@ -1,36 +0,0 @@
<?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,8 +13,6 @@ 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 setPassword($data)
public function reset($data)
{
$user = User::where([ 'email' => $data['email'] ])->first();
if ($user === null) {
@ -63,23 +63,4 @@ 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,39 +202,21 @@ paths:
description: Auth failed
403:
description: Auth failed
404:
description: User not found
requestBody:
description: |-
All fields of `user` are optional. If a field is specified, the database record will change to the field's value.
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
`password` is optional, only if you want to update the password.
`new_pass` is optional, only if you want to update the password.
Note: updating password will not revoke all current sessions of the user
Note: updating password will revoke all current sessions of the user
content:
application/json:
schema:
type: object
properties:
user:
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:
$ref: '#/components/schemas/User'
new_pass:
type: string
example: 'very_strong_password123456'
/api/users/private/trash/group:

View File

@ -17,7 +17,6 @@ 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');
});
});