Skip to content

Commit b9eabd3

Browse files
committed
implements ibootstrap and cleans up code
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent d4aeba3 commit b9eabd3

File tree

6 files changed

+132
-80
lines changed

6 files changed

+132
-80
lines changed

appinfo/app.php

Lines changed: 0 additions & 6 deletions
This file was deleted.

lib/AppInfo/Application.php

Lines changed: 14 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4,91 +4,35 @@
44

55
namespace OCA\LdapWriteSupport\AppInfo;
66

7-
use Exception;
8-
use OC;
9-
use OC\Group\Group;
10-
use OC\User\User;
11-
use OCA\LdapWriteSupport\LDAPConnect;
127
use OCA\LdapWriteSupport\LDAPUserManager;
138
use OCA\LdapWriteSupport\LDAPGroupManager;
14-
use OCA\LdapWriteSupport\Service\Configuration;
15-
use OCA\User_LDAP\GroupPluginManager;
16-
use OCA\User_LDAP\Helper;
17-
use OCA\User_LDAP\UserPluginManager;
9+
use OCA\LdapWriteSupport\Listener\UserBackendRegisteredListener;
10+
use OCA\LdapWriteSupport\Listener\GroupBackendRegisteredListener;
11+
use OCA\User_LDAP\Events\GroupBackendRegistered;
12+
use OCA\User_LDAP\Events\UserBackendRegistered;
1813
use OCP\AppFramework\App;
19-
use OCP\AppFramework\QueryException;
14+
use OCP\AppFramework\Bootstrap\IBootContext;
15+
use OCP\AppFramework\Bootstrap\IBootstrap;
16+
use OCP\AppFramework\Bootstrap\IRegistrationContext;
2017

