mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FEATURE SiteConfig (from r85339)
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@89190 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
88d5843cab
commit
ae24151fe5
@ -402,6 +402,10 @@ HTML;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function SiteConfig() {
|
||||
return SiteConfig::current_site_config();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the xml:lang and lang attributes
|
||||
|
152
core/model/SiteConfig.php
Normal file
152
core/model/SiteConfig.php
Normal file
@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Sitewide configuration
|
||||
*
|
||||
* @author Tom Rix
|
||||
*/
|
||||
class SiteConfig extends DataObject {
|
||||
static $db = array(
|
||||
"Title" => "Varchar(255)",
|
||||
"Tagline" => "Varchar(255)",
|
||||
"CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')",
|
||||
"CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')"
|
||||
);
|
||||
|
||||
static $many_many = array(
|
||||
"ViewerGroups" => "Group",
|
||||
"EditorGroups" => "Group"
|
||||
);
|
||||
|
||||
/**
|
||||
* Get the fields that are sent to the CMS. In
|
||||
* your decorators: updateEditFormFields(&$fields)
|
||||
*
|
||||
* @return Fieldset
|
||||
*/
|
||||
function getFormFields() {
|
||||
$fields = new FieldSet(
|
||||
new TabSet("Root",
|
||||
new Tab('Main',
|
||||
$titleField = new TextField("Title", "Title"),
|
||||
$taglineField = new TextField("Tagline", "Tagline")
|
||||
),
|
||||
new Tab('Access',
|
||||
new HeaderField('WhoCanViewHeader', "Who can view pages on this site?", 2),
|
||||
$viewersOptionsField = new OptionsetField("CanViewType"),
|
||||
$viewerGroupsField = new TreeMultiselectField("ViewerGroups", _t('SiteTree.VIEWERGROUPS', "Viewer Groups")),
|
||||
new HeaderField('WhoCanEditHeader', "Who can edit pages on this site?", 2),
|
||||
$editorsOptionsField = new OptionsetField("CanEditType"),
|
||||
$editorGroupsField = new TreeMultiselectField("EditorGroups", _t('SiteTree.EDITORGROUPS', "Editor Groups"))
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$viewersOptionsSource = array();
|
||||
$viewersOptionsSource["Anyone"] = _t('SiteTree.ACCESSANYONE', "Anyone");
|
||||
$viewersOptionsSource["LoggedInUsers"] = _t('SiteTree.ACCESSLOGGEDIN', "Logged-in users");
|
||||
$viewersOptionsSource["OnlyTheseUsers"] = _t('SiteTree.ACCESSONLYTHESE', "Only these people (choose from list)");
|
||||
$viewersOptionsField->setSource($viewersOptionsSource);
|
||||
|
||||
$editorsOptionsSource = array();
|
||||
$editorsOptionsSource["LoggedInUsers"] = _t('SiteTree.EDITANYONE', "Anyone who can log-in to the CMS");
|
||||
$editorsOptionsSource["OnlyTheseUsers"] = _t('SiteTree.EDITONLYTHESE', "Only these people (choose from list)");
|
||||
$editorsOptionsField->setSource($editorsOptionsSource);
|
||||
|
||||
if(!Permission::check('ADMIN')) {
|
||||
$fields->makeFieldReadonly($viewersOptionsField);
|
||||
$fields->makeFieldReadonly($viewerGroupsField);
|
||||
$fields->makeFieldReadonly($editorsOptionsField);
|
||||
$fields->makeFieldReadonly($editorGroupsField);
|
||||
$fields->makeFieldReadonly($taglineField);
|
||||
$fields->makeFieldReadonly($titleField);
|
||||
}
|
||||
|
||||
$this->extend('updateEditFormFields', $fields);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actions that are sent to the CMS. In
|
||||
* your decorators: updateEditFormActions(&$actions)
|
||||
*
|
||||
* @return Fieldset
|
||||
*/
|
||||
function getFormActions() {
|
||||
if(Permission::check('ADMIN')) {
|
||||
$actions = new FieldSet(
|
||||
new FormAction('save_siteconfig', 'Save')
|
||||
);
|
||||
} else {
|
||||
$actions = new FieldSet();
|
||||
}
|
||||
|
||||
$this->extend('updateEditFormActions', $actions);
|
||||
return $actions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current sites SiteConfig
|
||||
*
|
||||
* @return SiteConfig
|
||||
*/
|
||||
static function current_site_config() {
|
||||
$siteConfig = DataObject::get_one('SiteConfig');
|
||||
if (!$siteConfig) {
|
||||
self::make_site_config();
|
||||
$siteConfig = DataObject::get_one('SiteConfig');
|
||||
}
|
||||
return $siteConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup a default SiteConfig record if none exists
|
||||
*
|
||||
*/
|
||||
function requireDefaultRecords() {
|
||||
parent::requireDefaultRecords();
|
||||
self::make_site_config();
|
||||
Database::alteration_message("Added default site config","created");
|
||||
}
|
||||
|
||||
static function make_site_config() {
|
||||
if(!DataObject::get_one('SiteConfig')){
|
||||
$siteConfig = new SiteConfig();
|
||||
$siteConfig->Title = 'Your Site Name';
|
||||
$siteConfig->Tagline = 'your tagline here';
|
||||
$siteConfig->write();
|
||||
}
|
||||
}
|
||||
|
||||
public function canView($member = null) {
|
||||
if ($this->CanViewType == 'Anyone') return true;
|
||||
|
||||
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
|
||||
$member = Member::currentUserID();
|
||||
}
|
||||
|
||||
// check for any logged-in users
|
||||
if($this->CanViewType == 'LoggedInUsers' && $member) return true;
|
||||
|
||||
// check for specific groups
|
||||
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
|
||||
if($this->CanViewType == 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function canEdit($member = null) {
|
||||
if(!$member || !(is_a($member, 'Member')) || is_numeric($member)) {
|
||||
$member = Member::currentUserID();
|
||||
}
|
||||
|
||||
// check for any logged-in users
|
||||
if($this->CanEditType == 'LoggedInUsers' && $member) return true;
|
||||
|
||||
// check for specific groups
|
||||
if($member && is_numeric($member)) $member = DataObject::get_by_id('Member', $member);
|
||||
if($this->CanEditType == 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
@ -752,7 +752,7 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
// check for inherit
|
||||
if($this->CanViewType == 'Inherit') {
|
||||
if($this->ParentID) return $this->Parent()->canView($member);
|
||||
else return true;
|
||||
else return SiteConfig::current_site_config()->canView($member);
|
||||
}
|
||||
|
||||
// check for any logged-in users
|
||||
@ -1021,8 +1021,10 @@ class SiteTree extends DataObject implements PermissionProvider,i18nEntityProvid
|
||||
if($potentiallyInherited) {
|
||||
// Group $potentiallyInherited by ParentID; we'll look at the permission of all those
|
||||
// parents and then see which ones the user has permission on
|
||||
$canEditSiteConfig = SiteConfig::current_site_config()->canEdit($member);
|
||||
foreach($potentiallyInherited as $item) {
|
||||
$groupedByParent[$item->ParentID][] = $item->ID;
|
||||
if ($item->ParentID) $groupedByParent[$item->ParentID][] = $item->ID;
|
||||
else $result[$item->ID] = $canEditSiteConfig;
|
||||
}
|
||||
|
||||
$actuallyInherited = self::can_edit_multiple(array_keys($groupedByParent), $memberID);
|
||||
|
Loading…
Reference in New Issue
Block a user