user_ldap.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. use OCA\user_ldap\lib\BackendUtility;
  26. class USER_LDAP extends BackendUtility implements \OCP\UserInterface {
  27. /**
  28. * checks whether the user is allowed to change his avatar in ownCloud
  29. * @param string $uid the ownCloud user name
  30. * @return boolean either the user can or cannot
  31. */
  32. public function canChangeAvatar($uid) {
  33. $user = $this->access->userManager->get($uid);
  34. if(is_null($user)) {
  35. return false;
  36. }
  37. if($user->getAvatarImage() === false) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. /**
  43. * Check if the password is correct
  44. * @param string $uid The username
  45. * @param string $password The password
  46. * @return boolean
  47. *
  48. * Check if the password is correct without logging in the user
  49. */
  50. public function checkPassword($uid, $password) {
  51. $uid = $this->access->escapeFilterPart($uid);
  52. //find out dn of the user name
  53. $filter = \OCP\Util::mb_str_replace(
  54. '%uid', $uid, $this->access->connection->ldapLoginFilter, 'UTF-8');
  55. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  56. if(count($ldap_users) < 1) {
  57. return false;
  58. }
  59. $dn = $ldap_users[0];
  60. $user = $this->access->userManager->get($dn);
  61. if(is_null($user)) {
  62. \OCP\Util::writeLog('user_ldap',
  63. 'LDAP Login: Could not get user object for DN ' . $dn .
  64. '. Maybe the LDAP entry has no set display name attribute?',
  65. \OCP\Util::WARN);
  66. return false;
  67. }
  68. if($user->getUsername() !== false) {
  69. //are the credentials OK?
  70. if(!$this->access->areCredentialsValid($dn, $password)) {
  71. return false;
  72. }
  73. $user->markLogin();
  74. return $user->getUsername();
  75. }
  76. return false;
  77. }
  78. /**
  79. * Get a list of all users
  80. * @return string[] with all uids
  81. *
  82. * Get a list of all users.
  83. */
  84. public function getUsers($search = '', $limit = 10, $offset = 0) {
  85. $search = $this->access->escapeFilterPart($search);
  86. $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
  87. //check if users are cached, if so return
  88. $ldap_users = $this->access->connection->getFromCache($cachekey);
  89. if(!is_null($ldap_users)) {
  90. return $ldap_users;
  91. }
  92. // if we'd pass -1 to LDAP search, we'd end up in a Protocol
  93. // error. With a limit of 0, we get 0 results. So we pass null.
  94. if($limit <= 0) {
  95. $limit = null;
  96. }
  97. $filter = $this->access->combineFilterWithAnd(array(
  98. $this->access->connection->ldapUserFilter,
  99. $this->access->getFilterPartForUserSearch($search)
  100. ));
  101. \OCP\Util::writeLog('user_ldap',
  102. 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
  103. \OCP\Util::DEBUG);
  104. //do the search and translate results to owncloud names
  105. $ldap_users = $this->access->fetchListOfUsers(
  106. $filter,
  107. array($this->access->connection->ldapUserDisplayName, 'dn'),
  108. $limit, $offset);
  109. $ldap_users = $this->access->ownCloudUserNames($ldap_users);
  110. \OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
  111. $this->access->connection->writeToCache($cachekey, $ldap_users);
  112. return $ldap_users;
  113. }
  114. /**
  115. * check if a user exists
  116. * @param string $uid the username
  117. * @return boolean
  118. */
  119. public function userExists($uid) {
  120. if($this->access->connection->isCached('userExists'.$uid)) {
  121. return $this->access->connection->getFromCache('userExists'.$uid);
  122. }
  123. //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
  124. $user = $this->access->userManager->get($uid);
  125. if(is_null($user)) {
  126. \OCP\Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
  127. $this->access->connection->ldapHost, \OCP\Util::DEBUG);
  128. $this->access->connection->writeToCache('userExists'.$uid, false);
  129. return false;
  130. }
  131. $dn = $user->getDN();
  132. //check if user really still exists by reading its entry
  133. if(!is_array($this->access->readAttribute($dn, ''))) {
  134. \OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn.' on '.
  135. $this->access->connection->ldapHost, \OCP\Util::DEBUG);
  136. $this->access->connection->writeToCache('userExists'.$uid, false);
  137. return false;
  138. }
  139. $this->access->connection->writeToCache('userExists'.$uid, true);
  140. $user->update();
  141. return true;
  142. }
  143. /**
  144. * delete a user
  145. * @param string $uid The username of the user to delete
  146. * @return bool
  147. *
  148. * Deletes a user
  149. */
  150. public function deleteUser($uid) {
  151. return false;
  152. }
  153. /**
  154. * get the user's home directory
  155. * @param string $uid the username
  156. * @return boolean
  157. */
  158. public function getHome($uid) {
  159. // user Exists check required as it is not done in user proxy!
  160. if(!$this->userExists($uid)) {
  161. return false;
  162. }
  163. $cacheKey = 'getHome'.$uid;
  164. if($this->access->connection->isCached($cacheKey)) {
  165. return $this->access->connection->getFromCache($cacheKey);
  166. }
  167. if(strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0) {
  168. $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
  169. $homedir = $this->access->readAttribute(
  170. $this->access->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->access->connection->writeToCache($cacheKey, $homedir);
  186. return $homedir;
  187. }
  188. }
  189. //false will apply default behaviour as defined and done by OC_User
  190. $this->access->connection->writeToCache($cacheKey, false);
  191. return false;
  192. }
  193. /**
  194. * get display name of the user
  195. * @param string $uid user ID of the user
  196. * @return string 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->access->connection->getFromCache($cacheKey))) {
  204. return $displayName;
  205. }
  206. $displayName = $this->access->readAttribute(
  207. $this->access->username2dn($uid),
  208. $this->access->connection->ldapUserDisplayName);
  209. if($displayName && (count($displayName) > 0)) {
  210. $this->access->connection->writeToCache($cacheKey, $displayName[0]);
  211. return $displayName[0];
  212. }
  213. return null;
  214. }
  215. /**
  216. * Get a list of all display names
  217. * @return 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->access->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->access->connection->writeToCache($cacheKey, $displayNames);
  232. return $displayNames;
  233. }
  234. /**
  235. * Check if backend implements actions
  236. * @param int $actions bitwise-or'ed actions
  237. * @return 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. | OC_USER_BACKEND_PROVIDE_AVATAR
  247. | OC_USER_BACKEND_COUNT_USERS)
  248. & $actions);
  249. }
  250. /**
  251. * @return bool
  252. */
  253. public function hasUserListings() {
  254. return true;
  255. }
  256. /**
  257. * counts the users in LDAP
  258. *
  259. * @return int|bool
  260. */
  261. public function countUsers() {
  262. $filter = $this->access->getFilterForUserCount();
  263. $entries = $this->access->countUsers($filter);
  264. return $entries;
  265. }
  266. }