APICHANGE: add the ability to remove some permissions specified by their code in the rendered field html of PermissionChecksetBoxField and full-covered unit tests of this ability. (from r92428)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@92463 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2009-11-21 01:44:52 +00:00
parent 0f2c93d9f7
commit bf3b7e0e9e
3 changed files with 42 additions and 1 deletions

View File

@ -82,7 +82,13 @@ class Permission extends DataObject {
* @var bool
*/
static $admin_implies_all = true;
/**
* a list of permission codes which doesn't appear in the Permission list
* when make the {@link PermissionCheckboxSetField}
* @var array;
*/
static $hidden_permissions = array();
/**
* Check that the current member has the given permission.
@ -559,6 +565,26 @@ class Permission extends DataObject {
return $a['sort'] < $b['sort'] ? -1 : 1;
}
}
/**
* add a permission represented by the $code to the {@link slef::$hidden_permissions} list
*
* @param $code string - the permissions code
* @return void
*/
static function add_to_hidden_permissions($code){
self::$hidden_permissions[] = $code;
}
/**
* remove a permission represented by the $code from the {@link slef::$hidden_permissions} list
*
* @param $code string - the permissions code
* @return void
*/
static function remove_from_hidden_permissions($code){
self::$hidden_permissions = array_diff(self::$hidden_permissions, array($code));
}
/**
* Declare an array of permissions for the system.

View File

@ -69,7 +69,9 @@ class PermissionCheckboxSetField extends CheckboxSetField {
if($source) {
foreach($source as $categoryName => $permissions) {
$options .= "<li><h5>$categoryName</h5></li>";
$hiddens = Permission::$hidden_permissions;
foreach($permissions as $code => $permission) {
if(in_array($code, $hiddens)) continue;
$key = $code;
$value = $permission['name'];

View File

@ -41,4 +41,17 @@ class PermissionTest extends SapphireTest {
$this->assertFalse(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin"));
}
function testHiddenPermissions(){
$permissionCheckboxSet = new PermissionCheckboxSetField('Permissions','Permissions','Permission','GroupID');
$this->assertContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::add_to_hidden_permissions('CMS_ACCESS_CMSMain');
Permission::add_to_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertNotContains('CMS_ACCESS_CMSMain', $permissionCheckboxSet->Field());
$this->assertNotContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
Permission::remove_from_hidden_permissions('CMS_ACCESS_AssetAdmin');
$this->assertContains('CMS_ACCESS_AssetAdmin', $permissionCheckboxSet->Field());
}
}