UserPluginManager.php 5.7 KB

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