PageRenderTime 28ms CodeModel.GetById 3ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/user_ldap/user_ldap.php

https://github.com/jlgg/simple_trash
PHP | 223 lines | 119 code | 27 blank | 77 comment | 21 complexity | d7b44f3fc938243e22676062dc707bcc 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 error. With a limit of 0, we get 0 results. So we pass null.
  101. if($limit <= 0) {
  102. $limit = null;
  103. }
  104. $search = empty($search) ? '*' : '*'.$search.'*';
  105. $filter = $this->combineFilterWithAnd(array(
  106. $this->connection->ldapUserFilter,
  107. $this->connection->ldapUserDisplayName.'='.$search
  108. ));
  109. \OCP\Util::writeLog('user_ldap', 'getUsers: Options: search '.$search.' limit '.$limit.' offset '.$offset.' Filter: '.$filter, \OCP\Util::DEBUG);
  110. //do the search and translate results to owncloud names
  111. $ldap_users = $this->fetchListOfUsers($filter, array($this->connection->ldapUserDisplayName, 'dn'), $limit, $offset);
  112. $ldap_users = $this->ownCloudUserNames($ldap_users);
  113. \OCP\Util::writeLog('user_ldap', 'getUsers: '.count($ldap_users). ' Users found', \OCP\Util::DEBUG);
  114. $this->connection->writeToCache($cachekey, $ldap_users);
  115. return $ldap_users;
  116. }
  117. /**
  118. * @brief check if a user exists
  119. * @param string $uid the username
  120. * @return boolean
  121. */
  122. public function userExists($uid) {
  123. if($this->connection->isCached('userExists'.$uid)) {
  124. return $this->connection->getFromCache('userExists'.$uid);
  125. }
  126. //getting dn, if false the user does not exist. If dn, he may be mapped only, requires more checking.
  127. $dn = $this->username2dn($uid);
  128. if(!$dn) {
  129. $this->connection->writeToCache('userExists'.$uid, false);
  130. return false;
  131. }
  132. //check if user really still exists by reading its entry
  133. if(!is_array($this->readAttribute($dn, ''))) {
  134. $this->connection->writeToCache('userExists'.$uid, false);
  135. return false;
  136. }
  137. $this->connection->writeToCache('userExists'.$uid, true);
  138. return true;
  139. }
  140. /**
  141. * @brief delete a user
  142. * @param $uid The username of the user to delete
  143. * @returns true/false
  144. *
  145. * Deletes a user
  146. */
  147. public function deleteUser($uid) {
  148. return false;
  149. }
  150. /**
  151. * @brief determine the user's home directory
  152. * @param string $uid the owncloud username
  153. * @return boolean
  154. */
  155. private function determineHomeDir($uid) {
  156. if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) {
  157. $attr = substr($this->connection->homeFolderNamingRule, strlen('attr:'));
  158. $homedir = $this->readAttribute($this->username2dn($uid), $attr);
  159. if($homedir) {
  160. $homedir = \OCP\Config::getSystemValue( "datadirectory", \OC::$SERVERROOT."/data" ) . '/' . $homedir[0];
  161. \OCP\Config::setUserValue($uid, 'user_ldap', 'homedir', $homedir);
  162. return $homedir;
  163. }
  164. }
  165. //fallback and default: username
  166. $homedir = \OCP\Config::getSystemValue( "datadirectory", \OC::$SERVERROOT."/data" ) . '/' . $uid;
  167. \OCP\Config::setUserValue($uid, 'user_ldap', 'homedir', $homedir);
  168. return $homedir;
  169. }
  170. /**
  171. * @brief get the user's home directory
  172. * @param string $uid the username
  173. * @return boolean
  174. */
  175. public function getHome($uid) {
  176. if($this->userExists($uid)) {
  177. $homedir = \OCP\Config::getUserValue($uid, 'user_ldap', 'homedir', false);
  178. if(!$homedir) {
  179. $homedir = $this->determineHomeDir($uid);
  180. }
  181. return $homedir;
  182. }
  183. return false;
  184. }
  185. /**
  186. * @brief Check if backend implements actions
  187. * @param $actions bitwise-or'ed actions
  188. * @returns boolean
  189. *
  190. * Returns the supported actions as int to be
  191. * compared with OC_USER_BACKEND_CREATE_USER etc.
  192. */
  193. public function implementsActions($actions) {
  194. return (bool)((OC_USER_BACKEND_CHECK_PASSWORD | OC_USER_BACKEND_GET_HOME) & $actions);
  195. }
  196. }