|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OCA\Provisioning_API\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use OCA\Provisioning_API\Middleware\Exceptions\NotSubAdminException; |
|
6
|
|
|
use OCP\AppFramework\Http\Response; |
|
7
|
|
|
use OCP\AppFramework\Middleware; |
|
8
|
|
|
use OCP\AppFramework\OCS\OCSException; |
|
9
|
|
|
use OCP\AppFramework\Utility\IControllerMethodReflector; |
|
10
|
|
|
|
|
11
|
|
|
class ProvisioningApiMiddleware extends Middleware { |
|
12
|
|
|
|
|
13
|
|
|
/** @var IControllerMethodReflector */ |
|
14
|
|
|
private $reflector; |
|
15
|
|
|
|
|
16
|
|
|
/** @var bool */ |
|
17
|
|
|
private $isAdmin; |
|
18
|
|
|
|
|
19
|
|
|
/** @var bool */ |
|
20
|
|
|
private $isSubAdmin; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ProvisioningApiMiddleware constructor. |
|
24
|
|
|
* |
|
25
|
|
|
* @param IControllerMethodReflector $reflector |
|
26
|
|
|
* @param bool $isAdmin |
|
27
|
|
|
* @param bool $isSubAdmin |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct( |
|
30
|
|
|
IControllerMethodReflector $reflector, |
|
31
|
|
|
$isAdmin, |
|
32
|
|
|
$isSubAdmin) { |
|
33
|
|
|
$this->reflector = $reflector; |
|
34
|
|
|
$this->isAdmin = $isAdmin; |
|
35
|
|
|
$this->isSubAdmin = $isSubAdmin; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param \OCP\AppFramework\Controller $controller |
|
40
|
|
|
* @param string $methodName |
|
41
|
|
|
* |
|
42
|
|
|
* @throws NotSubAdminException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function beforeController($controller, $methodName) { |
|
45
|
|
|
if (!$this->isAdmin && !$this->reflector->hasAnnotation('NoSubAdminRequired') && !$this->isSubAdmin) { |
|
46
|
|
|
throw new NotSubAdminException(); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \OCP\AppFramework\Controller $controller |
|
52
|
|
|
* @param string $methodName |
|
53
|
|
|
* @param \Exception $exception |
|
54
|
|
|
* @throws \Exception |
|
55
|
|
|
* @return Response |
|
56
|
|
|
*/ |
|
57
|
|
|
public function afterException($controller, $methodName, \Exception $exception) { |
|
58
|
|
|
if ($exception instanceof NotSubAdminException) { |
|
59
|
|
|
throw new OCSException($exception->getMessage(), \OCP\API::RESPOND_UNAUTHORISED); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
throw $exception; |
|
63
|
|
|
} |
|
64
|
|
|
} |