Passed
Push — master ( 2445b9...d011df )
by Morris
15:00 queued 11s
created

UserPluginManager::setDisplayName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
4
 *
5
 * @author Christoph Wurst <[email protected]>
6
 * @author Filis Futsarov <[email protected]>
7
 * @author Vinicius Cubas Brand <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
namespace OCA\User_LDAP;
27
28
use OC\User\Backend;
29
30
class UserPluginManager {
31
	private $respondToActions = 0;
32
33
	private $which = [
34
		Backend::CREATE_USER => null,
35
		Backend::SET_PASSWORD => null,
36
		Backend::GET_HOME => null,
37
		Backend::GET_DISPLAYNAME => null,
38
		Backend::SET_DISPLAYNAME => null,
39
		Backend::PROVIDE_AVATAR => null,
40
		Backend::COUNT_USERS => null,
41
		'deleteUser' => null
42
	];
43
44
	/** @var bool */
45
	private $suppressDeletion = false;
46
47
	/**
48
	 * @return int All implemented actions, except for 'deleteUser'
49
	 */
50
	public function getImplementedActions() {
51
		return $this->respondToActions;
52
	}
53
54
	/**
55
	 * Registers a user plugin that may implement some actions, overriding User_LDAP's user actions.
56
	 *
57
	 * @param ILDAPUserPlugin $plugin
58
	 */
59
	public function register(ILDAPUserPlugin $plugin) {
60
		$respondToActions = $plugin->respondToActions();
61
		$this->respondToActions |= $respondToActions;
62
63
		foreach ($this->which as $action => $v) {
64
			if (is_int($action) && (bool)($respondToActions & $action)) {
65
				$this->which[$action] = $plugin;
66
				\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
67
			}
68
		}
69
		if (method_exists($plugin,'deleteUser')) {
70
			$this->which['deleteUser'] = $plugin;
71
			\OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
72
		}
73
	}
74
75
	/**
76
	 * Signal if there is a registered plugin that implements some given actions
77
	 * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER
78
	 * @return bool
79
	 */
80
	public function implementsActions($actions) {
81
		return ($actions & $this->respondToActions) == $actions;
82
	}
83
84
	/**
85
	 * Create a new user in LDAP Backend
86
	 *
87
	 * @param string $username The username of the user to create
88
	 * @param string $password The password of the new user
89
	 * @return string | false The user DN if user creation was successful.
90
	 * @throws \Exception
91
	 */
92
	public function createUser($username, $password) {
93
		$plugin = $this->which[Backend::CREATE_USER];
94
95
		if ($plugin) {
96
			return $plugin->createUser($username,$password);
97
		}
98
		throw new \Exception('No plugin implements createUser in this LDAP Backend.');
99
	}
100
101
	/**
102
	 * Change the password of a user*
103
	 * @param string $uid The username
104
	 * @param string $password The new password
105
	 * @return bool
106
	 * @throws \Exception
107
	 */
108
	public function setPassword($uid, $password) {
109
		$plugin = $this->which[Backend::SET_PASSWORD];
110
111
		if ($plugin) {
112
			return $plugin->setPassword($uid,$password);
113
		}
114
		throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
115
	}
116
117
	/**
118
	 * checks whether the user is allowed to change his avatar in Nextcloud
119
	 * @param string $uid the Nextcloud user name
120
	 * @return boolean either the user can or cannot
121
	 * @throws \Exception
122
	 */
123
	public function canChangeAvatar($uid) {
124
		$plugin = $this->which[Backend::PROVIDE_AVATAR];
125
126
		if ($plugin) {
127
			return $plugin->canChangeAvatar($uid);
128
		}
129
		throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
130
	}
131
132
	/**
133
	 * Get the user's home directory
134
	 * @param string $uid the username
135
	 * @return boolean
136
	 * @throws \Exception
137
	 */
138
	public function getHome($uid) {
139
		$plugin = $this->which[Backend::GET_HOME];
140
141
		if ($plugin) {
142
			return $plugin->getHome($uid);
143
		}
144
		throw new \Exception('No plugin implements getHome in this LDAP Backend.');
145
	}
146
147
	/**
148
	 * Get display name of the user
149
	 * @param string $uid user ID of the user
150
	 * @return string display name
151
	 * @throws \Exception
152
	 */
153
	public function getDisplayName($uid) {
154
		$plugin = $this->which[Backend::GET_DISPLAYNAME];
155
156
		if ($plugin) {
157
			return $plugin->getDisplayName($uid);
158
		}
159
		throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
160
	}
161
162
	/**
163
	 * Set display name of the user
164
	 * @param string $uid user ID of the user
165
	 * @param string $displayName new user's display name
166
	 * @return string display name
167
	 * @throws \Exception
168
	 */
169
	public function setDisplayName($uid, $displayName) {
170
		$plugin = $this->which[Backend::SET_DISPLAYNAME];
171
172
		if ($plugin) {
173
			return $plugin->setDisplayName($uid, $displayName);
174
		}
175
		throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
176
	}
177
178
	/**
179
	 * Count the number of users
180
	 * @return int|bool
181
	 * @throws \Exception
182
	 */
183
	public function countUsers() {
184
		$plugin = $this->which[Backend::COUNT_USERS];
185
186
		if ($plugin) {
187
			return $plugin->countUsers();
188
		}
189
		throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
190
	}
191
192
	/**
193
	 * @return bool
194
	 */
195
	public function canDeleteUser() {
196
		return !$this->suppressDeletion && $this->which['deleteUser'] !== null;
197
	}
198
199
	/**
200
	 * @param $uid
201
	 * @return bool
202
	 * @throws \Exception
203
	 */
204
	public function deleteUser($uid) {
205
		$plugin = $this->which['deleteUser'];
206
		if ($plugin) {
207
			if ($this->suppressDeletion) {
208
				return false;
209
			}
210
			return $plugin->deleteUser($uid);
211
		}
212
		throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
213
	}
214
215
	/**
216
	 * @param bool $value
217
	 * @return bool – the value before the change
218
	 */
219
	public function setSuppressDeletion(bool $value): bool {
220
		$old = $this->suppressDeletion;
221
		$this->suppressDeletion = $value;
222
		return $old;
223
	}
224
}
225