]> source.dussan.org Git - nextcloud-server.git/commitdiff
planned refactorings for OC 8
authorArthur Schiwon <blizzz@owncloud.com>
Sat, 20 Dec 2014 16:08:26 +0000 (17:08 +0100)
committerArthur Schiwon <blizzz@owncloud.com>
Sat, 20 Dec 2014 17:28:29 +0000 (18:28 +0100)
apps/user_ldap/appinfo/register_command.php
apps/user_ldap/command/checkuser.php
apps/user_ldap/lib/jobs/cleanup.php
apps/user_ldap/lib/mapping/abstractmapping.php
apps/user_ldap/lib/user/deletedusersindex.php
apps/user_ldap/tests/jobs/cleanup.php

index 314c73e6c4a619ecfd54f0dfeed7333d6798c1b2..e0013c4eb7a72333fb2031457acda0b028894279 100644 (file)
@@ -10,6 +10,17 @@ use OCA\user_ldap\lib\Helper;
 use OCA\user_ldap\lib\LDAP;
 use OCA\user_ldap\User_Proxy;
 use OCA\User_LDAP\Mapping\UserMapping;
+use OCA\User_LDAP\lib\User\DeletedUsersIndex;
+
+$dbConnection = \OC::$server->getDatabaseConnection();
+$userMapping = new UserMapping($dbConnection);
+$helper = new Helper();
+$uBackend = new User_Proxy(
+       $helper->getServerConfigurationPrefixes(true),
+       new LDAP()
+);
+$deletedUsersIndex = new DeletedUsersIndex(
+       \OC::$server->getConfig(), $dbConnection, $userMapping);
 
 $application->add(new OCA\user_ldap\Command\ShowConfig());
 $application->add(new OCA\user_ldap\Command\SetConfig());
@@ -17,13 +28,6 @@ $application->add(new OCA\user_ldap\Command\TestConfig());
 $application->add(new OCA\user_ldap\Command\CreateEmptyConfig());
 $application->add(new OCA\user_ldap\Command\DeleteConfig());
 $application->add(new OCA\user_ldap\Command\Search());
-$userMapping = new UserMapping(\OC::$server->getDatabaseConnection());
 $application->add(new OCA\user_ldap\Command\ShowRemnants($userMapping));
-$helper = new Helper();
-$uBackend = new User_Proxy(
-       $helper->getServerConfigurationPrefixes(true),
-       new LDAP()
-);
 $application->add(new OCA\user_ldap\Command\CheckUser(
-       $uBackend, $helper, \OC::$server->getConfig()
-));
+       $uBackend, $helper, $deletedUsersIndex, $userMapping));
index 96c6c8323565e77570fc8b30868efbbb7229ea11..5b1dc36c1d0ab92d6e3d7839496870dbbcca5db5 100644 (file)
@@ -16,6 +16,7 @@ use Symfony\Component\Console\Output\OutputInterface;
 
 use OCA\user_ldap\lib\user\User;
 use OCA\User_LDAP\lib\user\Manager;
+use OCA\User_LDAP\lib\User\DeletedUsersIndex;
 use OCA\user_ldap\lib\Helper;
 use OCA\user_ldap\User_Proxy;
 
