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

UserPluginManager::setPassword()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
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 OC\User\Backend;
27
28
class UserPluginManager {
29
30
	public $test = false;
31
32
	private $respondToActions = 0;
33
34
	private $which = array(
35
		Backend::CREATE_USER => null,
36
		Backend::SET_PASSWORD => null,
37
		Backend::GET_HOME => null,
38
		Backend::GET_DISPLAYNAME => null,
39
		Backend::SET_DISPLAYNAME => null,
40
		Backend::PROVIDE_AVATAR => null,
41
		Backend::COUNT_USERS => null,
42
		'deleteUser' => null
43
	);
44
45
	/**
46
	 * @return int All implemented actions, except for 'deleteUser'
47
	 */
48
	public function getImplementedActions() {
49
		return $this->respondToActions;
50
	}
51
52
	/**
53
	 * Registers a group plugin that may implement some actions, overriding User_LDAP's user actions.
54
	 *
55
	 * @param ILDAPUserPlugin $plugin
56
	 */
57
	public function register(ILDAPUserPlugin $plugin) {
58
		$respondToActions = $plugin->respondToActions();
59
		$this->respondToActions |= $respondToActions;
60
61 View Code Duplication
		foreach($this->which as $action => $v) {
62
			if (is_int($action) && (bool)($respondToActions & $action)) {
63
				$this->which[$action] = $plugin;
64
				\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
65
			}
66
		}
67
		if (method_exists($plugin,'deleteUser')) {
68
			$this->which['deleteUser'] = $plugin;
69
			\OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
70
		}
71
	}
72
73
	/**
74
	 * Signal if there is a registered plugin that implements some given actions
75
	 * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER
76
	 * @return bool
77
	 */
78
	public function implementsActions($actions) {
79
		return ($actions & $this->respondToActions) == $actions;
80
	}
81
82
	/**
83
	 * Create a new user in LDAP Backend
84
	 *
85
	 * @param string $username The username of the user to create
86
	 * @param string $password The password of the new user
87
	 * @return bool
88
	 * @throws \Exception
89
	 */
90
	public function createUser($username, $password) {
91
		$plugin = $this->which[Backend::CREATE_USER];
92
93
		if ($plugin) {
94
			return $plugin->createUser($username,$password);
95
		}
96
		throw new \Exception('No plugin implements createUser in this LDAP Backend.');
97
	}
98
99
	/**
100
	 * Change the password of a user*
101
	 * @param string $uid The username
102
	 * @param string $password The new password
103
	 * @return bool
104
	 * @throws \Exception
105
	 */
106
	public function setPassword($uid, $password) {
107
		$plugin = $this->which[Backend::SET_PASSWORD];
108
109
		if ($plugin) {
110
			return $plugin->setPassword($uid,$password);
111
		}
112
		throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
113
	}
114
115
	/**
116
	 * checks whether the user is allowed to change his avatar in Nextcloud
117
	 * @param string $uid the Nextcloud user name
118
	 * @return boolean either the user can or cannot
119
	 * @throws \Exception
120
	 */
121 View Code Duplication
	public function canChangeAvatar($uid) {
122
		$plugin = $this->which[Backend::PROVIDE_AVATAR];
123
124
		if ($plugin) {
125
			return $plugin->canChangeAvatar($uid);
126
		}
127
		throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
128
	}
129
130
	/**
131
	 * Get the user's home directory
132
	 * @param string $uid the username
133
	 * @return boolean
134
	 * @throws \Exception
135
	 */
136 View Code Duplication
	public function getHome($uid) {
137
		$plugin = $this->which[Backend::GET_HOME];
138
139
		if ($plugin) {
140
			return $plugin->getHome($uid);
141
		}
142
		throw new \Exception('No plugin implements getHome in this LDAP Backend.');
143
	}
144
145
	/**
146
	 * Get display name of the user
147
	 * @param string $uid user ID of the user
148
	 * @return string display name
149
	 * @throws \Exception
150
	 */
151 View Code Duplication
	public function getDisplayName($uid) {
152
		$plugin = $this->which[Backend::GET_DISPLAYNAME];
153
154
		if ($plugin) {
155
			return $plugin->getDisplayName($uid);
156
		}
157
		throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
158
	}
159
160
	/**
161
	 * Set display name of the user
162
	 * @param string $uid user ID of the user
163
	 * @param string $displayName new user's display name
164
	 * @return string display name
165
	 * @throws \Exception
166
	 */
167
	public function setDisplayName($uid, $displayName) {
168
		$plugin = $this->which[Backend::SET_DISPLAYNAME];
169
170
		if ($plugin) {
171
			return $plugin->setDisplayName($uid, $displayName);
172
		}
173
		throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
174
	}
175
176
	/**
177
	 * Count the number of users
178
	 * @return int|bool
179
	 * @throws \Exception
180
	 */
181 View Code Duplication
	public function countUsers() {
182
		$plugin = $this->which[Backend::COUNT_USERS];
183
184
		if ($plugin) {
185
			return $plugin->countUsers();
186
		}
187
		throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
188
	}
189
190
	/**
191
	 * @return bool
192
	 */
193
	public function canDeleteUser() {
194
		return $this->which['deleteUser'] !== null;
195
	}
196
197
	/**
198
	 * @param $uid
199
	 * @return bool
200
	 * @throws \Exception
201
	 */
202
	public function deleteUser($uid) {
203
		$plugin = $this->which['deleteUser'];
204
		if ($plugin) {
205
			return $plugin->deleteUser($uid);
206
		}
207
		throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
208
	}
209
}
210
211