user share manager to determine share ownership

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2020-10-26 14:44:15 +01:00
parent 951887e922
commit fd1fd5afa4
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
17 changed files with 123 additions and 156 deletions

View File

@ -61,9 +61,10 @@ $userManager = new \OCA\User_LDAP\User\Manager(
new \OCA\User_LDAP\LogWrapper(),
\OC::$server->getAvatarManager(),
new \OCP\Image(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager());
\OC::$server->getNotificationManager(),
\OC::$server->get(\OCP\Share\IManager::class)
);
$access = new \OCA\User_LDAP\Access(
$con,

View File

@ -52,6 +52,7 @@ use OC\Hooks\PublicEmitter;
use OC\ServerNotAvailableException;
use OCA\User_LDAP\Exceptions\ConstraintViolationException;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser;
use OCP\IConfig;
@ -74,9 +75,7 @@ class Access extends LDAPUtility {
protected $pagedSearchedSuccessful;
/**
* protected $cookies = [];
*
* @var AbstractMapping $userMapper
* @var UserMapping $userMapper
*/
protected $userMapper;
@ -123,12 +122,9 @@ class Access extends LDAPUtility {
}
/**
* returns the User Mapper
*
* @return AbstractMapping
* @throws \Exception
*/
public function getUserMapper() {
public function getUserMapper(): UserMapping {
if (is_null($this->userMapper)) {
throw new \Exception('UserMapper was not assigned to this Access instance.');
}

View File

@ -30,7 +30,6 @@ namespace OCA\User_LDAP\Jobs;
use OC\BackgroundJob\TimedJob;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\DeletedUsersIndex;
use OCA\User_LDAP\User_LDAP;
@ -68,11 +67,12 @@ class CleanUp extends TimedJob {
/** @var DeletedUsersIndex */
protected $dui;
public function __construct(User_Proxy $userBackend) {
public function __construct(User_Proxy $userBackend, DeletedUsersIndex $dui) {
$minutes = \OC::$server->getConfig()->getSystemValue(
'ldapUserCleanupInterval', (string)$this->defaultIntervalMin);
$this->setInterval((int)$minutes * 60);
$this->userBackend = $userBackend;
$this->dui = $dui;
}
/**
@ -115,9 +115,6 @@ class CleanUp extends TimedJob {
if (isset($arguments['deletedUsersIndex'])) {
$this->dui = $arguments['deletedUsersIndex'];
} else {
$this->dui = new DeletedUsersIndex(
$this->ocConfig, $this->db, $this->mapping);
}
}

View File

@ -29,16 +29,13 @@ use OC\ServerNotAvailableException;
use OCA\User_LDAP\AccessFactory;
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ConnectionFactory;
use OCA\User_LDAP\FilesystemHelper;
use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Notification\IManager;
@ -68,7 +65,8 @@ class Sync extends TimedJob {
/** @var AccessFactory */
protected $accessFactory;
public function __construct() {
public function __construct(Manager $userManager) {
$this->userManager = $userManager;
$this->setInterval(
\OC::$server->getConfig()->getAppValue(
'user_ldap',
@ -345,17 +343,6 @@ class Sync extends TimedJob {
if (isset($argument['userManager'])) {
$this->userManager = $argument['userManager'];
} else {
$this->userManager = new Manager(
$this->config,
new FilesystemHelper(),
new LogWrapper(),
$this->avatarManager,
new Image(),
$this->dbc,
$this->ncUserManager,
$this->notificationManager
);
}
if (isset($argument['mapper'])) {
@ -363,7 +350,7 @@ class Sync extends TimedJob {
} else {
$this->mapper = new UserMapping($this->dbc);
}
if (isset($argument['connectionFactory'])) {
$this->connectionFactory = $argument['connectionFactory'];
} else {

View File

@ -26,38 +26,19 @@
namespace OCA\User_LDAP;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\DeletedUsersIndex;
use OCP\IServerContainer;
use OCP\LDAP\ILDAPProvider;
use OCP\LDAP\ILDAPProviderFactory;
class LDAPProviderFactory implements ILDAPProviderFactory {
/**
* Server container
*
* @var IServerContainer
*/
/** * @var IServerContainer */
private $serverContainer;
/**
* Constructor for the LDAP provider factory
*
* @param IServerContainer $serverContainer server container
*/
public function __construct(IServerContainer $serverContainer) {
$this->serverContainer = $serverContainer;
}
/**
* creates and returns an instance of the ILDAPProvider
*
* @return OCP\LDAP\ILDAPProvider
*/
public function getLDAPProvider() {
$dbConnection = $this->serverContainer->getDatabaseConnection();
$userMapping = new UserMapping($dbConnection);
return new LDAPProvider($this->serverContainer, new Helper($this->serverContainer->getConfig()),
new DeletedUsersIndex($this->serverContainer->getConfig(),
$dbConnection, $userMapping));
public function getLDAPProvider(): ILDAPProvider {
return $this->serverContainer->get(LDAPProvider::class);
}
}

View File

@ -36,6 +36,7 @@ namespace OCA\User_LDAP;
use OCA\User_LDAP\Mapping\GroupMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager;
use OCP\Share\IManager;
abstract class Proxy {
private static $accesses = [];
@ -67,7 +68,7 @@ abstract class Proxy {
static $avatarM;
static $userMap;
static $groupMap;
static $db;
static $shareManager;
static $coreUserManager;
static $coreNotificationManager;
if ($fs === null) {
@ -80,10 +81,11 @@ abstract class Proxy {
$groupMap = new GroupMapping($db);
$coreUserManager = \OC::$server->getUserManager();
$coreNotificationManager = \OC::$server->getNotificationManager();
$shareManager = \OC::$server->get(IManager::class);
}
$userManager =
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(), $db,
$coreUserManager, $coreNotificationManager);
new Manager($ocConfig, $fs, $log, $avatarM, new \OCP\Image(),
$coreUserManager, $coreNotificationManager, $shareManager);
$connector = new Connection($this->ldap, $configPrefix);
$access = new Access($connector, $this->ldap, $userManager, new Helper($ocConfig), $ocConfig, $coreUserManager);
$access->setUserMapper($userMap);

View File

@ -26,6 +26,7 @@
namespace OCA\User_LDAP\User;
use OCA\User_LDAP\Mapping\UserMapping;
use OCP\Share\IManager;
/**
* Class DeletedUsersIndex
@ -37,11 +38,6 @@ class DeletedUsersIndex {
*/
protected $config;
/**
* @var \OCP\IDBConnection $db
*/
protected $db;
/**
* @var \OCA\User_LDAP\Mapping\UserMapping $mapping
*/
@ -51,16 +47,13 @@ class DeletedUsersIndex {
* @var array $deletedUsers
*/
protected $deletedUsers;
/** @var IManager */
private $shareManager;
/**
* @param \OCP\IConfig $config
* @param \OCP\IDBConnection $db
* @param \OCA\User_LDAP\Mapping\UserMapping $mapping
*/
public function __construct(\OCP\IConfig $config, \OCP\IDBConnection $db, UserMapping $mapping) {
public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager $shareManager) {
$this->config = $config;
$this->db = $db;
$this->mapping = $mapping;
$this->shareManager = $shareManager;
}
/**
@ -73,7 +66,7 @@ class DeletedUsersIndex {
$userObjects = [];
foreach ($deletedUsers as $user) {
$userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping);
$userObjects[] = new OfflineUser($user, $this->config, $this->mapping, $this->shareManager);
}
$this->deletedUsers = $userObjects;

View File

@ -39,6 +39,7 @@ use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\IManager;
/**
* Manager
@ -82,32 +83,29 @@ class Manager {
* @var CappedMemoryCache $usersByUid
*/
protected $usersByUid;
/** @var IManager */
private $shareManager;
/**
* @param IConfig $ocConfig
* @param \OCA\User_LDAP\FilesystemHelper $ocFilesystem object that
* gives access to necessary functions from the OC filesystem
* @param \OCA\User_LDAP\LogWrapper $ocLog
* @param IAvatarManager $avatarManager
* @param Image $image an empty image instance
* @param IDBConnection $db
* @throws \Exception when the methods mentioned above do not exist
*/
public function __construct(IConfig $ocConfig,
FilesystemHelper $ocFilesystem, LogWrapper $ocLog,
IAvatarManager $avatarManager, Image $image,
IDBConnection $db, IUserManager $userManager,
INotificationManager $notificationManager) {
public function __construct(
IConfig $ocConfig,
FilesystemHelper $ocFilesystem,
LogWrapper $ocLog,
IAvatarManager $avatarManager,
Image $image,
IUserManager $userManager,
INotificationManager $notificationManager,
IManager $shareManager
) {
$this->ocConfig = $ocConfig;
$this->ocFilesystem = $ocFilesystem;
$this->ocLog = $ocLog;
$this->avatarManager = $avatarManager;
$this->image = $image;
$this->db = $db;
$this->userManager = $userManager;
$this->notificationManager = $notificationManager;
$this->usersByDN = new CappedMemoryCache();
$this->usersByUid = new CappedMemoryCache();
$this->shareManager = $shareManager;
}
/**
@ -229,8 +227,9 @@ class Manager {
return new OfflineUser(
$id,
$this->ocConfig,
$this->db,
$this->access->getUserMapper());
$this->access->getUserMapper(),
$this->shareManager
);
}
/**

View File

@ -28,6 +28,8 @@ namespace OCA\User_LDAP\User;
use OCA\User_LDAP\Mapping\UserMapping;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Share\IManager;
use OCP\Share\IShare;
class OfflineUser {
/**
@ -78,18 +80,19 @@ class OfflineUser {
* @var \OCA\User_LDAP\Mapping\UserMapping
*/
protected $mapping;
/** @var IManager */
private $shareManager;
/**
* @param string $ocName
* @param IConfig $config
* @param IDBConnection $db
* @param \OCA\User_LDAP\Mapping\UserMapping $mapping
*/
public function __construct($ocName, IConfig $config, IDBConnection $db, UserMapping $mapping) {
public function __construct(
$ocName,
IConfig $config,
UserMapping $mapping,
IManager $shareManager
) {
$this->ocName = $ocName;
$this->config = $config;
$this->db = $db;
$this->mapping = $mapping;
$this->shareManager = $shareManager;
}
/**
@ -236,29 +239,33 @@ class OfflineUser {
$this->determineShares();
}
/**
* finds out whether the user has active shares. The result is stored in
* $this->hasActiveShares
*/
protected function determineShares() {
$query = $this->db->prepare('
SELECT `uid_owner`
FROM `*PREFIX*share`
WHERE `uid_owner` = ?
', 1);
$query->execute([$this->ocName]);
if ($query->rowCount() > 0) {
$this->hasActiveShares = true;
return;
$shareInterface = new \ReflectionClass(IShare::class);
$shareConstants = $shareInterface->getConstants();
foreach ($shareConstants as $constantName => $constantValue) {
if (strpos($constantName, 'TYPE_') !== 0
|| $constantValue === IShare::TYPE_USERGROUP
) {
continue;
}
$shares = $this->shareManager->getSharesBy(
$this->ocName,
$constantValue,
null,
false,
1
);
if (!empty($shares)) {
$this->hasActiveShares = true;
return;
}
}
$query = $this->db->prepare('
SELECT `owner`
FROM `*PREFIX*share_external`
WHERE `owner` = ?
', 1);
$query->execute([$this->ocName]);
$this->hasActiveShares = $query->rowCount() > 0;
$this->hasActiveShares = false;
}
}

View File

@ -48,10 +48,10 @@ use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\IManager;
use Test\TestCase;
/**
@ -64,6 +64,8 @@ use Test\TestCase;
class AccessTest extends TestCase {
/** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
protected $userMapper;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $shareManager;
/** @var Connection|\PHPUnit\Framework\MockObject\MockObject */
private $connection;
/** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
@ -87,6 +89,7 @@ class AccessTest extends TestCase {
$this->config = $this->createMock(IConfig::class);
$this->userMapper = $this->createMock(UserMapping::class);
$this->ncUserManager = $this->createMock(IUserManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->access = new Access(
$this->connection,
@ -111,9 +114,9 @@ class AccessTest extends TestCase {
$this->createMock(LogWrapper::class),
$this->createMock(IAvatarManager::class),
$this->createMock(Image::class),
$this->createMock(IDBConnection::class),
$this->createMock(IUserManager::class),
$this->createMock(INotificationManager::class)])
$this->createMock(INotificationManager::class),
$this->shareManager])
->getMock();
$helper = new Helper(\OC::$server->getConfig());

View File

@ -37,6 +37,7 @@ use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\UserPluginManager;
use OCP\Share\IManager;
abstract class AbstractIntegrationTest {
/** @var LDAP */
@ -126,9 +127,9 @@ abstract class AbstractIntegrationTest {
new LogWrapper(),
\OC::$server->getAvatarManager(),
new \OCP\Image(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager(),
\OC::$server->getNotificationManager()
\OC::$server->getNotificationManager(),
\OC::$server->get(IManager::class)
);
}

View File

@ -44,7 +44,7 @@ class CleanUpTest extends TestCase {
public function setUp(): void {
$this->createMocks();
$this->bgJob = new CleanUp($this->mocks['userBackend']);
$this->bgJob = new CleanUp($this->mocks['userBackend'], $this->mocks['deletedUsersIndex']);
$this->bgJob->setArguments($this->mocks);
}

View File

@ -89,7 +89,6 @@ class SyncTest extends TestCase {
$this->arguments = [
'helper' => $this->helper,
'ldapWrapper' => $this->ldapWrapper,
'userManager' => $this->userManager,
'mapper' => $this->mapper,
'config' => $this->config,
'avatarManager' => $this->avatarManager,
@ -100,7 +99,7 @@ class SyncTest extends TestCase {
'accessFactory' => $this->accessFactory,
];
$this->sync = new Sync();
$this->sync = new Sync($this->userManager);
}
public function intervalDataProvider() {

View File

@ -30,6 +30,7 @@ use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\DeletedUsersIndex;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Share\IManager;
/**
* Class DeletedUsersIndexTest
@ -50,6 +51,8 @@ class DeletedUsersIndexTest extends \Test\TestCase {
/** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
protected $mapping;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $shareManager;
protected function setUp(): void {
parent::setUp();
@ -62,8 +65,9 @@ class DeletedUsersIndexTest extends \Test\TestCase {
$this->config->deleteAppFromAllUsers('user_ldap');
$this->mapping = $this->createMock(UserMapping::class);
$this->shareManager = $this->createMock(IManager::class);
$this->dui = new DeletedUsersIndex($this->config, $this->db, $this->mapping);
$this->dui = new DeletedUsersIndex($this->config, $this->mapping, $this->shareManager);
}
protected function tearDown(): void {

View File

@ -42,6 +42,7 @@ use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager;
use OCP\Share\IManager;
/**
* Class Test_User_Manager
@ -86,6 +87,8 @@ class ManagerTest extends \Test\TestCase {
/** @var Manager */
protected $manager;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $shareManager;
protected function setUp(): void {
parent::setUp();
@ -96,11 +99,11 @@ class ManagerTest extends \Test\TestCase {
$this->log = $this->createMock(LogWrapper::class);
$this->avatarManager = $this->createMock(IAvatarManager::class);
$this->image = $this->createMock(Image::class);
$this->dbc = $this->createMock(IDBConnection::class);
$this->ncUserManager = $this->createMock(IUserManager::class);
$this->notificationManager = $this->createMock(INotificationManager::class);
$this->ldapWrapper = $this->createMock(ILDAPWrapper::class);
$this->shareManager = $this->createMock(IManager::class);
$this->connection = new Connection($this->ldapWrapper, '', null);
$this->access->expects($this->any())
@ -114,9 +117,9 @@ class ManagerTest extends \Test\TestCase {
$this->log,
$this->avatarManager,
$this->image,
$this->dbc,
$this->ncUserManager,
$this->notificationManager
$this->notificationManager,
$this->shareManager
);
$this->manager->setLdapAccess($this->access);

View File

@ -25,11 +25,11 @@ declare(strict_types=1);
namespace OCA\User_LDAP\Tests\User;
use Doctrine\DBAL\Driver\Statement;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\OfflineUser;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Test\TestCase;
class OfflineUserTest extends TestCase {
@ -42,53 +42,47 @@ class OfflineUserTest extends TestCase {
protected $uid;
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
protected $config;
/** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
protected $dbc;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $shareManager;
public function setUp(): void {
$this->uid = 'deborah';
$this->config = $this->createMock(IConfig::class);
$this->dbc = $this->createMock(IDBConnection::class);
$this->mapping = $this->createMock(UserMapping::class);
$this->shareManager = $this->createMock(IManager::class);
$this->offlineUser = new OfflineUser(
$this->uid,
$this->config,
$this->dbc,
$this->mapping
$this->mapping,
$this->shareManager
);
}
public function shareOwnerProvider(): array {
// tests for none, one, many
return [
[ 0, 0, false],
[ 1, 0, true],
[ 0, 1, true],
[ 1, 1, true],
[ 2, 0, true],
[ 0, 2, true],
[ 2, 2, true],
[[], false],
[[IShare::TYPE_USER], true],
[[IShare::TYPE_GROUP, IShare::TYPE_LINK], true],
[[IShare::TYPE_EMAIL, IShare::TYPE_REMOTE, IShare::TYPE_CIRCLE], true],
[[IShare::TYPE_GUEST, IShare::TYPE_REMOTE_GROUP, IShare::TYPE_ROOM], true],
];
}
/**
* @dataProvider shareOwnerProvider
*/
public function testHasActiveShares(int $internalOwnerships, int $externalOwnerships, bool $expected) {
$queryMock = $this->createMock(Statement::class);
$queryMock->expects($this->atLeastOnce())
->method('execute');
$queryMock->expects($this->atLeastOnce())
->method('rowCount')
->willReturnOnConsecutiveCalls(
$internalOwnerships > 0 ? 1 : 0,
$externalOwnerships > 0 ? 1 : 0
);
public function testHasActiveShares(array $existingShareTypes, bool $expected) {
$shareMock = $this->createMock(IShare::class);
$this->dbc->expects($this->atLeastOnce())
->method('prepare')
->willReturn($queryMock);
$this->shareManager->expects($this->atLeastOnce())
->method('getSharesBy')
->willReturnCallback(function (string $uid, int $shareType) use ($existingShareTypes, $shareMock) {
if (in_array($shareType, $existingShareTypes)) {
return [$shareMock];
}
return [];
});
$this->assertSame($expected, $this->offlineUser->getHasActiveShares());
}

View File

@ -1410,7 +1410,7 @@ class User_LDAPTest extends TestCase {
->with($this->isInstanceOf(AbstractMapping::class), $this->anything(), $uid, $uuid, true);
$this->access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(AbstractMapping::class));
->willReturn($this->createMock(UserMapping::class));
$this->assertEquals($this->backend->createUser($uid, $pwd),true);
}