Completed
Push — stable9 ( 485cb1...e094cf )
by Lukas
26:41 queued 26:23
created

SharingCheckMiddleware::externalSharesChecks()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 0
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Lukas Reschke <[email protected]>
6
 * @author Morris Jobke <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 * @author Thomas Müller <[email protected]>
9
 *
10
 * @license AGPL-3.0
11
 *
12
 * This code is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License, version 3,
14
 * as published by the Free Software Foundation.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License, version 3,
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
23
 *
24
 */
25
26
namespace OCA\Files_Sharing\Middleware;
27
28
use OCP\App\IAppManager;
29
use OCP\AppFramework\Http\NotFoundResponse;
30
use OCP\AppFramework\Middleware;
31
use OCP\Files\NotFoundException;
32
use OCP\IConfig;
33
use OCP\AppFramework\Utility\IControllerMethodReflector;
34
use OCA\Files_Sharing\Exceptions\S2SException;
35
use OCP\AppFramework\Http\JSONResponse;
36
37
/**
38
 * Checks whether the "sharing check" is enabled
39
 *
40
 * @package OCA\Files_Sharing\Middleware
41
 */
42
class SharingCheckMiddleware extends Middleware {
43
44
	/** @var string */
45
	protected $appName;
46
	/** @var IConfig */
47
	protected $config;
48
	/** @var IAppManager */
49
	protected $appManager;
50
	/** @var IControllerMethodReflector */
51
	protected $reflector;
52
53
	/***
54
	 * @param string $appName
55
	 * @param IConfig $config
56
	 * @param IAppManager $appManager
57
	 */
58
	public function __construct($appName,
59
								IConfig $config,
60
								IAppManager $appManager,
61
								IControllerMethodReflector $reflector
62
								) {
63
		$this->appName = $appName;
64
		$this->config = $config;
65
		$this->appManager = $appManager;
66
		$this->reflector = $reflector;
67
	}
68
69
	/**
70
	 * Check if sharing is enabled before the controllers is executed
71
	 *
72
	 * @param \OCP\AppFramework\Controller $controller
73
	 * @param string $methodName
74
	 * @throws NotFoundException
75
	 */
76
	public function beforeController($controller, $methodName) {
77
		if(!$this->isSharingEnabled()) {
78
			throw new NotFoundException('Sharing is disabled.');
79
		}
80
81
		if ($controller instanceof \OCA\Files_Sharing\Controllers\ExternalSharesController &&
82
			!$this->externalSharesChecks()) {
83
			throw new S2SException('Federated sharing not allowed');
84
		} else if ($controller instanceof \OCA\Files_Sharing\Controllers\ShareController &&
85
			!$this->isLinkSharingEnabled()) {
86
			throw new NotFoundException('Link sharing is disabled');
87
		}
88
	}
89
90
	/**
91
	 * Return 404 page in case of a not found exception
92
	 *
93
	 * @param \OCP\AppFramework\Controller $controller
94
	 * @param string $methodName
95
	 * @param \Exception $exception
96
	 * @return NotFoundResponse
97
	 * @throws \Exception
98
	 */
99
	public function afterException($controller, $methodName, \Exception $exception) {
100
		if(is_a($exception, '\OCP\Files\NotFoundException')) {
101
			return new NotFoundResponse();
102
		}
103
104
		if (is_a($exception, '\OCA\Files_Sharing\Exceptions\S2SException')) {
105
			return new JSONResponse($exception->getMessage(), 405);
0 ignored issues
show
Documentation introduced by
$exception->getMessage() is of type string, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug Best Practice introduced by
The return type of return new \OCP\AppFrame...on->getMessage(), 405); (OCP\AppFramework\Http\JSONResponse) is incompatible with the return type documented by OCA\Files_Sharing\Middle...dleware::afterException of type OCP\AppFramework\Http\NotFoundResponse.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
106
		}
107
108
		throw $exception;
109
	}
110
111
	/**
112
	 * Checks for externalshares controller
113
	 * @return bool
114
	 */
115
	private function externalSharesChecks() {
116
117
		if (!$this->reflector->hasAnnotation('NoIncomingFederatedSharingRequired') &&
118
			$this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') !== 'yes') {
119
			return false;
120
		}
121
122
		if (!$this->reflector->hasAnnotation('NoOutgoingFederatedSharingRequired') &&
123
		    $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') !== 'yes') {
124
			return false;
125
		}
126
127
		return true;
128
	}
129
130
	/**
131
	 * Check whether sharing is enabled
132
	 * @return bool
133
	 */
134
	private function isSharingEnabled() {
135
		// FIXME: This check is done here since the route is globally defined and not inside the files_sharing app
136
		// Check whether the sharing application is enabled
137
		if(!$this->appManager->isEnabledForUser($this->appName)) {
138
			return false;
139
		}
140
141
		return true;
142
	}
143
144
	/**
145
	 * Check if link sharing is allowed
146
	 * @return bool
147
	 */
148
	private function isLinkSharingEnabled() {
149
		// Check if the shareAPI is enabled
150
		if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
151
			return false;
152
		}
153
154
		// Check whether public sharing is enabled
155
		if($this->config->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') {
156
			return false;
157
		}
158
159
		return true;
160
	}
161
162
}
163