Add user parameter to tag manager

This commit is contained in:
Vincent Petry 2014-12-10 15:59:41 +01:00
parent 5101bc54fa
commit 745d8706b9
4 changed files with 52 additions and 34 deletions

View 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

View 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);
}
}

View 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);
}

View file

@ -23,7 +23,10 @@
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);
$tagger->tagAs(1, $testTag);
$other_user = $this->getUniqueID('user2_');
OC_User::createUser($other_user, 'pass');
$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)));
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));
$otherTagMgr = new OC\TagManager($this->tagMapper, $otherUserSession);
$otherTagger = $otherTagMgr->load('test');
$this->assertFalse($otherTagger->hasTag($testTag));
OC_User::setUserId($this->user);
OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $other_user, \OCP\Constants::PERMISSION_READ);
OC_User::setUserId($this->user->getUID());
OCP\Share::shareItem('test', 1, OCP\Share::SHARE_TYPE_USER, $otherUserId, \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));
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));
}
}