1
0

UserPluginManager.php 5.4 KB

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