aboutsummaryrefslogtreecommitdiffstats
path: root/ui
Commit message (Collapse)AuthorAgeFilesLines
* Core: Move scrollParent into its own moduleAlexander Schmitz2015-08-085-16/+47
| | | | Ref #9647
* Core: Move tabbable into its own moduleAlexander Schmitz2015-08-083-9/+38
| | | | Ref #9647
* Core: Move safe blur into its own moduleAlexander Schmitz2015-08-084-14/+25
| | | | Ref #9647
* Core: Move safeActiveElement into its own moduleAlexander Schmitz2015-08-088-36/+52
| | | | Ref #9647
* Core: Move plugin into its own moduleAlexander Schmitz2015-08-084-31/+46
| | | | Ref #9647
* Core: Move backcompat for core 1.7 into its own moduleAlexander Schmitz2015-08-082-62/+90
| | | | Ref #9647
* Core: Move labels into its own moduleAlexander Schmitz2015-08-084-38/+68
| | | | Ref #9647
* Core: Move keyCode into its own moduleAlexander Schmitz2015-08-0813-29/+63
| | | | Ref #9647
* Core: Move $.ui.ie into its own moduleAlexander Schmitz2015-08-084-3/+18
| | | | Ref #9647
* Core: Move form method into its own moduleAlexander Schmitz2015-08-083-8/+22
| | | | Ref #9647
* Core: Move escape selector into its own moduleAlexander Schmitz2015-08-084-9/+25
| | | | Ref #9647
* Core: Move focusable into its own moduleAlexander Schmitz2015-08-083-34/+67
| | | | Ref #9647
* Core: Move disable-selection into its own moduleAlexander Schmitz2015-08-083-20/+51
| | | | Ref #9647
* Core: Move data selector from core into its own moduleAlexander Schmitz2015-08-085-12/+43
| | | | Ref #9647
* Core: Move version and creation of the ui namespace into its own moduleAlexander Schmitz2015-08-0839-17/+60
| | | | Ref #9647
* Dialog: Fix removal of event listener for modal dialogsScott González2015-07-171-3/+2
| | | | Fixes #13649
* Sortable: Optimize `_intersectsWithPointer()`Victor Homyakov2015-07-081-5/+7
| | | | Closes gh-1574
* Position: Guard against passing window to offsetAlexander Schmitz2015-07-081-2/+3
| | | | | | | Starting in jQuery 3.0 this will throw an error Fixes #13493 Closes gh-1565
* Autocomplete: Revert mistaken event alias changeJörn Zaefferer2015-07-011-1/+1
| | | | | | | This is a call on the menu widget, not using an event alias. Ref 8b4ce807cd97e3cb953995934d6c4f614de9fa03 Ref #12770
* Spinner: Deprecate _uiSpinnerHtml and _buttonHtml extension pointsAlexander Schmitz2015-06-031-24/+56
| | | | | Fixes #11097 Closes gh-1560
* Resizable: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-4/+4
|
* Mouse: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-10/+10
|
* Core: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-3/+3
|
* Effects: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-2/+2
|
* Widget: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-6/+6
|
* Tabs: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-5/+5
|
* Spinner: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-1/+1
|
* Slider: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-1/+1
|
* Droppable: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-2/+2
|
* Draggable: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-1/+1
|
* Dialog: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-10/+12
|
* Datepicker: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-18/+18
|
* Button: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-14/+14
|
* Autocomplete: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-3/+3
|
* Accordion: Remove core event/alias and deprecated module dependenciesAlexander Schmitz2015-05-201-2/+2
|
* Form Reset: Add form reset mixinScott González2015-05-141-0/+62
| | | | | Fixes #12638 Closes gh-1555
* Widget: Support mixinsScott González2015-05-141-0/+4
| | | | | Fixes #12601 Closes gh-1554
* Tabs: Use $.ui.escapeSelector to avoid invalid selectorsAlexander Schmitz2015-05-051-1/+1
|
* Selectmenu: Use $.ui.escapeSelectorAlexander Schmitz2015-05-051-2/+3
|
* Core: Add $.fn.labels, $.fn.form, and $.ui.escapeSelector methodsAlexander Schmitz2015-05-052-4/+54
| | | | | | | $.fn.labels and $.fn.form mimic the native labels and form properties $.ui.escapeSelector is for escaping attributes and urls for use as selectors Closes gh-1546
* Effect: Make .transfer() callback optionalJörn Zaefferer2015-04-241-1/+3
| | | | | | | Adds two tests to at least check that no exception is thrown. Fixes #12223 Closes gh-1545
* Slider: Range fills all space after changing orientationAblay Keldibek2015-04-221-0/+12
| | | | | | | Resets width/height of range Fixes #12205 Closes gh-1533
* Tooltip: Remove name attribute from elements in the live regionJiabao Wu2015-04-221-0/+1
| | | | | Fixes #11272 Closes gh-1544
* Slider: Pass 0 value if slider reaches itAblay Keldibek2015-04-201-2/+2
| | | | | Fixes #12196 Closes gh-1540
* Tabs: Rename ui-tab class to ui-tabs-tab for consistent prefixesJörn Zaefferer2015-04-181-2/+17
| | | | | | | Fixes #12061 Closes gh-1538 Ref #7053 Ref jquery/api.jqueryui.com#256
* Revert "Draggable: Set explicit width/height instead of right/bottom"TJ VanToll2015-04-081-15/+0
| | | | | | This reverts commit 8eca7b8f45885d20c13f1bf64cad8bee5fc1d5c5. Fixes #10725
* Menu: Ignore bubbled mouseenter events on parent itemsJörn Zaefferer2015-04-081-1/+10
| | | | | | | | Test uses QUnit 2.x API - inconsistent with the other tests, but also a good reference. Fixes #11641 Closes gh-1535
* Slider: Proper animation of rangeAblay Keldibek2015-04-061-2/+2
| | | | | Fixes #9459 Closes gh-1530
* Core: Work around more IE activeElement bugsScott González2015-03-291-0/+7
| | | | Closes gh-1523
* All: Standard spacing inside selector stringsScott González2015-03-263-3/+3
| | | | Closes gh-1521
Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
aboutsummaryrefslogtreecommitdiffstats
path: root/lib/user/manager.php
blob: 13286bc28a48ad964ca7278de0373c554e8654c9 (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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php

/**
 * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace OC\User;

use OC\Hooks\PublicEmitter;

/**
 * Class Manager
 *
 * Hooks available in scope \OC\User:
 * - preSetPassword(\OC\User\User $user, string $password, string $recoverPassword)
 * - postSetPassword(\OC\User\User $user, string $password, string $recoverPassword)
 * - preDelete(\OC\User\User $user)
 * - postDelete(\OC\User\User $user)
 * - preCreateUser(string $uid, string $password)
 * - postCreateUser(\OC\User\User $user, string $password)
 *
 * @package OC\User
 */
class Manager extends PublicEmitter {
	/**
	 * @var \OC_User_Backend[] $backends
	 */
	private $backends = array();

	/**
	 * @var \OC\User\User[] $cachedUsers
	 */
	private $cachedUsers = array();

	public function __construct() {
		$cachedUsers = $this->cachedUsers;
		$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
			$i = array_search($user, $cachedUsers);
			if ($i !== false) {
				unset($cachedUsers[$i]);
			}
		});
	}

	/**
	 * register a user backend
	 *
	 * @param \OC_User_Backend $backend
	 */
	public function registerBackend($backend) {
		$this->backends[] = $backend;
	}

	/**
	 * remove a user backend
	 *
	 * @param \OC_User_Backend $backend
	 */
	public function removeBackend($backend) {
		$this->cachedUsers = array();
		if (($i = array_search($backend, $this->backends)) !== false) {
			unset($this->backends[$i]);
		}
	}

	/**
	 * remove all user backends
	 */
	public function clearBackends() {
		$this->cachedUsers = array();
		$this->backends = array();
	}

	/**
	 * get a user by user id
	 *
	 * @param string $uid
	 * @return \OC\User\User
	 */
	public function get($uid) {
		if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends
			return $this->cachedUsers[$uid];
		}
		foreach ($this->backends as $backend) {
			if ($backend->userExists($uid)) {
				return $this->getUserObject($uid, $backend);
			}
		}
		return null;
	}

	/**
	 * get or construct the user object
	 *
	 * @param string $uid
	 * @param \OC_User_Backend $backend
	 * @return \OC\User\User
	 */
	protected function getUserObject($uid, $backend) {
		if (isset($this->cachedUsers[$uid])) {
			return $this->cachedUsers[$uid];
		}
		$this->cachedUsers[$uid] = new User($uid, $backend, $this);
		return $this->cachedUsers[$uid];
	}

	/**
	 * check if a user exists
	 *
	 * @param string $uid
	 * @return bool
	 */
	public function userExists($uid) {
		$user = $this->get($uid);
		return ($user !== null);
	}

	/**
	 * Check if the password is valid for the user
	 *
	 * @param $loginname
	 * @param $password
	 * @return mixed the User object on success, false otherwise
	 */
	public function checkPassword($loginname, $password) {
		foreach ($this->backends as $backend) {
			if($backend->implementsActions(\OC_USER_BACKEND_CHECK_PASSWORD)) {
				$uid = $backend->checkPassword($loginname, $password);
				if ($uid !== false) {
					return $this->getUserObject($uid, $backend);
				}
			}
		}
		return false;
	}

	/**
	 * search by user id
	 *
	 * @param string $pattern
	 * @param int $limit
	 * @param int $offset
	 * @return \OC\User\User[]
	 */
	public function search($pattern, $limit = null, $offset = null) {
		$users = array();
		foreach ($this->backends as $backend) {
			$backendUsers = $backend->getUsers($pattern, $limit, $offset);
			if (is_array($backendUsers)) {
				foreach ($backendUsers as $uid) {
					$users[] = $this->getUserObject($uid, $backend);
					if (!is_null($limit)) {
						$limit--;
					}
					if (!is_null($offset) and $offset > 0) {
						$offset--;
					}

				}
			}
		}

		usort($users, function ($a, $b) {
			/**
			 * @var \OC\User\User $a
			 * @var \OC\User\User $b
			 */
			return strcmp($a->getUID(), $b->getUID());
		});
		return $users;
	}

	/**
	 * search by displayName
	 *
	 * @param string $pattern
	 * @param int $limit
	 * @param int $offset
	 * @return \OC\User\User[]
	 */
	public function searchDisplayName($pattern, $limit = null, $offset = null) {
		$users = array();
		foreach ($this->backends as $backend) {
			$backendUsers = $backend->getDisplayNames($pattern, $limit, $offset);
			if (is_array($backendUsers)) {
				foreach ($backendUsers as $uid => $displayName) {
					$users[] = $this->getUserObject($uid, $backend);
					if (!is_null($limit)) {
						$limit--;
					}
					if (!is_null($offset) and $offset > 0) {
						$offset--;
					}

				}
			}
		}

		usort($users, function ($a, $b) {
			/**
			 * @var \OC\User\User $a
			 * @var \OC\User\User $b
			 */
			return strcmp($a->getDisplayName(), $b->getDisplayName());
		});
		return $users;
	}

	/**
	 * @param string $uid
	 * @param string $password
	 * @throws \Exception
	 * @return bool | \OC\User\User the created user of false
	 */
	public function createUser($uid, $password) {
		// Check the name for bad characters
		// Allowed are: "a-z", "A-Z", "0-9" and "_.@-"
		if (preg_match('/[^a-zA-Z0-9 _\.@\-]/', $uid)) {
			throw new \Exception('Only the following characters are allowed in a username:'
			. ' "a-z", "A-Z", "0-9", and "_.@-"');
		}
		// No empty username
		if (trim($uid) == '') {
			throw new \Exception('A valid username must be provided');
		}
		// No empty password
		if (trim($password) == '') {
			throw new \Exception('A valid password must be provided');
		}

		// Check if user already exists
		if ($this->userExists($uid)) {
			throw new \Exception('The username is already being used');
		}

		$this->emit('\OC\User', 'preCreateUser', array($uid, $password));
		foreach ($this->backends as $backend) {
			if ($backend->implementsActions(\OC_USER_BACKEND_CREATE_USER)) {
				$backend->createUser($uid, $password);
				$user = $this->getUserObject($uid, $backend);
				$this->emit('\OC\User', 'postCreateUser', array($user, $password));
				return $user;
			}
		}
		return false;
	}
}