bakaphp /
phalcon-api
| 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\Subscription; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class AclMiddleware. |
||
| 15 | * |
||
| 16 | * @package Gewaer\Middleware |
||
| 17 | */ |
||
| 18 | class AclMiddleware extends TokenBase |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * Call me. |
||
| 22 | * |
||
| 23 | * @param Micro $api |
||
| 24 | * @todo need to check section for auth here |
||
| 25 | * @return bool |
||
| 26 | */ |
||
| 27 | 69 | public function call(Micro $api) |
|
| 28 | { |
||
| 29 | 69 | $router = $api->getService('router'); |
|
| 30 | 69 | $request = $api->getService('request'); |
|
| 31 | |||
| 32 | 69 | if ($this->isValidCheck($request, $api)) { |
|
| 33 | // explode() by / , postiion #1 is always the controller , so its the resource ^.^ |
||
| 34 | 61 | $matchRouter = explode('/', $router->getMatchedRoute()->getCompiledPattern()); |
|
| 35 | |||
| 36 | 61 | $resource = ucfirst(isset($matchRouter[2]) ? $matchRouter[2] : $matchRouter[1]); //2 is alwasy the controller of the router |
|
| 37 | 61 | $userData = $api->getService('userData'); |
|
| 38 | |||
| 39 | 61 | $action = null; |
|
| 40 | // GET -> read |
||
| 41 | // PUT -> update |
||
| 42 | // DELETE -> delete |
||
| 43 | // POST -> create |
||
| 44 | |||
| 45 | 61 | if (!Subscription::getPaymentStatus()) { |
|
| 46 | throw new ServerErrorHttpException('Subscription is not active.Please contact your admin'); |
||
| 47 | } |
||
| 48 | |||
| 49 | 61 | switch (strtolower($request->getMethod())) { |
|
| 50 | 61 | case 'get': |
|
| 51 | 38 | $action = 'list'; |
|
| 52 | 38 | if (preg_match("/\/([0-9]+)(?=[^\/]*$)/", $request->getURI())) { |
|
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 53 | 7 | $action = 'read'; |
|
| 54 | } |
||
| 55 | 38 | break; |
|
| 56 | 43 | case 'post': |
|
| 57 | 21 | $action = 'create'; |
|
| 58 | 21 | break; |
|
| 59 | 22 | case 'delete': |
|
| 60 | 8 | $action = 'delete'; |
|
| 61 | 8 | break; |
|
| 62 | 14 | case 'put': |
|
| 63 | case 'patch': |
||
| 64 | 14 | $action = 'update'; |
|
| 65 | 14 | break; |
|
| 66 | default: |
||
| 67 | throw new ServerErrorHttpException('No Permission define for this action'); |
||
| 68 | break; |
||
| 69 | } |
||
| 70 | //do you have permision |
||
| 71 | 61 | if (!$userData->can($resource . '.' . $action)) { |
|
| 72 | throw new PermissionException('You dont have permission to run this action ' . $action . ' at ' . $resource); |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | 69 | return true; |
|
| 77 | } |
||
| 78 | } |
||
| 79 |