-
-
Notifications
You must be signed in to change notification settings - Fork 366
Expand file tree
/
Copy pathUpdateProfileRequest.php
More file actions
88 lines (76 loc) · 2.3 KB
/
UpdateProfileRequest.php
File metadata and controls
88 lines (76 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* SPDX-License-Identifier: MIT
* Copyright (c) 2017-2018 Tobias Reich
* Copyright (c) 2018-2025 LycheeOrg.
*/
namespace App\Http\Requests\Profile;
use App\Contracts\Http\Requests\HasPassword;
use App\Contracts\Http\Requests\RequestAttribute;
use App\Http\Requests\BaseApiRequest;
use App\Http\Requests\Traits\HasPasswordTrait;
use App\Models\User;
use App\Policies\UserPolicy;
use App\Providers\AuthServiceProvider;
use App\Rules\CurrentPasswordRule;
use App\Rules\PasswordRule;
use App\Rules\UsernameRule;
use Illuminate\Support\Facades\Gate;
class UpdateProfileRequest extends BaseApiRequest implements HasPassword
{
use HasPasswordTrait;
protected ?string $username = null;
protected ?string $email = null;
/**
* {@inheritDoc}
*/
public function authorize(): bool
{
return Gate::check(UserPolicy::CAN_EDIT, [User::class]);
}
/**
* {@inheritDoc}
*/
public function rules(): array
{
if (!AuthServiceProvider::isBasicAuthEnabled()) {
return [
RequestAttribute::EMAIL_ATTRIBUTE => ['present', 'nullable', 'email:rfc', 'max:100'],
];
}
return [
RequestAttribute::USERNAME_ATTRIBUTE => ['required', new UsernameRule(true)],
RequestAttribute::PASSWORD_ATTRIBUTE => ['sometimes', 'confirmed', new PasswordRule(false)],
RequestAttribute::OLD_PASSWORD_ATTRIBUTE => ['required', new PasswordRule(false), new CurrentPasswordRule()],
RequestAttribute::EMAIL_ATTRIBUTE => ['present', 'nullable', 'email:rfc', 'max:100'],
];
}
/**
* {@inheritDoc}
*/
protected function processValidatedValues(array $values, array $files): void
{
$this->email = trim($values[RequestAttribute::EMAIL_ATTRIBUTE] ?? '');
$this->email = $this->email === '' ? null : $this->email;
if (AuthServiceProvider::isBasicAuthEnabled()) {
// If basic auth is enabled, we require the username.
$this->password = $values[RequestAttribute::PASSWORD_ATTRIBUTE] ?? null;
$this->username = trim($values[RequestAttribute::USERNAME_ATTRIBUTE] ?? '');
$this->username = $this->username === '' ? null : $this->username;
}
}
/**
* Return the new username chosen.
* if Username is null, this means that the user does not want to update it.
*
* @return ?string
*/
public function username(): ?string
{
return $this->username;
}
public function email(): ?string
{
return $this->email;
}
}