Skip to content

Commit

Permalink
Merge pull request #34557 from owncloud/static-tags-filtering-backend…
Browse files Browse the repository at this point in the history
…-stable10

[stable10] Backport of Fix static tags filtering in the backend
  • Loading branch information
Vincent Petry authored Feb 19, 2019
2 parents 1d37851 + 7385dc5 commit 837c62b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
14 changes: 14 additions & 0 deletions apps/dav/lib/SystemTag/SystemTagsByIdCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ public function getChildren() {
}

$tags = $this->tagManager->getAllTags($visibilityFilter, null);
/**
* Filter out static tags if the user does not have privilege to see it.
*/
$tags = \array_filter($tags, function ($tag) {
if (!$tag->isUserEditable() && $tag->isUserAssignable()) {
$user = $this->userSession->getUser();
if (($user !== null) &&
!$this->tagManager->canUserUseStaticTagInGroup($tag, $user)) {
return false;
}
}
return true;
});

return \array_map(function ($tag) {
return $this->makeNode($tag);
}, $tags);
Expand Down
68 changes: 64 additions & 4 deletions apps/dav/tests/unit/SystemTag/SystemTagsByIdCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\DAV\Tests\unit\SystemTag;

use OC\SystemTag\SystemTag;
use OCP\IUser;
use OCP\SystemTag\TagNotFoundException;

class SystemTagsByIdCollectionTest extends \Test\TestCase {
Expand Down Expand Up @@ -141,8 +142,8 @@ public function testGetChildUserNotVisible() {
}

public function testGetChildrenAdmin() {
$tag1 = new SystemTag(123, 'One', true, false);
$tag2 = new SystemTag(456, 'Two', true, true);
$tag1 = new SystemTag(123, 'One', true, false, false);
$tag2 = new SystemTag(456, 'Two', true, true, true);

$this->tagManager->expects($this->once())
->method('getAllTags')
Expand All @@ -160,8 +161,8 @@ public function testGetChildrenAdmin() {
}

public function testGetChildrenNonAdmin() {
$tag1 = new SystemTag(123, 'One', true, false);
$tag2 = new SystemTag(456, 'Two', true, true);
$tag1 = new SystemTag(123, 'One', true, false, false);
$tag2 = new SystemTag(456, 'Two', true, true, true);

$this->tagManager->expects($this->once())
->method('getAllTags')
Expand All @@ -178,6 +179,65 @@ public function testGetChildrenNonAdmin() {
$this->assertEquals($tag2, $children[1]->getSystemTag());
}

/**
* This test proves getChildren would provide staticTags if the user has the
* privilege to see the static tag
*/
public function testGetChildrenWithStaticTagsAndOtherTags() {
$visibleTag = new SystemTag(123, 'VisibleTag', true, true, true);
$restrictTag = new SystemTag(456, 'RestrictTag', true, false, false);
$staticTag = new SystemTag(789, 'StaticTag', true, false, true);

$this->tagManager->method('getAllTags')
->with(true)
->will($this->returnValue([$visibleTag, $restrictTag, $staticTag]));

$user = $this->createMock(IUser::class);

$this->tagManager->method('canUserUseStaticTagInGroup')
->with($staticTag, $user)
->willReturn(true);

$children = $this->getNode(false)->getChildren();

$this->assertCount(3, $children);
$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[0]);
$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[1]);
$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[2]);
$this->assertEquals($visibleTag, $children[0]->getSystemTag());
$this->assertEquals($restrictTag, $children[1]->getSystemTag());
$this->assertEquals($staticTag, $children[2]->getSystemTag());
}

/**
* This test proves getChildren would prohibit staticTags if the user doesn't
* have the privilege to see the static tag
*/
public function testGetChildrenWithoutStaticTagsAndOtherTags() {
$visibleTag = new SystemTag(123, 'VisibleTag', true, true, true);
$restrictTag = new SystemTag(456, 'RestrictTag', true, false, false);
$staticTag = new SystemTag(789, 'StaticTag', true, false, true);

$this->tagManager->method('getAllTags')
->with(true)
->will($this->returnValue([$visibleTag, $restrictTag]));

$user = $this->createMock(IUser::class);

$this->tagManager->method('canUserUseStaticTagInGroup')
->with($staticTag, $user)
->willReturn(false);

$children = $this->getNode(false)->getChildren();

$this->assertCount(2, $children);

$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[0]);
$this->assertInstanceOf('\OCA\DAV\SystemTag\SystemTagNode', $children[1]);
$this->assertEquals($visibleTag, $children[0]->getSystemTag());
$this->assertEquals($restrictTag, $children[1]->getSystemTag());
}

public function testGetChildrenEmpty() {
$this->tagManager->expects($this->once())
->method('getAllTags')
Expand Down

0 comments on commit 837c62b

Please sign in to comment.