This commit is contained in:
Saeed Vaziry
2024-09-27 20:36:03 +02:00
committed by GitHub
parent b62c40c97d
commit f6bc04763b
122 changed files with 6609 additions and 807 deletions

View File

@ -27,7 +27,12 @@ public function create(array $input): User
private function validate(array $input): void
{
Validator::make($input, [
Validator::make($input, self::rules())->validate();
}
public static function rules(): array
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|string|min:8',
@ -35,6 +40,6 @@ private function validate(array $input): void
'required',
Rule::in([UserRole::ADMIN, UserRole::USER]),
],
])->validate();
];
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Actions\User;
use App\Models\Project;
use App\Models\User;
use Illuminate\Validation\Rule;
class UpdateProjects
{
public function update(User $user, array $input): void
{
$this->validate($input);
$user->projects()->sync($input['projects']);
if ($user->currentProject && !$user->projects->contains($user->currentProject)) {
$user->current_project_id = null;
$user->save();
}
/** @var Project $firstProject */
$firstProject = $user->projects->first();
if (!$user->currentProject && $firstProject) {
$user->current_project_id = $firstProject->id;
$user->save();
}
}
private function validate(array $input): void
{
validator($input, self::rules())->validate();
}
public static function rules(): array
{
return [
'projects.*' => [
'required',
Rule::exists('projects', 'id'),
],
];
}
}

View File

@ -9,7 +9,7 @@
class UpdateUser
{
public function update(User $user, array $input): void
public function update(User $user, array $input): User
{
$this->validate($user, $input);
@ -18,18 +18,29 @@ public function update(User $user, array $input): void
$user->timezone = $input['timezone'];
$user->role = $input['role'];
if (isset($input['password']) && $input['password'] !== null) {
if (isset($input['password'])) {
$user->password = bcrypt($input['password']);
}
$user->save();
return $user;
}
private function validate(User $user, array $input): void
{
Validator::make($input, [
Validator::make($input, self::rules($user))->validate();
}
public static function rules(User $user): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'email' => [
'required',
'email', 'max:255',
Rule::unique('users', 'email')->ignore($user->id)
],
'timezone' => [
'required',
Rule::in(timezone_identifiers_list()),
@ -37,12 +48,7 @@ private function validate(User $user, array $input): void
'role' => [
'required',
Rule::in([UserRole::ADMIN, UserRole::USER]),
function ($attribute, $value, $fail) use ($user) {
if ($user->is(auth()->user()) && $value !== $user->role) {
$fail('You cannot change your own role');
}
},
],
])->validate();
];
}
}

View File

@ -3,23 +3,22 @@
namespace App\Actions\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class UpdateUserPassword
{
/**
* Validate and update the user's password.
*/
public function update($user, array $input): void
{
Validator::make($input, [
'current_password' => ['required', 'string', 'current-password'],
'password' => ['required', 'string'],
'password_confirmation' => ['required', 'same:password'],
])->validate();
$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
}
public static function rules(): array
{
return [
'current_password' => ['required', 'string', 'current-password'],
'password' => ['required', 'string'],
'password_confirmation' => ['required', 'same:password'],
];
}
}

View File

@ -3,28 +3,12 @@
namespace App\Actions\User;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
class UpdateUserProfileInformation
{
/**
* Validate and update the given user's profile information.
*
* @throws Exception
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'timezone' => [
'required',
Rule::in(timezone_identifiers_list()),
],
])->validateWithBag('updateProfileInformation');
if ($input['email'] !== $user->email) {
$this->updateVerifiedUser($user, $input);
} else {
@ -36,6 +20,18 @@ public function update(User $user, array $input): void
}
}
public static function rules(User $user): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'timezone' => [
'required',
Rule::in(timezone_identifiers_list()),
],
];
}
/**
* Update the given verified user's profile information.
*/