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