Completed
Push — stable9 ( ab68a4...eef0b3 )
by Olivier
10s
created

SharingCheckMiddleware::isSharingEnabled()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 0
crap 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-2016
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 34
	public function __construct(
48
		$appName,
49
		IRequest $request,
50
		IConfig $appConfig,
51
		IControllerMethodReflector $reflector,
52
		IURLGenerator $urlGenerator,
53
		ILogger $logger
54
	) {
55 34
		parent::__construct(
56
			$appName,
57
			$request,
58
			$urlGenerator,
59
			$logger
60
		);
61
62 34
		$this->config = $appConfig;
63 34
		$this->reflector = $reflector;
64 34
	}
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 31
	public function beforeController($controller, $methodName) {
78 31
		if ($this->reflector->hasAnnotation('Guest')) {
79 3
			return;
80
		}
81
82 30
		$sharingEnabled = $this->isSharingEnabled();
83 30
		$isPublicPage = $this->reflector->hasAnnotation('PublicPage');
84 30
		if ($isPublicPage && !$sharingEnabled) {
85 1
			throw new CheckException("'Sharing is disabled'", Http::STATUS_SERVICE_UNAVAILABLE);
86
		}
87 29
	}
88
89
	/**
90
	 * Checks whether public sharing (via links) is enabled
91
	 *
92
	 * @return bool
93
	 */
94 32
	private function isSharingEnabled() {
95 32
		$shareApiAllowLinks = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
96
97 32
		if ($shareApiAllowLinks !== 'yes') {
98 2
			return false;
99
		}
100
101 30
		return true;
102
	}
103
104
}
105