Skip to content
This repository was archived by the owner on Sep 21, 2018. It is now read-only.

Commit e8c5dd0

Browse files
author
Tuxity
committed
Merge branch 'dev'
2 parents 3ae5de0 + a4660c6 commit e8c5dd0

15 files changed

+99
-66
lines changed

src/Sheaker/Controller/CheckinController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getCheckinsListByUser(Request $request, Application $app, $user_
1414
$token = $app['jwt']->getDecodedToken();
1515

1616
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)){
17-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
17+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 3000);
1818
}
1919

2020
$params = [];
@@ -32,7 +32,7 @@ public function addCheckin(Request $request, Application $app, $user_id)
3232
$token = $app['jwt']->getDecodedToken();
3333

3434
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
35-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
35+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 3001);
3636
}
3737

3838
$checkin = new Checkin();

src/Sheaker/Controller/CheckinsGraphicsController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function getCheckinsFromDate(Request $request, Application $app)
1313
$token = $app['jwt']->getDecodedToken();
1414

1515
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
16-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
16+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4011);
1717
}
1818

1919
$getParams = [];
2020
$getParams['fromDate'] = $app->escape($request->get('from_date'));
2121

2222
foreach ($getParams as $value) {
2323
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
24+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4012);
2525
}
2626
}
2727

src/Sheaker/Controller/CheckinsStatisticsController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function getCheckinsFromDate(Request $request, Application $app)
1313
$token = $app['jwt']->getDecodedToken();
1414

1515
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
16-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
16+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4013);
1717
}
1818

1919
$getParams = [];
2020
$getParams['fromDate'] = $app->escape($request->get('from_date'));
2121

2222
foreach ($getParams as $value) {
2323
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
24+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4014);
2525
}
2626
}
2727

src/Sheaker/Controller/MainController.php

+20-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111

1212
class MainController
1313
{
14+
public function getRoot(Request $request, Application $app)
15+
{
16+
return $app->json(null, Response::HTTP_OK);
17+
}
18+
1419
public function createClient(Request $request, Application $app)
1520
{
1621
$getParams = [];
@@ -21,10 +26,12 @@ public function createClient(Request $request, Application $app)
2126

2227
foreach ($getParams as $value) {
2328
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
29+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 5000);
2530
}
2631
}
2732

33+
//@Todo: check if the subdomain doesn't exists
34+
2835
// create our new client
2936
$client = new Client();
3037
$client->setName($getParams['name']);
@@ -49,17 +56,23 @@ public function createClient(Request $request, Application $app)
4956
// Add user rights
5057
$app['dbs']['sheaker']->query("INSERT INTO users_access VALUES (LAST_INSERT_ID(), 3);");
5158

59+
if ($app['elasticsearch.client']->indices()->exists(['index' => $clientAppName])) {
60+
throw new AppException(Response::HTTP_CONFLICT, 'Already exists', 5002);
61+
}
62+
5263
// create indice ES
5364
self::createElasticIndex($app, $clientAppName);
5465

5566
// create AWS S3 bucket
5667
$s3 = $app['aws']->createS3();
5768

5869
$bucketName = 'sheaker-' . md5($clientAppName);
59-
if (!$s3->doesBucketExist($bucketName)) {
60-
$s3->createBucket(['Bucket' => $bucketName]);
70+
if ($s3->doesBucketExist($bucketName)) {
71+
throw new AppException(Response::HTTP_CONFLICT, 'Already exists', 5003);
6172
}
6273

74+
$s3->createBucket(['Bucket' => $bucketName]);
75+
6376
return $app->json($client, Response::HTTP_CREATED);
6477
}
6578

@@ -70,7 +83,7 @@ public function getClient(Request $request, Application $app)
7083

7184
foreach ($getParams as $value) {
7285
if (!isset($value)) {
73-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
86+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 5004);
7487
}
7588
}
7689

@@ -84,17 +97,13 @@ public function getClient(Request $request, Application $app)
8497

8598
public function indexClient(Request $request, Application $app)
8699
{
87-
$token = $app['jwt']->getDecodedToken();
88-
89-
if (!$app['debug'] && !in_array('admin', $token->user->permissions)) {
90-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
91-
}
92-
93100
$client = $app['client']->getClient();
94101
if (!$app['debug'] && !($client->getFlags() & ClientFlags::INDEX_ELASTICSEARCH)) {
95-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
102+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 5005);
96103
}
97104

