PageRenderTime 115ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 59ms app.codeStats 0ms

/apps/user_ldap/user_ldap.php

https://github.com/sezuan/core
PHP | 288 lines | 193 code | 25 blank | 70 comment | 27 complexity | 4bed5919d8b0e92418d7b1ad58e5ed69 MD5 | raw file
  1<?php
  2
  3/**
  4 * ownCloud
  5 *
  6 * @author Dominik Schmidt
  7 * @author Artuhr Schiwon
  8 * @copyright 2011 Dominik Schmidt dev@dominik-schmidt.de
  9 * @copyright 2012 Arthur Schiwon blizzz@owncloud.com
 10 *
 11 * This library is free software; you can redistribute it and/or
 12 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 13 * License as published by the Free Software Foundation; either
 14 * version 3 of the License, or any later version.
 15 *
 16 * This library 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
 22 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 23 *
 24 */
 25
 26namespace OCA\user_ldap;
 27
 28class USER_LDAP extends lib\Access implements \OCP\UserInterface {
 29
 30	private function updateQuota($dn) {
 31		$quota = null;
 32		$quotaDefault = $this->connection->ldapQuotaDefault;
 33		$quotaAttribute = $this->connection->ldapQuotaAttribute;
 34		if(!empty($quotaDefault)) {
 35			$quota = $quotaDefault;
 36		}
 37		if(!empty($quotaAttribute)) {
 38			$aQuota = $this->readAttribute($dn, $quotaAttribute);
 39
 40			if($aQuota && (count($aQuota) > 0)) {
 41				$quota = $aQuota[0];
 42			}
 43		}
 44		if(!is_null($quota)) {
 45			\OCP\Config::setUserValue($this->dn2username($dn), 'files', 'quota', \OCP\Util::computerFileSize($quota));
 46		}
 47	}
 48
 49	private function updateEmail($dn) {
 50		$email = null;
 51		$emailAttribute = $this->connection->ldapEmailAttribute;
 52		if(!empty($emailAttribute)) {
 53			$aEmail = $this->readAttribute($dn, $emailAttribute);
 54			if($aEmail && (count($aEmail) > 0)) {
 55				$email = $aEmail[0];
 56			}
 57			if(!is_null($email)) {
 58				\OCP\Config::setUserValue($this->dn2username($dn), 'settings', 'email', $email);
 59			}
 60		}
 61	}
 62
 63	/**
 64	 * @brief Check if the password is correct
 65	 * @param $uid The username
 66	 * @param $password The password
 67	 * @returns true/false
 68	 *
 69	 * Check if the password is correct without logging in the user
 70	 */
 71	public function checkPassword($uid, $password) {
 72		//find out dn of the user name
 73		$filter = \OCP\Util::mb_str_replace('%uid', $uid, $this->connection->ldapLoginFilter, 'UTF-8');
 74		$ldap_users = $this->fetchListOfUsers($filter, 'dn');
 75		if(count($ldap_users) < 1) {
 76			return false;
 77		}
 78		$dn = $ldap_users[0];
 79
 80		//are the credentials OK?
 81		if(!$this->areCredentialsValid($dn, $password)) {
 82			return false;
 83		}
 84
 85		//do we have a username for him/her?
 86		$ocname = $this->dn2username($dn);
 87
 88		if($ocname) {
 89			//update some settings, if necessary
 90			$this->updateQuota($dn);
 91			$this->updateEmail($dn);
 92
 93			//give back the display name
 94			return $ocname;
 95		}
 96
 97		return false;
 98	}
 99
100	/**
101	 * @brief Get a list of all users
102	 * @returns array with all uids
103	 *
104	 * Get a list of all users.
105	 */
106	public function getUsers($search = '', $limit = 10, $offset = 0) {
107		$cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
108
109		//check if users are cached, if so return
110		$ldap_users = $this->connection->getFromCache($cachekey);
111		if(!is_null($ldap_users)) {
112			return $ldap_users;
113		}
114
115		// if we'd pass -1 to LDAP search, we'd end up in a Protocol
116		// error. With a limit of 0, we get 0 results. So we pass null.
117		if($limit <= 0) {
118			$limit = null;
119		}
120		$filter = $this->combineFilterWithAnd(array(
121			$this->connection->ldapUserFilter,
122			$this->getFilterPartForUserSearch($search)
123		));
124
125		\OCP\Util::writeLog('user_ldap',
126			'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
127			\OCP\Util::DEBUG);
128		//do the search and translate results to owncloud names
129		$ldap_users = $this->fetchListOfUsers($filter, array($this->connection->ldapUserDisplayName, 'dn'),
130			$limit, $offset);
131		$ldap_users = $this->ownCloudUserNames($ldap_users);
132		\OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
133
134		$this->connection->writeToCache($cachekey, $ldap_users);
135		return $ldap_users;
136	}
137
138	/**
139	 * @brief check if a user exists
140	 * @param string $uid the username
141	 * @return boolean
142	 */
143	public function userExists($uid) {
144		if($this->connection->isCached('userExists'.$uid)) {
145			return $this->connection->getFromCache('userExists'.$uid);
146		}
147
148		//getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
149		$dn = $this->username2dn($uid);
150		if(!$dn) {
151			$this->connection->writeToCache('userExists'.$uid, false);
152			return false;
153		}
154
155		//check if user really still exists by reading its entry
156		if(!is_array($this->readAttribute($dn, ''))) {
157			$this->connection->writeToCache('userExists'.$uid, false);
158			return false;
159		}
160
161		$this->connection->writeToCache('userExists'.$uid, true);
162		$this->updateQuota($dn);
163		return true;
164	}
165
166	/**
167	* @brief delete a user
168	* @param $uid The username of the user to delete
169	* @returns true/false
170	*
171	* Deletes a user
172	*/
173	public function deleteUser($uid) {
174		return false;
175	}
176
177	/**
178	* @brief get the user's home directory
179	* @param string $uid the username
180	* @return boolean
181	*/
182	public function getHome($uid) {
183		// user Exists check required as it is not done in user proxy!
184		if(!$this->userExists($uid)) {
185			return false;
186		}
187
188		$cacheKey = 'getHome'.$uid;
189		if($this->connection->isCached($cacheKey)) {
190			return $this->connection->getFromCache($cacheKey);
191		}
192		if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
193			$attr = substr($this->connection->homeFolderNamingRule, strlen('attr:'));
194			$homedir = $this->readAttribute($this->username2dn($uid), $attr);
195			if($homedir && isset($homedir[0])) {
196				$path = $homedir[0];
197				//if attribute's value is an absolute path take this, otherwise append it to data dir
198				//check for / at the beginning or pattern c:\ resp. c:/
199				if(
200					'/' === $path[0]
201					|| (3 < strlen($path) && ctype_alpha($path[0])
202						&& $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2]))
203				) {
204					$homedir = $path;
205				} else {
206					$homedir = \OCP\Config::getSystemValue('datadirectory',
207						\OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
208				}
209				$this->connection->writeToCache($cacheKey, $homedir);
210				return $homedir;
211			}
212		}
213
214		//false will apply default behaviour as defined and done by OC_User
215		$this->connection->writeToCache($cacheKey, false);
216		return false;
217	}
218
219	/**
220	 * @brief get display name of the user
221	 * @param $uid user ID of the user
222	 * @return display name
223	 */
224	public function getDisplayName($uid) {
225		if(!$this->userExists($uid)) {
226			return false;
227		}
228
229		$cacheKey = 'getDisplayName'.$uid;
230		if(!is_null($displayName = $this->connection->getFromCache($cacheKey))) {
231			return $displayName;
232		}
233
234		$displayName = $this->readAttribute(
235			$this->username2dn($uid),
236			$this->connection->ldapUserDisplayName);
237
238		if($displayName && (count($displayName) > 0)) {
239			$this->connection->writeToCache($cacheKey, $displayName[0]);
240			return $displayName[0];
241		}
242
243		return null;
244	}
245
246	/**
247	 * @brief Get a list of all display names
248	 * @returns array with  all displayNames (value) and the correspondig uids (key)
249	 *
250	 * Get a list of all display names and user ids.
251	 */
252	public function getDisplayNames($search = '', $limit = null, $offset = null) {
253		$cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
254		if(!is_null($displayNames = $this->connection->getFromCache($cacheKey))) {
255			return $displayNames;
256		}
257
258		$displayNames = array();
259		$users = $this->getUsers($search, $limit, $offset);
260		foreach ($users as $user) {
261			$displayNames[$user] = $this->getDisplayName($user);
262		}
263		$this->connection->writeToCache($cacheKey, $displayNames);
264		return $displayNames;
265	}
266
267		/**
268	* @brief Check if backend implements actions
269	* @param $actions bitwise-or'ed actions
270	* @returns boolean
271	*
272	* Returns the supported actions as int to be
273	* compared with OC_USER_BACKEND_CREATE_USER etc.
274	*/
275	public function implementsActions($actions) {
276		return (bool)((OC_USER_BACKEND_CHECK_PASSWORD
277			| OC_USER_BACKEND_GET_HOME
278			| OC_USER_BACKEND_GET_DISPLAYNAME)
279			& $actions);
280	}
281
282	/**
283	 * @return bool
284	 */
285	public function hasUserListings() {
286		return true;
287	}
288}