Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable11] Backport of Fix Broken UUID Attribute Detection #3521 #3528

Merged
merged 2 commits into from
Feb 20, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apps/user_ldap/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ A user logs into ownCloud with their LDAP or AD credentials, and is granted acce
</description>
<licence>AGPL</licence>
<author>Dominik Schmidt and Arthur Schiwon</author>
<version>1.1.1</version>
<version>1.1.2</version>
<types>
<authentication/>
</types>
Expand All @@ -32,4 +32,10 @@ A user logs into ownCloud with their LDAP or AD credentials, and is granted acce
<admin>OCA\User_LDAP\Settings\Admin</admin>
<admin-section>OCA\User_LDAP\Settings\Section</admin-section>
</settings>

<repair-steps>
<post-migration>
<step>OCA\User_LDAP\Migration\UUIDFixInsert</step>
</post-migration>
</repair-steps>
</info>
2 changes: 1 addition & 1 deletion apps/user_ldap/lib/Access.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ private function detectUuidAttribute($dn, $isUser = true, $force = false) {
return true;
}

if ($uuidOverride !== '' && !$force) {
if (is_string($uuidOverride) && trim($uuidOverride) !== '' && !$force) {
$this->connection->$uuidAttr = $uuidOverride;
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/user_ldap/lib/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
* @property string[] ldapBaseUsers
* @property int|string ldapPagingSize holds an integer
* @property bool|mixed|void ldapGroupMemberAssocAttr
* @property string ldapUuidUserAttribute
* @property string ldapUuidGroupAttribute
*/
class Connection extends LDAPUtility {
private $ldapConnectionRes = null;
Expand Down
8 changes: 8 additions & 0 deletions apps/user_ldap/lib/Group_LDAP.php
Original file line number Diff line number Diff line change
Expand Up @@ -902,4 +902,12 @@ public function groupExists($gid) {
public function implementsActions($actions) {
return (bool)(\OC\Group\Backend::COUNT_USERS & $actions);
}

/**
* Return access for LDAP interaction.
* @return Access instance of Access for LDAP interaction
*/
public function getLDAPAccess() {
return $this->access;
}
}
9 changes: 9 additions & 0 deletions apps/user_ldap/lib/Group_Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,13 @@ public function implementsActions($actions) {
//it's the same across all our user backends obviously
return $this->refBackend->implementsActions($actions);
}

/**
* Return access for LDAP interaction.
* @param string $gid
* @return Access instance of Access for LDAP interaction
*/
public function getLDAPAccess($gid) {
return $this->handleRequest($gid, 'getLDAPAccess', []);
}
}
19 changes: 19 additions & 0 deletions apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ public function setDNbyUUID($fdn, $uuid) {
return $this->modify($query, array($fdn, $uuid));
}

/**
* Updates the UUID based on the given DN
*
* required by Migration/UUIDFix
*
* @param $uuid
* @param $fdn
* @return bool
*/
public function setUUIDbyDN($uuid, $fdn) {
$query = $this->dbc->prepare('
UPDATE `' . $this->getTableName() . '`
SET `directory_uuid` = ?
WHERE `ldap_dn` = ?
');

return $this->modify($query, [$uuid, $fdn]);
}

/**
* Gets the name based on the provided LDAP DN.
* @param string $fdn
Expand Down
60 changes: 60 additions & 0 deletions apps/user_ldap/lib/Migration/UUIDFix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\User_LDAP\Migration;


use OC\BackgroundJob\QueuedJob;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Proxy;
use OCA\User_LDAP\User_Proxy;

abstract class UUIDFix extends QueuedJob {
/** @var AbstractMapping */
protected $mapper;

/** @var Proxy */
protected $proxy;

public function run($argument) {
$isUser = $this->proxy instanceof User_Proxy;
foreach($argument['records'] as $record) {
$access = $this->proxy->getLDAPAccess($record['name']);
$uuid = $access->getUUID($record['dn'], $isUser);
if($uuid === false) {
// record not found, no prob, continue with the next
continue;
}
if($uuid !== $record['uuid']) {
$this->mapper->setUUIDbyDN($uuid, $record['dn']);
}
}
}

/**
* @param Proxy $proxy
*/
public function overrideProxy(Proxy $proxy) {
$this->proxy = $proxy;
}
}
37 changes: 37 additions & 0 deletions apps/user_ldap/lib/Migration/UUIDFixGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\User_LDAP\Migration;

use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\User_Proxy;
use OCP\IConfig;

class UUIDFixGroup extends UUIDFix {
public function __construct(GroupMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) {
$this->mapper = $mapper;
$this->proxy = new User_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config);
}
}
101 changes: 101 additions & 0 deletions apps/user_ldap/lib/Migration/UUIDFixInsert.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\User_LDAP\Migration;

use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCP\BackgroundJob\IJobList;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;

class UUIDFixInsert implements IRepairStep {

/** @var IConfig */
protected $config;

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

/** @var GroupMapping */
protected $groupMapper;

/** @var IJobList */
protected $jobList;

public function __construct(IConfig $config, UserMapping $userMapper, GroupMapping $groupMapper, IJobList $jobList) {
$this->config = $config;
$this->userMapper = $userMapper;
$this->groupMapper = $groupMapper;
$this->jobList = $jobList;
}

/**
* Returns the step's name
*
* @return string
* @since 9.1.0
*/
public function getName() {
return 'Insert UUIDFix background job for user and group in batches';
}

/**
* Run repair step.
* Must throw exception on error.
*
* @param IOutput $output
* @throws \Exception in case of failure
* @since 9.1.0
*/
public function run(IOutput $output) {
$installedVersion = $this->config->getAppValue('user_ldap', 'installed_version', '1.1.2');
if(version_compare($installedVersion, '1.1.2') !== -1) {
return;
}

foreach ([$this->userMapper, $this->groupMapper] as $mapper) {
$offset = 0;
$batchSize = 50;
$jobClass = $mapper instanceof UserMapping ? UUIDFixUser::class : UUIDFixGroup::class;
do {
$retry = false;
$records = $mapper->getList($offset, $batchSize);
if(count($records) === 0){
continue;
}
try {
$this->jobList->add($jobClass, ['records' => $records]);
$offset += $batchSize;
} catch (\InvalidArgumentException $e) {
if(strpos($e->getMessage(), 'Background job arguments can\'t exceed 4000') !== false) {
$batchSize = intval(floor(count($records) * 0.8));
$retry = true;
}
}
} while (count($records) === $batchSize || $retry);
}

}
}
37 changes: 37 additions & 0 deletions apps/user_ldap/lib/Migration/UUIDFixUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\User_LDAP\Migration;

use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\Group_Proxy;
use OCP\IConfig;

class UUIDFixUser extends UUIDFix {
public function __construct(UserMapping $mapper, LDAP $ldap, IConfig $config, Helper $helper) {
$this->mapper = $mapper;
$this->proxy = new Group_Proxy($helper->getServerConfigurationPrefixes(true), $ldap, $config);
}
}
6 changes: 6 additions & 0 deletions apps/user_ldap/lib/Proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ abstract protected function callOnLastSeenOn($id, $method, $parameters, $passOnW
*/
abstract protected function walkBackends($id, $method, $parameters);

/**
* @param string $id
* @return Access
*/
abstract public function getLDAPAccess($id);

/**
* Takes care of the request to the User backend
* @param string $id
Expand Down
12 changes: 11 additions & 1 deletion apps/user_ldap/tests/Integration/AbstractIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@

use OCA\User_LDAP\Access;
use OCA\User_LDAP\Connection;
use OCA\User_LDAP\FilesystemHelper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\User\Manager;

abstract class AbstractIntegrationTest {
Expand Down Expand Up @@ -106,7 +108,15 @@ protected function initConnection() {
* @return Manager
*/
protected function initUserManager() {
$this->userManager = new FakeManager();
$this->userManager = new Manager(
\OC::$server->getConfig(),
new FilesystemHelper(),
new LogWrapper(),
\OC::$server->getAvatarManager(),
new \OCP\Image(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager()
);
}

/**
Expand Down
Loading