105+
// @todo: check if the indexation process is already in progress, by adding a flag INDEX_ELASTICSEARCH_IN_PROGRESS ?
106+
98107
$params['index'] = 'client_' . $app['client.id'];
99108

100109
// First, delete existing index
@@ -195,10 +204,6 @@ private function createElasticIndex($app, $clientIndex)
195204
{
196205
$params['index'] = $clientIndex;
197206

198-
if ($app['elasticsearch.client']->indices()->exists(['index' => $params['index']])) {
199-
$app->abort(Response::HTTP_CONFLICT, 'Already exists');
200-
}
201-
202207
$params['body']['mappings']['user'] = [
203208
'_source' => [
204209
'enabled' => true

src/Sheaker/Controller/PaymentController.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function getPaymentsListByUser(Request $request, Application $app, $user_
1414
$token = $app['jwt']->getDecodedToken();
1515

1616
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
17-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
17+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 2000);
1818
}
1919

2020
$params = [];
@@ -32,12 +32,12 @@ public function getPayment(Request $request, Application $app, $payment_id)
3232
$token = $app['jwt']->getDecodedToken();
3333

3434
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
35-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
35+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 2001);
3636
}
3737

3838
$payment = $app['repository.payment']->find($payment_id);
3939
if (!$payment) {
40-
$app->abort(Response::HTTP_NOT_FOUND, 'Payment not found');
40+
throw new AppException(Response::HTTP_NOT_FOUND, 'Payment not found', 2002);
4141
}
4242

4343
return $app->json($payment, Response::HTTP_OK);
@@ -48,7 +48,7 @@ public function addPayment(Request $request, Application $app, $user_id)
4848
$token = $app['jwt']->getDecodedToken();
4949

5050
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions)) {
51-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
51+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 2003);
5252
}
5353

5454
$addParams = [];
@@ -60,7 +60,7 @@ public function addPayment(Request $request, Application $app, $user_id)
6060

6161
foreach ($addParams as $value) {
6262
if (!isset($value)) {
63-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
63+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 2004);
6464
}
6565
}
6666

src/Sheaker/Controller/PaymentsGraphicsController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function getGains(Request $request, Application $app)
1313
$token = $app['jwt']->getDecodedToken();
1414

1515
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
16-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
16+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4007);
1717
}
1818

1919
$getParams = [];
2020
$getParams['fromDate'] = $app->escape($request->get('from_date'));
2121

2222
foreach ($getParams as $value) {
2323
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
24+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4008);
2525
}
2626
}
2727

src/Sheaker/Controller/PaymentsStatisticsController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function getGainsFromDate(Request $request, Application $app)
1313
$token = $app['jwt']->getDecodedToken();
1414

1515
if (!in_array('admin', $token->user->permissions)) {
16-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
16+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4009);
1717
}
1818

1919
$getParams = [];
2020
$getParams['fromDate'] = $app->escape($request->get('from_date'));
2121

2222
foreach ($getParams as $value) {
2323
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
24+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4010);
2525
}
2626
}
2727

src/Sheaker/Controller/UserController.php

+16-15
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Sheaker\Controller;
44

5-
use Sheaker\Entity\User;
65
use Silex\Application;
76
use Symfony\Component\HttpFoundation\Request;
87
use Symfony\Component\HttpFoundation\Response;
8+
use Sheaker\Entity\User;
9+
use Sheaker\Exception\AppException;
910

1011
class UserController
1112
{
@@ -17,15 +18,15 @@ public function login(Request $request, Application $app)
1718

1819
foreach ($loginParams as $value) {
1920
if (!isset($value)) {
20-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
21+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 1000);
2122
}
2223
}
2324

2425
$loginParams['rememberMe'] = $app->escape($request->get('rememberMe'));
2526

2627
$user = $app['repository.user']->find($loginParams['id']);
2728
if (!$user) {
28-
$app->abort(Response::HTTP_NOT_FOUND, 'User not found');
29+
throw new AppException(Response::HTTP_UNAUTHORIZED, 'Id or password invalid', 1001);
2930
}
3031

3132
if (password_verify($loginParams['password'], $user->getPassword())) {
@@ -50,7 +51,7 @@ public function login(Request $request, Application $app)
5051
else {
5152
$user->setFailedLogins($user->getFailedLogins() + 1);
5253
$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);
5455
}
5556

