]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add user parameter to tag manager
authorVincent Petry <pvince81@owncloud.com>
Wed, 10 Dec 2014 14:59:41 +0000 (15:59 +0100)
committerVincent Petry <pvince81@owncloud.com>
Thu, 11 Dec 2014 11:22:28 +0000 (12:22 +0100)
lib/private/server.php
lib/private/tagmanager.php
lib/public/itagmanager.php
tests/lib/tags.php

index e0105506970e7e90cbb014d6be2796012eaaf704..02cc4e2755aeeead26c1be9316ab848d9a585bc2 100644 (file)
@@ -87,8 +87,7 @@ class Server extends SimpleContainer implements IServerContainer {
                });
                $this->registerService('TagManager', function (Server $c) {
                        $tagMapper = $c->query('TagMapper');
-                       $user = \OC_User::getUser();
-                       return new TagManager($tagMapper, $user);
+                       return new TagManager($tagMapper, $c->getUserSession());
                });
                $this->registerService('RootFolder', function (Server $c) {
                        // TODO: get user and user manager from container as well
index d5bff04acffe97fe6d3be0e7bf5e8e59bb05430a..6c7eeab87ebe92e35d86a1a3cb4fd51c09ef0947 100644 (file)
@@ -38,11 +38,11 @@ use OC\Tagging\TagMapper;
 class TagManager implements \OCP\ITagManager {
 
        /**
-        * User
+        * User session
         *
-        * @var string
+        * @var \OCP\IUserSession
         */
-       private $user;
+       private $userSession;
 
        /**
         * TagMapper
@@ -55,12 +55,11 @@ class TagManager implements \OCP\ITagManager {
        * Constructor.
        *
        * @param TagMapper $mapper Instance of the TagMapper abstraction layer.
-       * @param string $user The user whose data the object will operate on.
+       * @param \OCP\IUserSession $userSession the user session
        */
-       public function __construct(TagMapper $mapper, $user) {
-
+       public function __construct(TagMapper $mapper, \OCP\IUserSession $userSession) {
                $this->mapper = $mapper;
-               $this->user = $user;
+               $this->userSession = $userSession;
 
        }
 
@@ -71,10 +70,15 @@ class TagManager implements \OCP\ITagManager {
        * @param string $type The type identifier e.g. 'contact' or 'event'.
        * @param array $defaultTags An array of default tags to be used if none are stored.
        * @param boolean $includeShared Whether to include tags for items shared with this user by others.
+       * @param string $userId user for which to retrieve the tags, defaults to the currently
+       * logged in user
        * @return \OCP\ITags
        */
-       public function load($type, $defaultTags=array(), $includeShared=false) {
-               return new Tags($this->mapper, $this->user, $type, $defaultTags, $includeShared);
+       public function load($type, $defaultTags = array(), $includeShared = false, $userId = null) {
+               if (is_null($userId)) {
+                       $userId = $this->userSession->getUser()->getUId();
+               }
+               return new Tags($this->mapper, $userId, $type, $defaultTags, $includeShared);
        }
 
 }
index 54daa5cc1cbbc643bb04ffbab3561380411ee91f..ac80eebc72dba7884d210dac129e1cba08bdf70e 100644 (file)
@@ -43,14 +43,15 @@ namespace OCP;
 interface ITagManager {
 
        /**
-       * Create a new \OCP\ITags instance and load tags from db.
+       * Create a new \OCP\ITags instance and load tags from db for the current user.
        *
        * @see \OCP\ITags
        * @param string $type The type identifier e.g. 'contact' or 'event'.
        * @param array $defaultTags An array of default tags to be used if none are stored.
        * @param boolean $includeShared Whether to include tags for items shared with this user by others.
+       * @param string $userId user for which to retrieve the tags, defaults to the currently
+       * logged in user
        * @return \OCP\ITags
        */
-       public function load($type, $defaultTags=array(), $includeShared=false);
-
+       public function load($type, $defaultTags = array(), $includeShared = false, $userId = null);
 }
index 78f5085df399333df412f9be61118b01a3109c72..71296d2e34681086ddf8fea7640775fa9c348a89 100644 (file)
 class Test_Tags extends \Test\TestCase {
 
        protected $objectType;
+       /** @var \OC\IUser */
        protected $user;
+       /** @var \OC\IUserSession */
+       protected $userSession;
        protected $backupGlobals = FALSE;
        /** @var \OC\Tagging\TagMapper */
        protected $tagMapper;
@@ -35,12 +38,19 @@ class Test_Tags extends \Test\TestCase {
 
                OC_User::clearBackends();
                OC_User::useBackend('dummy');
-               $this->user = $this->getUniqueID('user_');
+               $userId = $this->getUniqueID('user_');
+               OC_User::createUser($userId, 'pass');
+               OC_User::setUserId($userId);
+               $this->user = new OC\User\User($userId, null);
+               $this->userSession = $this->getMock('\OCP\IUserSession');
+               $this->userSession
+                       ->expects($this->any())
+                       ->method('getUser')
+                       ->will($this->returnValue($this->user));
+
                $this->objectType = $this->getUniqueID('type_');
-               OC_User::createUser($this->user, 'pass');
-               OC_User::setUserId($this->user);
                $this->tagMapper = new OC\Tagging\TagMapper(\OC::$server->getDb());
-               $this->tagMgr = new OC\TagManager($this->tagMapper, $this->user);
+               $this->tagMgr = new OC\TagManager($this->tagMapper, $this->userSession);
 
        }
 
@@ -166,7 +176,7 @@ class Test_Tags extends \Test\TestCase {
                );
        }
 
-       public function testdeleteTags() {
+       public function testDeleteTags() {
                $defaultTags = array('Friends', 'Family', 'Work', 'Other');
                $tagger = $this->tagMgr->load($this->objectType, $defaultTags);
 
@@ -177,7 +187,6 @@ class Test_Tags extends \Test\TestCase {
 
                $tagger->delete(array('Friends', 'Work', 'Other'));
                $this->assertEquals(0, count($tagger->getTags()));
-
        }
 
        public function testRenameTag() {
@@ -233,27 +242,32 @@ class Test_Tags extends \Test\TestCase {
        }
 
        public function testShareTags() {
-               $test_tag = 'TestTag';
+               $testTag = 'TestTag';
                OCP\Share::registerBackend('test', 'Test_Share_Backend');
 
                $tagger = $this->tagMgr->load('test');
-               $tagger->tagAs(1, $test_tag);
-
-               $other_user = $this->getUniqueID('user2_');
-               OC_User::createUser($other_user, 'pass');
-
-               OC_User::setUserId($other_user);
-               $other_tagMgr = new OC\TagManager($this->tagMapper, $other_user);
-               $other_tagger = $other_tagMgr->load('test');
-               $this->assertFalse($other_tagger->hasTag($test_tag));
-
-               OC_User::setUserId($this->user);
-               OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $other_user, \OCP\Constants::PERMISSION_READ);
-
-               OC_User::setUserId($other_user);
-               $other_tagger = $other_tagMgr->load('test', array(), true); // Update tags, load shared ones.
-               $this->assertTrue($other_tagger->hasTag($test_tag));
-               $this->assertContains(1, $other_tagger->getIdsForTag($test_tag));
+               $tagger->tagAs(1, $testTag);
+
+               $otherUserId = $this->getUniqueID('user2_');
+               OC_User::createUser($otherUserId, 'pass');
+               OC_User::setUserId($otherUserId);
+               $otherUserSession = $this->getMock('\OCP\IUserSession');
+               $otherUserSession
+                       ->expects($this->any())
+                       ->method('getUser')
+                       ->will($this->returnValue(new OC\User\User($otherUserId, null)));
+
+               $otherTagMgr = new OC\TagManager($this->tagMapper, $otherUserSession);
+               $otherTagger = $otherTagMgr->load('test');
+               $this->assertFalse($otherTagger->hasTag($testTag));
+
+               OC_User::setUserId($this->user->getUID());
+               OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ);
+
+               OC_User::setUserId($otherUserId);
+               $otherTagger = $otherTagMgr->load('test', array(), true); // Update tags, load shared ones.
+               $this->assertTrue($otherTagger->hasTag($testTag));
+               $this->assertContains(1, $otherTagger->getIdsForTag($testTag));
        }
 
 }