2007-08-16 08:38:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Decorator designed to add subsites support to LeftAndMain
|
2013-10-16 02:25:06 +02:00
|
|
|
*
|
2008-11-24 04:22:01 +01:00
|
|
|
* @package subsites
|
2007-08-16 08:38:29 +02:00
|
|
|
*/
|
|
|
|
class LeftAndMainSubsites extends Extension {
|
2011-09-05 17:54:40 +02:00
|
|
|
|
2013-05-06 12:21:09 +02:00
|
|
|
private static $allowed_actions = array('CopyToSubsite');
|
2012-08-10 11:23:19 +02:00
|
|
|
|
2013-11-07 00:03:52 +01:00
|
|
|
/**
|
|
|
|
* Normally SubsiteID=0 on a DataObject means it is only accessible from the special "main site".
|
|
|
|
* However in some situations SubsiteID=0 will be understood as a "globally accessible" object in which
|
|
|
|
* case this property is set to true (i.e. in AssetAdmin).
|
|
|
|
*/
|
|
|
|
private static $treats_subsite_0_as_global = false;
|
|
|
|
|
2011-02-14 04:10:47 +01:00
|
|
|
function init() {
|
2007-08-16 08:38:29 +02:00
|
|
|
Requirements::css('subsites/css/LeftAndMain_Subsites.css');
|
|
|
|
Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
|
2008-12-02 04:50:06 +01:00
|
|
|
Requirements::javascript('subsites/javascript/VirtualPage_Subsites.js');
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2012-08-10 11:23:19 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
/**
|
|
|
|
* Set the title of the CMS tree
|
|
|
|
*/
|
|
|
|
function getCMSTreeTitle() {
|
|
|
|
$subsite = Subsite::currentSubSite();
|
2011-09-05 15:11:23 +02:00
|
|
|
return $subsite ? Convert::raw2xml($subsite->Title) : _t('LeftAndMain.SITECONTENTLEFT');
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2010-03-01 22:40:43 +01:00
|
|
|
|
|
|
|
function updatePageOptions(&$fields) {
|
|
|
|
$fields->push(new HiddenField('SubsiteID', 'SubsiteID', Subsite::currentSubsiteID()));
|
|
|
|
}
|
2013-10-16 02:25:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all subsites accessible for current user on this controller.
|
|
|
|
*
|
|
|
|
* @return ArrayList of {@link Subsite} instances.
|
2013-08-22 03:02:46 +02:00
|
|
|
*/
|
2013-10-16 02:25:06 +02:00
|
|
|
function sectionSites($includeMainSite = true, $mainSiteTitle = "Main site", $member = null) {
|
2013-10-30 13:43:59 +01:00
|
|
|
if($mainSiteTitle == 'Main site') {
|
|
|
|
$mainSiteTitle = _t('Subsites.MainSiteTitle', 'Main site');
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
// Rationalise member arguments
|
|
|
|
if(!$member) $member = Member::currentUser();
|
|
|
|
if(!$member) return new ArrayList();
|
|
|
|
if(!is_object($member)) $member = DataObject::get_by_id('Member', $member);
|
|
|
|
|
|
|
|
// Collect permissions - honour the LeftAndMain::required_permission_codes, current model requires
|
|
|
|
// us to check if the user satisfies ALL permissions. Code partly copied from LeftAndMain::canView.
|
|
|
|
$codes = array();
|
|
|
|
$extraCodes = Config::inst()->get($this->owner->class, 'required_permission_codes');
|
|
|
|
if($extraCodes !== false) {
|
|
|
|
if($extraCodes) $codes = array_merge($codes, (array)$extraCodes);
|
|
|
|
else $codes[] = "CMS_ACCESS_{$this->owner->class}";
|
|
|
|
} else {
|
|
|
|
// Check overriden - all subsites accessible.
|
|
|
|
return Subsite::all_sites();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find subsites satisfying all permissions for the Member.
|
|
|
|
$codesPerSite = array();
|
|
|
|
$sitesArray = array();
|
|
|
|
foreach ($codes as $code) {
|
|
|
|
$sites = Subsite::accessible_sites($code, $includeMainSite, $mainSiteTitle, $member);
|
|
|
|
foreach ($sites as $site) {
|
|
|
|
// Build the structure for checking how many codes match.
|
|
|
|
$codesPerSite[$site->ID][$code] = true;
|
|
|
|
|
|
|
|
// Retain Subsite objects for later.
|
|
|
|
$sitesArray[$site->ID] = $site;
|
2013-05-08 04:27:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
// Find sites that satisfy all codes conjuncitvely.
|
|
|
|
$accessibleSites = new ArrayList();
|
|
|
|
foreach ($codesPerSite as $siteID => $siteCodes) {
|
|
|
|
if (count($siteCodes)==count($codes)) {
|
|
|
|
$accessibleSites->push($sitesArray[$siteID]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $accessibleSites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a list of the subsites accessible to the current user.
|
|
|
|
* It's enough for any section to be accessible for the section to be included.
|
|
|
|
*/
|
|
|
|
public function Subsites() {
|
|
|
|
return Subsite::all_accessible_sites();
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates a list of subsites with the data needed to
|
|
|
|
* produce a dropdown site switcher
|
|
|
|
* @return ArrayList
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function ListSubsites(){
|
2007-08-16 08:38:29 +02:00
|
|
|
$list = $this->Subsites();
|
2010-04-06 05:15:44 +02:00
|
|
|
$currentSubsiteID = Subsite::currentSubsiteID();
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
if($list == null || $list->Count() == 1 && $list->First()->DefaultSite == true){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Requirements::javascript('subsites/javascript/LeftAndMain_Subsites.js');
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
$output = new ArrayList();
|
2013-05-08 04:27:56 +02:00
|
|
|
|
2013-08-22 03:02:46 +02:00
|
|
|
foreach($list as $subsite) {
|
|
|
|
$CurrentState = $subsite->ID == $currentSubsiteID ? 'selected' : '';
|
|
|
|
|
|
|
|
$output->push(new ArrayData(array(
|
|
|
|
'CurrentState' => $CurrentState,
|
|
|
|
'ID' => $subsite->ID,
|
|
|
|
'Title' => Convert::raw2xml($subsite->Title)
|
|
|
|
)));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
public function alternateMenuDisplayCheck($controllerName) {
|
|
|
|
if(!class_exists($controllerName)){
|
|
|
|
return false;
|
2013-08-22 03:02:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
// Check subsite support.
|
|
|
|
if(Subsite::currentSubsiteID() == 0){
|
|
|
|
// Main site always supports everything.
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
$controller = singleton($controllerName);
|
|
|
|
if($controller->hasMethod('subsiteCMSShowInMenu') && $controller->subsiteCMSShowInMenu()){
|
|
|
|
return true;
|
2013-08-22 03:02:46 +02:00
|
|
|
}
|
2013-10-16 02:25:06 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
// It's not necessary to check access permissions here. Framework calls canView on the controller,
|
|
|
|
// which in turn uses the Permission API which is augmented by our GroupSubsites.
|
2013-08-22 03:02:46 +02:00
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
return false;
|
2007-08-16 08:38:29 +02:00
|
|
|
}
|
2013-08-22 03:02:46 +02:00
|
|
|
|
2007-08-16 08:38:29 +02:00
|
|
|
public function CanAddSubsites() {
|
|
|
|
return Permission::check("ADMIN", "any", null, "all");
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
|
|
|
|
2013-11-07 00:03:52 +01:00
|
|
|
/**
|
|
|
|
* Helper for testing if the subsite should be adjusted.
|
|
|
|
*/
|
|
|
|
public function shouldChangeSubsite($adminClass, $recordSubsiteID, $currentSubsiteID) {
|
|
|
|
if (Config::inst()->get($adminClass, 'treats_subsite_0_as_global') && $recordSubsiteID==0) return false;
|
|
|
|
if ($recordSubsiteID!=$currentSubsiteID) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-31 02:29:25 +02:00
|
|
|
/**
|
2013-12-04 05:22:00 +01:00
|
|
|
* Check if the current controller is accessible for this user on this subsite.
|
|
|
|
*/
|
|
|
|
function canAccess() {
|
|
|
|
// Admin can access everything, no point in checking.
|
|
|
|
$member = Member::currentUser();
|
2014-01-09 05:12:47 +01:00
|
|
|
if($member &&
|
|
|
|
(
|
|
|
|
Permission::checkMember($member, 'ADMIN') || // 'Full administrative rights' in SecurityAdmin
|
|
|
|
Permission::checkMember($member, 'CMS_ACCESS_LeftAndMain') // 'Access to all CMS sections' in SecurityAdmin
|
|
|
|
)) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-12-04 05:22:00 +01:00
|
|
|
|
|
|
|
// Check if we have access to current section on the current subsite.
|
2014-01-09 05:12:47 +01:00
|
|
|
$accessibleSites = $this->owner->sectionSites(true, "Main site", $member);
|
2013-12-04 05:22:00 +01:00
|
|
|
if ($accessibleSites->count() && $accessibleSites->find('ID', Subsite::currentSubsiteID())) {
|
|
|
|
// Current section can be accessed on the current site, all good.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prevent accessing disallowed resources. This happens after onBeforeInit has executed,
|
|
|
|
* so all redirections should've already taken place.
|
|
|
|
*/
|
|
|
|
public function alternateAccessCheck() {
|
|
|
|
return $this->owner->canAccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect the user to something accessible if the current section/subsite is forbidden.
|
|
|
|
*
|
|
|
|
* This is done via onBeforeInit as it needs to be done before the LeftAndMain::init has a
|
|
|
|
* chance to forbids access via alternateAccessCheck.
|
|
|
|
*
|
|
|
|
* If we need to change the subsite we force the redirection to /admin/ so the frontend is
|
|
|
|
* fully re-synchronised with the internal session. This is better than risking some panels
|
|
|
|
* showing data from another subsite.
|
2007-08-31 02:29:25 +02:00
|
|
|
*/
|
2013-10-16 02:25:06 +02:00
|
|
|
public function onBeforeInit() {
|
|
|
|
// We are accessing the CMS, so we need to let Subsites know we will be using the session.
|
|
|
|
Subsite::$use_session_subsiteid = true;
|
|
|
|
|
2013-11-07 00:03:52 +01:00
|
|
|
// FIRST, check if we need to change subsites due to the URL.
|
|
|
|
|
2013-10-16 02:25:06 +02:00
|
|
|
// Catch forced subsite changes that need to cause CMS reloads.
|
|
|
|
if(isset($_GET['SubsiteID'])) {
|
|
|
|
// Clear current page when subsite changes (or is set for the first time)
|
|
|
|
if(!Session::get('SubsiteID') || $_GET['SubsiteID'] != Session::get('SubsiteID')) {
|
|
|
|
Session::clear("{$this->owner->class}.currentPage");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update current subsite in session
|
|
|
|
Subsite::changeSubsite($_GET['SubsiteID']);
|
|
|
|
|
|
|
|
//Redirect to clear the current page
|
2014-11-24 03:32:05 +01:00
|
|
|
if ($this->owner->canView(Member::currentUser())) {
|
|
|
|
//Redirect to clear the current page
|
|
|
|
return $this->owner->redirect($this->owner->Link());
|
|
|
|
}
|
|
|
|
//Redirect to the default CMS section
|
2013-10-16 02:25:06 +02:00
|
|
|
return $this->owner->redirect('admin/');
|
|
|
|
}
|
|
|
|
|
2014-06-04 17:41:28 +02:00
|
|
|
// Automatically redirect the session to appropriate subsite when requesting a record.
|
|
|
|
// This is needed to properly initialise the session in situations where someone opens the CMS via a link.
|
|
|
|
$record = $this->owner->currentPage();
|
|
|
|
if($record && isset($record->SubsiteID) && is_numeric($record->SubsiteID) && isset($this->owner->urlParams['ID'])) {
|
|
|
|
|
|
|
|
if ($this->shouldChangeSubsite($this->owner->class, $record->SubsiteID, Subsite::currentSubsiteID())) {
|
|
|
|
// Update current subsite in session
|
|
|
|
Subsite::changeSubsite($record->SubsiteID);
|
|
|
|
|
2014-11-24 03:32:05 +01:00
|
|
|
if ($this->owner->canView(Member::currentUser())) {
|
|
|
|
//Redirect to clear the current page
|
|
|
|
return $this->owner->redirect($this->owner->Link());
|
|
|
|
}
|
|
|
|
//Redirect to the default CMS section
|
2014-06-04 17:41:28 +02:00
|
|
|
return $this->owner->redirect('admin/');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-07 00:03:52 +01:00
|
|
|
// SECOND, check if we need to change subsites due to lack of permissions.
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2013-12-04 05:22:00 +01:00
|
|
|
if (!$this->owner->canAccess()) {
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2013-12-04 05:22:00 +01:00
|
|
|
$member = Member::currentUser();
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2013-12-04 05:22:00 +01:00
|
|
|
// Current section is not accessible, try at least to stick to the same subsite.
|
|
|
|
$menu = CMSMenu::get_menu_items();
|
|
|
|
foreach($menu as $candidate) {
|
|
|
|
if($candidate->controller && $candidate->controller!=$this->owner->class) {
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2013-12-04 05:22:00 +01:00
|
|
|
$accessibleSites = singleton($candidate->controller)->sectionSites(true, 'Main site', $member);
|
|
|
|
if ($accessibleSites->count() && $accessibleSites->find('ID', Subsite::currentSubsiteID())) {
|
|
|
|
// Section is accessible, redirect there.
|
|
|
|
return $this->owner->redirect(singleton($candidate->controller)->Link());
|
|
|
|
}
|
2010-03-01 22:39:19 +01:00
|
|
|
}
|
|
|
|
}
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2013-12-04 05:22:00 +01:00
|
|
|
// If no section is available, look for other accessible subsites.
|
|
|
|
foreach($menu as $candidate) {
|
|
|
|
if($candidate->controller) {
|
|
|
|
$accessibleSites = singleton($candidate->controller)->sectionSites(true, 'Main site', $member);
|
|
|
|
if ($accessibleSites->count()) {
|
|
|
|
Subsite::changeSubsite($accessibleSites->First()->ID);
|
|
|
|
return $this->owner->redirect(singleton($candidate->controller)->Link());
|
|
|
|
}
|
2010-03-01 22:39:19 +01:00
|
|
|
}
|
|
|
|
}
|
2013-12-04 05:22:00 +01:00
|
|
|
|
|
|
|
// We have not found any accessible section or subsite. User should be denied access.
|
|
|
|
return Security::permissionFailure($this->owner);
|
|
|
|
|
2010-03-01 22:39:19 +01:00
|
|
|
}
|
2013-12-04 05:22:00 +01:00
|
|
|
|
|
|
|
// Current site is accessible. Allow through.
|
|
|
|
return;
|
2007-08-31 02:29:25 +02:00
|
|
|
}
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2007-10-11 06:50:49 +02:00
|
|
|
function augmentNewSiteTreeItem(&$item) {
|
2010-03-01 22:40:43 +01:00
|
|
|
$item->SubsiteID = isset($_POST['SubsiteID']) ? $_POST['SubsiteID'] : Subsite::currentSubsiteID();
|
2007-10-11 06:50:49 +02:00
|
|
|
}
|
2013-10-16 02:25:06 +02:00
|
|
|
|
2010-03-01 04:01:50 +01:00
|
|
|
function onAfterSave($record) {
|
2010-03-01 04:11:48 +01:00
|
|
|
if($record->hasMethod('NormalRelated') && ($record->NormalRelated() || $record->ReverseRelated())) {
|
2012-10-12 01:06:09 +02:00
|
|
|
$this->owner->response->addHeader('X-Status', rawurlencode(_t('LeftAndMainSubsites.Saved', 'Saved, please update related pages.')));
|
2010-03-01 04:01:50 +01:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 01:06:09 +02:00
|
|
|
|
2012-08-10 11:23:19 +02:00
|
|
|
function copytosubsite($data, $form) {
|
|
|
|
$page = DataObject::get_by_id('SiteTree', $data['ID']);
|
|
|
|
$subsite = DataObject::get_by_id('Subsite', $data['CopyToSubsiteID']);
|
|
|
|
$newPage = $page->duplicateToSubsite($subsite->ID, true);
|
2013-01-03 17:38:51 +01:00
|
|
|
$response = $this->owner->getResponse();
|
|
|
|
$response->addHeader('X-Reload', true);
|
2012-08-10 11:23:19 +02:00
|
|
|
return $this->owner->redirect(Controller::join_links($this->owner->Link('show'), $newPage->ID));
|
|
|
|
}
|
2007-08-16 08:38:29 +02:00
|
|
|
|
2013-04-25 19:29:56 +02:00
|
|
|
}
|