UserPluginManager.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\User_LDAP;
  7. use OC\User\Backend;
  8. use Psr\Log\LoggerInterface;
  9. class UserPluginManager {
  10. private int $respondToActions = 0;
  11. private array $which = [
  12. Backend::CREATE_USER => null,
  13. Backend::SET_PASSWORD => null,
  14. Backend::GET_HOME => null,
  15. Backend::GET_DISPLAYNAME => null,
  16. Backend::SET_DISPLAYNAME => null,
  17. Backend::PROVIDE_AVATAR => null,
  18. Backend::COUNT_USERS => null,
  19. 'deleteUser' => null
  20. ];
  21. private bool $suppressDeletion = false;
  22. /**
  23. * @return int All implemented actions, except for 'deleteUser'
  24. */
  25. public function getImplementedActions() {
  26. return $this->respondToActions;
  27. }
  28. /**
  29. * Registers a user plugin that may implement some actions, overriding User_LDAP's user actions.
  30. *
  31. * @param ILDAPUserPlugin $plugin
  32. */
  33. public function register(ILDAPUserPlugin $plugin) {
  34. $respondToActions = $plugin->respondToActions();
  35. $this->respondToActions |= $respondToActions;
  36. foreach ($this->which as $action => $v) {
  37. if (is_int($action) && (bool)($respondToActions & $action)) {
  38. $this->which[$action] = $plugin;
  39. \OCP\Server::get(LoggerInterface::class)->debug('Registered action '.$action.' to plugin '.get_class($plugin), ['app' => 'user_ldap']);
  40. }
  41. }
  42. if (method_exists($plugin, 'deleteUser')) {
  43. $this->which['deleteUser'] = $plugin;
  44. \OCP\Server::get(LoggerInterface::class)->debug('Registered action deleteUser to plugin '.get_class($plugin), ['app' => 'user_ldap']);
  45. }
  46. }
  47. /**
  48. * Signal if there is a registered plugin that implements some given actions
  49. * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER
  50. * @return bool
  51. */
  52. public function implementsActions($actions) {
  53. return ($actions & $this->respondToActions) == $actions;
  54. }
  55. /**
  56. * Create a new user in LDAP Backend
  57. *
  58. * @param string $username The username of the user to create
  59. * @param string $password The password of the new user
  60. * @return string | false The user DN if user creation was successful.
  61. * @throws \Exception
  62. */
  63. public function createUser($username, $password) {
  64. $plugin = $this->which[Backend::CREATE_USER];
  65. if ($plugin) {
  66. return $plugin->createUser($username, $password);
  67. }
  68. throw new \Exception('No plugin implements createUser in this LDAP Backend.');
  69. }
  70. /**
  71. * Change the password of a user*
  72. * @param string $uid The username
  73. * @param string $password The new password
  74. * @return bool
  75. * @throws \Exception
  76. */
  77. public function setPassword($uid, $password) {
  78. $plugin = $this->which[Backend::SET_PASSWORD];
  79. if ($plugin) {
  80. return $plugin->setPassword($uid, $password);
  81. }
  82. throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
  83. }
  84. /**
  85. * checks whether the user is allowed to change his avatar in Nextcloud
  86. * @param string $uid the Nextcloud user name
  87. * @return boolean either the user can or cannot
  88. * @throws \Exception
  89. */
  90. public function canChangeAvatar($uid) {
  91. $plugin = $this->which[Backend::PROVIDE_AVATAR];
  92. if ($plugin) {
  93. return $plugin->canChangeAvatar($uid);
  94. }
  95. throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
  96. }
  97. /**
  98. * Get the user's home directory
  99. * @param string $uid the username
  100. * @return boolean
  101. * @throws \Exception
  102. */
  103. public function getHome($uid) {
  104. $plugin = $this->which[Backend::GET_HOME];
  105. if ($plugin) {
  106. return $plugin->getHome($uid);
  107. }
  108. throw new \Exception('No plugin implements getHome in this LDAP Backend.');
  109. }
  110. /**
  111. * Get display name of the user
  112. * @param string $uid user ID of the user
  113. * @return string display name
  114. * @throws \Exception
  115. */
  116. public function getDisplayName($uid) {
  117. $plugin = $this->which[Backend::GET_DISPLAYNAME];
  118. if ($plugin) {
  119. return $plugin->getDisplayName($uid);
  120. }
  121. throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
  122. }
  123. /**
  124. * Set display name of the user
  125. * @param string $uid user ID of the user
  126. * @param string $displayName new user's display name
  127. * @return string display name
  128. * @throws \Exception
  129. */
  130. public function setDisplayName($uid, $displayName) {
  131. $plugin = $this->which[Backend::SET_DISPLAYNAME];
  132. if ($plugin) {
  133. return $plugin->setDisplayName($uid, $displayName);
  134. }
  135. throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
  136. }
  137. /**
  138. * Count the number of users
  139. * @return int|false
  140. * @throws \Exception
  141. */
  142. public function countUsers() {
  143. $plugin = $this->which[Backend::COUNT_USERS];
  144. if ($plugin) {
  145. return $plugin->countUsers();
  146. }
  147. throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
  148. }
  149. /**
  150. * @return bool
  151. */
  152. public function canDeleteUser() {
  153. return !$this->suppressDeletion && $this->which['deleteUser'] !== null;
  154. }
  155. /**
  156. * @param $uid
  157. * @return bool
  158. * @throws \Exception
  159. */
  160. public function deleteUser($uid) {
  161. $plugin = $this->which['deleteUser'];
  162. if ($plugin) {
  163. if ($this->suppressDeletion) {
  164. return false;
  165. }
  166. return $plugin->deleteUser($uid);
  167. }
  168. throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
  169. }
  170. /**
  171. * @param bool $value
  172. * @return bool – the value before the change
  173. */
  174. public function setSuppressDeletion(bool $value): bool {
  175. $old = $this->suppressDeletion;
  176. $this->suppressDeletion = $value;
  177. return $old;
  178. }
  179. }