Passed
Push — master ( 34c9b5...835e28 )
by Blizzz
15:25 queued 23s
created

GroupPluginManager   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 48
c 1
b 1
f 0
dl 0
loc 157
rs 10
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getImplementedActions() 0 2 1
A countUsersInGroup() 0 7 2
A implementsActions() 0 2 1
A addToGroup() 0 7 2
A register() 0 8 3
A createGroup() 0 7 2
A getGroupDetails() 0 7 2
A canDeleteGroup() 0 2 2
A deleteGroup() 0 10 3
A removeFromGroup() 0 7 2
A setSuppressDeletion() 0 4 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Vinicius Cubas Brand <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\User_LDAP;
25
26
use OCP\GroupInterface;
27
28
class GroupPluginManager {
29
	private int $respondToActions = 0;
30
31
	/** @var array<int, ?ILDAPGroupPlugin> */
32
	private array $which = [
33
		GroupInterface::CREATE_GROUP => null,
34
		GroupInterface::DELETE_GROUP => null,
35
		GroupInterface::ADD_TO_GROUP => null,
36
		GroupInterface::REMOVE_FROM_GROUP => null,
37
		GroupInterface::COUNT_USERS => null,
38
		GroupInterface::GROUP_DETAILS => null
39
	];
40
41
	private bool $suppressDeletion = false;
42
43
	/**
44
	 * @return int All implemented actions
45
	 */
46
	public function getImplementedActions() {
47
		return $this->respondToActions;
48
	}
49
50
	/**
51
	 * Registers a group plugin that may implement some actions, overriding User_LDAP's group actions.
52
	 * @param ILDAPGroupPlugin $plugin
53
	 */
54
	public function register(ILDAPGroupPlugin $plugin) {
55
		$respondToActions = $plugin->respondToActions();
56
		$this->respondToActions |= $respondToActions;
57
58
		foreach ($this->which as $action => $v) {
59
			if ((bool)($respondToActions & $action)) {
60
				$this->which[$action] = $plugin;
61
				\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
62
			}
63
		}
64
	}
65
66
	/**
67
	 * Signal if there is a registered plugin that implements some given actions
68
	 * @param int $actions Actions defined in \OCP\GroupInterface, like GroupInterface::REMOVE_FROM_GROUP
69
	 * @return bool
70
	 */
71
	public function implementsActions($actions) {
72
		return ($actions & $this->respondToActions) == $actions;
73
	}
74
75
	/**
76
	 * Create a group
77
	 * @param string $gid Group Id
78
	 * @return string | null The group DN if group creation was successful.
79
	 * @throws \Exception
80
	 */
81
	public function createGroup($gid) {
82
		$plugin = $this->which[GroupInterface::CREATE_GROUP];
83
84
		if ($plugin) {
85
			return $plugin->createGroup($gid);
86
		}
87
		throw new \Exception('No plugin implements createGroup in this LDAP Backend.');
88
	}
89
90
	public function canDeleteGroup(): bool {
91
		return !$this->suppressDeletion && $this->implementsActions(GroupInterface::DELETE_GROUP);
92
	}
93
94
	/**
95
	 * @return bool – the value before the change
96
	 */
97
	public function setSuppressDeletion(bool $value): bool {
98
		$old = $this->suppressDeletion;
99
		$this->suppressDeletion = $value;
100
		return $old;
101
	}
102
103
	/**
104
	 * Delete a group
105
	 *
106
	 * @throws \Exception
107
	 */
108
	public function deleteGroup(string $gid): bool {
109
		$plugin = $this->which[GroupInterface::DELETE_GROUP];
110
111
		if ($plugin) {
112
			if ($this->suppressDeletion) {
113
				return false;
114
			}
115
			return $plugin->deleteGroup($gid);
116
		}
117
		throw new \Exception('No plugin implements deleteGroup in this LDAP Backend.');
118
	}
119
120
	/**
121
	 * Add a user to a group
122
	 * @param string $uid ID of the user to add to group
123
	 * @param string $gid ID of the group in which add the user
124
	 * @return bool
125
	 * @throws \Exception
126
	 *
127
	 * Adds a user to a group.
128
	 */
129
	public function addToGroup($uid, $gid) {
130
		$plugin = $this->which[GroupInterface::ADD_TO_GROUP];
131
132
		if ($plugin) {
133
			return $plugin->addToGroup($uid, $gid);
134
		}
135
		throw new \Exception('No plugin implements addToGroup in this LDAP Backend.');
136
	}
137
138
	/**
139
	 * Removes a user from a group
140
	 * @param string $uid ID of the user to remove from group
141
	 * @param string $gid ID of the group from which remove the user
142
	 * @return bool
143
	 * @throws \Exception
144
	 *
145
	 * removes the user from a group.
146
	 */
147
	public function removeFromGroup($uid, $gid) {
148
		$plugin = $this->which[GroupInterface::REMOVE_FROM_GROUP];
149
150
		if ($plugin) {
151
			return $plugin->removeFromGroup($uid, $gid);
152
		}
153
		throw new \Exception('No plugin implements removeFromGroup in this LDAP Backend.');
154
	}
155
156
	/**
157
	 * get the number of all users matching the search string in a group
158
	 * @param string $gid ID of the group
159
	 * @param string $search query string
160
	 * @return int|false
161
	 * @throws \Exception
162
	 */
163
	public function countUsersInGroup($gid, $search = '') {
164
		$plugin = $this->which[GroupInterface::COUNT_USERS];
165
166
		if ($plugin) {
167
			return $plugin->countUsersInGroup($gid,$search);
168
		}
169
		throw new \Exception('No plugin implements countUsersInGroup in this LDAP Backend.');
170
	}
171
172
	/**
173
	 * get an array with group details
174
	 * @param string $gid
175
	 * @return array|false
176
	 * @throws \Exception
177
	 */
178
	public function getGroupDetails($gid) {
179
		$plugin = $this->which[GroupInterface::GROUP_DETAILS];
180
181
		if ($plugin) {
182
			return $plugin->getGroupDetails($gid);
183
		}
184
		throw new \Exception('No plugin implements getGroupDetails in this LDAP Backend.');
185
	}
186
}
187