mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
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
This commit is contained in:
parent
feb30e194f
commit
74a04df3c8
@ -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
|
* @param bool $grouped Group results into an array of permission groups.
|
||||||
* 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.
|
|
||||||
* @return array Returns an array of all available permission codes. The
|
* @return array Returns an array of all available permission codes. The
|
||||||
* array indicies are the permission codes as used in
|
* array indicies are the permission codes as used in
|
||||||
* {@link Permission::check()}. The value is a description
|
* {@link Permission::check()}. The value is a description
|
||||||
* suitable for using in an interface.
|
* suitable for using in an interface.
|
||||||
*/
|
*/
|
||||||
public static function get_codes($blankItemText = null) {
|
public static function get_codes($grouped = true) {
|
||||||
$classes = ClassInfo::implementorsOf('PermissionProvider');
|
$classes = ClassInfo::implementorsOf('PermissionProvider');
|
||||||
|
|
||||||
$allCodes = array();
|
$allCodes = array();
|
||||||
// if($blankItemText){
|
|
||||||
// $allCodes[''] = ($blankItemText === true)
|
|
||||||
// ? '(select)'
|
|
||||||
// : $blankItemText;
|
|
||||||
// }
|
|
||||||
|
|
||||||
$adminCategory = _t('Permission.AdminGroup', 'Administrator');
|
$adminCategory = _t('Permission.AdminGroup', 'Administrator');
|
||||||
$allCodes[$adminCategory]['ADMIN'] = array(
|
$allCodes[$adminCategory]['ADMIN'] = array(
|
||||||
'name' => _t('Permission.FULLADMINRIGHTS', 'Full administrative rights'),
|
'name' => _t('Permission.FULLADMINRIGHTS', 'Full administrative rights'),
|
||||||
@ -523,7 +516,7 @@ class Permission extends DataObject {
|
|||||||
|
|
||||||
$flatCodeArray = array();
|
$flatCodeArray = array();
|
||||||
foreach($allCodes as $category) foreach($category as $code => $permission) $flatCodeArray[] = $code;
|
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($otherPerms) foreach($otherPerms as $otherPerm) {
|
||||||
if(!in_array($otherPerm, $flatCodeArray))
|
if(!in_array($otherPerm, $flatCodeArray))
|
||||||
@ -539,12 +532,17 @@ class Permission extends DataObject {
|
|||||||
|
|
||||||
ksort($allCodes);
|
ksort($allCodes);
|
||||||
|
|
||||||
|
$returnCodes = array();
|
||||||
foreach($allCodes as $category => $permissions) {
|
foreach($allCodes as $category => $permissions) {
|
||||||
|
if($grouped) {
|
||||||
uasort($permissions, array(__CLASS__, 'sort_permissions'));
|
uasort($permissions, array(__CLASS__, 'sort_permissions'));
|
||||||
$allCodes[$category] = $permissions;
|
$returnCodes[$category] = $permissions;
|
||||||
|
} else {
|
||||||
|
$returnCodes = array_merge($returnCodes, $permissions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $allCodes;
|
return $returnCodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -3,6 +3,16 @@
|
|||||||
class PermissionTest extends SapphireTest {
|
class PermissionTest extends SapphireTest {
|
||||||
static $fixture_file = 'sapphire/tests/security/PermissionTest.yml';
|
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() {
|
function testDirectlyAppliedPermissions() {
|
||||||
$member = $this->objFromFixture('Member', 'author');
|
$member = $this->objFromFixture('Member', 'author');
|
||||||
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
|
$this->assertTrue(Permission::checkMember($member, "SITETREE_VIEW_ALL"));
|
||||||
|
Loading…
Reference in New Issue
Block a user