summaryrefslogtreecommitdiffstats
path: root/apps/user_ldap/lib/UserPluginManager.php
blob: 374e545f4fdf6d288faa05b4e60f120fd1bbc8bf (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/**
 * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
 *
 * @author Vinicius Brand <vinicius@eita.org.br>
 * @author Daniel Tygel <dtygel@eita.org.br>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\User_LDAP;

use OC\User\Backend;

class UserPluginManager {

	public $test = false;

	private $respondToActions = 0;

	private $which = array(
		Backend::CREATE_USER => null,
		Backend::SET_PASSWORD => null,
		Backend::GET_HOME => null,
		Backend::GET_DISPLAYNAME => null,
		Backend::SET_DISPLAYNAME => null,
		Backend::PROVIDE_AVATAR => null,
		Backend::COUNT_USERS => null,
		'deleteUser' => null
	);

	/**
	 * @return int All implemented actions, except for 'deleteUser'
	 */
	public function getImplementedActions() {
		return $this->respondToActions;
	}

	/**
	 * Registers a group plugin that may implement some actions, overriding User_LDAP's user actions.
	 * @param ILDAPGroupPlugin $plugin
	 */
	public function register(ILDAPUserPlugin $plugin) {
		$respondToActions = $plugin->respondToActions();
		$this->respondToActions |= $respondToActions;

		foreach($this->which as $action => $v) {
			if ((bool)($respondToActions & $action)) {
				$this->which[$action] = $plugin;
				\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
			}
		}
		if (method_exists($plugin,'deleteUser')) {
			$this->which['deleteUser'] = $plugin;
		}
	}

	/**
	 * Signal if there is a registered plugin that implements some given actions
	 * @param int $action Actions defined in \OC\User\Backend, like Backend::CREATE_USER
	 * @return bool
	 */
	public function implementsActions($actions) {
		return ($actions & $this->respondToActions) == $actions;
	}

	/**
	 * Create a new user in LDAP Backend
	 *
	 * @param string $uid The username of the user to create
	 * @param string $password The password of the new user
	 * @return bool
	 * @throws \Exception
	 */
	public function createUser($username, $password) {
		$plugin = $this->which[Backend::CREATE_USER];

		if ($plugin) {
			return $plugin->createUser($username,$password);
		}
		throw new \Exception('No plugin implements createUser in this LDAP Backend.');
	}

	/**
	 * Change the password of a user*
	 * @param string $uid The username
	 * @param string $password The new password
	 * @return bool
	 * @throws \Exception
	 */
	public function setPassword($uid, $password) {
		$plugin = $this->which[Backend::SET_PASSWORD];

		if ($plugin) {
			return $plugin->setPassword($uid,$password);
		}
		throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
	}

	/**
	 * checks whether the user is allowed to change his avatar in Nextcloud
	 * @param string $uid the Nextcloud user name
	 * @return boolean either the user can or cannot
	 * @throws \Exception
	 */
	public function canChangeAvatar($uid) {
		$plugin = $this->which[Backend::PROVIDE_AVATAR];

		if ($plugin) {
			return $plugin->canChangeAvatar($uid);
		}
		throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
	}

	/**
	 * Get the user's home directory
	 * @param string $uid the username
	 * @return boolean
	 * @throws \Exception
	 */
	public function getHome($uid) {
		$plugin = $this->which[Backend::GET_HOME];

		if ($plugin) {
			return $plugin->getHome($uid);
		}
		throw new \Exception('No plugin implements getHome in this LDAP Backend.');
	}

	/**
	 * Get display name of the user
	 * @param string $uid user ID of the user
	 * @return string display name
	 * @throws \Exception
	 */
	public function getDisplayName($uid) {
		$plugin = $this->which[Backend::GET_DISPLAYNAME];

		if ($plugin) {
			return $plugin->getDisplayName($uid);
		}
		throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
	}

	/**
	 * Set display name of the user
	 * @param string $uid user ID of the user
	 * @param string $displayName new user's display name
	 * @return string display name
	 * @throws \Exception
	 */
	public function setDisplayName($uid, $displayName) {
		$plugin = $this->which[Backend::SET_DISPLAYNAME];

		if ($plugin) {
			return $plugin->setDisplayName($uid, $displayName);
		}
		throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
	}

	/**
	 * Count the number of users
	 * @return int|bool
	 * @throws \Exception
	 */
	public function countUsers() {
		$plugin = $this->which[Backend::COUNT_USERS];

		if ($plugin) {
			return $plugin->countUsers();
		}
		throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
	}

	/**
	 * @return bool
	 */
	public function canDeleteUser() {
		return $this->which['deleteUser'] !== null;
	}

	/**
	 * @param $uid
	 * @return bool
	 * @throws \Exception
	 */
	public function deleteUser($uid) {
		$plugin = $this->which['deleteUser'];
		if ($plugin) {
			return $plugin->deleteUser($uid);
		}
		throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
	}
}