Skip to content

Commit 30c0567

Browse files
fsamapoorcome-nc
andcommitted
Refactors user_ldap app commands.
To improve code readability. Signed-off-by: Faraz Samapoor <[email protected]> Co-authored-by: Côme Chilliet <[email protected]>
1 parent 063c051 commit 30c0567

File tree

12 files changed

+143
-223
lines changed

12 files changed

+143
-223
lines changed

apps/user_ldap/lib/Command/CheckGroup.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106106
$this->service->handleCreatedGroups([$gid]);
107107
}
108108
}
109-
return 0;
110-
} elseif ($wasMapped) {
109+
return self::SUCCESS;
110+
}
111+
112+
if ($wasMapped) {
111113
$output->writeln('The group does not exist on LDAP anymore.');
112114
if ($input->getOption('update')) {
113115
$this->backend->getLDAPAccess($gid)->connection->clearCache();
114116
$this->service->handleRemovedGroups([$gid]);
115117
}
116-
return 0;
117-
} else {
118-
throw new \Exception('The given group is not a recognized LDAP group.');
118+
return self::SUCCESS;
119119
}
120+
121+
throw new \Exception('The given group is not a recognized LDAP group.');
120122
} catch (\Exception $e) {
121123
$output->writeln('<error>' . $e->getMessage(). '</error>');
122-
return 1;
124+
return self::FAILURE;
123125
}
124126
}
125127

apps/user_ldap/lib/Command/CheckUser.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,15 @@
3737
use Symfony\Component\Console\Output\OutputInterface;
3838

3939
class CheckUser extends Command {
40-
/** @var User_Proxy */
41-
protected $backend;
40+
protected User_Proxy $backend;
4241

43-
/** @var Helper */
44-
protected $helper;
45-
46-
/** @var DeletedUsersIndex */
47-
protected $dui;
48-
49-
/** @var UserMapping */
50-
protected $mapping;
51-
52-
public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
42+
public function __construct(
43+
User_Proxy $uBackend,
44+
protected Helper $helper,
45+
protected DeletedUsersIndex $dui,
46+
protected UserMapping $mapping,
47+
) {
5348
$this->backend = $uBackend;
54-
$this->helper = $helper;
55-
$this->dui = $dui;
56-
$this->mapping = $mapping;
5749
parent::__construct();
5850
}
5951

@@ -98,19 +90,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9890
if ($input->getOption('update')) {
9991
$this->updateUser($uid, $output);
10092
}
101-
return 0;
102-
} elseif ($wasMapped) {
93+
return self::SUCCESS;
94+
}
95+
96+
if ($wasMapped) {
10397
$this->dui->markUser($uid);
10498
$output->writeln('The user does not exists on LDAP anymore.');
10599
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
106100
. $uid . '"');
107-
return 0;
108-
} else {
109-
throw new \Exception('The given user is not a recognized LDAP user.');
101+
return self::SUCCESS;
110102
}
103+
104+
throw new \Exception('The given user is not a recognized LDAP user.');
111105
} catch (\Exception $e) {
112106
$output->writeln('<error>' . $e->getMessage(). '</error>');
113-
return 1;
107+
return self::FAILURE;
114108
}
115109
}
116110

apps/user_ldap/lib/Command/CreateEmptyConfig.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,13 @@
3232
use Symfony\Component\Console\Output\OutputInterface;
3333

