Skip to content

Commit

Permalink
Refactors user_ldap app commands.
Browse files Browse the repository at this point in the history
To improve code readability.

Signed-off-by: Faraz Samapoor <f.samapoor@gmail.com>
Co-authored-by: Côme Chilliet <91878298+come-nc@users.noreply.github.com>
  • Loading branch information
fsamapoor and come-nc committed Jan 24, 2024
1 parent e8a67a7 commit b7e19af
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 223 deletions.
14 changes: 8 additions & 6 deletions apps/user_ldap/lib/Command/CheckGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->service->handleCreatedGroups([$gid]);
}
}
return 0;
} elseif ($wasMapped) {
return self::SUCCESS;
}

if ($wasMapped) {
$output->writeln('The group does not exist on LDAP anymore.');
if ($input->getOption('update')) {
$this->backend->getLDAPAccess($gid)->connection->clearCache();
$this->service->handleRemovedGroups([$gid]);
}
return 0;
} else {
throw new \Exception('The given group is not a recognized LDAP group.');
return self::SUCCESS;
}

throw new \Exception('The given group is not a recognized LDAP group.');
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage(). '</error>');
return 1;
return self::FAILURE;
}
}

Expand Down
36 changes: 15 additions & 21 deletions apps/user_ldap/lib/Command/CheckUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,15 @@
use Symfony\Component\Console\Output\OutputInterface;

class CheckUser extends Command {
/** @var User_Proxy */
protected $backend;
protected User_Proxy $backend;

/** @var Helper */
protected $helper;

/** @var DeletedUsersIndex */
protected $dui;

/** @var UserMapping */
protected $mapping;

public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
public function __construct(
User_Proxy $uBackend,
protected Helper $helper,
protected DeletedUsersIndex $dui,
protected UserMapping $mapping,
) {
$this->backend = $uBackend;
$this->helper = $helper;
$this->dui = $dui;
$this->mapping = $mapping;
parent::__construct();
}

Expand Down Expand Up @@ -98,19 +90,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($input->getOption('update')) {
$this->updateUser($uid, $output);
}
return 0;
} elseif ($wasMapped) {
return self::SUCCESS;
}

if ($wasMapped) {
$this->dui->markUser($uid);
$output->writeln('The user does not exists on LDAP anymore.');
$output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
. $uid . '"');
return 0;
} else {
throw new \Exception('The given user is not a recognized LDAP user.');
return self::SUCCESS;
}

throw new \Exception('The given user is not a recognized LDAP user.');
} catch (\Exception $e) {
$output->writeln('<error>' . $e->getMessage(). '</error>');
return 1;
return self::FAILURE;
}
}

Expand Down
15 changes: 5 additions & 10 deletions apps/user_ldap/lib/Command/CreateEmptyConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,13 @@
use Symfony\Component\Console\Output\OutputInterface;

class CreateEmptyConfig extends Command {
/** @var \OCA\User_LDAP\Helper */
protected $helper;

/**
* @param Helper $helper
*/
public function __construct(Helper $helper) {
$this->helper = $helper;
public function __construct(
protected Helper $helper,
) {
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this
->setName('ldap:create-empty-config')
->setDescription('creates an empty LDAP configuration')
Expand All @@ -67,6 +62,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$prose = 'Created new configuration with configID ';
}
$output->writeln($prose . "{$configPrefix}");
return 0;
return self::SUCCESS;
}
}
24 changes: 9 additions & 15 deletions apps/user_ldap/lib/Command/DeleteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,13 @@
use Symfony\Component\Console\Output\OutputInterface;

class DeleteConfig extends Command {
/** @var \OCA\User_LDAP\Helper */
protected $helper;

/**
* @param Helper $helper
*/
public function __construct(Helper $helper) {
$this->helper = $helper;
public function __construct(
protected Helper $helper,
) {
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this
->setName('ldap:delete-config')
->setDescription('deletes an existing LDAP configuration')
Expand All @@ -54,18 +49,17 @@ protected function configure() {
;
}


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

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

if ($success) {
$output->writeln("Deleted configuration with configID '{$configPrefix}'");
return 0;
} else {
if (!$success) {
$output->writeln("Cannot delete configuration with configID '{$configPrefix}'");
return 1;
return self::FAILURE;
}

$output->writeln("Deleted configuration with configID '{$configPrefix}'");
return self::SUCCESS;
}
}
19 changes: 6 additions & 13 deletions apps/user_ldap/lib/Command/ResetGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,11 @@
use Symfony\Component\Console\Question\Question;

class ResetGroup extends Command {
private IGroupManager $groupManager;
private GroupPluginManager $pluginManager;
private Group_Proxy $backend;

public function __construct(
IGroupManager $groupManager,
GroupPluginManager $pluginManager,
Group_Proxy $backend
private IGroupManager $groupManager,
private GroupPluginManager $pluginManager,
private Group_Proxy $backend,
) {
$this->groupManager = $groupManager;
$this->pluginManager = $pluginManager;
$this->backend = $backend;
parent::__construct();
}

Expand Down Expand Up @@ -96,16 +89,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
echo "calling delete $gid\n";
if ($group->delete()) {
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
return 0;
return self::SUCCESS;
}
} catch (\Throwable $e) {
if (isset($pluginManagerSuppressed)) {
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
}
$output->writeln('<error>' . $e->getMessage() . '</error>');
return 1;
return self::FAILURE;
}
$output->writeln('<error>Error while resetting group</error>');
return 2;
return self::INVALID;
}
}
24 changes: 7 additions & 17 deletions apps/user_ldap/lib/Command/ResetUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,15 @@
use Symfony\Component\Console\Question\Question;

