|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 4 | + * |
| 5 | + * @author Arthur Schiwon <[email protected]> |
| 6 | + * @author Christoph Wurst <[email protected]> |
| 7 | + * @author Côme Chilliet <[email protected]> |
| 8 | + * @author Joas Schilling <[email protected]> |
| 9 | + * @author Morris Jobke <[email protected]> |
| 10 | + * @author Roeland Jago Douma <[email protected]> |
| 11 | + * |
| 12 | + * @license AGPL-3.0 |
| 13 | + * |
| 14 | + * This code is free software: you can redistribute it and/or modify |
| 15 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 16 | + * as published by the Free Software Foundation. |
| 17 | + * |
| 18 | + * This program is distributed in the hope that it will be useful, |
| 19 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | + * GNU Affero General Public License for more details. |
| 22 | + * |
| 23 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 24 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 25 | + * |
| 26 | + */ |
| 27 | +namespace OCA\User_LDAP\Command; |
| 28 | + |
| 29 | +use OCA\User_LDAP\Helper; |
| 30 | +use OCA\User_LDAP\Mapping\UserMapping; |
| 31 | +use OCA\User_LDAP\Group_Proxy; |
| 32 | +use Symfony\Component\Console\Command\Command; |
| 33 | +use Symfony\Component\Console\Input\InputArgument; |
| 34 | +use Symfony\Component\Console\Input\InputInterface; |
| 35 | +use Symfony\Component\Console\Input\InputOption; |
| 36 | +use Symfony\Component\Console\Output\OutputInterface; |
| 37 | + |
| 38 | +class CheckGroup extends Command { |
| 39 | + public function __construct( |
| 40 | + protected Group_Proxy $backend, |
| 41 | + protected Helper $helper, |
| 42 | + protected UserMapping $mapping |
| 43 | + ) { |
| 44 | + parent::__construct(); |
| 45 | + } |
| 46 | + |
| 47 | + protected function configure(): void { |
| 48 | + $this |
| 49 | + ->setName('ldap:check-group') |
| 50 | + ->setDescription('checks whether a group exists on LDAP.') |
| 51 | + ->addArgument( |
| 52 | + 'ocName', |
| 53 | + InputArgument::REQUIRED, |
| 54 | + 'the group name as used in Nextcloud, or the LDAP DN' |
| 55 | + ) |
| 56 | + ->addOption( |
| 57 | + 'force', |
| 58 | + null, |
| 59 | + InputOption::VALUE_NONE, |
| 60 | + 'ignores disabled LDAP configuration' |
| 61 | + ) |
| 62 | + ->addOption( |
| 63 | + 'update', |
| 64 | + null, |
| 65 | + InputOption::VALUE_NONE, |
| 66 | + 'syncs values from LDAP' |
| 67 | + ) |
| 68 | + ; |
| 69 | + } |
| 70 | + |
| 71 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 72 | + try { |
| 73 | + $this->assertAllowed($input->getOption('force')); |
| 74 | + $gid = $input->getArgument('ocName'); |
| 75 | + if ($this->backend->getLDAPAccess($gid)->stringResemblesDN($gid)) { |
| 76 | + $groupname = $this->backend->dn2GroupName($gid); |
| 77 | + if ($groupname !== false) { |
| 78 | + $gid = $groupname; |
| 79 | + } |
| 80 | + } |
| 81 | + $wasMapped = $this->groupWasMapped($gid); |
| 82 | + $exists = $this->backend->groupExistsOnLDAP($gid, true); |
| 83 | + if ($exists === true) { |
| 84 | + $output->writeln('The group is still available on LDAP.'); |
| 85 | + if ($input->getOption('update')) { |
| 86 | + $this->updateGroup($gid, $output); |
| 87 | + } |
| 88 | + return 0; |
| 89 | + } elseif ($wasMapped) { |
| 90 | + $output->writeln('The group does not exists on LDAP anymore.'); |
| 91 | + return 0; |
| 92 | + } else { |
| 93 | + throw new \Exception('The given group is not a recognized LDAP group.'); |
| 94 | + } |
| 95 | + } catch (\Exception $e) { |
| 96 | + $output->writeln('<error>' . $e->getMessage(). '</error>'); |
| 97 | + return 1; |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * checks whether a group is actually mapped |
| 103 | + * @param string $ocName the groupname as used in Nextcloud |
| 104 | + */ |
| 105 | + protected function groupWasMapped(string $ocName): bool { |
| 106 | + $dn = $this->mapping->getDNByName($ocName); |
| 107 | + return $dn !== false; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * checks whether the setup allows reliable checking of LDAP group existence |
| 112 | + * @throws \Exception |
| 113 | + */ |
| 114 | + protected function assertAllowed(bool $force): void { |
| 115 | + if ($this->helper->haveDisabledConfigurations() && !$force) { |
| 116 | + throw new \Exception('Cannot check group existence, because ' |
| 117 | + . 'disabled LDAP configurations are present.'); |
| 118 | + } |
| 119 | + |
| 120 | + // we don't check ldapUserCleanupInterval from config.php because this |
| 121 | + // action is triggered manually, while the setting only controls the |
| 122 | + // background job. |
| 123 | + } |
| 124 | + |
| 125 | + private function updateGroup(string $gid, OutputInterface $output): void { |
| 126 | + try { |
| 127 | + $access = $this->backend->getLDAPAccess($gid); |
| 128 | + $attrs = $access->userManager->getAttributes(); |
| 129 | + $user = $access->userManager->get($gid); |
| 130 | + $avatarAttributes = $access->getConnection()->resolveRule('avatar'); |
| 131 | + $result = $access->search('objectclass=*', $user->getDN(), $attrs, 1, 0); |
| 132 | + foreach ($result[0] as $attribute => $valueSet) { |
| 133 | + $output->writeln(' ' . $attribute . ': '); |
| 134 | + foreach ($valueSet as $value) { |
| 135 | + if (in_array($attribute, $avatarAttributes)) { |
| 136 | + $value = '{ImageData}'; |
| 137 | + } |
| 138 | + $output->writeln(' ' . $value); |
| 139 | + } |
| 140 | + } |
| 141 | + $access->batchApplyUserAttributes($result); |
| 142 | + } catch (\Exception $e) { |
| 143 | + $output->writeln('<error>Error while trying to lookup and update attributes from LDAP</error>'); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments