2007-07-19 12:40:28 +02:00
|
|
|
<?php
|
2008-02-25 03:10:37 +01:00
|
|
|
/**
|
|
|
|
* A security group.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-02-25 03:10:37 +01:00
|
|
|
* @subpackage security
|
2014-01-26 04:17:17 +01:00
|
|
|
*
|
|
|
|
* @property string Title Name of the group
|
|
|
|
* @property string Description Description of the group
|
|
|
|
* @property string Code Group code
|
|
|
|
* @property string Locked Boolean indicating whether group is locked in security panel
|
|
|
|
* @property int Sort
|
|
|
|
* @property string HtmlEditorConfig
|
|
|
|
*
|
|
|
|
* @property int ParentID ID of parent group
|
|
|
|
*
|
|
|
|
* @method Group Parent() Return parent group
|
|
|
|
* @method HasManyList Permissions() List of group permissions
|
|
|
|
* @method HasManyList Groups() List of child groups
|
|
|
|
* @method ManyManyList Roles() List of PermissionRoles
|
2008-02-25 03:10:37 +01:00
|
|
|
*/
|
2007-07-19 12:40:28 +02:00
|
|
|
class Group extends DataObject {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $db = array(
|
2013-03-26 10:05:37 +01:00
|
|
|
"Title" => "Varchar(255)",
|
2007-07-19 12:40:28 +02:00
|
|
|
"Description" => "Text",
|
2013-03-26 10:05:37 +01:00
|
|
|
"Code" => "Varchar(255)",
|
2007-07-19 12:40:28 +02:00
|
|
|
"Locked" => "Boolean",
|
|
|
|
"Sort" => "Int",
|
2013-03-26 10:05:37 +01:00
|
|
|
"HtmlEditorConfig" => "Text"
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_one = array(
|
2008-11-21 02:06:21 +01:00
|
|
|
"Parent" => "Group",
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $has_many = array(
|
2008-08-09 08:53:26 +02:00
|
|
|
"Permissions" => "Permission",
|
2008-11-21 02:06:21 +01:00
|
|
|
"Groups" => "Group"
|
2008-08-09 08:53:26 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $many_many = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"Members" => "Member",
|
2009-10-16 00:27:56 +02:00
|
|
|
"Roles" => "PermissionRole",
|
2007-07-19 12:40:28 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
private static $extensions = array(
|
2007-07-19 12:40:28 +02:00
|
|
|
"Hierarchy",
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function populateDefaults() {
|
2011-03-15 22:16:01 +01:00
|
|
|
parent::populateDefaults();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-15 22:16:01 +01:00
|
|
|
if(!$this->Title) $this->Title = _t('SecurityAdmin.NEWGROUP',"New Group");
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function getAllChildren() {
|
2011-05-05 12:40:24 +02:00
|
|
|
$doSet = new ArrayList();
|
2010-04-12 03:46:20 +02:00
|
|
|
|
2013-06-21 00:32:08 +02:00
|
|
|
$children = DataObject::get('Group')->filter("ParentID", $this->ID);
|
|
|
|
foreach($children as $child) {
|
|
|
|
$doSet->push($child);
|
|
|
|
$doSet->merge($child->getAllChildren());
|
2010-04-12 03:46:20 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 03:46:20 +02:00
|
|
|
return $doSet;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:34:10 +02:00
|
|
|
/**
|
|
|
|
* Caution: Only call on instances, not through a singleton.
|
2012-03-08 18:20:11 +01:00
|
|
|
* The "root group" fields will be created through {@link SecurityAdmin->EditForm()}.
|
2008-04-09 13:34:10 +02:00
|
|
|
*
|
2011-10-28 03:37:27 +02:00
|
|
|
* @return FieldList
|
2008-04-09 13:34:10 +02:00
|
|
|
*/
|
2012-04-13 15:46:47 +02:00
|
|
|
public function getCMSFields() {
|
2012-03-24 04:38:57 +01:00
|
|
|
Requirements::javascript(FRAMEWORK_DIR . '/javascript/PermissionCheckboxSetField.js');
|
|
|
|
|
2011-05-11 09:51:54 +02:00
|
|
|
$fields = new FieldList(
|
2008-04-09 13:34:10 +02:00
|
|
|
new TabSet("Root",
|
2010-10-19 02:54:16 +02:00
|
|
|
new Tab('Members', _t('SecurityAdmin.MEMBERS', 'Members'),
|
2008-11-02 21:04:10 +01:00
|
|
|
new TextField("Title", $this->fieldLabel('Title')),
|
2014-08-15 08:53:05 +02:00
|
|
|
$parentidfield = DropdownField::create( 'ParentID',
|
|
|
|
$this->fieldLabel('Parent'),
|
2012-05-28 11:13:42 +02:00
|
|
|
Group::get()->exclude('ID', $this->ID)->map('ID', 'Breadcrumbs')
|
2013-04-14 15:45:13 +02:00
|
|
|
)->setEmptyString(' '),
|
|
|
|
new TextareaField('Description', $this->fieldLabel('Description'))
|
2008-04-09 13:34:10 +02:00
|
|
|
),
|
|
|
|
|
2010-10-19 02:54:16 +02:00
|
|
|
$permissionsTab = new Tab('Permissions', _t('SecurityAdmin.PERMISSIONS', 'Permissions'),
|
2012-03-06 01:22:10 +01:00
|
|
|
$permissionsField = new PermissionCheckboxSetField(
|
2009-10-29 00:03:35 +01:00
|
|
|
'Permissions',
|
2010-05-25 06:58:17 +02:00
|
|
|
false,
|
2009-10-29 00:03:35 +01:00
|
|
|
'Permission',
|
2009-11-03 02:00:54 +01:00
|
|
|
'GroupID',
|
2010-02-22 05:37:34 +01:00
|
|
|
$this
|
|
|
|
)
|
2008-04-09 13:34:10 +02:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-06-13 15:04:40 +02:00
|
|
|
$parentidfield->setDescription(
|
2012-03-27 15:31:04 +02:00
|
|
|
_t('Group.GroupReminder', 'If you choose a parent group, this group will take all it\'s roles')
|
|
|
|
);
|
2012-03-05 18:31:52 +01:00
|
|
|
|
2012-03-06 01:22:10 +01:00
|
|
|
// Filter permissions
|
2012-03-09 00:54:02 +01:00
|
|
|
// TODO SecurityAdmin coupling, not easy to get to the form fields through GridFieldDetailForm
|
2013-03-21 19:48:54 +01:00
|
|
|
$permissionsField->setHiddenPermissions((array)Config::inst()->get('SecurityAdmin', 'hidden_permissions'));
|
2012-03-06 01:22:10 +01:00
|
|
|
|
2012-03-05 18:31:52 +01:00
|
|
|
if($this->ID) {
|
2012-04-30 14:20:21 +02:00
|
|
|
$group = $this;
|
2015-09-11 23:55:02 +02:00
|
|
|
$config = GridFieldConfig_RelationEditor::create();
|
2013-06-21 15:22:00 +02:00
|
|
|
$config->addComponent(new GridFieldButtonRow('after'));
|
|
|
|
$config->addComponents(new GridFieldExportButton('buttons-after-left'));
|
|
|
|
$config->addComponents(new GridFieldPrintButton('buttons-after-left'));
|
2012-03-09 00:54:02 +01:00
|
|
|
$config->getComponentByType('GridFieldAddExistingAutocompleter')
|
2012-03-05 18:31:52 +01:00
|
|
|
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
|
2012-04-30 14:20:21 +02:00
|
|
|
$config->getComponentByType('GridFieldDetailForm')
|
2016-02-24 13:23:38 +01:00
|
|
|
->setValidator(Member_Validator::create())
|
2012-04-30 14:20:21 +02:00
|
|
|
->setItemEditFormCallback(function($form, $component) use($group) {
|
|
|
|
$record = $form->getRecord();
|
2013-04-05 00:49:40 +02:00
|
|
|
$groupsField = $form->Fields()->dataFieldByName('DirectGroups');
|
|
|
|
if($groupsField) {
|
|
|
|
// If new records are created in a group context,
|
|
|
|
// set this group by default.
|
|
|
|
if($record && !$record->ID) {
|
|
|
|
$groupsField->setValue($group->ID);
|
|
|
|
} elseif($record && $record->ID) {
|
|
|
|
// TODO Mark disabled once chosen.js supports it
|
|
|
|
// $groupsField->setDisabledItems(array($group->ID));
|
2013-08-21 08:54:05 +02:00
|
|
|
$form->Fields()->replaceField('DirectGroups',
|
|
|
|
$groupsField->performReadonlyTransformation());
|
2013-04-05 00:49:40 +02:00
|
|
|
}
|
2012-04-30 14:20:21 +02:00
|
|
|
}
|
|
|
|
});
|
2013-08-21 08:54:05 +02:00
|
|
|
$memberList = GridField::create('Members',false, $this->DirectMembers(), $config)
|
|
|
|
->addExtraClass('members_grid');
|
2012-03-05 18:31:52 +01:00
|
|
|
// @todo Implement permission checking on GridField
|
|
|
|
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));
|
|
|
|
$fields->addFieldToTab('Root.Members', $memberList);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 10:38:15 +01:00
|
|
|
// Only add a dropdown for HTML editor configurations if more than one is available.
|
|
|
|
// Otherwise Member->getHtmlEditorConfigForCMS() will default to the 'cms' configuration.
|
|
|
|
$editorConfigMap = HtmlEditorConfig::get_available_configs_map();
|
|
|
|
if(count($editorConfigMap) > 1) {
|
|
|
|
$fields->addFieldToTab('Root.Permissions',
|
|
|
|
new DropdownField(
|
2012-05-23 12:45:04 +02:00
|
|
|
'HtmlEditorConfig',
|
|
|
|
'HTML Editor Configuration',
|
2010-02-22 10:38:15 +01:00
|
|
|
$editorConfigMap
|
|
|
|
),
|
|
|
|
'Permissions'
|
|
|
|
);
|
|
|
|
}
|
2008-04-09 13:34:10 +02:00
|
|
|
|
2008-04-26 08:27:15 +02:00
|
|
|
if(!Permission::check('EDIT_PERMISSIONS')) {
|
|
|
|
$fields->removeFieldFromTab('Root', 'Permissions');
|
|
|
|
}
|
2009-10-16 00:13:19 +02:00
|
|
|
|
2010-05-25 06:58:17 +02:00
|
|
|
// Only show the "Roles" tab if permissions are granted to edit them,
|
|
|
|
// and at least one role exists
|
2014-08-15 08:53:05 +02:00
|
|
|
if(Permission::check('APPLY_ROLES') && DataObject::get('PermissionRole')) {
|
2010-10-13 03:02:00 +02:00
|
|
|
$fields->findOrMakeTab('Root.Roles', _t('SecurityAdmin.ROLES', 'Roles'));
|
2014-08-15 08:53:05 +02:00
|
|
|
$fields->addFieldToTab('Root.Roles',
|
|
|
|
new LiteralField(
|
|
|
|
"",
|
|
|
|
"<p>" .
|
2012-03-05 14:40:40 +01:00
|
|
|
_t(
|
2014-08-15 08:53:05 +02:00
|
|
|
'SecurityAdmin.ROLESDESCRIPTION',
|
2012-09-26 23:34:00 +02:00
|
|
|
"Roles are predefined sets of permissions, and can be assigned to groups.<br />"
|
|
|
|
. "They are inherited from parent groups if required."
|
2014-08-15 08:53:05 +02:00
|
|
|
) . '<br />' .
|
2012-03-05 14:40:40 +01:00
|
|
|
sprintf(
|
|
|
|
'<a href="%s" class="add-role">%s</a>',
|
|
|
|
singleton('SecurityAdmin')->Link('show/root#Root_Roles'),
|
|
|
|
// TODO This should include #Root_Roles to switch directly to the tab,
|
|
|
|
// but tabstrip.js doesn't display tabs when directly adressed through a URL pragma
|
|
|
|
_t('Group.RolesAddEditLink', 'Manage roles')
|
|
|
|
) .
|
2014-08-15 08:53:05 +02:00
|
|
|
"</p>"
|
|
|
|
)
|
2009-10-15 23:53:15 +02:00
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-02-22 07:11:59 +01:00
|
|
|
// Add roles (and disable all checkboxes for inherited roles)
|
2013-06-21 00:32:08 +02:00
|
|
|
$allRoles = PermissionRole::get();
|
2016-01-11 01:05:10 +01:00
|
|
|
if(!Permission::check('ADMIN')) {
|
2013-06-21 00:32:08 +02:00
|
|
|
$allRoles = $allRoles->filter("OnlyAdminCanApply", 0);
|
|
|
|
}
|
2012-03-05 18:31:52 +01:00
|
|
|
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();
|
2010-02-22 07:11:59 +01:00
|
|
|
}
|
2012-03-05 18:31:52 +01:00
|
|
|
|
2012-04-04 16:59:30 +02:00
|
|
|
$rolesField = ListboxField::create('Roles', false, $allRoles->map()->toArray())
|
2012-03-05 14:40:40 +01:00
|
|
|
->setMultiple(true)
|
|
|
|
->setDefaultItems($groupRoleIDs)
|
|
|
|
->setAttribute('data-placeholder', _t('Group.AddRole', 'Add a role for this group'))
|
2014-08-15 08:53:05 +02:00
|
|
|
->setDisabledItems($inheritedRoleIDs);
|
2012-09-26 23:34:00 +02:00
|
|
|
if(!$allRoles->Count()) {
|
|
|
|
$rolesField->setAttribute('data-placeholder', _t('Group.NoRoles', 'No roles found'));
|
|
|
|
}
|
2012-03-05 14:40:40 +01:00
|
|
|
$fields->addFieldToTab('Root.Roles', $rolesField);
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2008-04-09 13:34:10 +02:00
|
|
|
$fields->push($idField = new HiddenField("ID"));
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-14 01:57:53 +02:00
|
|
|
$this->extend('updateCMSFields', $fields);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-04-09 13:34:10 +02:00
|
|
|
return $fields;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-04-29 02:07:39 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param boolean $includerelations a boolean value to indicate if the labels returned include relation fields
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-04-29 02:07:39 +02:00
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function fieldLabels($includerelations = true) {
|
2009-04-29 02:07:39 +02:00
|
|
|
$labels = parent::fieldLabels($includerelations);
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Title'] = _t('SecurityAdmin.GROUPNAME', 'Group name');
|
|
|
|
$labels['Description'] = _t('Group.Description', 'Description');
|
2012-04-14 00:16:22 +02:00
|
|
|
$labels['Code'] = _t('Group.Code', 'Group Code', 'Programmatical code identifying a group');
|
|
|
|
$labels['Locked'] = _t('Group.Locked', 'Locked?', 'Group is locked in the security administration area');
|
2008-11-02 21:04:10 +01:00
|
|
|
$labels['Sort'] = _t('Group.Sort', 'Sort Order');
|
2009-04-29 02:07:39 +02:00
|
|
|
if($includerelations){
|
2012-04-14 00:16:22 +02:00
|
|
|
$labels['Parent'] = _t('Group.Parent', 'Parent Group', 'One group has one parent group');
|
|
|
|
$labels['Permissions'] = _t('Group.has_many_Permissions', 'Permissions', 'One group has many permissions');
|
|
|
|
$labels['Members'] = _t('Group.many_many_Members', 'Members', 'One group has many members');
|
2009-04-29 02:07:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-02 21:04:10 +01:00
|
|
|
return $labels;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
2012-03-06 15:48:56 +01:00
|
|
|
* Get many-many relation to {@link Member},
|
|
|
|
* including all members which are "inherited" from children groups of this record.
|
|
|
|
* See {@link DirectMembers()} for retrieving members without any inheritance.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2015-08-21 04:01:10 +02:00
|
|
|
* @param string $filter
|
2012-03-06 15:48:56 +01:00
|
|
|
* @return ManyManyList
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
2015-08-21 04:01:10 +02:00
|
|
|
public function Members($filter = "", $sort = "", $join = "", $limit = "") {
|
|
|
|
if($sort || $join || $limit) {
|
|
|
|
Deprecation::notice('4.0',
|
|
|
|
"The sort, join, and limit arguments are deprecated, use sort(), join() and limit() on the resulting"
|
|
|
|
. " DataList instead.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if($join) {
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'The $join argument has been removed. Use leftJoin($table, $joinClause) instead.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-03-06 15:48:56 +01:00
|
|
|
// First get direct members as a base result
|
|
|
|
$result = $this->DirectMembers();
|
2015-12-11 01:55:09 +01:00
|
|
|
|
|
|
|
// Unsaved group cannot have child groups because its ID is still 0.
|
|
|
|
if(!$this->exists()) return $result;
|
|
|
|
|
2012-03-06 15:48:56 +01:00
|
|
|
// Remove the default foreign key filter in prep for re-applying a filter containing all children groups.
|
2012-09-26 23:34:00 +02:00
|
|
|
// Filters are conjunctive in DataQuery by default, so this filter would otherwise overrule any less specific
|
|
|
|
// ones.
|
2013-05-23 17:31:19 +02:00
|
|
|
if(!($result instanceof UnsavedRelationList)) {
|
|
|
|
$result = $result->alterDataQuery(function($query){
|
|
|
|
$query->removeFilterOn('Group_Members');
|
|
|
|
});
|
|
|
|
}
|
2012-03-06 15:48:56 +01:00
|
|
|
// Now set all children groups as a new foreign key
|
2012-05-28 11:13:42 +02:00
|
|
|
$groups = Group::get()->byIDs($this->collateFamilyIDs());
|
2015-08-21 04:01:10 +02:00
|
|
|
$result = $result->forForeignID($groups->column('ID'))->where($filter)->sort($sort)->limit($limit);
|
2011-10-29 06:11:27 +02:00
|
|
|
|
|
|
|
return $result;
|
2009-11-22 06:30:14 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-22 06:30:14 +01:00
|
|
|
/**
|
|
|
|
* Return only the members directly added to this group
|
|
|
|
*/
|
|
|
|
public function DirectMembers() {
|
|
|
|
return $this->getManyManyComponents('Members');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Return a set of this record's "family" of IDs - the IDs of
|
2008-11-03 02:55:59 +01:00
|
|
|
* this record and all its descendants.
|
2015-12-11 01:55:09 +01:00
|
|
|
*
|
2008-11-03 02:55:59 +01:00
|
|
|
* @return array
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function collateFamilyIDs() {
|
2015-12-11 01:55:09 +01:00
|
|
|
if (!$this->exists()) {
|
|
|
|
throw new \InvalidArgumentException("Cannot call collateFamilyIDs on unsaved Group.");
|
|
|
|
}
|
|
|
|
|
2008-11-03 02:55:59 +01:00
|
|
|
$familyIDs = array();
|
2012-09-14 17:12:37 +02:00
|
|
|
$chunkToAdd = array($this->ID);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
while($chunkToAdd) {
|
2012-09-14 17:12:37 +02:00
|
|
|
$familyIDs = array_merge($familyIDs,$chunkToAdd);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
// Get the children of *all* the groups identified in the previous chunk.
|
2013-06-21 00:32:08 +02:00
|
|
|
// This minimises the number of SQL queries necessary
|
|
|
|
$chunkToAdd = Group::get()->filter("ParentID", $chunkToAdd)->column('ID');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-11-03 02:55:59 +01:00
|
|
|
return $familyIDs;
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Returns an array of the IDs of this group and all its parents
|
|
|
|
*/
|
|
|
|
public function collateAncestorIDs() {
|
|
|
|
$parent = $this;
|
2011-02-21 12:19:23 +01:00
|
|
|
while(isset($parent) && $parent instanceof Group) {
|
2007-07-19 12:40:28 +02:00
|
|
|
$items[] = $parent->ID;
|
|
|
|
$parent = $parent->Parent;
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
/**
|
|
|
|
* This isn't a decendant of SiteTree, but needs this in case
|
|
|
|
* the group is "reorganised";
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function cmsCleanup_parentChanged() {
|
2010-08-03 03:05:27 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Override this so groups are ordered in the CMS
|
|
|
|
*/
|
|
|
|
public function stageChildren() {
|
2013-06-21 00:32:08 +02:00
|
|
|
return Group::get()
|
|
|
|
->filter("ParentID", $this->ID)
|
|
|
|
->exclude("ID", $this->ID)
|
|
|
|
->sort('"Sort"');
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-12-16 06:16:06 +01:00
|
|
|
public function getTreeTitle() {
|
2012-12-08 12:20:20 +01:00
|
|
|
if($this->hasMethod('alternateTreeTitle')) return $this->alternateTreeTitle();
|
2010-04-12 05:21:00 +02:00
|
|
|
else return htmlspecialchars($this->Title, ENT_QUOTES);
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2007-07-19 12:40:28 +02:00
|
|
|
/**
|
|
|
|
* Overloaded to ensure the code is always descent.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2010-12-20 04:18:51 +01:00
|
|
|
* @param string
|
2007-07-19 12:40:28 +02:00
|
|
|
*/
|
|
|
|
public function setCode($val){
|
2010-12-20 04:18:51 +01:00
|
|
|
$this->setField("Code", Convert::raw2url($val));
|
2007-07-19 12:40:28 +02:00
|
|
|
}
|
2013-08-30 13:58:37 +02:00
|
|
|
|
2015-06-16 01:39:32 +02:00
|
|
|
protected function validate() {
|
2013-08-30 13:58:37 +02:00
|
|
|
$result = parent::validate();
|
|
|
|
|
|
|
|
// Check if the new group hierarchy would add certain "privileged permissions",
|
2014-08-15 08:53:05 +02:00
|
|
|
// and require an admin to perform this change in case it does.
|
2013-08-30 13:58:37 +02:00
|
|
|
// This prevents "sub-admin" users with group editing permissions to increase their privileges.
|
|
|
|
if($this->Parent()->exists() && !Permission::check('ADMIN')) {
|
|
|
|
$inheritedCodes = Permission::get()
|
|
|
|
->filter('GroupID', $this->Parent()->collateAncestorIDs())
|
|
|
|
->column('Code');
|
|
|
|
$privilegedCodes = Config::inst()->get('Permission', 'privileged_permissions');
|
|
|
|
if(array_intersect($inheritedCodes, $privilegedCodes)) {
|
|
|
|
$result->error(sprintf(
|
|
|
|
_t(
|
|
|
|
'Group.HierarchyPermsError',
|
|
|
|
'Can\'t assign parent group "%s" with privileged permissions (requires ADMIN access)'
|
|
|
|
),
|
|
|
|
$this->Parent()->Title
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onBeforeWrite() {
|
2007-10-30 04:17:18 +01:00
|
|
|
parent::onBeforeWrite();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-14 11:38:07 +02:00
|
|
|
// Only set code property when the group has a custom title, and no code exists.
|
|
|
|
// The "Code" attribute is usually treated as a more permanent identifier than database IDs
|
|
|
|
// in custom application logic, so can't be changed after its first set.
|
|
|
|
if(!$this->Code && $this->Title != _t('SecurityAdmin.NEWGROUP',"New Group")) {
|
|
|
|
if(!$this->Code) $this->setCode($this->Title);
|
2007-10-30 04:17:18 +01:00
|
|
|
}
|
|
|
|
}
|
2012-03-26 11:05:38 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function onBeforeDelete() {
|
2012-03-26 11:05:38 +02:00
|
|
|
parent::onBeforeDelete();
|
|
|
|
|
|
|
|
// if deleting this group, delete it's children as well
|
|
|
|
foreach($this->Groups() as $group) {
|
|
|
|
$group->delete();
|
|
|
|
}
|
|
|
|
|
2009-12-16 06:43:47 +01:00
|
|
|
// Delete associated permissions
|
2012-03-26 11:05:38 +02:00
|
|
|
foreach($this->Permissions() as $permission) {
|
2009-12-16 06:43:47 +01:00
|
|
|
$permission->delete();
|
|
|
|
}
|
|
|
|
}
|
2012-03-26 11:05:38 +02:00
|
|
|
|
2009-02-03 23:44:11 +01:00
|
|
|
/**
|
|
|
|
* Checks for permission-code CMS_ACCESS_SecurityAdmin.
|
|
|
|
* If the group has ADMIN permissions, it requires the user to have ADMIN permissions as well.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-03 23:44:11 +01:00
|
|
|
* @param $member Member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canEdit($member = null) {
|
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canEdit', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-12-08 12:20:20 +01:00
|
|
|
if(
|
2009-02-04 00:33:28 +01:00
|
|
|
// either we have an ADMIN
|
|
|
|
(bool)Permission::checkMember($member, "ADMIN")
|
|
|
|
|| (
|
|
|
|
// or a privileged CMS user and a group without ADMIN permissions.
|
|
|
|
// without this check, a user would be able to add himself to an administrators group
|
|
|
|
// with just access to the "Security" admin interface
|
2014-08-15 08:53:05 +02:00
|
|
|
Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin") &&
|
2013-02-07 09:19:57 +01:00
|
|
|
!Permission::get()->filter(array('GroupID' => $this->ID, 'Code' => 'ADMIN'))->exists()
|
2009-02-04 00:33:28 +01:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return true;
|
2008-04-26 08:35:39 +02:00
|
|
|
}
|
2009-02-04 00:33:28 +01:00
|
|
|
|
|
|
|
return false;
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-03 23:44:11 +01:00
|
|
|
/**
|
|
|
|
* Checks for permission-code CMS_ACCESS_SecurityAdmin.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2009-02-03 23:44:11 +01:00
|
|
|
* @param $member Member
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function canView($member = null) {
|
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canView', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
// user needs access to CMS_ACCESS_SecurityAdmin
|
|
|
|
if(Permission::checkMember($member, "CMS_ACCESS_SecurityAdmin")) return true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
public function canDelete($member = null) {
|
|
|
|
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) $member = Member::currentUser();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-04-15 11:35:30 +02:00
|
|
|
// extended access checks
|
2009-02-04 00:33:28 +01:00
|
|
|
$results = $this->extend('canDelete', $member);
|
|
|
|
if($results && is_array($results)) if(!min($results)) return false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-02-04 00:33:28 +01:00
|
|
|
return $this->canEdit($member);
|
2009-02-02 00:49:53 +01:00
|
|
|
}
|
2008-02-25 03:10:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all of the children for the CMS Tree.
|
|
|
|
* Filters to only those groups that the current user can edit
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
public function AllChildrenIncludingDeleted() {
|
2010-05-25 05:55:30 +02:00
|
|
|
$extInstance = $this->getExtensionInstance('Hierarchy');
|
2009-06-04 08:48:44 +02:00
|
|
|
$extInstance->setOwner($this);
|
|
|
|
$children = $extInstance->AllChildrenIncludingDeleted();
|
|
|
|
$extInstance->clearOwner();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-05-05 12:40:24 +02:00
|
|
|
$filteredChildren = new ArrayList();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
if($children) foreach($children as $child) {
|
2009-02-03 23:44:11 +01:00
|
|
|
if($child->canView()) $filteredChildren->push($child);
|
2008-02-25 03:10:37 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-02-25 03:10:37 +01:00
|
|
|
return $filteredChildren;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
/**
|
|
|
|
* Add default records to database.
|
|
|
|
*
|
|
|
|
* This function is called whenever the database is built, after the
|
|
|
|
* database tables have all been created.
|
|
|
|
*/
|
|
|
|
public function requireDefaultRecords() {
|
|
|
|
parent::requireDefaultRecords();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
// Add default author group if no other group exists
|
|
|
|
$allGroups = DataObject::get('Group');
|
2011-03-30 07:06:15 +02:00
|
|
|
if(!$allGroups->count()) {
|
2010-08-03 03:05:27 +02:00
|
|
|
$authorGroup = new Group();
|
|
|
|
$authorGroup->Code = 'content-authors';
|
|
|
|
$authorGroup->Title = _t('Group.DefaultGroupTitleContentAuthors', 'Content Authors');
|
|
|
|
$authorGroup->Sort = 1;
|
|
|
|
$authorGroup->write();
|
|
|
|
Permission::grant($authorGroup->ID, 'CMS_ACCESS_CMSMain');
|
|
|
|
Permission::grant($authorGroup->ID, 'CMS_ACCESS_AssetAdmin');
|
|
|
|
Permission::grant($authorGroup->ID, 'CMS_ACCESS_ReportAdmin');
|
|
|
|
Permission::grant($authorGroup->ID, 'SITETREE_REORGANISE');
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
// Add default admin group if none with permission code ADMIN exists
|
|
|
|
$adminGroups = Permission::get_groups_by_permission('ADMIN');
|
2011-03-30 07:06:15 +02:00
|
|
|
if(!$adminGroups->count()) {
|
2010-08-03 03:05:27 +02:00
|
|
|
$adminGroup = new Group();
|
|
|
|
$adminGroup->Code = 'administrators';
|
|
|
|
$adminGroup->Title = _t('Group.DefaultGroupTitleAdministrators', 'Administrators');
|
|
|
|
$adminGroup->Sort = 0;
|
|
|
|
$adminGroup->write();
|
|
|
|
Permission::grant($adminGroup->ID, 'ADMIN');
|
2014-08-15 08:53:05 +02:00
|
|
|
}
|
|
|
|
|
2010-08-03 03:05:27 +02:00
|
|
|
// Members are populated through Member->requireDefaultRecords()
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-05-23 12:45:04 +02:00
|
|
|
}
|