1 | <?php |
||
2 | declare(strict_types=1); |
||
3 | /** |
||
4 | * UserRequest.php |
||
5 | * Copyright (c) 2018 [email protected] |
||
6 | * |
||
7 | * This file is part of Firefly III. |
||
8 | * |
||
9 | * Firefly III is free software: you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation, either version 3 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * Firefly III is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License |
||
20 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | |||
24 | namespace FireflyIII\Api\V1\Requests; |
||
25 | |||
26 | use FireflyIII\User; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Class UserRequest |
||
31 | */ |
||
32 | class UserRequest extends Request |
||
33 | { |
||
34 | /** |
||
35 | * @return bool |
||
36 | */ |
||
37 | public function authorize(): bool |
||
38 | { |
||
39 | // Only allow authenticated users |
||
40 | if (!auth()->check()) { |
||
41 | return false; // @codeCoverageIgnore |
||
42 | } |
||
43 | /** @var User $user */ |
||
44 | $user = auth()->user(); |
||
45 | if (!$user->hasRole('owner')) { |
||
0 ignored issues
–
show
Deprecated Code
introduced
by
Loading history...
|
|||
46 | return false; // @codeCoverageIgnore |
||
47 | } |
||
48 | |||
49 | return true; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * @return array |
||
54 | */ |
||
55 | public function getAll(): array |
||
56 | { |
||
57 | $data = [ |
||
58 | 'email' => $this->string('email'), |
||
59 | 'blocked' => $this->boolean('blocked'), |
||
60 | 'blocked_code' => $this->string('blocked_code'), |
||
61 | ]; |
||
62 | |||
63 | return $data; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @return array |
||
68 | */ |
||
69 | public function rules(): array |
||
70 | { |
||
71 | $rules = [ |
||
72 | 'email' => 'required|email|unique:users,email,', |
||
73 | 'blocked' => 'required|boolean', |
||
74 | 'blocked_code' => 'in:email_changed', |
||
75 | ]; |
||
76 | switch ($this->method()) { |
||
77 | default: |
||
78 | break; |
||
79 | case 'PUT': |
||
80 | case 'PATCH': |
||
81 | $user = $this->route()->parameter('user'); |
||
82 | $rules['email'] = 'required|email|unique:users,email,' . $user->id; |
||
83 | break; |
||
84 | } |
||
85 | |||
86 | return $rules; |
||
87 | } |
||
88 | |||
89 | } |
||
90 |