@@ -26,18 +27,22 @@ class CheckUser extends Command {
        /** @var \OCA\User_LDAP\lib\Helper */
        protected $helper;
 
-       /** @var \OCP\IConfig */
-       protected $config;
+       /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */
+       protected $dui;
+
+       /** @var \OCA\User_LDAP\Mapping\UserMapping */
+       protected $mapping;
 
        /**
         * @param OCA\user_ldap\User_Proxy $uBackend
         * @param OCA\User_LDAP\lib\Helper $helper
         * @param OCP\IConfig $config
         */
-       public function __construct(User_Proxy $uBackend, Helper $helper, \OCP\IConfig $config) {
+       public function __construct(User_Proxy $uBackend, Helper $helper, DeletedUsersIndex $dui, UserMapping $mapping) {
                $this->backend = $uBackend;
                $this->helper = $helper;
-               $this->config = $config;
+               $this->dui = $dui;
+               $this->mapping = $mapping;
                parent::__construct();
        }
 
@@ -70,10 +75,7 @@ class CheckUser extends Command {
                                return;
                        }
 
-                       // TODO FIXME consolidate next line in DeletedUsersIndex
-                       // (impractical now, because of class dependencies)
-                       $this->config->setUserValue($uid, 'user_ldap', 'isDeleted', '1');
-
+                       $this->dui->markUser($uid);
                        $output->writeln('The user does not exists on LDAP anymore.');
                        $output->writeln('Clean up the user\'s remnants by: ./occ user:delete "'
                                . $uid . '"');
@@ -86,22 +88,11 @@ class CheckUser extends Command {
         * checks whether a user is actually mapped
         * @param string $ocName the username as used in ownCloud
         * @throws \Exception
-        * @return bool
+        * @return true
         */
        protected function confirmUserIsMapped($ocName) {
-               //TODO FIXME this should go to Mappings in OC 8
-               $db = \OC::$server->getDatabaseConnection();
-               $query = $db->prepare('
-                       SELECT
-                               `ldap_dn` AS `dn`
-                       FROM `*PREFIX*ldap_user_mapping`
-                       WHERE `owncloud_name` = ?'
-               );
-
-               $query->execute(array($ocName));
-               $result = $query->fetchColumn();
-
-               if($result === false) {
+               $dn = $this->mapping->getDNByName($ocName);
+               if ($dn === false) {
                        throw new \Exception('The given user is not a recognized LDAP user.');
                }
 
index 56fb296609d3e883e3227406d3f578ad98a6b89b..35252d6f6e5d06a5a25f9593c98b5385eeaa0dce 100644 (file)
@@ -11,6 +11,8 @@ namespace OCA\User_LDAP\Jobs;
 use \OCA\user_ldap\User_Proxy;
 use \OCA\user_ldap\lib\Helper;
 use \OCA\user_ldap\lib\LDAP;
+use \OCA\User_LDAP\lib\User\DeletedUsersIndex;
+use \OCA\User_LDAP\Mapping\UserMapping;
 
 /**
  * Class CleanUp
@@ -45,6 +47,12 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
         */
        protected $ldapHelper;
 
+       /** @var \OCA\User_LDAP\Mapping\UserMapping */
+       protected $mapping;
+
+       /** @var \OCA\User_LDAP\lib\User\DeletedUsersIndex */
+       protected $dui;
+
        /**
         * @var int $defaultIntervalMin default interval in minutes
         */
@@ -92,6 +100,19 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
                } else {
                        $this->db = \OC::$server->getDatabaseConnection();
                }
+
+               if(isset($arguments['mapping'])) {
+                       $this->mapping = $arguments['mapping'];
+               } else {
+                       $this->mapping = new UserMapping($this->db);
+               }
+
+               if(isset($arguments['deletedUsersIndex'])) {
+                       $this->dui = $arguments['deletedUsersIndex'];
+               } else {
+                       $this->dui = new DeletedUsersIndex(
+                               $this->ocConfig, $this->db, $this->mapping);
+               }
        }
 
        /**
@@ -104,7 +125,7 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
                if(!$this->isCleanUpAllowed()) {
                        return;
                }
-               $users = $this->getMappedUsers($this->limit, $this->getOffset());
+               $users = $this->mapping->getList($this->limit, $this->getOffset());
                if(!is_array($users)) {
                        //something wrong? Let's start from the beginning next time and
                        //abort
@@ -169,33 +190,11 @@ class CleanUp extends \OC\BackgroundJob\TimedJob {
        private function checkUser($user) {
                if($this->userBackend->userExistsOnLDAP($user['name'])) {
                        //still available, all good
+
                        return;
                }
 
-               // TODO FIXME consolidate next line in DeletedUsersIndex
-               // (impractical now, because of class dependencies)
-               $this->ocConfig->setUserValue($user['name'], 'user_ldap', 'isDeleted', '1');
-       }
-
-       /**
-        * returns a batch of users from the mappings table
-        * @param int $limit
-        * @param int $offset
-        * @return array
-        */
-       public function getMappedUsers($limit, $offset) {
-               $query = $this->db->prepare('
-                       SELECT
-                               `ldap_dn` AS `dn`,
-                               `owncloud_name` AS `name`,
-                               `directory_uuid` AS `uuid`
-                       FROM `*PREFIX*ldap_user_mapping`',
-                       $limit,
-                       $offset
-               );
-
-               $query->execute();
-               return $query->fetchAll();
+               $this->dui->markUser($user['name']);
        }
 
        /**
index 2c45c6bb1c153c47f2e61aecc903d7229f13d78b..19f173577f525f6145a76751ea4d498e844ecefc 100644 (file)
@@ -152,6 +152,27 @@ abstract class AbstractMapping {
                return $this->getXbyY('owncloud_name', 'directory_uuid', $uuid);
        }
 
+       /**
+        * gets a piece of the mapping list
+        * @param int $offset
+        * @param int $limit
+        * @return array
+        */
+       public function getList($offset = null, $limit = null) {
+               $query = $this->dbc->prepare('
+                       SELECT
+                               `ldap_dn` AS `dn`,
+                               `owncloud_name` AS `name`,
+                               `directory_uuid` AS `uuid`
+                       FROM `*PREFIX*ldap_user_mapping`',
+                       $limit,
+                       $offset
+               );
+
+               $query->execute();
+               return $query->fetchAll();
+       }
+
        /**
         * attempts to map the given entry
         * @param string $fdn fully distinguished name (from LDAP)
index 62abe2e10de7108a4ade8fb28cf7a1c8854613a5..6758553027982f6c98f8e6a749db42e2ee06b44a 100644 (file)
@@ -69,6 +69,7 @@ class DeletedUsersIndex {
                foreach($deletedUsers as $user) {
                        $userObjects[] = new OfflineUser($user, $this->config, $this->db, $this->mapping);
                }
+               $this->deletedUsers = $userObjects;
 
                return $this->deletedUsers;
        }
@@ -97,4 +98,12 @@ class DeletedUsersIndex {
                }
                return false;
        }
+
+       /**
+        * marks a user as deleted
+        * @param string ocName
+        */
+       public function markUser($ocName) {
+               $this->config->setUserValue($ocName, 'user_ldap', 'isDeleted', '1');
+       }
 }
index 3aa9a4a43c524c5e03ee40f380c0ced5695b18be..642bad571342633f5b922b982aab64b1429deaa3 100644 (file)
@@ -15,6 +15,10 @@ class Test_CleanUp extends \PHPUnit_Framework_TestCase {
                        $this->getMockBuilder('\OCA\user_ldap\User_Proxy')
                                ->disableOriginalConstructor()
                                ->getMock();
+               $mocks['deletedUsersIndex'] =
+                       $this->getMockBuilder('\OCA\user_ldap\lib\user\deletedUsersIndex')
+                               ->disableOriginalConstructor()
+                               ->getMock();
                $mocks['ocConfig']    = $this->getMock('\OCP\IConfig');
                $mocks['db']          = $this->getMock('\OCP\IDBConnection');
                $mocks['helper']      = $this->getMock('\OCA\user_ldap\lib\Helper');