1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller; |
4
|
|
|
|
5
|
|
|
use App\Form\User\RegistrationForm; |
6
|
|
|
use Del\Common\ContainerService; |
7
|
|
|
use Del\Exception\UserException; |
8
|
|
|
use Del\Service\UserService; |
9
|
|
|
use Exception; |
10
|
|
|
use OAuth\User; |
11
|
|
|
|
12
|
|
|
class UserController extends BaseController |
13
|
|
|
{ |
14
|
|
|
/** @var UserService */ |
15
|
|
|
private $userService; |
16
|
|
|
|
17
|
|
|
public function init() |
18
|
|
|
{ |
19
|
|
|
$c = ContainerService::getInstance()->getContainer(); |
20
|
|
|
$this->userService = $c['service.user']; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Fetch user details |
25
|
|
|
* @SWG\Get( |
26
|
|
|
* path="/user/{id}", |
27
|
|
|
* tags={"users"}, |
28
|
|
|
* @SWG\Parameter( |
29
|
|
|
* name="id", |
30
|
|
|
* in="path", |
31
|
|
|
* type="integer", |
32
|
|
|
* description="the type of response", |
33
|
|
|
* required=false, |
34
|
|
|
* default=1 |
35
|
|
|
* ), |
36
|
|
|
* @SWG\Response(response="200", description="Sends user details") |
37
|
|
|
* ) |
38
|
|
|
* |
39
|
|
|
*/ |
40
|
|
|
public function indexAction() |
41
|
|
|
{ |
42
|
|
|
$id = $this->getParam('id'); |
43
|
|
|
/** @var UserService $userSvc */ |
44
|
|
|
$userSvc = ContainerService::getInstance()->getContainer()['service.user']; |
45
|
|
|
/** @var User $user */ |
46
|
|
|
$user = $userSvc->findUserById($id); |
47
|
|
|
if (!$user) { |
48
|
|
|
$this->sendJsonResponse(['User not found']); |
49
|
|
|
} |
50
|
|
|
$this->sendJsonResponse(['email' => $user->getEmail()]); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Register as a new user. |
55
|
|
|
* @SWG\Post( |
56
|
|
|
* path="/user/register", |
57
|
|
|
* tags={"users"}, |
58
|
|
|
* @SWG\Response(response="200", description="Registers a new unactivated user"), |
59
|
|
|
* @SWG\Parameter( |
60
|
|
|
* name="email", |
61
|
|
|
* in="formData", |
62
|
|
|
* type="string", |
63
|
|
|
* description="the users email", |
64
|
|
|
* required=true, |
65
|
|
|
* default="[email protected]" |
66
|
|
|
* ), |
67
|
|
|
* @SWG\Parameter( |
68
|
|
|
* name="password", |
69
|
|
|
* in="formData", |
70
|
|
|
* type="string", |
71
|
|
|
* description="a password for the user", |
72
|
|
|
* required=true, |
73
|
|
|
* default="password" |
74
|
|
|
* ), |
75
|
|
|
* @SWG\Parameter( |
76
|
|
|
* name="confirm", |
77
|
|
|
* in="formData", |
78
|
|
|
* type="string", |
79
|
|
|
* description="password confirmation", |
80
|
|
|
* required=true, |
81
|
|
|
* default="password" |
82
|
|
|
* ) |
83
|
|
|
* ) |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
|
|
public function registerAction() |
87
|
|
|
{ |
88
|
|
|
$form = new RegistrationForm('register'); |
89
|
|
|
|
90
|
|
|
if ($this->getRequest()->getMethod() == 'POST') { |
91
|
|
|
|
92
|
|
|
$formData = $this->getRequest()->getParsedBody(); |
93
|
|
|
$form->populate($formData); |
|
|
|
|
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
$data = $form->getValues(); |
97
|
|
|
$user = $this->userService->registerUser($data); |
98
|
|
|
$link = $this->userService->generateEmailLink($user); |
99
|
|
|
$this->sendJsonResponse([ |
100
|
|
|
'user' => $this->userService->toArray($user), |
101
|
|
|
'link' => [ |
102
|
|
|
'token' => $link->getToken(), |
103
|
|
|
'expires' => $link->getExpiryDate()->format('Y-m-d H:i:s'), |
104
|
|
|
], |
105
|
|
|
]); |
106
|
|
|
|
107
|
|
|
} catch (UserException $e) { |
108
|
|
|
|
109
|
|
|
switch ($e->getMessage()) { |
110
|
|
|
case UserException::USER_EXISTS: |
111
|
|
|
throw new Exception($e->getMessage(), 400); |
112
|
|
|
break; |
|
|
|
|
113
|
|
|
case UserException::WRONG_PASSWORD: |
|
|
|
|
114
|
|
|
|
115
|
|
|
throw new Exception($e->getMessage(), 400); |
116
|
|
|
break; |
|
|
|
|
117
|
|
|
} |
118
|
|
|
throw $e; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$form->populate($formData); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Activate from the email link. |
129
|
|
|
* @SWG\Get( |
130
|
|
|
* path="/user/activate/{email}/{token}", |
131
|
|
|
* tags={"users"}, |
132
|
|
|
* @SWG\Response(response="200", description="Registers a new unactivated user"), |
133
|
|
|
* @SWG\Parameter( |
134
|
|
|
* name="email", |
135
|
|
|
* in="path", |
136
|
|
|
* type="string", |
137
|
|
|
* description="the users email", |
138
|
|
|
* required=true, |
139
|
|
|
* default="[email protected]" |
140
|
|
|
* ), |
141
|
|
|
* @SWG\Parameter( |
142
|
|
|
* name="token", |
143
|
|
|
* in="path", |
144
|
|
|
* type="string", |
145
|
|
|
* description="the email link token", |
146
|
|
|
* required=true, |
147
|
|
|
* default="r4nd0mT0k3n" |
148
|
|
|
* ) |
149
|
|
|
* ) |
150
|
|
|
* @throws Exception |
151
|
|
|
*/ |
152
|
|
|
public function activateAction() |
153
|
|
|
{ |
154
|
|
|
$email = $this->getParam('email'); |
155
|
|
|
$token = $this->getParam('token'); |
156
|
|
|
|
157
|
|
|
$this->sendJsonResponse([ |
158
|
|
|
'email' => $email, |
159
|
|
|
'token' => $token |
160
|
|
|
]); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.