Completed
Pull Request — master (#155)
by Sander
02:24
created

ShareMiddleware::beforeController()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 27
rs 5.3846
cc 8
eloc 19
nc 13
nop 2
1
<?php
2
3
namespace OCA\Passman\Middleware;
4
5
use OCA\Passman\Controller\ShareController;
6
use OCA\Passman\Service\SettingsService;
7
use OCP\AppFramework\Http\JSONResponse;
8
use \OCP\AppFramework\Middleware;
9
use OCP\AppFramework\Http;
10
11
class ShareMiddleware extends Middleware {
12
13
	private $settings;
14
15
	public function __construct(SettingsService $config) {
16
		$this->settings = $config;
17
	}
18
19
20
	public function beforeController($controller, $methodName) {
21
		if ($controller instanceof ShareController) {
22
			$http_response_code = \OCP\AppFramework\Http::STATUS_FORBIDDEN;
23
			$result = 'FORBIDDEN';
24
25
			if (in_array($methodName, array('updateSharedCredentialACL', 'getFile', 'getItemAcl'))) {
26
				$sharing_enabled = ($this->settings->isEnabled('link_sharing_enabled') || $this->settings->isEnabled('user_sharing_enabled'));
27
			} else {
28
				$publicMethods = array('createPublicShare', 'getPublicCredentialData');
29
				$setting = (in_array($methodName, $publicMethods)) ? 'link_sharing_enabled' : 'user_sharing_enabled';
30
				$sharing_enabled = ($this->settings->isEnabled($setting));
31
				if ($methodName === 'getVaultItems' || $methodName === 'getPendingRequests') {
32
					$http_response_code = Http::STATUS_OK;
33
					$result = array();
34
				}
35
			}
36
37
38
			if (!$sharing_enabled) {
39
				$response = new JSONResponse($result);
40
				http_response_code($http_response_code);
41
				header('Passman-sharing: disabled');
42
				header('Passman-method: ShareController.' . $methodName);
43
				die($response->render());
44
			}
45
		}
46
	}
47
}
48
49
50