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(), new \OCA\User_LDAP\LogWrapper(),
\OC::$server->getAvatarManager(), \OC::$server->getAvatarManager(),
new \OCP\Image(), new \OCP\Image(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager(), \OC::$server->getUserManager(),
\OC::$server->getNotificationManager()); \OC::$server->getNotificationManager(),
\OC::$server->get(\OCP\Share\IManager::class)
);
$access = new \OCA\User_LDAP\Access( $access = new \OCA\User_LDAP\Access(
$con, $con,

View File

@ -52,6 +52,7 @@ use OC\Hooks\PublicEmitter;
use OC\ServerNotAvailableException; use OC\ServerNotAvailableException;
use OCA\User_LDAP\Exceptions\ConstraintViolationException; use OCA\User_LDAP\Exceptions\ConstraintViolationException;
use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\Mapping\AbstractMapping;
use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\User\OfflineUser; use OCA\User_LDAP\User\OfflineUser;
use OCP\IConfig; use OCP\IConfig;
@ -74,9 +75,7 @@ class Access extends LDAPUtility {
protected $pagedSearchedSuccessful; protected $pagedSearchedSuccessful;
/** /**
* protected $cookies = []; * @var UserMapping $userMapper
*
* @var AbstractMapping $userMapper
*/ */
protected $userMapper; protected $userMapper;
@ -123,12 +122,9 @@ class Access extends LDAPUtility {
} }
/** /**
* returns the User Mapper
*
* @return AbstractMapping
* @throws \Exception * @throws \Exception
*/ */
public function getUserMapper() { public function getUserMapper(): UserMapping {
if (is_null($this->userMapper)) { if (is_null($this->userMapper)) {
throw new \Exception('UserMapper was not assigned to this Access instance.'); 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 OC\BackgroundJob\TimedJob;
use OCA\User_LDAP\Helper; use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User\DeletedUsersIndex;
use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\User_LDAP;
@ -68,11 +67,12 @@ class CleanUp extends TimedJob {
/** @var DeletedUsersIndex */ /** @var DeletedUsersIndex */
protected $dui; protected $dui;
public function __construct(User_Proxy $userBackend) { public function __construct(User_Proxy $userBackend, DeletedUsersIndex $dui) {
$minutes = \OC::$server->getConfig()->getSystemValue( $minutes = \OC::$server->getConfig()->getSystemValue(
'ldapUserCleanupInterval', (string)$this->defaultIntervalMin); 'ldapUserCleanupInterval', (string)$this->defaultIntervalMin);
$this->setInterval((int)$minutes * 60); $this->setInterval((int)$minutes * 60);
$this->userBackend = $userBackend; $this->userBackend = $userBackend;
$this->dui = $dui;
} }
/** /**
@ -115,9 +115,6 @@ class CleanUp extends TimedJob {
if (isset($arguments['deletedUsersIndex'])) { if (isset($arguments['deletedUsersIndex'])) {
$this->dui = $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\AccessFactory;
use OCA\User_LDAP\Configuration; use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ConnectionFactory; use OCA\User_LDAP\ConnectionFactory;
use OCA\User_LDAP\FilesystemHelper;
use OCA\User_LDAP\Helper; use OCA\User_LDAP\Helper;
use OCA\User_LDAP\LDAP; use OCA\User_LDAP\LDAP;
use OCA\User_LDAP\LogWrapper;
use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Mapping\UserMapping;
use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\Manager;
use OCP\IAvatarManager; use OCP\IAvatarManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Image;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Notification\IManager; use OCP\Notification\IManager;
@ -68,7 +65,8 @@ class Sync extends TimedJob {
/** @var AccessFactory */ /** @var AccessFactory */
protected $accessFactory; protected $accessFactory;
public function __construct() { public function __construct(Manager $userManager) {
$this->userManager = $userManager;
$this->setInterval( $this->setInterval(
\OC::$server->getConfig()->getAppValue( \OC::$server->getConfig()->getAppValue(
'user_ldap', 'user_ldap',
@ -345,17 +343,6 @@ class Sync extends TimedJob {
if (isset($argument['userManager'])) { if (isset($argument['userManager'])) {
$this->userManager = $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'])) { if (isset($argument['mapper'])) {
@ -363,7 +350,7 @@ class Sync extends TimedJob {
} else { } else {
$this->mapper = new UserMapping($this->dbc); $this->mapper = new UserMapping($this->dbc);
} }
if (isset($argument['connectionFactory'])) { if (isset($argument['connectionFactory'])) {
$this->connectionFactory = $argument['connectionFactory']; $this->connectionFactory = $argument['connectionFactory'];
} else { } else {

View File

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

View File

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

View File

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

View File

@ -39,6 +39,7 @@ use OCP\IDBConnection;
use OCP\Image; use OCP\Image;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use OCP\Share\IManager;
/** /**
* Manager * Manager
@ -82,32 +83,29 @@ class Manager {
* @var CappedMemoryCache $usersByUid * @var CappedMemoryCache $usersByUid
*/ */
protected $usersByUid; protected $usersByUid;
/** @var IManager */
private $shareManager;
/** public function __construct(
* @param IConfig $ocConfig IConfig $ocConfig,
* @param \OCA\User_LDAP\FilesystemHelper $ocFilesystem object that FilesystemHelper $ocFilesystem,
* gives access to necessary functions from the OC filesystem LogWrapper $ocLog,
* @param \OCA\User_LDAP\LogWrapper $ocLog IAvatarManager $avatarManager,
* @param IAvatarManager $avatarManager Image $image,
* @param Image $image an empty image instance IUserManager $userManager,
* @param IDBConnection $db INotificationManager $notificationManager,
* @throws \Exception when the methods mentioned above do not exist IManager $shareManager
*/ ) {
public function __construct(IConfig $ocConfig,
FilesystemHelper $ocFilesystem, LogWrapper $ocLog,
IAvatarManager $avatarManager, Image $image,
IDBConnection $db, IUserManager $userManager,
INotificationManager $notificationManager) {
$this->ocConfig = $ocConfig; $this->ocConfig = $ocConfig;
$this->ocFilesystem = $ocFilesystem; $this->ocFilesystem = $ocFilesystem;
$this->ocLog = $ocLog; $this->ocLog = $ocLog;
$this->avatarManager = $avatarManager; $this->avatarManager = $avatarManager;
$this->image = $image; $this->image = $image;
$this->db = $db;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->notificationManager = $notificationManager; $this->notificationManager = $notificationManager;
$this->usersByDN = new CappedMemoryCache(); $this->usersByDN = new CappedMemoryCache();
$this->usersByUid = new CappedMemoryCache(); $this->usersByUid = new CappedMemoryCache();
$this->shareManager = $shareManager;
} }
/** /**
@ -229,8 +227,9 @@ class Manager {
return new OfflineUser( return new OfflineUser(
$id, $id,
$this->ocConfig, $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 OCA\User_LDAP\Mapping\UserMapping;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection; use OCP\IDBConnection;
use OCP\Share\IManager;
use OCP\Share\IShare;
class OfflineUser { class OfflineUser {
/** /**
@ -78,18 +80,19 @@ class OfflineUser {
* @var \OCA\User_LDAP\Mapping\UserMapping * @var \OCA\User_LDAP\Mapping\UserMapping
*/ */
protected $mapping; protected $mapping;
/** @var IManager */
private $shareManager;
/** public function __construct(
* @param string $ocName $ocName,
* @param IConfig $config IConfig $config,
* @param IDBConnection $db UserMapping $mapping,
* @param \OCA\User_LDAP\Mapping\UserMapping $mapping IManager $shareManager
*/ ) {
public function __construct($ocName, IConfig $config, IDBConnection $db, UserMapping $mapping) {
$this->ocName = $ocName; $this->ocName = $ocName;
$this->config = $config; $this->config = $config;
$this->db = $db;
$this->mapping = $mapping; $this->mapping = $mapping;
$this->shareManager = $shareManager;
} }
/** /**
@ -236,29 +239,33 @@ class OfflineUser {
$this->determineShares(); $this->determineShares();
} }
/** /**
* finds out whether the user has active shares. The result is stored in * finds out whether the user has active shares. The result is stored in
* $this->hasActiveShares * $this->hasActiveShares
*/ */
protected function determineShares() { protected function determineShares() {
$query = $this->db->prepare(' $shareInterface = new \ReflectionClass(IShare::class);
SELECT `uid_owner` $shareConstants = $shareInterface->getConstants();
FROM `*PREFIX*share`
WHERE `uid_owner` = ? foreach ($shareConstants as $constantName => $constantValue) {
', 1); if (strpos($constantName, 'TYPE_') !== 0
$query->execute([$this->ocName]); || $constantValue === IShare::TYPE_USERGROUP
if ($query->rowCount() > 0) { ) {
$this->hasActiveShares = true; continue;
return; }
$shares = $this->shareManager->getSharesBy(
$this->ocName,
$constantValue,
null,
false,
1
);
if (!empty($shares)) {
$this->hasActiveShares = true;
return;
}
} }
$query = $this->db->prepare(' $this->hasActiveShares = false;
SELECT `owner`
FROM `*PREFIX*share_external`
WHERE `owner` = ?
', 1);
$query->execute([$this->ocName]);
$this->hasActiveShares = $query->rowCount() > 0;
} }
} }

View File

@ -48,10 +48,10 @@ use OCA\User_LDAP\User\OfflineUser;
use OCA\User_LDAP\User\User; use OCA\User_LDAP\User\User;
use OCP\IAvatarManager; use OCP\IAvatarManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Image; use OCP\Image;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use OCP\Share\IManager;
use Test\TestCase; use Test\TestCase;
/** /**
@ -64,6 +64,8 @@ use Test\TestCase;
class AccessTest extends TestCase { class AccessTest extends TestCase {
/** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */ /** @var UserMapping|\PHPUnit\Framework\MockObject\MockObject */
protected $userMapper; protected $userMapper;
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
protected $shareManager;
/** @var Connection|\PHPUnit\Framework\MockObject\MockObject */ /** @var Connection|\PHPUnit\Framework\MockObject\MockObject */
private $connection; private $connection;
/** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */ /** @var LDAP|\PHPUnit\Framework\MockObject\MockObject */
@ -87,6 +89,7 @@ class AccessTest extends TestCase {
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->userMapper = $this->createMock(UserMapping::class); $this->userMapper = $this->createMock(UserMapping::class);
$this->ncUserManager = $this->createMock(IUserManager::class); $this->ncUserManager = $this->createMock(IUserManager::class);
$this->shareManager = $this->createMock(IManager::class);
$this->access = new Access( $this->access = new Access(
$this->connection, $this->connection,
@ -111,9 +114,9 @@ class AccessTest extends TestCase {
$this->createMock(LogWrapper::class), $this->createMock(LogWrapper::class),
$this->createMock(IAvatarManager::class), $this->createMock(IAvatarManager::class),
$this->createMock(Image::class), $this->createMock(Image::class),
$this->createMock(IDBConnection::class),
$this->createMock(IUserManager::class), $this->createMock(IUserManager::class),
$this->createMock(INotificationManager::class)]) $this->createMock(INotificationManager::class),
$this->shareManager])
->getMock(); ->getMock();
$helper = new Helper(\OC::$server->getConfig()); $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\LogWrapper;
use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\Manager;
use OCA\User_LDAP\UserPluginManager; use OCA\User_LDAP\UserPluginManager;
use OCP\Share\IManager;
abstract class AbstractIntegrationTest { abstract class AbstractIntegrationTest {
/** @var LDAP */ /** @var LDAP */
@ -126,9 +127,9 @@ abstract class AbstractIntegrationTest {
new LogWrapper(), new LogWrapper(),
\OC::$server->getAvatarManager(), \OC::$server->getAvatarManager(),
new \OCP\Image(), new \OCP\Image(),
\OC::$server->getDatabaseConnection(),
\OC::$server->getUserManager(), \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 { public function setUp(): void {
$this->createMocks(); $this->createMocks();
$this->bgJob = new CleanUp($this->mocks['userBackend']); $this->bgJob = new CleanUp($this->mocks['userBackend'], $this->mocks['deletedUsersIndex']);
$this->bgJob->setArguments($this->mocks); $this->bgJob->setArguments($this->mocks);
} }

View File

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

View File

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

View File

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

View File

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