mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
ENHANCEMENT Unit tests for Member->inGroup() and Member->inGroups()
ENHANCEMENT Added a "strict-mode" for Member->inGroup() and Member->inGroups() to determine true membership to a group (without inheritance) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@65125 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
5556e1f5ae
commit
e57b7651ef
@ -520,41 +520,43 @@ class Member extends DataObject {
|
||||
|
||||
|
||||
/**
|
||||
* Check if the member is in one of the given groups
|
||||
* Check if the member is in one of the given groups.
|
||||
*
|
||||
* @param array $groups Groups to check
|
||||
* @return bool Returns TRUE if the member is in one of the given groups,
|
||||
* otherwise FALSE.
|
||||
* @param array|DataObjectSet $groups Collection of {@link Group} DataObjects to check
|
||||
* @param boolean $strict Only determine direct group membership if set to true (Default: false)
|
||||
* @return bool Returns TRUE if the member is in one of the given groups, otherwise FALSE.
|
||||
*/
|
||||
public function inGroups(array $groups) {
|
||||
foreach($this->Groups() as $group)
|
||||
$memberGroups[] = $group->Title;
|
||||
public function inGroups($groups, $strict = false) {
|
||||
foreach($groups as $group) {
|
||||
if($this->inGroup($group, $strict)) return true;
|
||||
}
|
||||
|
||||
return count(array_intersect($memberGroups, $groups)) > 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the member is in the given group
|
||||
* Check if the member is in the given group or any parent groups.
|
||||
*
|
||||
* @param int|Group|string $group Group instance, Group Code or ID
|
||||
* @param boolean $strict Only determine direct group membership if set to TRUE (Default: FALSE)
|
||||
* @return bool Returns TRUE if the member is in the given group, otherwise FALSE.
|
||||
*/
|
||||
public function inGroup($group) {
|
||||
public function inGroup($group, $strict = false) {
|
||||
if(is_numeric($group)) {
|
||||
$groupCheckObj = DataObject::get_by_id('Group', $group);
|
||||
} elseif(is_string($group)) {
|
||||
$SQL_group = Convert::raw2sql($group);
|
||||
$groupCheckObj = DataObject::get_one('Group', "Code = '{$SQL_group}'");
|
||||
} elseif(is_a('Group', $group)) {
|
||||
} elseif($group instanceof Group) {
|
||||
$groupCheckObj = $group;
|
||||
} else {
|
||||
user_error('Member::inGroup(): Wrong format for $group parameter', E_USER_ERROR);
|
||||
}
|
||||
|
||||
foreach($this->Groups() as $groupCandidateObj) {
|
||||
if($groupCandidateObj->ID == $groupCheckObj->ID)
|
||||
return true;
|
||||
$groupCandidateObjs = ($strict) ? $this->getManyManyComponents("Groups") : $this->Groups();
|
||||
foreach($groupCandidateObjs as $groupCandidateObj) {
|
||||
if($groupCandidateObj->ID == $groupCheckObj->ID) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
@ -182,4 +182,78 @@ class MemberTest extends SapphireTest {
|
||||
$this->assertFalse($member->isPasswordExpired());
|
||||
|
||||
}
|
||||
|
||||
function testInGroups() {
|
||||
$staffmember = $this->objFromFixture('Member', 'staffmember');
|
||||
$managementmember = $this->objFromFixture('Member', 'managementmember');
|
||||
$accountingmember = $this->objFromFixture('Member', 'accountingmember');
|
||||
$ceomember = $this->objFromFixture('Member', 'ceomember');
|
||||
|
||||
$staffgroup = $this->objFromFixture('Group', 'staffgroup');
|
||||
$managementgroup = $this->objFromFixture('Group', 'managementgroup');
|
||||
$accountinggroup = $this->objFromFixture('Group', 'accountinggroup');
|
||||
$ceogroup = $this->objFromFixture('Group', 'ceogroup');
|
||||
|
||||
$this->assertTrue(
|
||||
$staffmember->inGroups(array($staffgroup, $managementgroup)),
|
||||
'inGroups() succeeds if a membership is detected on one of many passed groups'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$staffmember->inGroups(array($ceogroup, $managementgroup)),
|
||||
'inGroups() fails if a membership is detected on none of the passed groups'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$ceomember->inGroups(array($staffgroup, $managementgroup), true),
|
||||
'inGroups() fails if no direct membership is detected on any of the passed groups (in strict mode)'
|
||||
);
|
||||
}
|
||||
|
||||
function testInGroup() {
|
||||
$staffmember = $this->objFromFixture('Member', 'staffmember');
|
||||
$managementmember = $this->objFromFixture('Member', 'managementmember');
|
||||
$accountingmember = $this->objFromFixture('Member', 'accountingmember');
|
||||
$ceomember = $this->objFromFixture('Member', 'ceomember');
|
||||
|
||||
$staffgroup = $this->objFromFixture('Group', 'staffgroup');
|
||||
$managementgroup = $this->objFromFixture('Group', 'managementgroup');
|
||||
$accountinggroup = $this->objFromFixture('Group', 'accountinggroup');
|
||||
$ceogroup = $this->objFromFixture('Group', 'ceogroup');
|
||||
|
||||
$this->assertTrue(
|
||||
$staffmember->inGroup($staffgroup),
|
||||
'Direct group membership is detected'
|
||||
);
|
||||
$this->assertTrue(
|
||||
$managementmember->inGroup($staffgroup),
|
||||
'Users of child group are members of a direct parent group (if not in strict mode)'
|
||||
);
|
||||
$this->assertTrue(
|
||||
$accountingmember->inGroup($staffgroup),
|
||||
'Users of child group are members of a direct parent group (if not in strict mode)'
|
||||
);
|
||||
$this->assertTrue(
|
||||
$ceomember->inGroup($staffgroup),
|
||||
'Users of indirect grandchild group are members of a parent group (if not in strict mode)'
|
||||
);
|
||||
$this->assertTrue(
|
||||
$ceomember->inGroup($ceogroup, true),
|
||||
'Direct group membership is dected (if in strict mode)'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$ceomember->inGroup($staffgroup, true),
|
||||
'Users of child group are not members of a direct parent group (if in strict mode)'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$staffmember->inGroup($managementgroup),
|
||||
'Users of parent group are not members of a direct child group'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$staffmember->inGroup($ceogroup),
|
||||
'Users of parent group are not members of an indirect grandchild group'
|
||||
);
|
||||
$this->assertFalse(
|
||||
$accountingmember->inGroup($managementgroup),
|
||||
'Users of group are not members of any siblings'
|
||||
);
|
||||
}
|
||||
}
|
@ -1,3 +1,19 @@
|
||||
Group:
|
||||
staffgroup:
|
||||
Title: staffgroup
|
||||
Code: staffgroup
|
||||
managementgroup:
|
||||
Title: managementgroup
|
||||
Code: managementgroup
|
||||
Parent: =>Group.staffgroup
|
||||
accountinggroup:
|
||||
Title: accountinggroup
|
||||
Code: accountinggroup
|
||||
Parent: =>Group.staffgroup
|
||||
ceogroup:
|
||||
Title: ceogroup
|
||||
Code: ceogroup
|
||||
Parent: =>Group.managementgroup
|
||||
Member:
|
||||
test:
|
||||
FirstName: Test
|
||||
@ -16,3 +32,15 @@ Member:
|
||||
Surname: User
|
||||
Email: noexpiry@silverstripe.com
|
||||
Password: 1nitialPassword
|
||||
staffmember:
|
||||
Email: staffmember@test.com
|
||||
Groups: =>Group.staffgroup
|
||||
managementmember:
|
||||
Email: managementmember@test.com
|
||||
Groups: =>Group.managementgroup
|
||||
accountingmember:
|
||||
Email: accountingmember@test.com
|
||||
Groups: =>Group.accountinggroup
|
||||
ceomember:
|
||||
Email: ceomember@test.com
|
||||
Groups: =>Group.ceogroup
|
Loading…
x
Reference in New Issue
Block a user