|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Tfboe\FmLib\Http\Controllers; |
|
5
|
|
|
|
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
|
8
|
|
|
use Illuminate\Http\JsonResponse; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Illuminate\Support\Facades\Auth; |
|
11
|
|
|
use Laravel\Lumen\Application; |
|
12
|
|
|
use Tfboe\FmLib\Entity\UserInterface; |
|
13
|
|
|
use Tfboe\FmLib\Exceptions\AuthenticationException; |
|
14
|
|
|
use Tfboe\FmLib\Service\ObjectCreatorServiceInterface; |
|
15
|
|
|
use Tymon\JWTAuth\Exceptions\JWTException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class UserController |
|
19
|
|
|
* @package App\Http\Controllers |
|
20
|
|
|
*/ |
|
21
|
|
|
class UserController extends BaseController |
|
22
|
|
|
{ |
|
23
|
|
|
//<editor-fold desc="Public Methods"> |
|
24
|
|
|
|
|
25
|
|
|
/** @var ObjectCreatorServiceInterface $objectCreatorService */ |
|
26
|
|
|
private $objectCreatorService; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @inheritDoc |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(EntityManagerInterface $entityManager, |
|
32
|
|
|
ObjectCreatorServiceInterface $objectCreatorService) |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct($entityManager); |
|
35
|
|
|
$this->objectCreatorService = $objectCreatorService; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* login action, checks credentials and returns token |
|
41
|
|
|
* @param Request $request the http request |
|
42
|
|
|
* @param Application $app |
|
43
|
|
|
* @return JsonResponse |
|
44
|
|
|
* @throws AuthenticationException wrong credentials or errors during creating a token |
|
45
|
|
|
*/ |
|
46
|
|
|
public function login(Request $request, Application $app): JsonResponse |
|
47
|
|
|
{ |
|
48
|
|
|
$specification = $this->getCredentialSpecification($app); |
|
49
|
|
|
$this->addAdditionalLoginSpecifications($specification); |
|
50
|
|
|
$this->validateBySpecification($request, $specification); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// grab credentials from the request |
|
54
|
|
|
$credentials = $request->only('email', 'password'); |
|
55
|
|
|
|
|
56
|
|
|
/** @var string $token */ |
|
57
|
|
|
$token = null; |
|
58
|
|
|
try { |
|
59
|
|
|
// attempt to verify the credentials and create a token for the user |
|
60
|
|
|
$token = Auth::attempt($credentials); |
|
61
|
|
|
if (!$token) { |
|
62
|
|
|
throw new AuthenticationException('invalid credentials'); |
|
63
|
|
|
} |
|
64
|
|
|
} /** @noinspection PhpRedundantCatchClauseInspection */ catch (JWTException $e) { |
|
65
|
|
|
// something went wrong whilst attempting to encode the token |
|
66
|
|
|
throw new AuthenticationException('could not create token'); |
|
67
|
|
|
} |
|
68
|
|
|
return $this->getLoginResponse($request, $token); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* register action, registers a new user with email and password |
|
74
|
|
|
* |
|
75
|
|
|
* @param Request $request the http request |
|
76
|
|
|
* @param Application $app |
|
77
|
|
|
* @return JsonResponse |
|
78
|
|
|
*/ |
|
79
|
|
|
public function register(Request $request, Application $app): JsonResponse |
|
80
|
|
|
{ |
|
81
|
|
|
$userClass = config('fm-lib')['entityMaps']['Tfboe\FmLib\Entity\UserInterface']; |
|
82
|
|
|
$specification = []; |
|
83
|
|
|
$specification['user'] = $this->getCredentialSpecification($app); |
|
84
|
|
|
$specification['user']['email']['validation'] .= '|unique:' . $userClass . ',email'; |
|
85
|
|
|
$specification['user']['confirmedAGBVersion'] = ['validation' => 'integer|min:0']; |
|
86
|
|
|
|
|
87
|
|
|
$this->addAdditionalRegisterSpecifications($specification); |
|
88
|
|
|
|
|
89
|
|
|
$this->validateBySpecification($request, array_merge(...array_values($specification))); |
|
90
|
|
|
|
|
91
|
|
|
$input = $request->input(); |
|
92
|
|
|
/** @var UserInterface $user */ |
|
93
|
|
|
|
|
94
|
|
|
$user = $this->setFromSpecification($this->newUser(), $specification['user'], $input); |
|
|
|
|
|
|
95
|
|
|
$this->getEntityManager()->persist($user); //sets the user id |
|
96
|
|
|
|
|
97
|
|
|
$this->createAdditionalRegisterEntities($user, $specification, $input); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$this->getEntityManager()->flush(); |
|
100
|
|
|
|
|
101
|
|
|
return $this->getRegisterResponse($request, $app, $user); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Creates a new user |
|
106
|
|
|
* @return UserInterface |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function newUser(): UserInterface |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->objectCreatorService->createObjectFromInterface(UserInterface::class); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Gets the response for a successful register action |
|
115
|
|
|
* @param Request $request the request |
|
116
|
|
|
* @param Application $app the application |
|
117
|
|
|
* @param UserInterface $user the newly registered user |
|
118
|
|
|
* @return JsonResponse the json response |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function getRegisterResponse(/** @noinspection PhpUnusedParameterInspection */ |
|
121
|
|
|
Request $request, /** @noinspection PhpUnusedParameterInspection */ |
|
|
|
|
|
|
122
|
|
|
Application $app, UserInterface $user) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
return response()->json(['id' => $user->getId()]); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @return JsonResponse |
|
129
|
|
|
*/ |
|
130
|
|
|
public function userId(): JsonResponse |
|
131
|
|
|
{ |
|
132
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */ |
|
133
|
|
|
return response()->json(['id' => Auth::user()->getAuthIdentifier()]); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
//</editor-fold desc="Public Methods"> |
|
136
|
|
|
|
|
137
|
|
|
//<editor-fold desc="Protected Methods"> |
|
138
|
|
|
/** |
|
139
|
|
|
* Gets additional input specifications for the login action |
|
140
|
|
|
* @param array $specification the specification to add to / modify |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function addAdditionalLoginSpecifications(array &$specification) |
|
|
|
|
|
|
143
|
|
|
{ |
|
144
|
|
|
//do nothing by default |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* adds additional register specifications |
|
149
|
|
|
* @param array $specification the specification to add to / modify |
|
150
|
|
|
*/ |
|
151
|
|
|
protected function addAdditionalRegisterSpecifications(array &$specification) |
|
|
|
|
|
|
152
|
|
|
{ |
|
153
|
|
|
//do nothing by default |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* creates additional entities after registration using the specification and the given input |
|
158
|
|
|
* @param UserInterface $user the newly registered user |
|
159
|
|
|
* @param array $specification the specification |
|
160
|
|
|
* @param array $input the given request input |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function createAdditionalRegisterEntities(UserInterface $user, array $specification, array $input) |
|
|
|
|
|
|
163
|
|
|
{ |
|
164
|
|
|
//do nothing by default |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Gets the response for a successful login action |
|
169
|
|
|
* @param Request $request the request |
|
170
|
|
|
* @param string $token the login token |
|
171
|
|
|
* @return JsonResponse the response |
|
172
|
|
|
*/ |
|
173
|
|
|
protected function getLoginResponse(Request $request, string $token): JsonResponse |
|
174
|
|
|
{ |
|
175
|
|
|
$user = $request->user(); |
|
176
|
|
|
return response()->json(['id' => $user->getId()], 200, ['jwt-token' => $token]); |
|
|
|
|
|
|
177
|
|
|
} |
|
178
|
|
|
//</editor-fold desc="Protected Methods"> |
|
179
|
|
|
|
|
180
|
|
|
//<editor-fold desc="Private Methods"> |
|
181
|
|
|
/** |
|
182
|
|
|
* Gets the specification for the login credentials |
|
183
|
|
|
* @param Application $app |
|
184
|
|
|
* @return array |
|
185
|
|
|
*/ |
|
186
|
|
|
private function getCredentialSpecification(Application $app) |
|
187
|
|
|
{ |
|
188
|
|
|
/** @var Hasher $hasher */ |
|
189
|
|
|
return [ |
|
190
|
|
|
'email' => ['validation' => 'required|email'], |
|
191
|
|
|
'password' => ['validation' => 'required|string|min:8', |
|
192
|
|
|
'transformer' => function ($value) use ($app) { |
|
193
|
|
|
return $app['hash']->make($value); |
|
194
|
|
|
}] |
|
195
|
|
|
]; |
|
196
|
|
|
} |
|
197
|
|
|
//</editor-fold desc="Private Methods"> |
|
198
|
|
|
} |
|
199
|
|
|
|
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: