Skip to content

Commit 7c84721

Browse files
committed
Simplify code and fix cases for AD
1 parent d616f90 commit 7c84721

File tree

2 files changed

+53
-55
lines changed

2 files changed

+53
-55
lines changed

lib/Command/RemapUser.php

+25-35
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ protected function configure() {
7878
protected function execute(InputInterface $input, OutputInterface $output) {
7979
$uid = $input->getArgument('ocName');
8080
$this->isAllowed($input->getOption('force'));
81-
//$this->confirmUserIsMapped($uid);
8281

8382
$mappedData = $this->getMappedUUIDAndDN($uid);
8483
if ($mappedData['mappedDN'] === false || $mappedData['mappedUUID'] === false) {
@@ -93,7 +92,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
9392
$table1->render();
9493

9594
$entries = $this->backend->findUsername($uid);
96-
$entryCount = \count($entries);
9795

9896
$output->writeln('');
9997
$output->writeln('Candidates found in LDAP:');
@@ -104,25 +102,13 @@ protected function execute(InputInterface $input, OutputInterface $output) {
104102
}
105103
$table2->render();
106104

107-
if ($entryCount > 1) {
108-
$output->writeln('<error>Found too many candidates in LDAP for the target user, remapping isn\'t possible</error>');
109-
return 1;
110-
} elseif ($entryCount < 1) {
111-
$output->writeln('<error>User not found in LDAP. Consider removing the ownCloud\'s account</error>');
112-
return 2;
113-
}
114-
115-
if ($mappedData['mappedDN'] === $entries[0]['dn'] && $mappedData['mappedUUID'] === $entries[0]['directory_uuid']) {
116-
$output->writeln('The same user is already mapped. Nothing to do');
117-
return 0; // just show a message and return a success code
118-
}
119-
120-
$result = $this->mapping->replaceUUIDAndDN($uid, $entries[0]['dn'], $entries[0]['directory_uuid']);
121-
if ($result === false) {
122-
$output->writeln("<error>Failed to replace mapping data for user {$uid}</error>");
123-
return 3;
105+
try {
106+
$message = $this->remapUser($uid, $mappedData, $entries);
107+
$output->writeln($message);
108+
} catch (\UnexpectedValueException $e) {
109+
$output->writeln("<error>{$e->getMessage()}</error>");
110+
return $e->getCode();
124111
}
125-
$output->writeln('Mapping data replaced');
126112
}
127113

128114
private function getMappedUUIDAndDN($username) {
@@ -134,21 +120,6 @@ private function getMappedUUIDAndDN($username) {
134120
];
135121
}
136122

137-
/**
138-
* checks whether a user is actually mapped
139-
* @param string $ocName the username as used in ownCloud
140-
* @throws \Exception
141-
* @return true
142-
*/
143-
private function confirmUserIsMapped($ocName) {
144-
$dn = $this->mapping->getDNByName($ocName);
145-
if ($dn === false) {
146-
throw new \Exception('The given user is not a recognized LDAP user.');
147-
}
148-
149-
return true;
150-
}
151-
152123
/**
153124
* checks whether the setup allows reliable checking of LDAP user existence
154125
* @throws \Exception
@@ -162,4 +133,23 @@ private function isAllowed($force) {
162133

163134
return true;
164135
}
136+
137+
private function remapUser($uid, $mappedData, $entries) {
138+
$entryCount = \count($entries);
139+
if ($entryCount > 1) {
140+
throw new \UnexpectedValueException('Found too many candidates in LDAP for the target user, remapping isn\'t possible', 1);
141+
} elseif ($entryCount < 1) {
142+
throw new \UnexpectedValueException('User not found in LDAP. Consider removing the ownCloud\'s account', 2);
143+
}
144+
145+
if ($mappedData['mappedDN'] === $entries[0]['dn'] && $mappedData['mappedUUID'] === $entries[0]['directory_uuid']) {
146+
return 'The same user is already mapped. Nothing to do';
147+
}
148+
149+
$result = $this->mapping->replaceUUIDAndDN($uid, $entries[0]['dn'], $entries[0]['directory_uuid']);
150+
if ($result === false) {
151+
throw new \UnexpectedValueException("Failed to replace mapping data for user {$uid}", 3);
152+
}
153+
return 'Mapping data replaced';
154+
}
165155
}

lib/User/Manager.php

+28-20
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ public function findUsersByUsername($uid) {
556556
$ldapConfig = $this->getConnection();
557557

558558
$uuidAttrs = [$ldapConfig->ldapExpertUUIDUserAttr];
559-
if ($ldapConfig->ldapExpertUUIDUserAttr === 'auto') {
559+
if ($ldapConfig->ldapExpertUUIDUserAttr === 'auto' || $ldapConfig->ldapExpertUUIDUserAttr === '') {
560560
$uuidAttrs = $ldapConfig->uuidAttributes;
561561
}
562562

@@ -566,19 +566,20 @@ public function findUsersByUsername($uid) {
566566
}
567567

568568
$escapedUid = $this->access->escapeFilterPart($uid);
569-
if (\count($usernameAttrs) === 1) {
570-
$innerFilter = "{$usernameAttrs[0]}={$escapedUid}";
571-
} else {
572-
$attrFilters = [];
573-
foreach ($usernameAttrs as $attr) {
569+
$attrFilters = [];
570+
foreach ($usernameAttrs as $attr) {
571+
if ($attr === 'objectguid' || $attr === 'guid') {
572+
// needs special formatting because we need to send as binary data
573+
$attrFilters[] = "{$attr}=" . $this->access->formatGuid2ForFilterUser($uid);
574+
} else {
574575
$attrFilters[] = "{$attr}={$escapedUid}";
575576
}
576-
$innerFilter = $this->access->combineFilterWithOr($attrFilters);
577577
}
578+
$innerFilter = $this->access->combineFilterWithOr($attrFilters);
578579

579580
$filter = $this->access->combineFilterWithAnd([
580581
$this->getConnection()->ldapUserFilter,
581-
$this->getConnection()->ldapUserDisplayName . '=*', // TODO why do we need this? =* basically selects all
582+
$this->getConnection()->ldapUserDisplayName . '=*',
582583
$innerFilter,
583584
]);
584585

@@ -589,18 +590,8 @@ public function findUsersByUsername($uid) {
589590

590591
$entries = [];
591592
foreach ($ldap_users as $ldapEntry) {
592-
$chosenUsername = null;
593-
foreach ($usernameAttrs as $usernameAttr) {
594-
if (isset($ldapEntry[$usernameAttr][0])) {
595-
$chosenUsername = $ldapEntry[$usernameAttr][0];
596-
}
597-
}
598-
$chosenUuid = null;
599-
foreach ($uuidAttrs as $uuidAttr) {
600-
if (isset($ldapEntry[$uuidAttr][0])) {
601-
$chosenUuid = $ldapEntry[$uuidAttr][0];
602-
}
603-
}
593+
$chosenUsername = $this->getValueFromEntry($ldapEntry, $usernameAttrs);
594+
$chosenUuid = $this->getValueFromEntry($ldapEntry, $uuidAttrs);
604595

605596
$entryData = [
606597
'dn' => $ldapEntry['dn'][0],
@@ -613,6 +604,23 @@ public function findUsersByUsername($uid) {
613604
return $entries;
614605
}
615606

607+
/**
608+
* Get the value of the first attribute of the attrs list found inside the ldapEntry
609+
*/
610+
private function getValueFromEntry($ldapEntry, $attrs) {
611+
$chosenValue = null;
612+
foreach ($attrs as $attr) {
613+
if (isset($ldapEntry[$attr][0])) {
614+
$chosenValue = $ldapEntry[$attr][0];
615+
if ($attr === 'objectguid' || $attr === 'guid') {
616+
$chosenValue = Access::binGUID2str($chosenValue);
617+
}
618+
break;
619+
}
620+
}
621+
return $chosenValue;
622+
}
623+
616624
// TODO find better places for the delegations to Access
617625

618626
/**

0 commit comments

Comments
 (0)