|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Gewaer\Middleware; |
|
6
|
|
|
|
|
7
|
|
|
use Phalcon\Mvc\Micro; |
|
8
|
|
|
use Phalcon\Mvc\Micro\MiddlewareInterface; |
|
9
|
|
|
use Gewaer\Exception\ServerErrorHttpException; |
|
10
|
|
|
use Gewaer\Exception\PermissionException; |
|
11
|
|
|
use Gewaer\Models\CompaniesSettings; |
|
12
|
|
|
use Phalcon\Di; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AclMiddleware |
|
16
|
|
|
* |
|
17
|
|
|
* @package Gewaer\Middleware |
|
18
|
|
|
*/ |
|
19
|
|
|
class AclMiddleware implements MiddlewareInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Call me |
|
23
|
|
|
* |
|
24
|
|
|
* @param Micro $api |
|
25
|
|
|
* @todo need to check section for auth here |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
69 |
|
public function call(Micro $api) |
|
29
|
|
|
{ |
|
30
|
69 |
|
$auth = $api->getService('auth'); |
|
31
|
69 |
|
$router = $api->getService('router'); |
|
32
|
69 |
|
$request = $api->getService('request'); |
|
33
|
|
|
|
|
34
|
69 |
|
if (!$auth->isIgnoreUri()) { |
|
35
|
|
|
// explode() by / , postiion #1 is always the controller , so its the resource ^.^ |
|
36
|
|
|
$matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern()); |
|
37
|
|
|
$resource = ucfirst($matchRouter[2]); //2 is alwasy the controller of the router |
|
38
|
|
|
$userData = $api->getService('userData'); |
|
39
|
|
|
|
|
40
|
|
|
$action = null; |
|
41
|
|
|
// GET -> read |
|
42
|
|
|
// PUT -> update |
|
43
|
|
|
// DELETE -> delete |
|
44
|
|
|
// POST -> create |
|
45
|
|
|
|
|
46
|
|
|
//Search current company's app setting with key paid to verify payment status for current company |
|
47
|
|
|
$subscriptionPaid = CompaniesSettings::findFirst([ |
|
48
|
|
|
'conditions' => "companies_id = ?0 and name = 'paid' and is_deleted = 0", |
|
49
|
|
|
'bind' => [Di::getDefault()->getUserData()->default_company] |
|
50
|
|
|
]); |
|
51
|
|
|
|
|
52
|
|
|
if (!$subscriptionPaid->value) { |
|
53
|
|
|
throw new ServerErrorHttpException('Subscription is not active.Please contact your admin'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
switch (strtolower($request->getMethod())) { |
|
57
|
|
|
case 'get': |
|
58
|
|
|
$action = 'list'; |
|
59
|
|
|
if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) { |
|
60
|
|
|
$action = 'read'; |
|
61
|
|
|
} |
|
62
|
|
|
break; |
|
63
|
|
|
case 'post': |
|
64
|
|
|
$action = 'create'; |
|
65
|
|
|
break; |
|
66
|
|
|
case 'delete': |
|
67
|
|
|
$action = 'delete'; |
|
68
|
|
|
break; |
|
69
|
|
|
case 'put': |
|
70
|
|
|
case 'patch': |
|
71
|
|
|
$action = 'update'; |
|
72
|
|
|
break; |
|
73
|
|
|
default: |
|
74
|
|
|
throw new ServerErrorHttpException('No Permission define for this action'); |
|
75
|
|
|
break; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
//do you have permision |
|
79
|
|
|
if (!$userData->can($resource . '.' . $action)) { |
|
80
|
|
|
throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
69 |
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|