3434
class CreateEmptyConfig extends Command {
35-
/** @var \OCA\User_LDAP\Helper */
36-
protected $helper;
37-
38-
/**
39-
* @param Helper $helper
40-
*/
41-
public function __construct(Helper $helper) {
42-
$this->helper = $helper;
35+
public function __construct(
36+
protected Helper $helper,
37+
) {
4338
parent::__construct();
4439
}
4540

46-
protected function configure() {
41+
protected function configure(): void {
4742
$this
4843
->setName('ldap:create-empty-config')
4944
->setDescription('creates an empty LDAP configuration')
@@ -67,6 +62,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6762
$prose = 'Created new configuration with configID ';
6863
}
6964
$output->writeln($prose . "{$configPrefix}");
70-
return 0;
65+
return self::SUCCESS;
7166
}
7267
}

apps/user_ldap/lib/Command/DeleteConfig.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,13 @@
3131
use Symfony\Component\Console\Output\OutputInterface;
3232

3333
class DeleteConfig extends Command {
34-
/** @var \OCA\User_LDAP\Helper */
35-
protected $helper;
36-
37-
/**
38-
* @param Helper $helper
39-
*/
40-
public function __construct(Helper $helper) {
41-
$this->helper = $helper;
34+
public function __construct(
35+
protected Helper $helper,
36+
) {
4237
parent::__construct();
4338
}
4439

45-
protected function configure() {
40+
protected function configure(): void {
4641
$this
4742
->setName('ldap:delete-config')
4843
->setDescription('deletes an existing LDAP configuration')
@@ -54,18 +49,17 @@ protected function configure() {
5449
;
5550
}
5651

57-
5852
protected function execute(InputInterface $input, OutputInterface $output): int {
5953
$configPrefix = $input->getArgument('configID');
6054

6155
$success = $this->helper->deleteServerConfiguration($configPrefix);
6256

63-
if ($success) {
64-
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
65-
return 0;
66-
} else {
57+
if (!$success) {
6758
$output->writeln("Cannot delete configuration with configID '{$configPrefix}'");
68-
return 1;
59+
return self::FAILURE;
6960
}
61+
62+
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
63+
return self::SUCCESS;
7064
}
7165
}

apps/user_ldap/lib/Command/ResetGroup.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,11 @@
3636
use Symfony\Component\Console\Question\Question;
3737

3838
class ResetGroup extends Command {
39-
private IGroupManager $groupManager;
40-
private GroupPluginManager $pluginManager;
41-
private Group_Proxy $backend;
42-
4339
public function __construct(
44-
IGroupManager $groupManager,
45-
GroupPluginManager $pluginManager,
46-
Group_Proxy $backend
40+
private IGroupManager $groupManager,
41+
private GroupPluginManager $pluginManager,
42+
private Group_Proxy $backend,
4743
) {
48-
$this->groupManager = $groupManager;
49-
$this->pluginManager = $pluginManager;
50-
$this->backend = $backend;
5144
parent::__construct();
5245
}
5346

@@ -96,16 +89,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9689
echo "calling delete $gid\n";
9790
if ($group->delete()) {
9891
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
99-
return 0;
92+
return self::SUCCESS;
10093
}
10194
} catch (\Throwable $e) {
10295
if (isset($pluginManagerSuppressed)) {
10396
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
10497
}
10598
$output->writeln('<error>' . $e->getMessage() . '</error>');
106-
return 1;
99+
return self::FAILURE;
107100
}
108101
$output->writeln('<error>Error while resetting group</error>');
109-
return 2;
102+
return self::INVALID;
110103
}
111104
}

apps/user_ldap/lib/Command/ResetUser.php

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,15 @@
3636
use Symfony\Component\Console\Question\Question;
3737

3838
class ResetUser extends Command {
39-
/** @var DeletedUsersIndex */
40-
protected $dui;
41-
/** @var IUserManager */
42-
private $userManager;
43-
/** @var UserPluginManager */
44-
private $pluginManager;
45-
4639
public function __construct(
47-
DeletedUsersIndex $dui,
48-
IUserManager $userManager,
49-
UserPluginManager $pluginManager
40+
protected DeletedUsersIndex $dui,
41+
private IUserManager $userManager,
42+
private UserPluginManager $pluginManager,
5043
) {
51-
$this->dui = $dui;
52-
$this->userManager = $userManager;
53-
$this->pluginManager = $pluginManager;
5444
parent::__construct();
5545
}
5646

57-
protected function configure() {
47+
protected function configure(): void {
5848
$this
5949
->setName('ldap:reset-user')
6050
->setDescription('deletes an LDAP user independent of the user state')
@@ -96,16 +86,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9686
$pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
9787
if ($user->delete()) {
9888
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
99-
return 0;
89+
return self::SUCCESS;
10090
}
10191
} catch (\Throwable $e) {
10292
if (isset($pluginManagerSuppressed)) {
10393
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
10494
}
10595
$output->writeln('<error>' . $e->getMessage() . '</error>');
106-
return 1;
96+
return self::FAILURE;
10797
}
10898
$output->writeln('<error>Error while resetting user</error>');
109-
return 2;
99+
return self::INVALID;
110100
}
111101
}

apps/user_ldap/lib/Command/Search.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,15 @@
3939
use Symfony\Component\Console\Output\OutputInterface;
4040

4141
class Search extends Command {
42-
/** @var \OCP\IConfig */
43-
protected $ocConfig;
44-
/** @var User_Proxy */
45-
private $userProxy;
46-
/** @var Group_Proxy */
47-
private $groupProxy;
48-
49-
public function __construct(IConfig $ocConfig, User_Proxy $userProxy, Group_Proxy $groupProxy) {
42+
public function __construct(
43+
protected IConfig $ocConfig,
44+
private User_Proxy $userProxy,
45+
private Group_Proxy $groupProxy,
46+
) {
5047
parent::__construct();
51-
$this->ocConfig = $ocConfig;
52-
$this->userProxy = $userProxy;
53-
$this->groupProxy = $groupProxy;
5448
}
5549

56-
protected function configure() {
50+
protected function configure(): void {
5751
$this
5852
->setName('ldap:search')
5953
->setDescription('executes a user or group search')
@@ -87,11 +81,10 @@ protected function configure() {
8781

8882
/**
8983
* Tests whether the offset and limit options are valid
90-
* @param int $offset
91-
* @param int $limit
84+
*
9285
* @throws \InvalidArgumentException
9386
*/
94-
protected function validateOffsetAndLimit($offset, $limit) {
87+
protected function validateOffsetAndLimit($offset, $limit): void {
9588
if ($limit < 0) {
9689
throw new \InvalidArgumentException('limit must be 0 or greater');
9790
}
@@ -135,6 +128,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
135128
$line = $name . ($printID ? ' ('.$id.')' : '');
136129
$output->writeln($line);
137130
}
138-
return 0;
131+
return self::SUCCESS;
139132
}
140133
}

apps/user_ldap/lib/Command/SetConfig.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
use Symfony\Component\Console\Output\OutputInterface;
3636

3737
class SetConfig extends Command {
38-
protected function configure() {
38+
protected function configure(): void {
3939
$this
4040
->setName('ldap:set-config')
4141
->setDescription('modifies an LDAP configuration')
@@ -63,24 +63,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6363
$configID = $input->getArgument('configID');
6464
if (!in_array($configID, $availableConfigs)) {
6565
$output->writeln("Invalid configID");
66-
return 1;
66+
return self::FAILURE;
6767
}
6868

6969
$this->setValue(
7070
$configID,
7171
$input->getArgument('configKey'),
7272
$input->getArgument('configValue')
7373
);
74-
return 0;
74+
return self::SUCCESS;
7575
}
7676

7777
/**
7878
* save the configuration value as provided
79-
* @param string $configID
80-
* @param string $configKey
81-
* @param string $configValue
8279
*/
83-
protected function setValue($configID, $key, $value) {
80+
protected function setValue(string $configID, string $key, string $value): void {
8481
$configHolder = new Configuration($configID);
8582
$configHolder->$key = $value;
8683
$configHolder->saveConfiguration();

0 commit comments

Comments
 (0)