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.

UserPluginManager.php 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 EITA Cooperative (eita.org.br)
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Filis Futsarov <filisko@users.noreply.github.com>
  8. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OCA\User_LDAP;
  27. use OC\User\Backend;
  28. class UserPluginManager {
  29. private int $respondToActions = 0;
  30. private array $which = [
  31. Backend::CREATE_USER => null,
  32. Backend::SET_PASSWORD => null,
  33. Backend::GET_HOME => null,
  34. Backend::GET_DISPLAYNAME => null,
  35. Backend::SET_DISPLAYNAME => null,
  36. Backend::PROVIDE_AVATAR => null,
  37. Backend::COUNT_USERS => null,
  38. 'deleteUser' => null
  39. ];
  40. private bool $suppressDeletion = false;
  41. /**
  42. * @return int All implemented actions, except for 'deleteUser'
  43. */
  44. public function getImplementedActions() {
  45. return $this->respondToActions;
  46. }
  47. /**
  48. * Registers a user plugin that may implement some actions, overriding User_LDAP's user actions.
  49. *
  50. * @param ILDAPUserPlugin $plugin
  51. */
  52. public function register(ILDAPUserPlugin $plugin) {
  53. $respondToActions = $plugin->respondToActions();
  54. $this->respondToActions |= $respondToActions;
  55. foreach ($this->which as $action => $v) {
  56. if (is_int($action) && (bool)($respondToActions & $action)) {
  57. $this->which[$action] = $plugin;
  58. \OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
  59. }
  60. }
  61. if (method_exists($plugin, 'deleteUser')) {
  62. $this->which['deleteUser'] = $plugin;
  63. \OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
  64. }
  65. }
  66. /**
  67. * Signal if there is a registered plugin that implements some given actions
  68. * @param int $actions Actions defined in \OC\User\Backend, like Backend::CREATE_USER
  69. * @return bool
  70. */
  71. public function implementsActions($actions) {
  72. return ($actions & $this->respondToActions) == $actions;
  73. }
  74. /**
  75. * Create a new user in LDAP Backend
  76. *
  77. * @param string $username The username of the user to create
  78. * @param string $password The password of the new user
  79. * @return string | false The user DN if user creation was successful.
  80. * @throws \Exception
  81. */
  82. public function createUser($username, $password) {
  83. $plugin = $this->which[Backend::CREATE_USER];
  84. if ($plugin) {
  85. return $plugin->createUser($username, $password);
  86. }
  87. throw new \Exception('No plugin implements createUser in this LDAP Backend.');
  88. }
  89. /**
  90. * Change the password of a user*
  91. * @param string $uid The username
  92. * @param string $password The new password
  93. * @return bool
  94. * @throws \Exception
  95. */
  96. public function setPassword($uid, $password) {
  97. $plugin = $this->which[Backend::SET_PASSWORD];
  98. if ($plugin) {
  99. return $plugin->setPassword($uid, $password);
  100. }
  101. throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
  102. }
  103. /**
  104. * checks whether the user is allowed to change his avatar in Nextcloud
  105. * @param string $uid the Nextcloud user name
  106. * @return boolean either the user can or cannot
  107. * @throws \Exception
  108. */
  109. public function canChangeAvatar($uid) {
  110. $plugin = $this->which[Backend::PROVIDE_AVATAR];
  111. if ($plugin) {
  112. return $plugin->canChangeAvatar($uid);
  113. }
  114. throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
  115. }
  116. /**
  117. * Get the user's home directory
  118. * @param string $uid the username
  119. * @return boolean
  120. * @throws \Exception
  121. */
  122. public function getHome($uid) {
  123. $plugin = $this->which[Backend::GET_HOME];
  124. if ($plugin) {
  125. return $plugin->getHome($uid);
  126. }
  127. throw new \Exception('No plugin implements getHome in this LDAP Backend.');
  128. }
  129. /**
  130. * Get display name of the user
  131. * @param string $uid user ID of the user
  132. * @return string display name
  133. * @throws \Exception
  134. */
  135. public function getDisplayName($uid) {
  136. $plugin = $this->which[Backend::GET_DISPLAYNAME];
  137. if ($plugin) {
  138. return $plugin->getDisplayName($uid);
  139. }
  140. throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
  141. }
  142. /**
  143. * Set display name of the user
  144. * @param string $uid user ID of the user
  145. * @param string $displayName new user's display name
  146. * @return string display name
  147. * @throws \Exception
  148. */
  149. public function setDisplayName($uid, $displayName) {
  150. $plugin = $this->which[Backend::SET_DISPLAYNAME];
  151. if ($plugin) {
  152. return $plugin->setDisplayName($uid, $displayName);
  153. }
  154. throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
  155. }
  156. /**
  157. * Count the number of users
  158. * @return int|false
  159. * @throws \Exception
  160. */
  161. public function countUsers() {
  162. $plugin = $this->which[Backend::COUNT_USERS];
  163. if ($plugin) {
  164. return $plugin->countUsers();
  165. }
  166. throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
  167. }
  168. /**
  169. * @return bool
  170. */
  171. public function canDeleteUser() {
  172. return !$this->suppressDeletion && $this->which['deleteUser'] !== null;
  173. }
  174. /**
  175. * @param $uid
  176. * @return bool
  177. * @throws \Exception
  178. */
  179. public function deleteUser($uid) {
  180. $plugin = $this->which['deleteUser'];
  181. if ($plugin) {
  182. if ($this->suppressDeletion) {
  183. return false;
  184. }
  185. return $plugin->deleteUser($uid);
  186. }
  187. throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
  188. }
  189. /**
  190. * @param bool $value
  191. * @return bool – the value before the change
  192. */
  193. public function setSuppressDeletion(bool $value): bool {
  194. $old = $this->suppressDeletion;
  195. $this->suppressDeletion = $value;
  196. return $old;
  197. }
  198. }