Completed
Push — master ( 9debcc...2df5b9 )
by
unknown
16:34
created

SharingBlacklist::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Juan Pablo Villafáñez <[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
22
namespace OCA\Files_Sharing;
23
24
use OCP\IConfig;
25
use OCP\IGroup;
26
27
/**
28
 * Class to handle a blacklist for sharing. The main functionality is to check if a particular group
29
 * has been blacklisted for sharing, which means that noone should share with that group.
30
 *
31
 * Note that this class will only handle the configuration and perform the checks against the configuration
32
 * This class won't prevent the sharing action by itself.
33
 */
34
class SharingBlacklist {
35
	/** @var IConfig */
36
	private $config;
37
38
	private $blacklistCache = null;
39
40
	public function __construct(IConfig $config) {
41
		$this->config = $config;
42
	}
43
44
	/**
45
	 * Check if the target group is blacklisted
46
	 * @param IGroup $group the group to check
47
	 * @return bool true if the group is blacklisted, false otherwise
48
	 */
49
	public function isGroupBlacklisted(IGroup $group) {
50
		$this->initCache();
51
52
		$groupId = $group->getGID();
53
54
		if (isset($this->blacklistCache['receivers']['ids'][$groupId])) {
55
			return true;
56
		}
57
		return false;
58
	}
59
60
	/**
61
	 * Clear the internal cache of this class. Use this function if any of the keys used by this class is changed
62
	 * outside of this class, such as a direct change of the 'blacklisted_group_displaynames' in the appconfig table
63
	 * Note that this is an object-based cache. It won't persist for multiple HTTP requests
64
	 */
65
	public function clearCache() {
66
		$this->blacklistCache = null;
67
	}
68
69
	/**
70
	 * Set the list of groups to be blacklisted by id.
71
	 * @param string[] $ids a list with the ids of the groups to be blacklisted
72
	 */
73
	public function setBlacklistedReceiverGroups(array $ids) {
74
		$this->config->setAppValue('files_sharing', 'blacklisted_receiver_groups', \json_encode($ids));
75
		$this->blacklistCache = null;  // clear the cache
76
	}
77
78
	/**
79
	 * Get the list of blacklisted group ids
80
	 * Note that this might contain wrong information
81
	 * @return string[] the list of group ids
82
	 */
83
	public function getBlacklistedReceiverGroups() {
84
		return \json_decode($this->config->getAppValue('files_sharing', 'blacklisted_receiver_groups', '[]'), true);
85
	}
86
87
	private function initCache() {
88
		if ($this->blacklistCache === null) {
89
			$this->blacklistCache = [
90
				'receivers' => [
91
					'ids' => $this->fetchBlacklistedReceiverGroupIds(),
92
				],
93
			];
94
		}
95
	}
96
97
	private function fetchBlacklistedReceiverGroupIds() {
98
		$configuredBlacklist = $this->config->getAppValue('files_sharing', 'blacklisted_receiver_groups', '[]');
99
		$decodedGroups = \json_decode($configuredBlacklist, true);
100
		// expected a plain array here
101
		$groupSet = [];
102
		foreach ($decodedGroups as $group) {
103
			$groupSet[$group] = true;
104
		}
105
		return $groupSet;
106
	}
107
}
108