Completed
Push — master ( def5c2...24411c )
by
unknown
33:44 queued 20:37
created

Capabilities   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 5.51 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 7
loc 127
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A isAdmin() 7 7 2
C getCapabilities() 0 87 10

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * @author Roeland Jago Douma <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
namespace OCA\Files_Sharing;
22
23
use OCP\Capabilities\ICapability;
24
use OCP\IConfig;
25
use OCP\Util\UserSearch;
26
27
/**
28
 * Class Capabilities
29
 *
30
 * @package OCA\Files_Sharing
31
 */
32
class Capabilities implements ICapability {
33
34
	/** @var IConfig */
35
	private $config;
36
37
	/**
38
	 * @var UserSearch
39
	 */
40
	private $userSearch;
41
42
	/**
43
	 * Capabilities constructor.
44
	 *
45
	 * @param IConfig $config
46
	 * @param UserSearch $userSearch
47
	 */
48
	public function __construct(IConfig $config, UserSearch $userSearch) {
49
		$this->config = $config;
50
		$this->userSearch = $userSearch;
51
	}
52
53
	/**
54
	 * Returns whether the currently logged in user is an administrator
55
	 *
56
	 * @return bool true if the user is an admin
57
	 */
58 View Code Duplication
	private function isAdmin() {
59
		$user = $this->userSession->getUser();
0 ignored issues
show
Bug introduced by
The property userSession does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
60
		if ($user !== null) {
61
			return $this->groupManager->isAdmin($user->getUID());
0 ignored issues
show
Bug introduced by
The property groupManager does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
62
		}
63
		return false;
64
	}
65
66
	/**
67
	 * Return this classes capabilities
68
	 *
69
	 * @return array
70
	 */
71
	public function getCapabilities() {
72
		$res = [];
73
74
		if ($this->config->getAppValue('core', 'shareapi_enabled', 'yes') !== 'yes') {
75
			$res['api_enabled'] = false;
76
			$res['public'] = ['enabled' => false];
77
			$res['user'] = ['send_mail' => false];
78
			$res['resharing'] = false;
79
			$res['can_share'] = false;
80
		} else {
81
			$res['api_enabled'] = true;
82
83
			$public = [];
84
			$public['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes';
85
			if ($public['enabled']) {
86
				$public['password'] = [];
87
				$public['password']['enforced_for'] = [];
88
				$roPasswordEnforced = $this->config->getAppValue('core', 'shareapi_enforce_links_password_read_only', 'no') === 'yes';
89
				$rwPasswordEnforced = $this->config->getAppValue('core', 'shareapi_enforce_links_password_read_write', 'no') === 'yes';
90
				$woPasswordEnforced = $this->config->getAppValue('core', 'shareapi_enforce_links_password_write_only', 'no') === 'yes';
91
				$public['password']['enforced_for']['read_only'] = $roPasswordEnforced;
92
				$public['password']['enforced_for']['read_write'] = $rwPasswordEnforced;
93
				$public['password']['enforced_for']['upload_only'] = $woPasswordEnforced;
94
				$public['password']['enforced'] = $roPasswordEnforced || $rwPasswordEnforced || $woPasswordEnforced;
95
96
				$public['expire_date'] = [];
97
				$public['expire_date']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes';
98
				if ($public['expire_date']['enabled']) {
99
					$public['expire_date']['days'] = $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7');
100
					$public['expire_date']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
101
				}
102
103
				$public['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no') === 'yes';
104
				$public['social_share'] = $this->config->getAppValue('core', 'shareapi_allow_social_share', 'yes') === 'yes';
105
				$public['upload'] = $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
106
				$public['multiple'] = true;
107
				$public['supports_upload_only'] = true;
108
			}
109
			$res["public"] = $public;
110
111
			$res['user']['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no') === 'yes';
112
113
			$res['resharing'] = $this->config->getAppValue('core', 'shareapi_allow_resharing', 'yes') === 'yes';
114
115
			$res['group_sharing'] = $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes';
116
117
			$res['auto_accept_share'] = $this->config->getAppValue('core', 'shareapi_auto_accept_share', 'yes') === 'yes';
118
119
			$res['share_with_group_members_only'] = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'yes') === 'yes';
120
			$res['share_with_membership_groups_only'] = $this->config->getAppValue('core', 'shareapi_only_share_with_membership_groups', 'yes') === 'yes';
121
122
			if (\OCP\Util::isSharingDisabledForUser()) {
123
				$res['can_share'] = false;
124
			} else {
125
				$res['can_share'] = true;
126
			}
127
128
			if ($this->isAdmin()) {
129
				$res['exclude_groups_from_sharing'] = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'yes') === 'yes';
130
131
				if ($res['exclude_groups_from_sharing']) {
132
					$res['groups_excluded_from_sharing'] = \json_decode($this->config->getAppValue('core', 'shareapi_exclude_groups_list', '[]'), true);
133
				}
134
			}
135
136
			$user_enumeration = [];
137
			$user_enumeration['enabled'] = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
138
			if ($user_enumeration['enabled']) {
139
				$user_enumeration['group_members_only'] = $this->config->getAppValue('core', 'shareapi_share_dialog_user_enumeration_group_members', 'no') === 'yes';
140
			}
141
			$res["user_enumeration"] = $user_enumeration;
142
143
			$res['default_permissions'] = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', \OCP\Constants::PERMISSION_ALL);
144
		}
145
146
		//Federated sharing
147
		$res['federation'] = [
148
			'outgoing'  => $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes',
149
			'incoming' => $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'yes'
150
		];
151
152
		$res['search_min_length'] = $this->userSearch->getSearchMinLength();
153
154
		return [
155
			'files_sharing' => $res,
156
		];
157
	}
158
}
159