Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only allow admins to delete tags #2512

Merged
merged 2 commits into from
Jan 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions apps/dav/lib/SystemTag/SystemTagNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,13 @@ public function getLastModified() {

public function delete() {
try {
if (!$this->isAdmin) {
throw new Forbidden('No permission to delete tag ' . $this->tag->getId());
}

if (!$this->tagManager->canUserSeeTag($this->tag, $this->user)) {
throw new NotFound('Tag with id ' . $this->tag->getId() . ' not found');
}
if (!$this->tagManager->canUserAssignTag($this->tag, $this->user)) {
throw new Forbidden('No permission to delete tag ' . $this->tag->getId());
}

$this->tagManager->deleteTags($this->tag->getId());
} catch (TagNotFoundException $e) {
Expand Down
42 changes: 13 additions & 29 deletions apps/dav/tests/unit/SystemTag/SystemTagNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,17 @@

namespace OCA\DAV\Tests\unit\SystemTag;

use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\Conflict;

use OC\SystemTag\SystemTag;
use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagAlreadyExistsException;
use OCP\SystemTag\ISystemTag;
use Sabre\DAV\Exception\Forbidden;

class SystemTagNodeTest extends \Test\TestCase {

/**
* @var \OCP\SystemTag\ISystemTagManager
* @var \OCP\SystemTag\ISystemTagManager|\PHPUnit_Framework_MockObject_MockObject
*/
private $tagManager;

Expand Down Expand Up @@ -113,7 +111,7 @@ public function tagNodeProvider() {
/**
* @dataProvider tagNodeProvider
*/
public function testUpdateTag($isAdmin, $originalTag, $changedArgs) {
public function testUpdateTag($isAdmin, ISystemTag $originalTag, $changedArgs) {
$this->tagManager->expects($this->once())
->method('canUserSeeTag')
->with($originalTag)
Expand Down Expand Up @@ -173,7 +171,7 @@ public function tagNodeProviderPermissionException() {
/**
* @dataProvider tagNodeProviderPermissionException
*/
public function testUpdateTagPermissionException($originalTag, $changedArgs, $expectedException = null) {
public function testUpdateTagPermissionException(ISystemTag $originalTag, $changedArgs, $expectedException = null) {
$this->tagManager->expects($this->any())
->method('canUserSeeTag')
->with($originalTag)
Expand Down Expand Up @@ -242,17 +240,16 @@ public function testUpdateTagNotFound() {
*/
public function testDeleteTag($isAdmin) {
$tag = new SystemTag(1, 'tag1', true, true);
$this->tagManager->expects($this->once())
$this->tagManager->expects($isAdmin ? $this->once() : $this->never())
->method('canUserSeeTag')
->with($tag)
->will($this->returnValue(true));
$this->tagManager->expects($this->once())
->method('canUserAssignTag')
->with($tag)
->will($this->returnValue(true));
$this->tagManager->expects($this->once())
$this->tagManager->expects($isAdmin ? $this->once() : $this->never())
->method('deleteTags')
->with('1');
if (!$isAdmin) {
$this->setExpectedException(Forbidden::class);
}
$this->getTagNode($isAdmin, $tag)->delete();
}

Expand All @@ -261,7 +258,7 @@ public function tagNodeDeleteProviderPermissionException() {
[
// cannot delete invisible tag
new SystemTag(1, 'Original', false, true),
'Sabre\DAV\Exception\NotFound',
'Sabre\DAV\Exception\Forbidden',
],
[
// cannot delete non-assignable tag
Expand All @@ -279,20 +276,11 @@ public function testDeleteTagPermissionException(ISystemTag $tag, $expectedExcep
->method('canUserSeeTag')
->with($tag)
->will($this->returnValue($tag->isUserVisible()));
$this->tagManager->expects($this->any())
->method('canUserAssignTag')
->with($tag)
->will($this->returnValue($tag->isUserAssignable()));
$this->tagManager->expects($this->never())
->method('deleteTags');

try {
$this->getTagNode(false, $tag)->delete();
} catch (\Exception $e) {
$thrown = $e;
}

$this->assertInstanceOf($expectedException, $thrown);
$this->setExpectedException($expectedException);
$this->getTagNode(false, $tag)->delete();
}

/**
Expand All @@ -304,14 +292,10 @@ public function testDeleteTagNotFound() {
->method('canUserSeeTag')
->with($tag)
->will($this->returnValue($tag->isUserVisible()));
$this->tagManager->expects($this->any())
->method('canUserAssignTag')
->with($tag)
->will($this->returnValue($tag->isUserAssignable()));
$this->tagManager->expects($this->once())
->method('deleteTags')
->with('1')
->will($this->throwException(new TagNotFoundException()));
$this->getTagNode(false, $tag)->delete();
$this->getTagNode(true, $tag)->delete();
}
}
13 changes: 10 additions & 3 deletions build/integration/features/tags.feature
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ Feature: tags
When "user0" edits the tag with name "TagWithGroups" and sets its groups to "group1|group3"
Then The response should have a status code "403"

Scenario: Deleting a normal tag as regular user should work
Scenario: Deleting a normal tag as regular user should fail
Given user "user0" exists
Given "admin" creates a "normal" tag with name "MySuperAwesomeTagName"
When "user0" deletes the tag with name "MySuperAwesomeTagName"
Then The response should have a status code "204"
And "0" tags should exist for "admin"
Then The response should have a status code "403"
And The following tags should exist for "admin"
|MySuperAwesomeTagName|true|true|

Scenario: Deleting a not user-assignable tag as regular user should fail
Given user "user0" exists
Expand All @@ -93,6 +94,12 @@ Feature: tags
And The following tags should exist for "admin"
|MySuperAwesomeTagName|false|true|

Scenario: Deleting a normal tag as admin should work
Given "admin" creates a "normal" tag with name "MySuperAwesomeTagName"
When "admin" deletes the tag with name "MySuperAwesomeTagName"
Then The response should have a status code "204"
And "0" tags should exist for "admin"

Scenario: Deleting a not user-assignable tag as admin should work
Given "admin" creates a "not user-assignable" tag with name "MySuperAwesomeTagName"
When "admin" deletes the tag with name "MySuperAwesomeTagName"
Expand Down
7 changes: 5 additions & 2 deletions core/js/systemtags/systemtagsinputfield.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
'<form class="systemtags-rename-form">' +
' <label class="hidden-visually" for="{{cid}}-rename-input">{{renameLabel}}</label>' +
' <input id="{{cid}}-rename-input" type="text" value="{{name}}">' +
' <a href="#" class="delete icon icon-delete" title="{{deleteTooltip}}"></a>' +
' {{#if isAdmin}}' +
' <a href="#" class="delete icon icon-delete" title="{{deleteTooltip}}"></a>' +
' {{/if}}' +
'</form>';

/**
Expand Down Expand Up @@ -148,7 +150,8 @@
cid: this.cid,
name: oldName,
deleteTooltip: t('core', 'Delete'),
renameLabel: t('core', 'Rename')
renameLabel: t('core', 'Rename'),
isAdmin: this._isAdmin
}));
$item.find('.label').after($renameForm);
$item.find('.label, .systemtags-actions').addClass('hidden');
Expand Down
87 changes: 71 additions & 16 deletions core/js/tests/specs/systemtags/systemtagsinputfieldSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,20 +267,6 @@ describe('OC.SystemTags.SystemTagsInputField tests', function() {

saveStub.restore();
});
it('deletes model and submits change when clicking delete', function() {
var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');

expect($dropdown.find('.delete').length).toEqual(0);
$dropdown.find('.rename').mouseup();
// delete button appears
expect($dropdown.find('.delete').length).toEqual(1);
$dropdown.find('.delete').mouseup();

expect(destroyStub.calledOnce).toEqual(true);
expect(destroyStub.calledOn(view.collection.get('1')));

destroyStub.restore();
});
});
describe('setting data', function() {
it('sets value when calling setValues', function() {
Expand All @@ -299,12 +285,18 @@ describe('OC.SystemTags.SystemTagsInputField tests', function() {
});

describe('as admin', function() {
var $dropdown;

beforeEach(function() {
view = new OC.SystemTags.SystemTagsInputField({
isAdmin: true
});
view.render();
$('.testInputContainer').append(view.$el);
$dropdown = $('<div class="select2-dropdown"></div>');
select2Stub.withArgs('dropdown').returns($dropdown);
$('#testArea').append($dropdown);

view.render();
});
it('formatResult renders tag name with visibility', function() {
var opts = select2Stub.getCall(0).args[0];
Expand Down Expand Up @@ -431,15 +423,50 @@ describe('OC.SystemTags.SystemTagsInputField tests', function() {
]);
});
});
describe('tag actions', function() {
var opts;

beforeEach(function() {

opts = select2Stub.getCall(0).args[0];

view.collection.add([
new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
]);

$dropdown.append(opts.formatResult(view.collection.get('1').toJSON()));

});
it('deletes model and submits change when clicking delete', function() {
var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');

expect($dropdown.find('.delete').length).toEqual(0);
$dropdown.find('.rename').mouseup();
// delete button appears
expect($dropdown.find('.delete').length).toEqual(1);
$dropdown.find('.delete').mouseup();

expect(destroyStub.calledOnce).toEqual(true);
expect(destroyStub.calledOn(view.collection.get('1')));

destroyStub.restore();
});
});
});

describe('as user', function() {
var $dropdown;

beforeEach(function() {
view = new OC.SystemTags.SystemTagsInputField({
isAdmin: false
});
view.render();
$('.testInputContainer').append(view.$el);
$dropdown = $('<div class="select2-dropdown"></div>');
select2Stub.withArgs('dropdown').returns($dropdown);
$('#testArea').append($dropdown);

view.render();
});
it('formatResult renders tag name only', function() {
var opts = select2Stub.getCall(0).args[0];
Expand Down Expand Up @@ -570,5 +597,33 @@ describe('OC.SystemTags.SystemTagsInputField tests', function() {
]);
});
});
describe('tag actions', function() {
var opts;

beforeEach(function() {

opts = select2Stub.getCall(0).args[0];

view.collection.add([
new OC.SystemTags.SystemTagModel({id: '1', name: 'abc'}),
]);

$dropdown.append(opts.formatResult(view.collection.get('1').toJSON()));

});
it('deletes model and submits change when clicking delete', function() {
var destroyStub = sinon.stub(OC.SystemTags.SystemTagModel.prototype, 'destroy');

expect($dropdown.find('.delete').length).toEqual(0);
$dropdown.find('.rename').mouseup();
// delete button appears only for admins
expect($dropdown.find('.delete').length).toEqual(0);
$dropdown.find('.delete').mouseup();

expect(destroyStub.notCalled).toEqual(true);

destroyStub.restore();
});
});
});
});