Skip to content

Commit

Permalink
Merge pull request #92 from owncloud/delete-group-through-manager
Browse files Browse the repository at this point in the history
Go through GroupManager for group deletion
  • Loading branch information
Vincent Petry authored Sep 15, 2017
2 parents ee8b04a + 368be2d commit 02e493b
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 11 deletions.
17 changes: 16 additions & 1 deletion lib/CustomGroupsBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
* @return boolean
*/
public function implementsActions($actions) {
return ($actions & self::GROUP_DETAILS) !== 0;
return ($actions & (self::GROUP_DETAILS | self::DELETE_GROUP)) !== 0;
}

/**
Expand Down Expand Up @@ -204,4 +204,19 @@ private function formatGroupId($uri) {
public function isVisibleForScope($scope) {
return ($scope === 'sharing');
}

/**
* Delete group
*
* @param string $gid group id
*/
public function deleteGroup($gid) {
$uri = $this->extractUri($gid);
if ($uri !== null) {
$groupInfo = $this->handler->getGroupByUri($uri);
if ($groupInfo !== null) {
$this->handler->deleteGroup($groupInfo['group_id']);
}
}
}
}
17 changes: 16 additions & 1 deletion lib/Dav/GroupMembershipCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use OCA\CustomGroups\Search;
use OCA\CustomGroups\Service\MembershipHelper;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCP\IGroupManager;

/**
* Group memberships collection for a given group
Expand All @@ -56,6 +57,13 @@ class GroupMembershipCollection implements \Sabre\DAV\ICollection, \Sabre\DAV\IP
*/
private $helper;

/**
* Group manager from core
*
* @var IGroupManager
*/
private $groupManager;