5657
return $app->json(['token' => $token], Response::HTTP_OK);
@@ -61,7 +62,7 @@ public function getUsersList(Request $request, Application $app)
6162
$token = $app['jwt']->getDecodedToken();
6263

6364
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);
6566
}
6667

6768
$getParams = [];
@@ -119,7 +120,7 @@ public function searchUsers(Request $request, Application $app)
119120
$token = $app['jwt']->getDecodedToken();
120121

121122
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);
123124
}
124125

125126
$getParams = [];
@@ -167,7 +168,7 @@ public function getUser(Request $request, Application $app, $user_id)
167168
$token = $app['jwt']->getDecodedToken();
168169

169170
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);
171172
}
172173

173174
$params = [];
@@ -189,7 +190,7 @@ public function getUser(Request $request, Application $app, $user_id)
189190
$queryResponse = $app['elasticsearch.client']->search($params);
190191

191192
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);
193194
}
194195

195196
// There should have only 1 user, no need to iterate
@@ -215,7 +216,7 @@ public function addUser(Request $request, Application $app)
215216
$token = $app['jwt']->getDecodedToken();
216217

217218
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);
219220
}
220221

221222
$addParams = [];
@@ -224,7 +225,7 @@ public function addUser(Request $request, Application $app)
224225

225226
foreach ($addParams as $value) {
226227
if (!isset($value)) {
227-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
228+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 1008);
228229
}
229230
}
230231

@@ -327,7 +328,7 @@ public function editUser(Request $request, Application $app, $user_id)
327328
$token = $app['jwt']->getDecodedToken();
328329

329330
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);
331332
}
332333

333334
$editParams = [];
@@ -336,7 +337,7 @@ public function editUser(Request $request, Application $app, $user_id)
336337

337338
foreach ($editParams as $value) {
338339
if (!isset($value)) {
339-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
340+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 1010);
340341
}
341342
}
342343

@@ -355,7 +356,7 @@ public function editUser(Request $request, Application $app, $user_id)
355356

356357
$user = $app['repository.user']->find($user_id);
357358
if (!$user) {
358-
$app->abort(Response::HTTP_NOT_FOUND, 'User not found');
359+
throw new AppException(Response::HTTP_NOT_FOUND, 'User not found', 1011);
359360
}
360361

361362
$photoURL = '';
@@ -440,12 +441,12 @@ public function deleteUser(Request $request, Application $app, $user_id)
440441
$token = $app['jwt']->getDecodedToken();
441442

442443
if (!in_array('admin', $token->user->permissions)) {
443-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
444+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 1012);
444445
}
445446

446447
$user = $app['repository.user']->find($user_id);
447448
if (!$user) {
448-
$app->abort(Response::HTTP_NOT_FOUND, 'User not found');
449+
throw new AppException(Response::HTTP_NOT_FOUND, 'User not found', 1013);
449450
}
450451

451452
$user->setDeletedAt(date('Y-m-d H:i:s'));

src/Sheaker/Controller/UsersGraphicsController.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public function getNewUsersFromDate(Request $request, Application $app)
1313
$token = $app['jwt']->getDecodedToken();
1414

1515
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
16-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
16+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4000);
1717
}
1818

1919
$getParams = [];
2020
$getParams['fromDate'] = $app->escape($request->get('from_date'));
2121

2222
foreach ($getParams as $value) {
2323
if (!isset($value)) {
24-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
24+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4001);
2525
}
2626
}
2727

@@ -86,15 +86,15 @@ public function getGenderRepartition(Request $request, Application $app)
8686
$token = $app['jwt']->getDecodedToken();
8787

8888
if (!in_array('admin', $token->user->permissions) && !in_array('modo', $token->user->permissions) && !in_array('user', $token->user->permissions)) {
89-
$app->abort(Response::HTTP_FORBIDDEN, 'Forbidden');
89+
throw new AppException(Response::HTTP_FORBIDDEN, 'Forbidden', 4002);
9090
}
9191

9292
$getParams = [];
9393
$getParams['fromDate'] = $app->escape($request->get('from_date'));
9494

9595
foreach ($getParams as $value) {
9696
if (!isset($value)) {
97-
$app->abort(Response::HTTP_BAD_REQUEST, 'Missing parameters');
97+
throw new AppException(Response::HTTP_BAD_REQUEST, 'Missing parameters', 4003);
9898
}
9999
}
100100

0 commit comments

Comments
 (0)