silverstripe-framework/admin/code/SecurityAdmin.php

365 lines
11 KiB
PHP
Raw Normal View History

<?php
2013-10-15 00:26:23 +02:00
2016-08-11 01:14:02 +02:00
namespace SilverStripe\Admin;
2016-09-09 08:43:05 +02:00
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Convert;
use SilverStripe\Forms\GridField\GridFieldDataColumns;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Forms\Tab;
use SilverStripe\Forms\TabSet;
use SilverStripe\Forms\HiddenField;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\Form;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldExportButton;
use SilverStripe\Forms\GridField\GridField;
2016-06-23 01:37:22 +02:00
use SilverStripe\Security\Security;
use SilverStripe\Security\Member;
use SilverStripe\Security\Group;
use SilverStripe\Security\Permission;
use SilverStripe\Security\PermissionRole;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\View\Requirements;
use SilverStripe\View\ArrayData;
2016-06-23 01:37:22 +02:00
/**
* Security section of the CMS
*/
class SecurityAdmin extends LeftAndMain implements PermissionProvider {
private static $url_segment = 'security';
private static $url_rule = '/$Action/$ID/$OtherID';
private static $menu_title = 'Security';
2016-06-23 01:37:22 +02:00
private static $tree_class = 'SilverStripe\\Security\\Group';
2016-06-23 01:37:22 +02:00
private static $subitem_class = 'SilverStripe\\Security\\Member';
private static $required_permission_codes = 'CMS_ACCESS_SecurityAdmin';
private static $allowed_actions = array(
'EditForm',
'MemberImportForm',
'memberimport',
'GroupImportForm',
'groupimport',
'groups',
'users',
'roles'
);
/**
* Shortcut action for setting the correct active tab.
2016-06-23 01:37:22 +02:00
*
2016-09-09 08:43:05 +02:00
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function users($request) {
return $this->index($request);
}
/**
* Shortcut action for setting the correct active tab.
2016-06-23 01:37:22 +02:00
*
2016-09-09 08:43:05 +02:00
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function groups($request) {
return $this->index($request);
}
/**
* Shortcut action for setting the correct active tab.
2016-06-23 01:37:22 +02:00
*
2016-09-09 08:43:05 +02:00
* @param HTTPRequest $request
* @return HTTPResponse
*/
public function roles($request) {
return $this->index($request);
}
public function getEditForm($id = null, $fields = null) {
// TODO Duplicate record fetching (see parent implementation)
if(!$id) $id = $this->currentPageID();
$form = parent::getEditForm($id);
// TODO Duplicate record fetching (see parent implementation)
$record = $this->getRecord($id);
2013-10-15 00:26:23 +02:00
if($record && !$record->canView()) {
return Security::permissionFailure($this);
}
$memberList = GridField::create(
'Members',
false,
Member::get(),
$memberListConfig = GridFieldConfig_RecordEditor::create()
->addComponent(new GridFieldButtonRow('after'))
->addComponent(new GridFieldExportButton('buttons-after-left'))
)->addExtraClass("members_grid");
if($record && method_exists($record, 'getValidator')) {
$validator = $record->getValidator();
} else {
2016-06-23 01:37:22 +02:00
$validator = Member::singleton()->getValidator();
}
/** @var GridFieldDetailForm $detailForm */
$detailForm = $memberListConfig->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDetailForm');
$detailForm
->setValidator($validator);
$groupList = GridField::create(
'Groups',
false,
Group::get(),
GridFieldConfig_RecordEditor::create()
);
/** @var GridFieldDataColumns $columns */
$columns = $groupList->getConfig()->getComponentByType('SilverStripe\\Forms\\GridField\\GridFieldDataColumns');
$columns->setDisplayFields(array(
'Breadcrumbs' => Group::singleton()->fieldLabel('Title')
));
$columns->setFieldFormatting(array(
'Breadcrumbs' => function($val, $item) {
/** @var Group $item */
return Convert::raw2xml($item->getBreadcrumbs(' > '));
}
));
$fields = new FieldList(
2011-10-29 04:41:40 +02:00
$root = new TabSet(
'Root',
$usersTab = new Tab('Users', _t('SecurityAdmin.Users', 'Users'),
2016-07-01 03:37:29 +02:00
new LiteralField('MembersCautionText',
2016-07-01 03:37:29 +02:00
sprintf('<div class="alert alert-warning" role="alert">%s</div>',
_t(
'SecurityAdmin.MemberListCaution',
2016-07-01 03:37:29 +02:00
'Caution: Removing members from this list will remove them from all groups and the database'
)
)
2016-07-01 03:37:29 +02:00
),
$memberList
),
2016-06-23 01:37:22 +02:00
$groupsTab = new Tab('Groups', singleton('SilverStripe\\Security\\Group')->i18n_plural_name(),
$groupList
)
),
// necessary for tree node selection in LeftAndMain.EditForm.js
new HiddenField('ID', false, 0)
);
// Add import capabilities. Limit to admin since the import logic can affect assigned permissions
if(Permission::check('ADMIN')) {
$fields->addFieldsToTab('Root.Users', array(
new HeaderField('ImportUsersHeader', _t('SecurityAdmin.IMPORTUSERS', 'Import users'), 3),
new LiteralField(
'MemberImportFormIframe',
sprintf(
'<iframe src="%s" id="MemberImportFormIframe" width="100%%" height="330px" frameBorder="0">'
. '</iframe>',
$this->Link('memberimport')
)
)
));
$fields->addFieldsToTab('Root.Groups', array(
new HeaderField('ImportGroupsHeader', _t('SecurityAdmin.IMPORTGROUPS', 'Import groups'), 3),
new LiteralField(
'GroupImportFormIframe',
sprintf(
'<iframe src="%s" id="GroupImportFormIframe" width="100%%" height="330px" frameBorder="0">'
. '</iframe>',
$this->Link('groupimport')
)
)
));
}
// Tab nav in CMS is rendered through separate template
$root->setTemplate('SilverStripe\\Forms\\CMSTabSet');
// Add roles editing interface
$rolesTab = null;
if(Permission::check('APPLY_ROLES')) {
$rolesField = GridField::create('Roles',
false,
PermissionRole::get(),
GridFieldConfig_RecordEditor::create()
);
$rolesTab = $fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.TABROLES', 'Roles'));
$rolesTab->push($rolesField);
}
$actionParam = $this->getRequest()->param('Action');
if($actionParam == 'groups') {
$groupsTab->addExtraClass('ui-state-active');
} elseif($actionParam == 'users') {
$usersTab->addExtraClass('ui-state-active');
} elseif($actionParam == 'roles' && $rolesTab) {
$rolesTab->addExtraClass('ui-state-active');
}
$actions = new FieldList();
$form = Form::create(
$this,
'EditForm',
$fields,
$actions
)->setHTMLID('Form_EditForm');
$form->addExtraClass('cms-edit-form fill-height');
$form->setTemplate($this->getTemplatesWithSuffix('_EditForm'));
// Tab nav in CMS is rendered through separate template
if($form->Fields()->hasTabSet()) {
$form->Fields()->findOrMakeTab('Root')->setTemplate('SilverStripe\\Forms\\CMSTabSet');
}
$form->addExtraClass('ss-tabset cms-tabset ' . $this->BaseCSSClasses());
$form->setAttribute('data-pjax-fragment', 'CurrentForm');
$this->extend('updateEditForm', $form);
return $form;
}
public function memberimport() {
Requirements::clear();
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js');
Requirements::javascript(ltrim(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js', '/'));
Requirements::css(ltrim(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css', '/'));
API Use Webpack The bundle is generated by running “webpack” directly - gulp is no longer needed as an intermediary. The resulting config is a lot shorter, although more configuration is pushed into lib.js. Modules are shared between javascript files as global variables. Although this global state pollution is a bit messy, I don’t think it’s practically any worse than the previous state, and it highlights the heavy coupling between the different packages we have in place. Reducing the width of the coupling between the core javascript and add-on modules would probably be a better way of dealing with this than replacing global variables with some other kind of global state. The web pack execution seems roughly twice as fast - if I clear out my framework/client/dist/js folder, it takes 13.3s to rebuild. However, it’s not rebuilding other files inside dist, only the bundle files. CSS files are now included from javascript and incorporated into bundle.css by the webpack. Although the style-loader is helpful in some dev workflows (it allows live reload), it introduces a flash of unstyled content which makes it inappropriate for production. Instead ExtractTextPlugin is used to write all the aggregated CSS into a single bundle.css file. A style-loader-based configuration could be introduced for dev environments, if we make use of the webpack live reloader in the future. Note that the following features have been removed as they don't appear to be necessary when using Webpack: - UMD module generation - thirdparty dist file copying LeftAndMain.js deps: Without it, ssui.core.js gets loaded too late, which leads e.g. to buttons being initialised without this added behaviour.
2016-08-21 03:17:50 +02:00
return $this->renderWith('BlankPage', array(
'Form' => $this->MemberImportForm()->forTemplate(),
2012-01-07 18:57:14 +01:00
'Content' => ' '
));
}
/**
* @see SecurityAdmin_MemberImportForm
*
* @return Form
*/
public function MemberImportForm() {
if(!Permission::check('ADMIN')) {
return null;
}
/** @var Group $group */
$group = $this->currentPage();
/** @skipUpgrade */
$form = new MemberImportForm(
$this,
'MemberImportForm'
);
$form->setGroup($group);
return $form;
}
public function groupimport() {
Requirements::clear();
Requirements::javascript(FRAMEWORK_ADMIN_DIR . '/client/dist/js/vendor.js');
Requirements::javascript(ltrim(FRAMEWORK_ADMIN_DIR . '/client/dist/js/MemberImportForm.js', '/'));
Requirements::css(ltrim(FRAMEWORK_ADMIN_DIR . '/client/dist/styles/bundle.css', '/'));
API Use Webpack The bundle is generated by running “webpack” directly - gulp is no longer needed as an intermediary. The resulting config is a lot shorter, although more configuration is pushed into lib.js. Modules are shared between javascript files as global variables. Although this global state pollution is a bit messy, I don’t think it’s practically any worse than the previous state, and it highlights the heavy coupling between the different packages we have in place. Reducing the width of the coupling between the core javascript and add-on modules would probably be a better way of dealing with this than replacing global variables with some other kind of global state. The web pack execution seems roughly twice as fast - if I clear out my framework/client/dist/js folder, it takes 13.3s to rebuild. However, it’s not rebuilding other files inside dist, only the bundle files. CSS files are now included from javascript and incorporated into bundle.css by the webpack. Although the style-loader is helpful in some dev workflows (it allows live reload), it introduces a flash of unstyled content which makes it inappropriate for production. Instead ExtractTextPlugin is used to write all the aggregated CSS into a single bundle.css file. A style-loader-based configuration could be introduced for dev environments, if we make use of the webpack live reloader in the future. Note that the following features have been removed as they don't appear to be necessary when using Webpack: - UMD module generation - thirdparty dist file copying LeftAndMain.js deps: Without it, ssui.core.js gets loaded too late, which leads e.g. to buttons being initialised without this added behaviour.
2016-08-21 03:17:50 +02:00
return $this->renderWith('BlankPage', array(
'Content' => ' ',
'Form' => $this->GroupImportForm()->forTemplate()
));
}
/**
* @see SecurityAdmin_MemberImportForm
*
* @skipUpgrade
* @return Form
*/
public function GroupImportForm() {
if(!Permission::check('ADMIN')) {
return null;
}
$form = new GroupImportForm($this, 'GroupImportForm');
return $form;
}
/**
* Disable GridFieldDetailForm backlinks for this view, as its
*/
public function Backlink() {
return false;
}
public function Breadcrumbs($unlinked = false) {
$crumbs = parent::Breadcrumbs($unlinked);
// 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->getRequest()->allParams();
if(isset($params['FieldName'])) {
// TODO FieldName param gets overwritten by nested GridFields,
// so shows "Members" rather than "Groups" for the following URL:
// admin/security/EditForm/field/Groups/item/2/ItemEditForm/field/Members/item/1/edit
$firstCrumb = $crumbs->shift();
if($params['FieldName'] == 'Groups') {
$crumbs->unshift(new ArrayData(array(
'Title' => Group::singleton()->i18n_plural_name(),
'Link' => $this->Link('groups')
)));
} elseif($params['FieldName'] == 'Users') {
$crumbs->unshift(new ArrayData(array(
'Title' => _t('SecurityAdmin.Users', 'Users'),
'Link' => $this->Link('users')
)));
} elseif($params['FieldName'] == 'Roles') {
$crumbs->unshift(new ArrayData(array(
'Title' => _t('SecurityAdmin.TABROLES', 'Roles'),
'Link' => $this->Link('roles')
)));
}
$crumbs->unshift($firstCrumb);
}
return $crumbs;
}
public function providePermissions() {
$title = $this->menu_title();
return array(
"CMS_ACCESS_SecurityAdmin" => array(
'name' => _t('CMSMain.ACCESS', "Access to '{title}' section", array('title' => $title)),
'category' => _t('Permission.CMS_ACCESS_CATEGORY', 'CMS Access'),
'help' => _t(
'SecurityAdmin.ACCESS_HELP',
'Allow viewing, adding and editing users, as well as assigning permissions and roles to them.'
)
),
'EDIT_PERMISSIONS' => array(
'name' => _t('SecurityAdmin.EDITPERMISSIONS', 'Manage permissions for groups'),
'category' => _t('Permissions.PERMISSIONS_CATEGORY', 'Roles and access permissions'),
'help' => _t('SecurityAdmin.EDITPERMISSIONS_HELP',
'Ability to edit Permissions and IP Addresses for a group.'
. ' Requires the "Access to \'Security\' section" permission.'),
'sort' => 0
),
'APPLY_ROLES' => array(
'name' => _t('SecurityAdmin.APPLY_ROLES', 'Apply roles to groups'),
'category' => _t('Permissions.PERMISSIONS_CATEGORY', 'Roles and access permissions'),
'help' => _t('SecurityAdmin.APPLY_ROLES_HELP', 'Ability to edit the roles assigned to a group.'
. ' Requires the "Access to \'Users\' section" permission.'),
'sort' => 0
)
);
}
}