summaryrefslogtreecommitdiffstats
path: root/lib/private/Share20/ShareDisableChecker.php
blob: 9b8bc01558c591fdf6c3cab7e2d46ac3810824b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php

/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Share20;

use OCP\Cache\CappedMemoryCache;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUserManager;

/**
 * split of from the share manager to allow using it with minimal DI
 */
class ShareDisableChecker {
	private CappedMemoryCache $sharingDisabledForUsersCache;

	public function __construct(
		private IConfig $config,
		private IUserManager $userManager,
		private IGroupManager $groupManager,
	) {
		$this->sharingDisabledForUsersCache = new CappedMemoryCache();
	}


	/**
	 * @param ?string $userId
	 * @return bool
	 */
	public function sharingDisabledForUser(?string $userId) {
		if ($userId === null) {
			return false;
		}

		if (isset($this->sharingDisabledForUsersCache[$userId])) {
			return $this->sharingDisabledForUsersCache[$userId];
		}

		$excludeGroups = $this->config->getAppValue('core', 'shareapi_exclude_groups', 'no');

		if ($excludeGroups && $excludeGroups !== 'no') {
			$groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', '');
			$excludedGroups = json_decode($groupsList);
			if (is_null($excludedGroups)) {
				$excludedGroups = explode(',', $groupsList);
				$newValue = json_encode($excludedGroups);
				$this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue);
			}
			$user = $this->userManager->get($userId);
			if (!$user) {
				return false;
			}
			$usersGroups = $this->groupManager->getUserGroupIds($user);
			if ($excludeGroups !== 'allow') {
				if (!empty($usersGroups)) {
					$remainingGroups = array_diff($usersGroups, $excludedGroups);
					// if the user is only in groups which are disabled for sharing then
					// sharing is also disabled for the user
					if (empty($remainingGroups)) {
						$this->sharingDisabledForUsersCache[$userId] = true;
						return true;
					}
				}
			} else {
				if (!empty($usersGroups)) {
					$remainingGroups = array_intersect($usersGroups, $excludedGroups);
					// if the user is in any group which is allowed for sharing then
					// sharing is also allowed for the user
					if (!empty($remainingGroups)) {
						$this->sharingDisabledForUsersCache[$userId] = false;
						return false;
					}
				}
				$this->sharingDisabledForUsersCache[$userId] = true;
				return true;
			}
		}

		$this->sharingDisabledForUsersCache[$userId] = false;
		return false;
	}
}