2
2
3
3
namespace Sheaker \Controller ;
4
4
5
- use Sheaker \Entity \User ;
6
5
use Silex \Application ;
7
6
use Symfony \Component \HttpFoundation \Request ;
8
7
use Symfony \Component \HttpFoundation \Response ;
8
+ use Sheaker \Entity \User ;
9
+ use Sheaker \Exception \AppException ;
9
10
10
11
class UserController
11
12
{
@@ -17,15 +18,15 @@ public function login(Request $request, Application $app)
17
18
18
19
foreach ($ loginParams as $ value ) {
19
20
if (!isset ($ value )) {
20
- $ app -> abort (Response::HTTP_BAD_REQUEST , 'Missing parameters ' );
21
+ throw new AppException (Response::HTTP_BAD_REQUEST , 'Missing parameters ' , 1000 );
21
22
}
22
23
}
23
24
24
25
$ loginParams ['rememberMe ' ] = $ app ->escape ($ request ->get ('rememberMe ' ));
25
26
26
27
$ user = $ app ['repository.user ' ]->find ($ loginParams ['id ' ]);
27
28
if (!$ user ) {
28
- $ app -> abort (Response::HTTP_NOT_FOUND , 'User not found ' );
29
+ throw new AppException (Response::HTTP_UNAUTHORIZED , 'Id or password invalid ' , 1001 );
29
30
}
30
31
31
32
if (password_verify ($ loginParams ['password ' ], $ user ->getPassword ())) {
@@ -50,7 +51,7 @@ public function login(Request $request, Application $app)
50
51
else {
51
52
$ user ->setFailedLogins ($ user ->getFailedLogins () + 1 );
52
53
$ app ['repository.user ' ]->save ($ user );
53
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Wrong password' );
54
+ throw new AppException (Response::HTTP_UNAUTHORIZED , 'Id or password invalid ' , 1002 );
54
55
}
55
56
56
57
return $ app ->json (['token ' => $ token ], Response::HTTP_OK );
@@ -61,7 +62,7 @@ public function getUsersList(Request $request, Application $app)
61
62
$ token = $ app ['jwt ' ]->getDecodedToken ();
62
63
63
64
if (!in_array ('admin ' , $ token ->user ->permissions ) && !in_array ('modo ' , $ token ->user ->permissions ) && !in_array ('user ' , $ token ->user ->permissions )) {
64
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
65
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1003 );
65
66
}
66
67
67
68
$ getParams = [];
@@ -119,7 +120,7 @@ public function searchUsers(Request $request, Application $app)
119
120
$ token = $ app ['jwt ' ]->getDecodedToken ();
120
121
121
122
if (!in_array ('admin ' , $ token ->user ->permissions ) && !in_array ('modo ' , $ token ->user ->permissions ) && !in_array ('user ' , $ token ->user ->permissions )) {
122
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
123
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1004 );
123
124
}
124
125
125
126
$ getParams = [];
@@ -167,7 +168,7 @@ public function getUser(Request $request, Application $app, $user_id)
167
168
$ token = $ app ['jwt ' ]->getDecodedToken ();
168
169
169
170
if (!in_array ('admin ' , $ token ->user ->permissions ) && !in_array ('modo ' , $ token ->user ->permissions ) && !in_array ('user ' , $ token ->user ->permissions )) {
170
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
171
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1005 );
171
172
}
172
173
173
174
$ params = [];
@@ -189,7 +190,7 @@ public function getUser(Request $request, Application $app, $user_id)
189
190
$ queryResponse = $ app ['elasticsearch.client ' ]->search ($ params );
190
191
191
192
if ($ queryResponse ['hits ' ]['total ' ] === 0 ) {
192
- $ app -> abort (Response::HTTP_NOT_FOUND , 'User not found ' );
193
+ throw new AppException (Response::HTTP_NOT_FOUND , 'User not found ' , 1006 );
193
194
}
194
195
195
196
// There should have only 1 user, no need to iterate
@@ -215,7 +216,7 @@ public function addUser(Request $request, Application $app)
215
216
$ token = $ app ['jwt ' ]->getDecodedToken ();
216
217
217
218
if (!in_array ('admin ' , $ token ->user ->permissions ) && !in_array ('modo ' , $ token ->user ->permissions )) {
218
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
219
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1007 );
219
220
}
220
221
221
222
$ addParams = [];
@@ -224,7 +225,7 @@ public function addUser(Request $request, Application $app)
224
225
225
226
foreach ($ addParams as $ value ) {
226
227
if (!isset ($ value )) {
227
- $ app -> abort (Response::HTTP_BAD_REQUEST , 'Missing parameters ' );
228
+ throw new AppException (Response::HTTP_BAD_REQUEST , 'Missing parameters ' , 1008 );
228
229
}
229
230
}
230
231
@@ -327,7 +328,7 @@ public function editUser(Request $request, Application $app, $user_id)
327
328
$ token = $ app ['jwt ' ]->getDecodedToken ();
328
329
329
330
if (!in_array ('admin ' , $ token ->user ->permissions ) && !in_array ('modo ' , $ token ->user ->permissions )) {
330
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
331
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1009 );
331
332
}
332
333
333
334
$ editParams = [];
@@ -336,7 +337,7 @@ public function editUser(Request $request, Application $app, $user_id)
336
337
337
338
foreach ($ editParams as $ value ) {
338
339
if (!isset ($ value )) {
339
- $ app -> abort (Response::HTTP_BAD_REQUEST , 'Missing parameters ' );
340
+ throw new AppException (Response::HTTP_BAD_REQUEST , 'Missing parameters ' , 1010 );
340
341
}
341
342
}
342
343
@@ -355,7 +356,7 @@ public function editUser(Request $request, Application $app, $user_id)
355
356
356
357
$ user = $ app ['repository.user ' ]->find ($ user_id );
357
358
if (!$ user ) {
358
- $ app -> abort (Response::HTTP_NOT_FOUND , 'User not found ' );
359
+ throw new AppException (Response::HTTP_NOT_FOUND , 'User not found ' , 1011 );
359
360
}
360
361
361
362
$ photoURL = '' ;
@@ -440,12 +441,12 @@ public function deleteUser(Request $request, Application $app, $user_id)
440
441
$ token = $ app ['jwt ' ]->getDecodedToken ();
441
442
442
443
if (!in_array ('admin ' , $ token ->user ->permissions )) {
443
- $ app -> abort (Response::HTTP_FORBIDDEN , 'Forbidden ' );
444
+ throw new AppException (Response::HTTP_FORBIDDEN , 'Forbidden ' , 1012 );
444
445
}
445
446
446
447
$ user = $ app ['repository.user ' ]->find ($ user_id );
447
448
if (!$ user ) {
448
- $ app -> abort (Response::HTTP_NOT_FOUND , 'User not found ' );
449
+ throw new AppException (Response::HTTP_NOT_FOUND , 'User not found ' , 1013 );
449
450
}
450
451
451
452
$ user ->setDeletedAt (date ('Y-m-d H:i:s ' ));
0 commit comments