21-
class Application extends App {
18+
class Application extends App implements IBootstrap {
2219
/** @var LDAPUserManager */
2320
protected $ldapUserManager;
2421

2522
/** @var LDAPGroupManager */
2623
protected $ldapGroupManager;
2724

28-
/** @var bool */
29-
protected $ldapEnabled = false;
30-
31-
const APP_ID = 'ldap_write_support';
25+
public const APP_ID = 'ldap_write_support';
3226

3327
public function __construct(array $urlParams = []) {
3428
parent::__construct(self::APP_ID, $urlParams);
35-
$this->ldapEnabled = OC::$server->getAppManager()->isEnabledForUser('user_ldap');
3629
}
3730

38-
/**
39-
* @throws QueryException
40-
* @throws Exception
41-
*/
42-
public function registerLDAPPlugins(): void {
43-
if(!$this->ldapEnabled) {
44-
return;
45-
}
46-
47-
if(!\OC_App::isAppLoaded('user_ldap')) {
48-
\OC_App::loadApp('user_ldap');
49-
}
50-
$c = $this->getContainer();
51-
$s = $this->getContainer()->getServer();
52-
try {
53-
$provider = $s->getLDAPProvider();
54-
} catch (\Exception $e) {
55-
if(strpos($e->getMessage(), 'user_ldap app must be enabled') !== false) {
56-
$s->getLogger()->info (
57-
'Not registering plugins, because there are no active LDAP configs',
58-
['app' => self::APP_ID]
59-
);
60-
return;
61-
}
62-
throw $e;
63-
}
64-
65-
$ldapConnect = new LDAPConnect($s->query(Helper::class), $s->getLogger());
66-
67-
// resolving LDAP provider fails indeed
68-
$this->ldapUserManager = new LDAPUserManager(
69-
$s->getUserManager(),
70-
$s->getUserSession(),
71-
$ldapConnect,
72-
$provider,
73-
$c->query(Configuration::class),
74-
$s->getL10N(self::APP_ID),
75-
$s->getLogger()
76-
);
77-
78-
$this->ldapGroupManager = new LDAPGroupManager(
79-
$s->getGroupManager(),
80-
$ldapConnect,
81-
$s->getLogger(),
82-
$provider
83-
);
84-
85-
/** @var UserPluginManager $userPluginManager */
86-
$userPluginManager = OC::$server->query(UserPluginManager::class);
87-
/** @var GroupPluginManager $groupPluginManager */
88-
$groupPluginManager = OC::$server->query(GroupPluginManager::class);
89-
90-
$userPluginManager->register($this->ldapUserManager);
91-
$groupPluginManager->register($this->ldapGroupManager);
31+
public function register(IRegistrationContext $context): void {
32+
$context->registerEventListener(UserBackendRegistered::class, UserBackendRegisteredListener::class);
33+
$context->registerEventListener(GroupBackendRegistered::class, GroupBackendRegisteredListener::class);
9234
}
9335

36+
public function boot(IBootContext $context): void {
37+
}
9438
}

lib/LDAPGroupManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ class LDAPGroupManager implements ILDAPGroupPlugin {
4949
/** @var ILogger */
5050
private $logger;
5151

52-
public function __construct(IGroupManager $groupManager, LDAPConnect $ldapConnect, ILogger $logger, ILDAPProvider $ldapProvider) {
52+
public function __construct(IGroupManager $groupManager, LDAPConnect $ldapConnect, ILogger $logger, ILDAPProvider $LDAPProvider) {
5353
$this->groupManager = $groupManager;
5454
$this->ldapConnect = $ldapConnect;
5555
$this->logger = $logger;
56-
$this->ldapProvider = $ldapProvider;
56+
$this->ldapProvider = $LDAPProvider;
5757

5858
if($this->ldapConnect->groupsEnabled()) {
5959
$this->makeLdapBackendFirst();

lib/LDAPUserManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ class LDAPUserManager implements ILDAPUserPlugin {
7676
* @param IL10N $l10n
7777
* @param ILogger $logger
7878
*/
79-
public function __construct(IUserManager $userManager, IUserSession $userSession, LDAPConnect $ldapConnect, ILDAPProvider $ldapProvider, Configuration $configuration, IL10N $l10n, ILogger $logger) {
79+
public function __construct(IUserManager $userManager, IUserSession $userSession, LDAPConnect $ldapConnect, ILDAPProvider $LDAPProvider, Configuration $configuration, IL10N $l10n, ILogger $logger) {
8080
$this->userManager = $userManager;
8181
$this->userSession = $userSession;
8282
$this->ldapConnect = $ldapConnect;
83-
$this->ldapProvider = $ldapProvider;
83+
$this->ldapProvider = $LDAPProvider;
8484
$this->configuration = $configuration;
8585
$this->l10n = $l10n;
8686
$this->logger = $logger;
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\LdapWriteSupport\Listener;
27+
28+
use OCA\LdapWriteSupport\LDAPGroupManager;
29+
use OCA\User_LDAP\Events\GroupBackendRegistered;
30+
use OCP\App\IAppManager;
31+
use OCP\EventDispatcher\Event;
32+
use OCP\EventDispatcher\IEventListener;
33+
34+
class GroupBackendRegisteredListener implements IEventListener {
35+
36+
/** @var IAppManager */
37+
private $appManager;
38+
/** @var LDAPGroupManager */
39+
private $ldapGroupManager;
40+
41+
public function __construct(IAppManager $appManager, LDAPGroupManager $ldapGroupManager) {
42+
$this->appManager = $appManager;
43+
$this->ldapGroupManager = $ldapGroupManager;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function handle(Event $event): void {
50+
if (!$event instanceof GroupBackendRegistered
51+
|| !$this->appManager->isEnabledForUser('user_ldap')
52+
) {
53+
return;
54+
}
55+
$event->getPluginManager()->register($this->ldapGroupManager);
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\LdapWriteSupport\Listener;
27+
28+
use OCA\LdapWriteSupport\LDAPUserManager;
29+
use OCA\User_LDAP\Events\UserBackendRegistered;
30+
use OCP\App\IAppManager;
31+
use OCP\EventDispatcher\Event;
32+
use OCP\EventDispatcher\IEventListener;
33+
34+
class UserBackendRegisteredListener implements IEventListener {
35+
36+
/** @var IAppManager */
37+
private $appManager;
38+
/** @var LDAPUserManager */
39+
private $ldapUserManager;
40+
41+
public function __construct(IAppManager $appManager, LDAPUserManager $ldapUserManager) {
42+
$this->appManager = $appManager;
43+
$this->ldapUserManager = $ldapUserManager;
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function handle(Event $event): void {
50+
if (!$event instanceof UserBackendRegistered
51+
|| !$this->appManager->isEnabledForUser('user_ldap')
52+
) {
53+
return;
54+
}
55+
$event->getPluginManager()->register($this->ldapUserManager);
56+
}
57+
}

0 commit comments

Comments
 (0)