123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace OCA\User_LDAP;
- use OC\User\Backend;
- use OCP\Server;
- use Psr\Log\LoggerInterface;
- class UserPluginManager {
- private int $respondToActions = 0;
- private array $which = [
- Backend::CREATE_USER => null,
- Backend::SET_PASSWORD => null,
- Backend::GET_HOME => null,
- Backend::GET_DISPLAYNAME => null,
- Backend::SET_DISPLAYNAME => null,
- Backend::PROVIDE_AVATAR => null,
- Backend::COUNT_USERS => null,
- 'deleteUser' => null
- ];
- private bool $suppressDeletion = false;
-
- public function getImplementedActions() {
- return $this->respondToActions;
- }
-
- public function register(ILDAPUserPlugin $plugin) {
- $respondToActions = $plugin->respondToActions();
- $this->respondToActions |= $respondToActions;
- foreach ($this->which as $action => $v) {
- if (is_int($action) && (bool)($respondToActions & $action)) {
- $this->which[$action] = $plugin;
- Server::get(LoggerInterface::class)->debug('Registered action ' . $action . ' to plugin ' . get_class($plugin), ['app' => 'user_ldap']);
- }
- }
- if (method_exists($plugin, 'deleteUser')) {
- $this->which['deleteUser'] = $plugin;
- Server::get(LoggerInterface::class)->debug('Registered action deleteUser to plugin ' . get_class($plugin), ['app' => 'user_ldap']);
- }
- }
-
- public function implementsActions($actions) {
- return ($actions & $this->respondToActions) == $actions;
- }
-
- public function createUser($username, $password) {
- $plugin = $this->which[Backend::CREATE_USER];
- if ($plugin) {
- return $plugin->createUser($username, $password);
- }
- throw new \Exception('No plugin implements createUser in this LDAP Backend.');
- }
-
- public function setPassword($uid, $password) {
- $plugin = $this->which[Backend::SET_PASSWORD];
- if ($plugin) {
- return $plugin->setPassword($uid, $password);
- }
- throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
- }
-
- public function canChangeAvatar($uid) {
- $plugin = $this->which[Backend::PROVIDE_AVATAR];
- if ($plugin) {
- return $plugin->canChangeAvatar($uid);
- }
- throw new \Exception('No plugin implements canChangeAvatar in this LDAP Backend.');
- }
-
- public function getHome($uid) {
- $plugin = $this->which[Backend::GET_HOME];
- if ($plugin) {
- return $plugin->getHome($uid);
- }
- throw new \Exception('No plugin implements getHome in this LDAP Backend.');
- }
-
- public function getDisplayName($uid) {
- $plugin = $this->which[Backend::GET_DISPLAYNAME];
- if ($plugin) {
- return $plugin->getDisplayName($uid);
- }
- throw new \Exception('No plugin implements getDisplayName in this LDAP Backend.');
- }
-
- public function setDisplayName($uid, $displayName) {
- $plugin = $this->which[Backend::SET_DISPLAYNAME];
- if ($plugin) {
- return $plugin->setDisplayName($uid, $displayName);
- }
- throw new \Exception('No plugin implements setDisplayName in this LDAP Backend.');
- }
-
- public function countUsers() {
- $plugin = $this->which[Backend::COUNT_USERS];
- if ($plugin) {
- return $plugin->countUsers();
- }
- throw new \Exception('No plugin implements countUsers in this LDAP Backend.');
- }
-
- public function canDeleteUser() {
- return !$this->suppressDeletion && $this->which['deleteUser'] !== null;
- }
-
- public function deleteUser($uid) {
- $plugin = $this->which['deleteUser'];
- if ($plugin) {
- if ($this->suppressDeletion) {
- return false;
- }
- return $plugin->deleteUser($uid);
- }
- throw new \Exception('No plugin implements deleteUser in this LDAP Backend.');
- }
-
- public function setSuppressDeletion(bool $value): bool {
- $old = $this->suppressDeletion;
- $this->suppressDeletion = $value;
- return $old;
- }
- }
|