UserPluginManager.php 5.7 KB

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