1
|
|
|
<?php |
2
|
|
|
namespace App\Controller; |
3
|
|
|
|
4
|
|
|
use App\Model\User; |
5
|
|
|
use App\Common\JsonException; |
6
|
|
|
use App\Requests\RequestPasswordResetRequest; |
7
|
|
|
use App\Requests\PasswordResetRequest; |
8
|
|
|
use App\Requests\UserCreateRequest; |
9
|
|
|
use App\Requests\UserUpdateRequest; |
10
|
|
|
use Slim\Http\Request; |
11
|
|
|
use Slim\Http\Response; |
12
|
|
|
|
13
|
|
|
final class UserController extends CrudController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param Request $request |
17
|
|
|
* @param Response $response |
18
|
|
|
* @param array $args |
19
|
|
|
* |
20
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
21
|
|
|
* @throws JsonException |
22
|
|
|
*/ |
23
|
|
|
public function actionCreate(Request $request, Response $response, $args) |
24
|
|
|
{ |
25
|
|
|
$params = $request->getParsedBody(); |
26
|
|
|
|
27
|
|
|
$this->validationRequest($params, $args['entity'], new UserCreateRequest()); |
28
|
|
|
|
29
|
|
|
$exist = User::exist($params['data']['attributes']['email']); |
30
|
|
|
|
31
|
|
|
if ($exist) { |
32
|
|
|
throw new JsonException($args['entity'], 400, 'User already exists', 'User already exists'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$user = new User($params['data']['attributes']); |
36
|
|
|
$user->setPassword($params['data']['attributes']['password']); |
37
|
|
|
$user->save(); |
38
|
|
|
|
39
|
|
|
$result = $this->encoder->encode($request, $user); |
40
|
|
|
|
41
|
|
|
return $this->renderer->jsonApiRender($response, 200, $result); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param Request $request |
46
|
|
|
* @param Response $response |
47
|
|
|
* @param array $args |
48
|
|
|
* |
49
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
50
|
|
|
* @throws JsonException |
51
|
|
|
*/ |
52
|
|
|
public function actionUpdate(Request $request, Response $response, $args) |
53
|
|
|
{ |
54
|
|
|
$user = User::find($args['id']); |
55
|
|
|
|
56
|
|
|
if (!$user) { |
57
|
|
|
throw new JsonException($args['entity'], 404, 'Not found', 'Entity not found'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$params = $request->getParsedBody(); |
61
|
|
|
|
62
|
|
|
$this->validationRequest($params, $args['entity'], new UserUpdateRequest()); |
63
|
|
|
|
64
|
|
|
$user->update($params['data']['attributes']); |
65
|
|
|
|
66
|
|
|
if (isset($params['data']['attributes']['password'])) { |
67
|
|
|
$user->setPassword($params['data']['attributes']['password']); |
68
|
|
|
$user->save(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$result = $this->encoder->encode($request, $user); |
72
|
|
|
|
73
|
|
|
return $this->renderer->jsonApiRender($response, 200, $result); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param Request $request |
78
|
|
|
* @param Response $response |
79
|
|
|
* @param array $args |
80
|
|
|
* |
81
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
82
|
|
|
* @throws JsonException |
83
|
|
|
*/ |
84
|
|
|
public function actionRequestPasswordReset(Request $request, Response $response, $args) |
85
|
|
|
{ |
86
|
|
|
$params = $request->getParsedBody(); |
87
|
|
|
|
88
|
|
|
$this->validationRequest($params, $args['entity'], new RequestPasswordResetRequest()); |
89
|
|
|
|
90
|
|
|
$user = User::findUserByEmail($params['data']['attributes']['email']); |
91
|
|
|
|
92
|
|
|
if (!$user) { |
93
|
|
|
throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!User::isPasswordResetTokenValid($user->password_reset_token)) { |
97
|
|
|
$user->generatePasswordResetToken(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if (!$user->save()) { |
101
|
|
|
throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$message = \Swift_Message::newInstance('Восстановление пароля для доступа в example.com') |
105
|
|
|
->setFrom(['[email protected]' => 'Почтовик example.com']) |
106
|
|
|
->setTo([$user->email => $user->full_name]) |
107
|
|
|
->setBody($this->mailRenderer->render( |
108
|
|
|
'/RequestPasswordReset.php', |
109
|
|
|
[ |
110
|
|
|
'host' => $this->settings['params']['host'], |
111
|
|
|
'token' => $user->password_reset_token |
112
|
|
|
] |
113
|
|
|
), 'text/html'); |
114
|
|
|
|
115
|
|
|
if ($this->mailer->send($message)) { |
|
|
|
|
116
|
|
|
return $this->renderer->jsonApiRender($response, 204); |
117
|
|
|
}; |
118
|
|
|
|
119
|
|
|
throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Request $request |
124
|
|
|
* @param Response $response |
125
|
|
|
* @param array $args |
126
|
|
|
* |
127
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
128
|
|
|
* @throws JsonException |
129
|
|
|
*/ |
130
|
|
|
public function actionPasswordReset(Request $request, Response $response, $args) |
131
|
|
|
{ |
132
|
|
|
$params = $request->getParsedBody(); |
133
|
|
|
|
134
|
|
|
$this->validationRequest($params, $args['entity'], new PasswordResetRequest()); |
135
|
|
|
|
136
|
|
|
$user = User::findByPasswordResetToken($params['data']['attributes']['token']); |
137
|
|
|
|
138
|
|
|
if ($user) { |
139
|
|
|
$user->setPassword($params['data']['attributes']['password']); |
140
|
|
|
$user->removePasswordResetToken(); |
141
|
|
|
|
142
|
|
|
if ($user->save()) { |
143
|
|
|
return $this->renderer->jsonApiRender($response, 204); |
144
|
|
|
}; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
throw new JsonException($args['entity'], 400, 'Bad request', 'Bad request'); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: