UserPluginManager.php 6.2 KB

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