ENHANCEMENT Replaced SecurityAdmin tree view for groups with three top-level tabs: "Users", "Groups" and "Roles"

This commit is contained in:
Ingo Schommer 2012-03-05 18:31:52 +01:00
parent e2c21cce8e
commit 6f71186e2c
3 changed files with 90 additions and 96 deletions

View File

@ -84,16 +84,29 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
* @return FieldList
*/
function RootForm() {
$config = new GridFieldConfig_RecordEditor();
$config->addComponent(new GridFieldExporter());
$config->getComponentByType('GridFieldPopupForms')->setValidator(new Member_Validator());
$memberList = new GridField('Members', 'All members', DataList::create('Member'), $config);
$memberList->addExtraClass("members_grid");
$memberList = Object::create('GridField',
'Members',
false,
DataList::create('Member'),
$memberListConfig = GridFieldConfig_RecordEditor::create()
->addComponent(new GridFieldExporter())
)->addExtraClass("members_grid");
$memberListConfig->getComponentByType('GridFieldPopupForms')->setValidator(new Member_Validator());
$groupList = Object::create('GridField',
'Groups',
false,
DataList::create('Group'),
GridFieldConfig_RecordEditor::create()
)->setDisplayFields(array(
'Breadcrumbs' => singleton('Group')->fieldLabel('Title')
));
$fields = new FieldList(
$root = new TabSet(
'Root',
new Tab('Members', singleton('Member')->i18n_plural_name(),
new Tab('Users', _t('SecurityAdmin.Users', 'Users'),
$memberList,
new LiteralField('MembersCautionText',
sprintf('<p class="caution-remove"><strong>%s</strong></p>',
@ -104,14 +117,8 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
)
)
),
new Tab('Import', _t('SecurityAdmin.TABIMPORT', 'Import'),
new LiteralField(
'GroupImportFormIframe',
sprintf(
'<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="400px" border="0"></iframe>',
$this->Link('groupimport')
)
)
new Tab('Groups', singleton('Group')->plural_name(),
$groupList
)
),
// necessary for tree node selection in LeftAndMain.EditForm.js
@ -138,6 +145,17 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
$rolesTab->push($rolesField);
}
$fields->findOrMakeTab('Root.Import', _t('SecurityAdmin.TABIMPORT', 'Import'));
$fields->addFieldToTab('Root.Import',
new LiteralField(
'GroupImportFormIframe',
sprintf(
'<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="400px" border="0"></iframe>',
$this->Link('groupimport')
)
)
);
$actions = new FieldList();
$this->extend('updateRootFormFields', $fields, $actions);
@ -220,12 +238,28 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
return $form;
}
function getCMSTreeTitle() {
return _t('SecurityAdmin.SGROUPS', 'Security Groups');
}
public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
public function EditedMember() {
if(Session::get('currentMember')) return DataObject::get_by_id('Member', (int) Session::get('currentMember'));
// Name root breadcrumb based on which record is edited,
// which can only be determined by looking for the fieldname of the GridField.
// Note: Titles should be same titles as tabs in RootForm().
$params = $this->request->allParams();
if(isset($params['FieldName'])) {
if($params['FieldName'] == 'Groups') {
$crumbs->First()->Title = singleton('Group')->plural_name();
} elseif($params['FieldName'] == 'Users') {
$crumbs->First()->Title = _t('SecurityAdmin.Users', 'Users');
} elseif($params['FieldName'] == 'Roles') {
$crumbs->First()->Title = _t('SecurityAdmin.TABROLES', 'Roles');
}
} else {
// Avoid writing "Users" (the controller menu title) as a breadcrumb
// because its confusing and inaccurate.
$crumbs = new ArrayList();
}
return $crumbs;
}
function providePermissions() {
@ -286,37 +320,4 @@ class SecurityAdmin extends LeftAndMain implements PermissionProvider {
static function clear_hidden_permissions(){
self::$hidden_permissions = array();
}
}
/**
* Delete multiple {@link Group} records. Usually used through the {@link SecurityAdmin} interface.
*
* @package cms
* @subpackage batchactions
*/
class SecurityAdmin_DeleteBatchAction extends CMSBatchAction {
function getActionTitle() {
return _t('AssetAdmin_DeleteBatchAction.TITLE', 'Delete groups');
}
function run(SS_List $records) {
$status = array(
'modified'=>array(),
'deleted'=>array()
);
foreach($records as $record) {
// TODO Provide better feedback if permission was denied
if(!$record->canDelete()) continue;
$id = $record->ID;
$record->delete();
$status['deleted'][$id] = array();
$record->destroy();
unset($record);
}
return Convert::raw2json($status);
}
}
}

View File

@ -1,21 +0,0 @@
<div class="cms-content center $BaseCSSClasses" data-layout-type="border">
<div class="cms-content-tools west">
<div class="cms-content-header north">
<div>
<h2>
<% include CMSBreadcrumbs %>
</h2>
</div>
</div>
$AddForm
<div data-url-tree="$Link(getsubtree)" data-url-savetreenode="$Link(savetreenode)" class="cms-tree draggable jstree jstree-apple">
$SiteTreeAsUL
</div>
</div>
$EditForm
</div>

View File

@ -62,23 +62,19 @@ class Group extends DataObject {
public function getCMSFields() {
Requirements::javascript(SAPPHIRE_DIR . '/javascript/PermissionCheckboxSetField.js');
$config = new GridFieldConfig_RelationEditor();
$config->addComponents(new GridFieldExporter());
$config->getComponentByType('GridFieldRelationAdd')
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
$config->getComponentByType('GridFieldPopupForms')->setValidator(new Member_Validator());
$memberList = new GridField('Members','Members', $this->Members(), $config);
$memberList->addExtraClass('members_grid');
// @todo Implement permission checking on GridField
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));
//$memberList->setPopupCaption(_t('SecurityAdmin.VIEWUSER', 'View User'));
$fields = new FieldList(
new TabSet("Root",
new Tab('Members', _t('SecurityAdmin.MEMBERS', 'Members'),
new TextField("Title", $this->fieldLabel('Title')),
$memberList
Object::create('DropdownField',
'ParentID',
$this->fieldLabel('Parent'),
DataList::create('Group')->exclude('ID', $this->ID)->map('ID', 'Breadcrumbs')
)->setEmptyString(' '),
new LiteralField(
'ParentIDDescription',
'<p><em>' . _t('Group.GroupReminder', 'If you choose a parent group, this group will take all it\'s roles') . '</em></p>'
)
),
$permissionsTab = new Tab('Permissions', _t('SecurityAdmin.PERMISSIONS', 'Permissions'),
@ -92,6 +88,18 @@ class Group extends DataObject {
)
)
);
if($this->ID) {
$config = new GridFieldConfig_RelationEditor();
$config->addComponents(new GridFieldExporter());
$config->getComponentByType('GridFieldRelationAdd')
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
$config->getComponentByType('GridFieldPopupForms')->setValidator(new Member_Validator());
$memberList = Object::create('GridField', 'Members',false, $this->Members(), $config)->addExtraClass('members_grid');
// @todo Implement permission checking on GridField
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));
$fields->addFieldToTab('Root.Members', $memberList);
}
// Only add a dropdown for HTML editor configurations if more than one is available.
// Otherwise Member->getHtmlEditorConfigForCMS() will default to the 'cms' configuration.
@ -109,7 +117,6 @@ class Group extends DataObject {
if(!Permission::check('EDIT_PERMISSIONS')) {
$fields->removeFieldFromTab('Root', 'Permissions');
$fields->removeFieldFromTab('Root', 'IP Addresses');
}
// Only show the "Roles" tab if permissions are granted to edit them,
@ -137,19 +144,26 @@ class Group extends DataObject {
// Add roles (and disable all checkboxes for inherited roles)
$allRoles = Permission::check('ADMIN') ? DataObject::get('PermissionRole') : DataObject::get('PermissionRole', 'OnlyAdminCanApply = 0');
$groupRoles = $this->Roles();
$inheritedRoles = new ArrayList();
$ancestors = $this->getAncestors();
foreach($ancestors as $ancestor) {
$ancestorRoles = $ancestor->Roles();
if($ancestorRoles) $inheritedRoles->merge($ancestorRoles);
if($this->ID) {
$groupRoles = $this->Roles();
$inheritedRoles = new ArrayList();
$ancestors = $this->getAncestors();
foreach($ancestors as $ancestor) {
$ancestorRoles = $ancestor->Roles();
if($ancestorRoles) $inheritedRoles->merge($ancestorRoles);
}
$groupRoleIDs = $groupRoles->column('ID') + $inheritedRoles->column('ID');
$inheritedRoleIDs = $inheritedRoles->column('ID');
} else {
$groupRoleIDs = array();
$inheritedRoleIDs = array();
}
$groupRoleIDs = $groupRoles->column('ID') + $inheritedRoles->column('ID');
$rolesField = Object::create('ListboxField', 'Roles', false, $allRoles->map()->toArray())
->setMultiple(true)
->setDefaultItems($groupRoleIDs)
->setAttribute('data-placeholder', _t('Group.AddRole', 'Add a role for this group'))
->setDisabledItems($inheritedRoles->column('ID'));
->setDisabledItems($inheritedRoleIDs);
if(!$allRoles->Count()) $rolesField->setAttribute('data-placeholder', _t('Group.NoRoles', 'No roles found'));
$fields->addFieldToTab('Root.Roles', $rolesField);
}