Skip to content

Commit

Permalink
BUGFIX Nested Group records should be removed, along with the parent.
Browse files Browse the repository at this point in the history
  • Loading branch information
halkyon committed Mar 28, 2012
1 parent 86b805d commit bd95bca
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
18 changes: 11 additions & 7 deletions security/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,17 +337,21 @@ function onBeforeWrite() {
if(!$this->Code) $this->setCode($this->Title);
}
}

function onAfterDelete() {
parent::onAfterDelete();


function onBeforeDelete() {
parent::onBeforeDelete();

// if deleting this group, delete it's children as well
foreach($this->Groups() as $group) {
$group->delete();
}

// Delete associated permissions
$permissions = $this->Permissions();
foreach ( $permissions as $permission ) {
foreach($this->Permissions() as $permission) {
$permission->delete();
}
}

/**
* Checks for permission-code CMS_ACCESS_SecurityAdmin.
* If the group has ADMIN permissions, it requires the user to have ADMIN permissions as well.
Expand Down
22 changes: 13 additions & 9 deletions tests/security/GroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,6 @@ function testMemberGroupRelationForm() {

}

function testDelete() {
$adminGroup = $this->objFromFixture('Group', 'admingroup');

$adminGroup->delete();

$this->assertEquals(0, DataObject::get('Group', "\"ID\"={$adminGroup->ID}")->count(), 'Group is removed');
$this->assertEquals(0, DataObject::get('Permission',"\"GroupID\"={$adminGroup->ID}")->count(), 'Permissions removed along with the group');
}

function testCollateAncestorIDs() {
$parentGroup = $this->objFromFixture('Group', 'parentgroup');
$childGroup = $this->objFromFixture('Group', 'childgroup');
Expand All @@ -128,6 +119,19 @@ function testCollateAncestorIDs() {
'Orphaned nodes dont contain invalid parent IDs'
);
}

public function testDelete() {
$group = $this->objFromFixture('Group', 'parentgroup');
$groupID = $group->ID;
$childGroupID = $this->idFromFixture('Group', 'childgroup');
$group->delete();

$this->assertEquals(0, DataObject::get('Group', "\"ID\" = {$groupID}")->Count(), 'Group is removed');
$this->assertEquals(0, DataObject::get('Permission', "\"GroupID\" = {$groupID}")->Count(), 'Permissions removed along with the group');
$this->assertEquals(0, DataObject::get('Group', "\"ParentID\" = {$groupID}")->Count(), 'Child groups are removed');
$this->assertEquals(0, DataObject::get('Group', "\"ParentID\" = {$childGroupID}")->Count(), 'Grandchild groups are removed');
}

}

class GroupTest_Member extends Member implements TestOnly {
Expand Down
5 changes: 4 additions & 1 deletion tests/security/GroupTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Group:
childgroup:
Code: childgroup
Parent: =>Group.parentgroup
grandchildgroup:
Code: grandchildgroup
Parent: =>Group.childgroup
group1:
Title: Group 1
group2:
Expand All @@ -26,4 +29,4 @@ GroupTest_Member:
Permission:
admincode:
Code: ADMIN
Group: =>Group.admingroup
Group: =>Group.admingroup

0 comments on commit bd95bca

Please sign in to comment.