user_ldap.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. class OC_USER_LDAP extends OC_User_Backend {
  25. // cached settings
  26. protected $ldapUserFilter;
  27. protected $ldapQuotaAttribute;
  28. protected $ldapQuotaDefault;
  29. protected $ldapEmailAttribute;
  30. // will be retrieved from LDAP server
  31. protected $ldap_dc = false;
  32. public function __construct() {
  33. $this->ldapUserFilter = OCP\Config::getAppValue('user_ldap', 'ldap_userlist_filter', '(objectClass=posixAccount)');
  34. $this->ldapQuotaAttribute = OCP\Config::getAppValue('user_ldap', 'ldap_quota_attr', '');
  35. $this->ldapQuotaDefault = OCP\Config::getAppValue('user_ldap', 'ldap_quota_def', '');
  36. $this->ldapEmailAttribute = OCP\Config::getAppValue('user_ldap', 'ldap_email_attr', '');
  37. }
  38. private function updateQuota($dn) {
  39. $quota = null;
  40. if(!empty($this->ldapQuotaDefault)) {
  41. $quota = $this->ldapQuotaDefault;
  42. }
  43. if(!empty($this->ldapQuotaAttribute)) {
  44. $aQuota = OC_LDAP::readAttribute($dn, $this->ldapQuotaAttribute);
  45. if($aQuota && (count($aQuota) > 0)) {
  46. $quota = $aQuota[0];
  47. }
  48. }
  49. if(!is_null($quota)) {
  50. OCP\Config::setUserValue(OC_LDAP::dn2username($dn), 'files', 'quota', OCP\Util::computerFileSize($quota));
  51. }
  52. }
  53. private function updateEmail($dn) {
  54. $email = null;
  55. if(!empty($this->ldapEmailAttribute)) {
  56. $aEmail = OC_LDAP::readAttribute($dn, $this->ldapEmailAttribute);
  57. if($aEmail && (count($aEmail) > 0)) {
  58. $email = $aEmail[0];
  59. }
  60. if(!is_null($email)){
  61. OCP\Config::setUserValue(OC_LDAP::dn2username($dn), 'settings', 'email', $email);
  62. }
  63. }
  64. }
  65. /**
  66. * @brief Check if the password is correct
  67. * @param $uid The username
  68. * @param $password The password
  69. * @returns true/false
  70. *
  71. * Check if the password is correct without logging in the user
  72. */
  73. public function checkPassword($uid, $password){
  74. //find out dn of the user name
  75. $filter = str_replace('%uid', $uid, OC_LDAP::conf('ldapLoginFilter'));
  76. $ldap_users = OC_LDAP::fetchListOfUsers($filter, 'dn');
  77. if(count($ldap_users) < 1) {
  78. return false;
  79. }
  80. $dn = $ldap_users[0];
  81. //are the credentials OK?
  82. if(!OC_LDAP::areCredentialsValid($dn, $password)) {
  83. return false;
  84. }
  85. //update some settings, if necessary
  86. $this->updateQuota($dn);
  87. $this->updateEmail($dn);
  88. //give back the display name
  89. return OC_LDAP::dn2username($dn);
  90. }
  91. /**
  92. * @brief Get a list of all users
  93. * @returns array with all uids
  94. *
  95. * Get a list of all users.
  96. */
  97. public function getUsers(){
  98. $ldap_users = OC_LDAP::fetchListOfUsers($this->ldapUserFilter, array(OC_LDAP::conf('ldapUserDisplayName'), 'dn'));
  99. $users = OC_LDAP::ownCloudUserNames($ldap_users);
  100. return $users;
  101. }
  102. /**
  103. * @brief check if a user exists
  104. * @param string $uid the username
  105. * @return boolean
  106. */
  107. public function userExists($uid){
  108. return in_array($uid, self::getUsers());
  109. }
  110. }
  111. ?>