Completed
Push — stable8.2 ( 09e830...ae9bfd )
by Olivier
12:09
created

SharingCheckMiddleware   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 75
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A beforeController() 0 11 4
A isSharingEnabled() 0 9 2
1
<?php
2
/**
3
 * ownCloud - galleryplus
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Olivier Paroz <[email protected]>
10
 *
11
 * @copyright Lukas Reschke 2014-2015
12
 * @copyright Olivier Paroz 2014-2015
13
 */
14
15
namespace OCA\GalleryPlus\Middleware;
16
17
use OCP\IConfig;
18
use OCP\IRequest;
19
use OCP\ILogger;
20
use OCP\IURLGenerator;
21
22
use OCP\AppFramework\Http;
23
use OCP\AppFramework\Utility\IControllerMethodReflector;
24
25
/**
26
 * Checks whether the "sharing check" is enabled
27
 *
28
 * @package OCA\GalleryPlus\SharingCheckMiddleware
29
 */
30
class SharingCheckMiddleware extends CheckMiddleware {
31
32
	/** @var IConfig */
33
	private $config;
34
	/** @var IControllerMethodReflector */
35
	protected $reflector;
36
37
	/***
38
	 * Constructor
39
	 *
40
	 * @param string $appName
41
	 * @param IRequest $request
42
	 * @param IConfig $appConfig
43
	 * @param IControllerMethodReflector $reflector
44
	 * @param IURLGenerator $urlGenerator
45
	 * @param ILogger $logger
46
	 */
47 33
	public function __construct(
48
		$appName,
49
		IRequest $request,
50
		IConfig $appConfig,
51
		IControllerMethodReflector $reflector,
52
		IURLGenerator $urlGenerator,
53
		ILogger $logger
54
	) {
55 33
		parent::__construct(
56
			$appName,
57
			$request,
58
			$urlGenerator,
59
			$logger
60
		);
61
62 33
		$this->config = $appConfig;
63 33
		$this->reflector = $reflector;
64 33
	}
65
66
	/**
67
	 * Checks if sharing is enabled before the controllers is executed
68
	 *
69
	 * Inspects the controller method annotations and if PublicPage is found
70
	 * it makes sure that sharing is enabled in the configuration settings
71
	 *
72
	 * The check is not performed on "guest" pages which don't require sharing
73
	 * to be enabled
74
	 *
75
	 * @inheritDoc
76
	 */
77 30
	public function beforeController($controller, $methodName) {
78 30
		if ($this->reflector->hasAnnotation('Guest')) {
79 3
			return;
80
		}
81
82 29
		$sharingEnabled = $this->isSharingEnabled();
83 29
		$isPublicPage = $this->reflector->hasAnnotation('PublicPage');
84 29
		if ($isPublicPage && !$sharingEnabled) {
85 1
			throw new CheckException("'Sharing is disabled'", Http::STATUS_SERVICE_UNAVAILABLE);
86
		}
87 28
	}
88
89
	/**
90
	 * Checks whether public sharing (via links) is enabled
91
	 *
92
	 * @return bool
93
	 */
94 31
	private function isSharingEnabled() {
95 31
		$shareApiAllowLinks = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
96
97 31
		if ($shareApiAllowLinks !== 'yes') {
98 2
			return false;
99
		}
100
101 29
		return true;
102
	}
103
104
}
105