From 74a04df3c86f48882541ca77cef1d5f132d6d12c Mon Sep 17 00:00:00 2001 From: Ingo Schommer Date: Mon, 12 Apr 2010 23:41:33 +0000 Subject: [PATCH] API CHANGE Removed $blankItemText parameter from Permission::get_codes() ENHANCEMENT Allow ungrouped retrieval of Permission::get_codes() through new $grouped switch (from r97819) git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@102536 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- security/Permission.php | 38 +++++++++++++++---------------- tests/security/PermissionTest.php | 14 ++++++++++-- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/security/Permission.php b/security/Permission.php index 140926e58..deba3e524 100755 --- a/security/Permission.php +++ b/security/Permission.php @@ -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; - // } - $adminCategory = _t('Permission.AdminGroup', 'Administrator'); $allCodes[$adminCategory]['ADMIN'] = array( 'name' => _t('Permission.FULLADMINRIGHTS', 'Full administrative rights'), @@ -523,7 +516,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)) @@ -539,12 +532,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; } /** diff --git a/tests/security/PermissionTest.php b/tests/security/PermissionTest.php index a28e61d4f..ea8720668 100644 --- a/tests/security/PermissionTest.php +++ b/tests/security/PermissionTest.php @@ -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"));