/**
* Group information
*
Expand All @@ -77,10 +85,12 @@ class GroupMembershipCollection implements \Sabre\DAV\ICollection, \Sabre\DAV\IP
*/
public function __construct(
array $groupInfo,
IGroupManager $groupManager,
CustomGroupsDatabaseHandler $groupsHandler,
MembershipHelper $helper
) {
$this->groupsHandler = $groupsHandler;
$this->groupManager = $groupManager;
$this->groupInfo = $groupInfo;
$this->helper = $helper;

Expand All @@ -97,7 +107,12 @@ public function delete() {
if (!$this->helper->isUserAdmin($groupId)) {
throw new Forbidden("No permission to delete group \"$groupId\"");
}
$this->groupsHandler->deleteGroup($groupId);

$group = $this->groupManager->get('customgroup_' . $this->groupInfo['uri']);
if ($group === null) {
throw new NotFound("Group not found \"$groupId\"");
}
$group->delete();

$event = new GenericEvent(null, ['groupName' => $this->groupInfo['display_name']]);
$this->dispatcher->dispatch('\OCA\CustomGroups::deleteGroup', $event);
Expand Down
10 changes: 10 additions & 0 deletions lib/Dav/GroupsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use OCA\CustomGroups\Service\MembershipHelper;
use Symfony\Component\EventDispatcher\GenericEvent;
use Sabre\DAV\Exception\Conflict;
use OCP\IGroupManager;

/**
* Collection of custom groups
Expand All @@ -45,6 +46,12 @@ class GroupsCollection implements IExtendedCollection {
*/
private $groupsHandler;

/**
* Group manager from core
* @var IGroupManager
*/
private $groupManager;

/**
* Membership helper
*
Expand All @@ -71,10 +78,12 @@ class GroupsCollection implements IExtendedCollection {
* @param MembershipHelper $helper helper
*/
public function __construct(
IGroupManager $groupManager,
CustomGroupsDatabaseHandler $groupsHandler,
MembershipHelper $helper,
$userId = null
) {
$this->groupManager = $groupManager;
$this->groupsHandler = $groupsHandler;
$this->helper = $helper;
$this->userId = $userId;
Expand Down Expand Up @@ -248,6 +257,7 @@ public function getLastModified() {
private function createMembershipsCollection(array $groupInfo) {
return new GroupMembershipCollection(
$groupInfo,
$this->groupManager,
$this->groupsHandler,
$this->helper
);
Expand Down
6 changes: 6 additions & 0 deletions lib/Dav/RootCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\SimpleCollection;
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;

/**
* Root collection for the custom groups and members
Expand All @@ -34,18 +35,23 @@ class RootCollection extends SimpleCollection {
/**
* Constructor
*
* @param IGroupManager $groupManager group manager
* @param CustomGroupsDatabaseHandler $groupsHandler groups database handler
* @param MembershipHelper $helper membership helper
*/
public function __construct(
IGroupManager $groupManager,
CustomGroupsDatabaseHandler $groupsHandler,
MembershipHelper $helper
) {
$children = [
new GroupsCollection(
$groupManager,
$groupsHandler,
$helper
),
new UsersCollection(
$groupManager,
$groupsHandler,
$helper
),
Expand Down
9 changes: 9 additions & 0 deletions lib/Dav/UsersCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Sabre\DAV\Exception\MethodNotAllowed;
use Sabre\DAV\Exception\Forbidden;
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;

/**
* Collection of users
Expand All @@ -47,16 +48,23 @@ class UsersCollection implements ICollection {
*/
private $helper;

/**
* @var IGroupManager
*/
private $groupManager;

/**
* Constructor
*
* @param CustomGroupsDatabaseHandler $groupsHandler custom groups handler
* @param MembershipHelper $helper
*/
public function __construct(
IGroupManager $groupManager,
CustomGroupsDatabaseHandler $groupsHandler,
MembershipHelper $helper
) {
$this->groupManager = $groupManager;
$this->groupsHandler = $groupsHandler;
$this->helper = $helper;
}
Expand Down Expand Up @@ -94,6 +102,7 @@ public function getChild($name) {
// but ownCloud admin can query membership of any user
if ($name === $this->helper->getUserId() || $this->helper->isUserSuperAdmin()) {
return new GroupsCollection(
$this->groupManager,
$this->groupsHandler,
$this->helper,
$name
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/CustomGroupsBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setUp() {
public function testImplementsAction() {
$this->assertTrue($this->backend->implementsActions(GroupInterface::GROUP_DETAILS));
$this->assertFalse($this->backend->implementsActions(GroupInterface::CREATE_GROUP));
$this->assertFalse($this->backend->implementsActions(GroupInterface::DELETE_GROUP));
$this->assertTrue($this->backend->implementsActions(GroupInterface::DELETE_GROUP));
$this->assertFalse($this->backend->implementsActions(GroupInterface::ADD_TO_GROUP));
$this->assertFalse($this->backend->implementsActions(GroupInterface::REMOVE_FROM_GROUP));
$this->assertFalse($this->backend->implementsActions(GroupInterface::COUNT_USERS));
Expand Down
13 changes: 10 additions & 3 deletions tests/unit/Dav/GroupMembershipCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use OCP\IConfig;
use Symfony\Component\EventDispatcher\GenericEvent;
use OCA\CustomGroups\Dav\Roles;
use OCP\IGroup;

/**
* Class GroupMembershipCollectionTest
Expand Down Expand Up @@ -111,6 +112,7 @@ public function setUp() {

$this->node = new GroupMembershipCollection(
['group_id' => 1, 'uri' => 'group1', 'display_name' => 'Group One', 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN],
$this->groupManager,
$this->handler,
$this->helper
);
Expand Down Expand Up @@ -143,9 +145,14 @@ public function testBase() {
public function testDeleteAsAdmin() {
$this->setCurrentUserMemberInfo(['group_id' => 1, 'user_id' => self::CURRENT_USER, 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN]);

$this->handler->expects($this->at(1))
->method('deleteGroup')
->with(1);
$group = $this->createMock(IGroup::class);
$group->expects($this->once())
->method('delete');

$this->groupManager->expects($this->once())
->method('get')
->with('customgroup_group1')
->willReturn($group);

$called = array();
\OC::$server->getEventDispatcher()->addListener('\OCA\CustomGroups::deleteGroup', function ($event) use (&$called) {
Expand Down
26 changes: 22 additions & 4 deletions tests/unit/Dav/GroupsCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ public function setUp() {
$this->createMock(IURLGenerator::class),
$this->config
);
$this->collection = new GroupsCollection($this->handler, $this->helper);
$this->collection = new GroupsCollection(
$this->createMock(IGroupManager::class),
$this->handler,
$this->helper
);
}

public function testBase() {
Expand Down Expand Up @@ -148,7 +152,12 @@ public function testListGroupsSearchPattern() {
}

public function testListGroupsForUser() {
$collection = new GroupsCollection($this->handler, $this->helper, 'user1');
$collection = new GroupsCollection(
$this->createMock(IGroupManager::class),
$this->handler,
$this->helper,
'user1'
);
$this->handler->expects($this->never())->method('getGroups');
$this->handler->expects($this->at(0))
->method('getUserMemberships')
Expand All @@ -170,7 +179,12 @@ public function testListGroupsForUser() {
public function testListGroupsForUserSearchPattern() {
$search = new Search('gr', 16, 256);

$collection = new GroupsCollection($this->handler, $this->helper, 'user1');
$collection = new GroupsCollection(
$this->createMock(IGroupManager::class),
$this->handler,
$this->helper,
'user1'
);
$this->handler->expects($this->never())->method('getGroups');
$this->handler->expects($this->at(0))
->method('getUserMemberships')
Expand Down Expand Up @@ -259,7 +273,11 @@ public function testCreateGroupNoPermission() {
->method('canCreateGroups')
->willReturn(false);

$this->collection = new GroupsCollection($this->handler, $helper);
$this->collection = new GroupsCollection(
$this->createMock(IGroupManager::class),
$this->handler,
$helper
);

$this->handler->expects($this->never())
->method('createGroup');
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/Dav/RootCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use OCA\CustomGroups\Dav\GroupsCollection;
use OCA\CustomGroups\CustomGroupsDatabaseHandler;
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;

/**
* Class RootCollectionTest
Expand All @@ -39,6 +40,7 @@ public function setUp() {
$helper = $this->createMock(MembershipHelper::class);

$this->collection = new RootCollection(
$this->createMock(IGroupManager::class),
$handler,
$helper
);
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/Dav/UsersCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public function setUp() {
$this->createMock(IConfig::class)
);

$this->collection = new UsersCollection($this->handler, $this->helper);
$this->collection = new UsersCollection(
$this->createMock(IGroupManager::class),
$this->handler,
$this->helper
);
}

public function testBase() {
Expand Down

0 comments on commit 02e493b

Please sign in to comment.