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.

LDAPProvider.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2016, Roger Szabo (roger.szabo@web.de)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\User_LDAP;
  23. use OCP\LDAP\ILDAPProvider;
  24. use OCP\LDAP\IDeletionFlagSupport;
  25. use OCP\IServerContainer;
  26. use OCA\User_LDAP\User\DeletedUsersIndex;
  27. /**
  28. * LDAP provider for pulic access to the LDAP backend.
  29. */
  30. class LDAPProvider implements ILDAPProvider, IDeletionFlagSupport {
  31. private $backend;
  32. private $logger;
  33. private $helper;
  34. private $deletedUsersIndex;
  35. /**
  36. * Create new LDAPProvider
  37. * @param \OCP\IServerContainer $serverContainer
  38. * @throws \Exception if user_ldap app was not enabled
  39. */
  40. public function __construct(IServerContainer $serverContainer, Helper $helper, DeletedUsersIndex $deletedUsersIndex) {
  41. $this->logger = $serverContainer->getLogger();
  42. $this->helper = $helper;
  43. $this->deletedUsersIndex = $deletedUsersIndex;
  44. foreach ($serverContainer->getUserManager()->getBackends() as $backend){
  45. $this->logger->debug('instance '.get_class($backend).' backend.', ['app' => 'user_ldap']);
  46. if ($backend instanceof IUserLDAP) {
  47. $this->backend = $backend;
  48. return;
  49. }
  50. }
  51. throw new \Exception('To use the LDAPProvider, user_ldap app must be enabled');
  52. }
  53. /**
  54. * Translate an user id to LDAP DN
  55. * @param string $uid user id
  56. * @return string with the LDAP DN
  57. * @throws \Exception if translation was unsuccessful
  58. */
  59. public function getUserDN($uid) {
  60. if(!$this->backend->userExists($uid)){
  61. throw new \Exception('User id not found in LDAP');
  62. }
  63. $result = $this->backend->getLDAPAccess($uid)->username2dn($uid);
  64. if(!$result){
  65. throw new \Exception('Translation to LDAP DN unsuccessful');
  66. }
  67. return $result;
  68. }
  69. /**
  70. * Translate a LDAP DN to an internal user name. If there is no mapping between
  71. * the DN and the user name, a new one will be created.
  72. * @param string $dn LDAP DN
  73. * @return string with the internal user name
  74. * @throws \Exception if translation was unsuccessful
  75. */
  76. public function getUserName($dn) {
  77. $result = $this->backend->dn2UserName($dn);
  78. if(!$result){
  79. throw new \Exception('Translation to internal user name unsuccessful');
  80. }
  81. return $result;
  82. }
  83. /**
  84. * Convert a stored DN so it can be used as base parameter for LDAP queries.
  85. * @param string $dn the DN in question
  86. * @return string
  87. */
  88. public function DNasBaseParameter($dn) {
  89. return $this->helper->DNasBaseParameter($dn);
  90. }
  91. /**
  92. * Sanitize a DN received from the LDAP server.
  93. * @param array $dn the DN in question
  94. * @return array the sanitized DN
  95. */
  96. public function sanitizeDN($dn) {
  97. return $this->helper->sanitizeDN($dn);
  98. }
  99. /**
  100. * Return a new LDAP connection resource for the specified user.
  101. * The connection must be closed manually.
  102. * @param string $uid user id
  103. * @return resource of the LDAP connection
  104. * @throws \Exception if user id was not found in LDAP
  105. */
  106. public function getLDAPConnection($uid) {
  107. if(!$this->backend->userExists($uid)){
  108. throw new \Exception('User id not found in LDAP');
  109. }
  110. return $this->backend->getNewLDAPConnection($uid);
  111. }
  112. /**
  113. * Get the LDAP base for users.
  114. * @param string $uid user id
  115. * @return string the base for users
  116. * @throws \Exception if user id was not found in LDAP
  117. */
  118. public function getLDAPBaseUsers($uid) {
  119. if(!$this->backend->userExists($uid)){
  120. throw new \Exception('User id not found in LDAP');
  121. }
  122. return $this->backend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_users'];
  123. }
  124. /**
  125. * Get the LDAP base for groups.
  126. * @param string $uid user id
  127. * @return string the base for groups
  128. * @throws \Exception if user id was not found in LDAP
  129. */
  130. public function getLDAPBaseGroups($uid) {
  131. if(!$this->backend->userExists($uid)){
  132. throw new \Exception('User id not found in LDAP');
  133. }
  134. return $this->backend->getLDAPAccess($uid)->getConnection()->getConfiguration()['ldap_base_groups'];
  135. }
  136. /**
  137. * Clear the cache if a cache is used, otherwise do nothing.
  138. * @param string $uid user id
  139. * @throws \Exception if user id was not found in LDAP
  140. */
  141. public function clearCache($uid) {
  142. if(!$this->backend->userExists($uid)){
  143. throw new \Exception('User id not found in LDAP');
  144. }
  145. $this->backend->getLDAPAccess($uid)->getConnection()->clearCache();
  146. }
  147. /**
  148. * Check whether a LDAP DN exists
  149. * @param string $dn LDAP DN
  150. * @return bool whether the DN exists
  151. */
  152. public function dnExists($dn) {
  153. $result = $this->backend->dn2UserName($dn);
  154. return !$result ? false : true;
  155. }
  156. /**
  157. * Flag record for deletion.
  158. * @param string $uid user id
  159. */
  160. public function flagRecord($uid) {
  161. $this->deletedUsersIndex->markUser($uid);
  162. }
  163. /**
  164. * Unflag record for deletion.
  165. * @param string $uid user id
  166. */
  167. public function unflagRecord($uid) {
  168. //do nothing
  169. }
  170. }