Completed
Push — master ( 0256f6...5411d6 )
by Morris
22:12 queued 06:46
created

GroupPluginManager::getGroupDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 8
Ratio 100 %

Importance

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