user_ldap.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. $cacheKey = 'getHome'.$uid;
  161. if($this->connection->isCached($cacheKey)) {
  162. return $this->connection->getFromCache($cacheKey);
  163. }
  164. if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
  165. $attr = substr($this->connection->homeFolderNamingRule, strlen('attr:'));
  166. $homedir = $this->readAttribute($this->username2dn($uid), $attr);
  167. if($homedir && isset($homedir[0])) {
  168. $path = $homedir[0];
  169. //if attribute's value is an absolute path take this, otherwise append it to data dir
  170. //check for / at the beginning or pattern c:\ resp. c:/
  171. if(
  172. '/' == $path[0]
  173. || (3 < strlen($path) && ctype_alpha($path[0])
  174. && $path[1] == ':' && ('\\' == $path[2] || '/' == $path[2]))
  175. ) {
  176. $homedir = $path;
  177. } else {
  178. $homedir = \OCP\Config::getSystemValue('datadirectory',
  179. \OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
  180. }
  181. $this->connection->writeToCache($cacheKey, $homedir);
  182. return $homedir;
  183. }
  184. }
  185. //false will apply default behaviour as defined and done by OC_User
  186. $this->connection->writeToCache($cacheKey, false);
  187. return false;
  188. }
  189. /**
  190. * @brief get display name of the user
  191. * @param $uid user ID of the user
  192. * @return display name
  193. */
  194. public function getDisplayName($uid) {
  195. $cacheKey = 'getDisplayName'.$uid;
  196. if(!is_null($displayName = $this->connection->getFromCache($cacheKey))) {
  197. return $displayName;
  198. }
  199. $displayName = $this->readAttribute(
  200. $this->username2dn($uid),
  201. $this->connection->ldapUserDisplayName);
  202. if($displayName && (count($displayName) > 0)) {
  203. $this->connection->writeToCache($cacheKey, $displayName[0]);
  204. return $displayName[0];
  205. }
  206. return null;
  207. }
  208. /**
  209. * @brief Get a list of all display names
  210. * @returns array with all displayNames (value) and the correspondig uids (key)
  211. *
  212. * Get a list of all display names and user ids.
  213. */
  214. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  215. $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
  216. if(!is_null($displayNames = $this->connection->getFromCache($cacheKey))) {
  217. return $displayNames;
  218. }
  219. $displayNames = array();
  220. $users = $this->getUsers($search, $limit, $offset);
  221. foreach ($users as $user) {
  222. $displayNames[$user] = $this->getDisplayName($user);
  223. }
  224. $this->connection->writeToCache($cacheKey, $displayNames);
  225. return $displayNames;
  226. }
  227. /**
  228. * @brief Check if backend implements actions
  229. * @param $actions bitwise-or'ed actions
  230. * @returns boolean
  231. *
  232. * Returns the supported actions as int to be
  233. * compared with OC_USER_BACKEND_CREATE_USER etc.
  234. */
  235. public function implementsActions($actions) {
  236. return (bool)((OC_USER_BACKEND_CHECK_PASSWORD
  237. | OC_USER_BACKEND_GET_HOME
  238. | OC_USER_BACKEND_GET_DISPLAYNAME)
  239. & $actions);
  240. }
  241. /**
  242. * @return bool
  243. */
  244. public function hasUserListings() {
  245. return true;
  246. }
  247. }