You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

user_ldap.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. private function updateQuota($dn) {
  28. $quota = null;
  29. $quotaDefault = $this->access->connection->ldapQuotaDefault;
  30. $quotaAttribute = $this->access->connection->ldapQuotaAttribute;
  31. if(!empty($quotaDefault)) {
  32. $quota = $quotaDefault;
  33. }
  34. if(!empty($quotaAttribute)) {
  35. $aQuota = $this->access->readAttribute($dn, $quotaAttribute);
  36. if($aQuota && (count($aQuota) > 0)) {
  37. $quota = $aQuota[0];
  38. }
  39. }
  40. if(!is_null($quota)) {
  41. \OCP\Config::setUserValue( $this->access->dn2username($dn),
  42. 'files',
  43. 'quota',
  44. \OCP\Util::computerFileSize($quota));
  45. }
  46. }
  47. private function updateEmail($dn) {
  48. $email = null;
  49. $emailAttribute = $this->access->connection->ldapEmailAttribute;
  50. if(!empty($emailAttribute)) {
  51. $aEmail = $this->access->readAttribute($dn, $emailAttribute);
  52. if($aEmail && (count($aEmail) > 0)) {
  53. $email = $aEmail[0];
  54. }
  55. if(!is_null($email)) {
  56. \OCP\Config::setUserValue( $this->access->dn2username($dn),
  57. 'settings',
  58. 'email',
  59. $email);
  60. }
  61. }
  62. }
  63. /**
  64. * @brief reads jpegPhoto and set is as avatar if available
  65. * @param $uid string ownCloud user name
  66. * @param $dn string the user's LDAP DN
  67. * @return void
  68. */
  69. private function updateAvatar($uid, $dn) {
  70. $hasLoggedIn = \OCP\Config::getUserValue($uid, 'user_ldap',
  71. 'firstLoginAccomplished', 0);
  72. $lastChecked = \OCP\Config::getUserValue($uid, 'user_ldap',
  73. 'lastJpegPhotoLookup', 0);
  74. if(($hasLoggedIn !== '1') || (time() - intval($lastChecked)) < 86400 ) {
  75. //update only once a day
  76. return;
  77. }
  78. $avatarImage = $this->getAvatarImage($uid, $dn);
  79. if($avatarImage === false) {
  80. //not set, nothing left to do;
  81. return;
  82. }
  83. $image = new \OCP\Image();
  84. $image->loadFromBase64(base64_encode($avatarImage));
  85. if(!$image->valid()) {
  86. \OCP\Util::writeLog('user_ldap', 'jpegPhoto data invalid for '.$dn,
  87. \OCP\Util::ERROR);
  88. return;
  89. }
  90. //make sure it is a square and not bigger than 128x128
  91. $size = min(array($image->width(), $image->height(), 128));
  92. if(!$image->centerCrop($size)) {
  93. \OCP\Util::writeLog('user_ldap',
  94. 'croping image for avatar failed for '.$dn,
  95. \OCP\Util::ERROR);
  96. return;
  97. }
  98. if(!\OC\Files\Filesystem::$loaded) {
  99. \OC_Util::setupFS($uid);
  100. }
  101. $avatarManager = \OC::$server->getAvatarManager();
  102. $avatar = $avatarManager->getAvatar($uid);
  103. $avatar->set($image);
  104. }
  105. /**
  106. * @brief checks whether the user is allowed to change his avatar in ownCloud
  107. * @param $uid string the ownCloud user name
  108. * @return boolean either the user can or cannot
  109. */
  110. public function canChangeAvatar($uid) {
  111. $dn = $this->access->username2dn($uid);
  112. if(!$dn) {
  113. return false;
  114. }
  115. if($this->getAvatarImage($uid, $dn) === false) {
  116. //The user is allowed to change his avatar in ownCloud only if no
  117. //avatar is provided by LDAP
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * @brief reads the image from LDAP that shall be used as Avatar
  124. * @param $uid string, the ownCloud user name
  125. * @param $dn string, the user DN
  126. * @return string data (provided by LDAP) | false
  127. */
  128. private function getAvatarImage($uid, $dn) {
  129. $attributes = array('jpegPhoto', 'thumbnailPhoto');
  130. foreach($attributes as $attribute) {
  131. $result = $this->access->readAttribute($dn, $attribute);
  132. \OCP\Config::setUserValue($uid, 'user_ldap', 'lastJpegPhotoLookup',
  133. time());
  134. if($result !== false && is_array($result) && isset($result[0])) {
  135. return $result[0];
  136. }
  137. }
  138. return false;
  139. }
  140. /**
  141. * @brief Check if the password is correct
  142. * @param string $uid The username
  143. * @param string $password The password
  144. * @return boolean
  145. *
  146. * Check if the password is correct without logging in the user
  147. */
  148. public function checkPassword($uid, $password) {
  149. $uid = $this->access->escapeFilterPart($uid);
  150. //find out dn of the user name
  151. $filter = \OCP\Util::mb_str_replace(
  152. '%uid', $uid, $this->access->connection->ldapLoginFilter, 'UTF-8');
  153. $ldap_users = $this->access->fetchListOfUsers($filter, 'dn');
  154. if(count($ldap_users) < 1) {
  155. return false;
  156. }
  157. $dn = $ldap_users[0];
  158. //do we have a username for him/her?
  159. $ocname = $this->access->dn2username($dn);
  160. if($ocname) {
  161. //update some settings, if necessary
  162. $this->updateQuota($dn);
  163. $this->updateEmail($dn);
  164. //are the credentials OK?
  165. if(!$this->access->areCredentialsValid($dn, $password)) {
  166. return false;
  167. }
  168. \OCP\Config::setUserValue($ocname, 'user_ldap',
  169. 'firstLoginAccomplished', 1);
  170. $this->updateAvatar($ocname, $dn);
  171. //give back the display name
  172. return $ocname;
  173. }
  174. return false;
  175. }
  176. /**
  177. * @brief Get a list of all users
  178. * @returns array with all uids
  179. *
  180. * Get a list of all users.
  181. */
  182. public function getUsers($search = '', $limit = 10, $offset = 0) {
  183. $search = $this->access->escapeFilterPart($search);
  184. $cachekey = 'getUsers-'.$search.'-'.$limit.'-'.$offset;
  185. //check if users are cached, if so return
  186. $ldap_users = $this->access->connection->getFromCache($cachekey);
  187. if(!is_null($ldap_users)) {
  188. return $ldap_users;
  189. }
  190. // if we'd pass -1 to LDAP search, we'd end up in a Protocol
  191. // error. With a limit of 0, we get 0 results. So we pass null.
  192. if($limit <= 0) {
  193. $limit = null;
  194. }
  195. $filter = $this->access->combineFilterWithAnd(array(
  196. $this->access->connection->ldapUserFilter,
  197. $this->access->getFilterPartForUserSearch($search)
  198. ));
  199. \OCP\Util::writeLog('user_ldap',
  200. 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter,
  201. \OCP\Util::DEBUG);
  202. //do the search and translate results to owncloud names
  203. $ldap_users = $this->access->fetchListOfUsers(
  204. $filter,
  205. array($this->access->connection->ldapUserDisplayName, 'dn'),
  206. $limit, $offset);
  207. $ldap_users = $this->access->ownCloudUserNames($ldap_users);
  208. \OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
  209. $this->access->connection->writeToCache($cachekey, $ldap_users);
  210. return $ldap_users;
  211. }
  212. /**
  213. * @brief check if a user exists
  214. * @param string $uid the username
  215. * @return boolean
  216. */
  217. public function userExists($uid) {
  218. if($this->access->connection->isCached('userExists'.$uid)) {
  219. return $this->access->connection->getFromCache('userExists'.$uid);
  220. }
  221. //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
  222. $dn = $this->access->username2dn($uid);
  223. if(!$dn) {
  224. \OCP\Util::writeLog('user_ldap', 'No DN found for '.$uid.' on '.
  225. $this->access->connection->ldapHost, \OCP\Util::DEBUG);
  226. $this->access->connection->writeToCache('userExists'.$uid, false);
  227. return false;
  228. }
  229. //check if user really still exists by reading its entry
  230. if(!is_array($this->access->readAttribute($dn, ''))) {
  231. \OCP\Util::writeLog('user_ldap', 'LDAP says no user '.$dn.' on '.
  232. $this->access->connection->ldapHost, \OCP\Util::DEBUG);
  233. $this->access->connection->writeToCache('userExists'.$uid, false);
  234. return false;
  235. }
  236. $this->access->connection->writeToCache('userExists'.$uid, true);
  237. $this->updateQuota($dn);
  238. $this->updateAvatar($uid, $dn);
  239. return true;
  240. }
  241. /**
  242. * @brief delete a user
  243. * @param $uid The username of the user to delete
  244. * @returns true/false
  245. *
  246. * Deletes a user
  247. */
  248. public function deleteUser($uid) {
  249. return false;
  250. }
  251. /**
  252. * @brief get the user's home directory
  253. * @param string $uid the username
  254. * @return boolean
  255. */
  256. public function getHome($uid) {
  257. // user Exists check required as it is not done in user proxy!
  258. if(!$this->userExists($uid)) {
  259. return false;
  260. }
  261. $cacheKey = 'getHome'.$uid;
  262. if($this->access->connection->isCached($cacheKey)) {
  263. return $this->access->connection->getFromCache($cacheKey);
  264. }
  265. if(strpos($this->access->connection->homeFolderNamingRule, 'attr:') === 0) {
  266. $attr = substr($this->access->connection->homeFolderNamingRule, strlen('attr:'));
  267. $homedir = $this->access->readAttribute(
  268. $this->access->username2dn($uid), $attr);
  269. if($homedir && isset($homedir[0])) {
  270. $path = $homedir[0];
  271. //if attribute's value is an absolute path take this, otherwise append it to data dir
  272. //check for / at the beginning or pattern c:\ resp. c:/
  273. if(
  274. '/' === $path[0]
  275. || (3 < strlen($path) && ctype_alpha($path[0])
  276. && $path[1] === ':' && ('\\' === $path[2] || '/' === $path[2]))
  277. ) {
  278. $homedir = $path;
  279. } else {
  280. $homedir = \OCP\Config::getSystemValue('datadirectory',
  281. \OC::$SERVERROOT.'/data' ) . '/' . $homedir[0];
  282. }
  283. $this->access->connection->writeToCache($cacheKey, $homedir);
  284. return $homedir;
  285. }
  286. }
  287. //false will apply default behaviour as defined and done by OC_User
  288. $this->access->connection->writeToCache($cacheKey, false);
  289. return false;
  290. }
  291. /**
  292. * @brief get display name of the user
  293. * @param $uid user ID of the user
  294. * @return display name
  295. */
  296. public function getDisplayName($uid) {
  297. if(!$this->userExists($uid)) {
  298. return false;
  299. }
  300. $cacheKey = 'getDisplayName'.$uid;
  301. if(!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) {
  302. return $displayName;
  303. }
  304. $displayName = $this->access->readAttribute(
  305. $this->access->username2dn($uid),
  306. $this->access->connection->ldapUserDisplayName);
  307. if($displayName && (count($displayName) > 0)) {
  308. $this->access->connection->writeToCache($cacheKey, $displayName[0]);
  309. return $displayName[0];
  310. }
  311. return null;
  312. }
  313. /**
  314. * @brief Get a list of all display names
  315. * @returns array with all displayNames (value) and the correspondig uids (key)
  316. *
  317. * Get a list of all display names and user ids.
  318. */
  319. public function getDisplayNames($search = '', $limit = null, $offset = null) {
  320. $cacheKey = 'getDisplayNames-'.$search.'-'.$limit.'-'.$offset;
  321. if(!is_null($displayNames = $this->access->connection->getFromCache($cacheKey))) {
  322. return $displayNames;
  323. }
  324. $displayNames = array();
  325. $users = $this->getUsers($search, $limit, $offset);
  326. foreach ($users as $user) {
  327. $displayNames[$user] = $this->getDisplayName($user);
  328. }
  329. $this->access->connection->writeToCache($cacheKey, $displayNames);
  330. return $displayNames;
  331. }
  332. /**
  333. * @brief Check if backend implements actions
  334. * @param $actions bitwise-or'ed actions
  335. * @returns boolean
  336. *
  337. * Returns the supported actions as int to be
  338. * compared with OC_USER_BACKEND_CREATE_USER etc.
  339. */
  340. public function implementsActions($actions) {
  341. return (bool)((OC_USER_BACKEND_CHECK_PASSWORD
  342. | OC_USER_BACKEND_GET_HOME
  343. | OC_USER_BACKEND_GET_DISPLAYNAME
  344. | OC_USER_BACKEND_PROVIDE_AVATAR
  345. | OC_USER_BACKEND_COUNT_USERS)
  346. & $actions);
  347. }
  348. /**
  349. * @return bool
  350. */
  351. public function hasUserListings() {
  352. return true;
  353. }
  354. /**
  355. * counts the users in LDAP
  356. *
  357. * @return int | bool
  358. */
  359. public function countUsers() {
  360. $filter = \OCP\Util::mb_str_replace(
  361. '%uid', '*', $this->access->connection->ldapLoginFilter, 'UTF-8');
  362. $entries = $this->access->countUsers($filter);
  363. return $entries;
  364. }
  365. }