class ResetUser extends Command {
/** @var DeletedUsersIndex */
protected $dui;
/** @var IUserManager */
private $userManager;
/** @var UserPluginManager */
private $pluginManager;

public function __construct(
DeletedUsersIndex $dui,
IUserManager $userManager,
UserPluginManager $pluginManager
protected DeletedUsersIndex $dui,
private IUserManager $userManager,
private UserPluginManager $pluginManager,
) {
$this->dui = $dui;
$this->userManager = $userManager;
$this->pluginManager = $pluginManager;
parent::__construct();
}

protected function configure() {
protected function configure(): void {
$this
->setName('ldap:reset-user')
->setDescription('deletes an LDAP user independent of the user state')
Expand Down Expand Up @@ -96,16 +86,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$pluginManagerSuppressed = $this->pluginManager->setSuppressDeletion(true);
if ($user->delete()) {
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
return 0;
return self::SUCCESS;
}
} catch (\Throwable $e) {
if (isset($pluginManagerSuppressed)) {
$this->pluginManager->setSuppressDeletion($pluginManagerSuppressed);
}
$output->writeln('<error>' . $e->getMessage() . '</error>');
return 1;
return self::FAILURE;
}
$output->writeln('<error>Error while resetting user</error>');
return 2;
return self::INVALID;
}
}
25 changes: 9 additions & 16 deletions apps/user_ldap/lib/Command/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,15 @@
use Symfony\Component\Console\Output\OutputInterface;

class Search extends Command {
/** @var \OCP\IConfig */
protected $ocConfig;
/** @var User_Proxy */
private $userProxy;
/** @var Group_Proxy */
private $groupProxy;

public function __construct(IConfig $ocConfig, User_Proxy $userProxy, Group_Proxy $groupProxy) {
public function __construct(
protected IConfig $ocConfig,
private User_Proxy $userProxy,
private Group_Proxy $groupProxy,
) {
parent::__construct();
$this->ocConfig = $ocConfig;
$this->userProxy = $userProxy;
$this->groupProxy = $groupProxy;
}

protected function configure() {
protected function configure(): void {
$this
->setName('ldap:search')
->setDescription('executes a user or group search')
Expand Down Expand Up @@ -87,11 +81,10 @@ protected function configure() {

/**
* Tests whether the offset and limit options are valid
* @param int $offset
* @param int $limit
*
* @throws \InvalidArgumentException
*/
protected function validateOffsetAndLimit($offset, $limit) {
protected function validateOffsetAndLimit($offset, $limit): void {

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $offset has no provided type

Check notice

Code scanning / Psalm

MissingParamType Note

Parameter $limit has no provided type
if ($limit < 0) {
throw new \InvalidArgumentException('limit must be 0 or greater');
}
Expand Down Expand Up @@ -135,6 +128,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$line = $name . ($printID ? ' ('.$id.')' : '');
$output->writeln($line);
}
return 0;
return self::SUCCESS;
}
}
11 changes: 4 additions & 7 deletions apps/user_ldap/lib/Command/SetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
use Symfony\Component\Console\Output\OutputInterface;

class SetConfig extends Command {
protected function configure() {
protected function configure(): void {
$this
->setName('ldap:set-config')
->setDescription('modifies an LDAP configuration')
Expand Down Expand Up @@ -63,24 +63,21 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configID = $input->getArgument('configID');
if (!in_array($configID, $availableConfigs)) {
$output->writeln("Invalid configID");
return 1;
return self::FAILURE;
}

$this->setValue(
$configID,
$input->getArgument('configKey'),
$input->getArgument('configValue')
);
return 0;
return self::SUCCESS;
}

/**
* save the configuration value as provided
* @param string $configID
* @param string $configKey
* @param string $configValue
*/
protected function setValue($configID, $key, $value) {
protected function setValue(string $configID, string $key, string $value): void {
$configHolder = new Configuration($configID);
$configHolder->$key = $value;
$configHolder->saveConfiguration();
Expand Down
Loading

0 comments on commit b7e19af

Please sign in to comment.