Completed
Pull Request — master (#163)
by Sander
02:41
created

ShareMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B beforeController() 0 23 6
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 = Http::STATUS_OK;
23
			$result = array();
24
			$publicMethods = array('createPublicShare', 'getPublicCredentialData');
25
			$user_pub_methods = array('updateSharedCredentialACL', 'getFile', 'getItemAcl');
26
			$setting = (in_array($methodName, $publicMethods)) ? 'link_sharing_enabled' : 'user_sharing_enabled';
27
			$sharing_enabled = ($this->settings->isEnabled($setting));
28
29
			if(in_array($methodName, $user_pub_methods)){
30
				$sharing_enabled = ($this->settings->isEnabled('link_sharing_enabled') || $this->settings->isEnabled('user_sharing_enabled'));
31
			}
32
33
34
			if (!$sharing_enabled) {
35
				$response = new JSONResponse($result);
36
				http_response_code($http_response_code);
37
				header('Passman-sharing: disabled');
38
				header('Passman-method: ShareController.' . $methodName);
39
				die($response->render());
40
			}
41
		}
42
	}
43
}
44
45
46