API CHANGE Removed $blankItemText parameter from Permission::get_codes()

ENHANCEMENT Allow ungrouped retrieval of Permission::get_codes() through new $grouped switch

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@97819 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-01-30 03:56:46 +00:00 committed by Sam Minnee
parent 4557ba87aa
commit db31ff5ad1
2 changed files with 30 additions and 22 deletions

View File

@ -461,28 +461,21 @@ class Permission extends DataObject {
/**
* Get a list of all available permission codes
* Get a list of all available permission codes, both defined through the
* {@link PermissionProvider} interface, and all not explicitly defined codes existing
* as a {@link Permission} database record. By default, the results are
* grouped as denoted by {@link Permission_Group}.
*
* @param bool|string $blankItemText Text for permission with the empty
* code (""). If set to TRUE it will be
* set to "(select)"; if set to NULL or
* FALSE the empty permission is not
* included in the list.
* @param bool $grouped Group results into an array of permission groups.
* @return array Returns an array of all available permission codes. The
* array indicies are the permission codes as used in
* {@link Permission::check()}. The value is a description
* suitable for using in an interface.
* array indicies are the permission codes as used in
* {@link Permission::check()}. The value is a description
* suitable for using in an interface.
*/
public static function get_codes($blankItemText = null) {
public static function get_codes($grouped = true) {
$classes = ClassInfo::implementorsOf('PermissionProvider');
$allCodes = array();
// if($blankItemText){
// $allCodes[''] = ($blankItemText === true)
// ? '(select)'
// : $blankItemText;
// }
$allCodes['Roles and access permissions']['ADMIN'] = array(
'name' => _t('Permission.FULLADMINRIGHTS', 'Full administrative rights'),
'help' => null,
@ -522,7 +515,7 @@ class Permission extends DataObject {
$flatCodeArray = array();
foreach($allCodes as $category) foreach($category as $code => $permission) $flatCodeArray[] = $code;
$otherPerms = DB::query("SELECT DISTINCT \"Code\" From \"Permission\"")->column();
$otherPerms = DB::query("SELECT DISTINCT \"Code\" From \"Permission\" WHERE \"Code\" != ''")->column();
if($otherPerms) foreach($otherPerms as $otherPerm) {
if(!in_array($otherPerm, $flatCodeArray))
@ -538,12 +531,17 @@ class Permission extends DataObject {
ksort($allCodes);
$returnCodes = array();
foreach($allCodes as $category => $permissions) {
uasort($permissions, array(__CLASS__, 'sort_permissions'));
$allCodes[$category] = $permissions;
if($grouped) {
uasort($permissions, array(__CLASS__, 'sort_permissions'));
$returnCodes[$category] = $permissions;
} else {
$returnCodes = array_merge($returnCodes, $permissions);
}
}
return $allCodes;
return $returnCodes;
}
/**

View File

@ -3,6 +3,16 @@
class PermissionTest extends SapphireTest {
static $fixture_file = 'sapphire/tests/security/PermissionTest.yml';
function testGetCodesGrouped() {
$codes = Permission::get_codes();
$this->assertArrayNotHasKey('SITETREE_VIEW_ALL', $codes);
}
function testGetCodesUngrouped() {
$codes = Permission::get_codes(null, false);
$this->assertArrayHasKey('SITETREE_VIEW_ALL', $codes);
}
function testDirectlyAppliedPermissions() {
$member = $this->objFromFixture('Member', 'author');
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
@ -14,7 +24,7 @@ class PermissionTest extends SapphireTest {
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
}
function testPermissionAreInheritedFromMultipleRoles() {
$member = $this->objFromFixture('Member', 'access');
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_CMSMain"));
@ -33,7 +43,7 @@ class PermissionTest extends SapphireTest {
// Check that roles from parent groups are there
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_CMSMain"));
$this->assertTrue(Permission::checkMember($member, "CMS_ACCESS_AssetAdmin"));
// Check that permissions from parent groups are there
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));