Source of file SiteConfig.php
Size: 16,390 Bytes - Last Modified: 2021-12-23T10:34:29+00:00
/var/www/docs.ssmods.com/process/src/code/SiteConfig.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 | <?php namespace SilverStripe\SiteConfig; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Forms\FieldList; use SilverStripe\Forms\FormAction; use SilverStripe\Forms\HiddenField; use SilverStripe\Forms\ListboxField; use SilverStripe\Forms\LiteralField; use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\Tab; use SilverStripe\Forms\TabSet; use SilverStripe\Forms\TextField; use SilverStripe\ORM\DB; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ManyManyList; use SilverStripe\Security\Group; use SilverStripe\Security\Member; use SilverStripe\Security\Permission; use SilverStripe\Security\PermissionProvider; use SilverStripe\Security\Security; use SilverStripe\View\TemplateGlobalProvider; use SilverStripe\CMS\Controllers\CMSMain; /** * SiteConfig * * @property string Title Title of the website. * @property string Tagline Tagline of the website. * @property string CanViewType Type of restriction used for view permissions. * @property string CanEditType Type of restriction used for edit permissions. * @property string CanCreateTopLevelType Type of restriction used for creation of root-level pages. * @method ManyManyList ViewerGroups() List of groups that can view SiteConfig. * @method ManyManyList EditorGroups() List of groups that can edit SiteConfig. * @method ManyManyList CreateTopLevelGroups() List of groups that can create root-level pages. */ class SiteConfig extends DataObject implements PermissionProvider, TemplateGlobalProvider { private static $db = [ "Title" => "Varchar(255)", "Tagline" => "Varchar(255)", "CanViewType" => "Enum('Anyone, LoggedInUsers, OnlyTheseUsers', 'Anyone')", "CanEditType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", "CanCreateTopLevelType" => "Enum('LoggedInUsers, OnlyTheseUsers', 'LoggedInUsers')", ]; private static $many_many = [ "ViewerGroups" => Group::class, "EditorGroups" => Group::class, "CreateTopLevelGroups" => Group::class, ]; private static $defaults = [ "CanViewType" => "Anyone", "CanEditType" => "LoggedInUsers", "CanCreateTopLevelType" => "LoggedInUsers", ]; private static $table_name = 'SiteConfig'; /** * Default permission to check for 'LoggedInUsers' to create or edit pages * * @var array * @config */ private static $required_permission = [ 'CMS_ACCESS_CMSMain', 'CMS_ACCESS_LeftAndMain' ]; public function populateDefaults() { $this->Title = _t(self::class . '.SITENAMEDEFAULT', "Your Site Name"); $this->Tagline = _t(self::class . '.TAGLINEDEFAULT', "your tagline here"); // Allow these defaults to be overridden parent::populateDefaults(); } /** * Get the fields that are sent to the CMS. * * In your extensions: updateCMSFields($fields). * * @return FieldList */ public function getCMSFields() { $mapFn = function ($groups = []) { $map = []; foreach ($groups as $group) { // Listboxfield values are escaped, use ASCII char instead of » $map[$group->ID] = $group->getBreadcrumbs(' > '); } asort($map); return $map; }; $groupsMap = $mapFn(Group::get()); $viewAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_VIEW_ALL', 'ADMIN'])); $editAllGroupsMap = $mapFn(Permission::get_groups_by_permission(['SITETREE_EDIT_ALL', 'ADMIN'])); $fields = new FieldList( new TabSet( "Root", $tabMain = new Tab( 'Main', $titleField = new TextField("Title", _t(self::class . '.SITETITLE', "Site title")), $taglineField = new TextField("Tagline", _t(self::class . '.SITETAGLINE', "Site Tagline/Slogan")) ), $tabAccess = new Tab( 'Access', $viewersOptionsField = new OptionsetField( "CanViewType", _t(self::class . '.VIEWHEADER', "Who can view pages on this site?") ), $viewerGroupsField = ListboxField::create( "ViewerGroups", _t('SilverStripe\\CMS\\Model\\SiteTree.VIEWERGROUPS', "Viewer Groups") ) ->setSource($groupsMap) ->setAttribute( 'data-placeholder', _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') ), $editorsOptionsField = new OptionsetField( "CanEditType", _t(self::class . '.EDITHEADER', "Who can edit pages on this site?") ), $editorGroupsField = ListboxField::create( "EditorGroups", _t('SilverStripe\\CMS\\Model\\SiteTree.EDITORGROUPS', "Editor Groups") ) ->setSource($groupsMap) ->setAttribute( 'data-placeholder', _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') ), $topLevelCreatorsOptionsField = new OptionsetField( "CanCreateTopLevelType", _t(self::class . '.TOPLEVELCREATE', "Who can create pages in the root of the site?") ), $topLevelCreatorsGroupsField = ListboxField::create( "CreateTopLevelGroups", _t(self::class . '.TOPLEVELCREATORGROUPS', "Top level creators") ) ->setSource($groupsMap) ->setAttribute( 'data-placeholder', _t('SilverStripe\\CMS\\Model\\SiteTree.GroupPlaceholder', 'Click to select group') ) ) ), new HiddenField('ID') ); $viewersOptionsSource = []; $viewersOptionsSource["Anyone"] = _t('SilverStripe\\CMS\\Model\\SiteTree.ACCESSANYONE', "Anyone"); $viewersOptionsSource["LoggedInUsers"] = _t( 'SilverStripe\\CMS\\Model\\SiteTree.ACCESSLOGGEDIN', "Logged-in users" ); $viewersOptionsSource["OnlyTheseUsers"] = _t( 'SilverStripe\\CMS\\Model\\SiteTree.ACCESSONLYTHESE', "Only these groups (choose from list)" ); $viewersOptionsField->setSource($viewersOptionsSource); if ($viewAllGroupsMap) { $viewerGroupsField->setDescription(_t( 'SilverStripe\\CMS\\Model\\SiteTree.VIEWER_GROUPS_FIELD_DESC', 'Groups with global view permissions: {groupList}', ['groupList' => implode(', ', array_values($viewAllGroupsMap))] )); } if ($editAllGroupsMap) { $editorGroupsField->setDescription(_t( 'SilverStripe\\CMS\\Model\\SiteTree.EDITOR_GROUPS_FIELD_DESC', 'Groups with global edit permissions: {groupList}', ['groupList' => implode(', ', array_values($editAllGroupsMap))] )); } $editorsOptionsSource = []; $editorsOptionsSource["LoggedInUsers"] = _t( 'SilverStripe\\CMS\\Model\\SiteTree.EDITANYONE', "Anyone who can log-in to the CMS" ); $editorsOptionsSource["OnlyTheseUsers"] = _t( 'SilverStripe\\CMS\\Model\\SiteTree.EDITONLYTHESE', "Only these groups (choose from list)" ); $editorsOptionsField->setSource($editorsOptionsSource); $topLevelCreatorsOptionsField->setSource($editorsOptionsSource); if (!Permission::check('EDIT_SITECONFIG')) { $fields->makeFieldReadonly($viewersOptionsField); $fields->makeFieldReadonly($viewerGroupsField); $fields->makeFieldReadonly($editorsOptionsField); $fields->makeFieldReadonly($editorGroupsField); $fields->makeFieldReadonly($topLevelCreatorsOptionsField); $fields->makeFieldReadonly($topLevelCreatorsGroupsField); $fields->makeFieldReadonly($taglineField); $fields->makeFieldReadonly($titleField); } if (file_exists(BASE_PATH . '/install.php')) { $fields->addFieldToTab( 'Root.Main', LiteralField::create( 'InstallWarningHeader', '<div class="alert alert-warning">' . _t( 'SilverStripe\\CMS\\Model\\SiteTree.REMOVE_INSTALL_WARNING', 'Warning: You should remove install.php from this SilverStripe install for security reasons.' ) . '</div>' ), 'Title' ); } $tabMain->setTitle(_t(self::class . '.TABMAIN', "Main")); $tabAccess->setTitle(_t(self::class . '.TABACCESS', "Access")); $this->extend('updateCMSFields', $fields); return $fields; } /** * Get the actions that are sent to the CMS. * * In your extensions: updateEditFormActions($actions) * * @return FieldList */ public function getCMSActions() { if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) { $actions = new FieldList( FormAction::create( 'save_siteconfig', _t('SilverStripe\\CMS\\Controllers\\CMSMain.SAVE', 'Save') )->addExtraClass('btn-primary font-icon-save') ); } else { $actions = new FieldList(); } $this->extend('updateCMSActions', $actions); return $actions; } /** * @return string */ public function CMSEditLink() { return SiteConfigLeftAndMain::singleton()->Link(); } /** * Get the current sites SiteConfig, and creates a new one through * {@link make_site_config()} if none is found. * * @return SiteConfig */ public static function current_site_config() { /** @var SiteConfig $siteConfig */ $siteConfig = DataObject::get_one(SiteConfig::class); if (!$siteConfig) { $siteConfig = self::make_site_config(); } static::singleton()->extend('updateCurrentSiteConfig', $siteConfig); return $siteConfig; } /** * Setup a default SiteConfig record if none exists. */ public function requireDefaultRecords() { parent::requireDefaultRecords(); $config = DataObject::get_one(SiteConfig::class); if (!$config) { self::make_site_config(); DB::alteration_message("Added default site config", "created"); } } /** * Create SiteConfig with defaults from language file. * * @return SiteConfig */ public static function make_site_config() { $config = SiteConfig::create(); $config->write(); return $config; } /** * Can a user view this SiteConfig instance? * * @param Member $member * @return boolean */ public function canView($member = null) { if (!$member) { $member = Security::getCurrentUser(); } $extended = $this->extendedCan('canView', $member); if ($extended !== null) { return $extended; } // Assuming all that can edit this object can also view it return $this->canEdit($member); } /** * Can a user view pages on this site? This method is only * called if a page is set to Inherit, but there is nothing * to inherit from. * * @param Member $member * @return boolean */ public function canViewPages($member = null) { if (!$member) { $member = Security::getCurrentUser(); } if ($member && Permission::checkMember($member, "ADMIN")) { return true; } $extended = $this->extendedCan('canViewPages', $member); if ($extended !== null) { return $extended; } if (!$this->CanViewType || $this->CanViewType == 'Anyone') { return true; } // check for any logged-in users if ($this->CanViewType === 'LoggedInUsers' && $member) { return true; } // check for specific groups if ($this->CanViewType === 'OnlyTheseUsers' && $member && $member->inGroups($this->ViewerGroups())) { return true; } return false; } /** * Can a user edit pages on this site? This method is only * called if a page is set to Inherit, but there is nothing * to inherit from, or on new records without a parent. * * @param Member $member * @return boolean */ public function canEditPages($member = null) { if (!$member) { $member = Security::getCurrentUser(); } if ($member && Permission::checkMember($member, "ADMIN")) { return true; } $extended = $this->extendedCan('canEditPages', $member); if ($extended !== null) { return $extended; } // check for any logged-in users with CMS access if ($this->CanEditType === 'LoggedInUsers' && Permission::checkMember($member, $this->config()->get('required_permission')) ) { return true; } // check for specific groups if ($this->CanEditType === 'OnlyTheseUsers' && $member && $member->inGroups($this->EditorGroups())) { return true; } return false; } public function canEdit($member = null) { if (!$member) { $member = Security::getCurrentUser(); } $extended = $this->extendedCan('canEdit', $member); if ($extended !== null) { return $extended; } return Permission::checkMember($member, "EDIT_SITECONFIG"); } /** * @return array */ public function providePermissions() { return [ 'EDIT_SITECONFIG' => [ 'name' => _t(self::class . '.EDIT_PERMISSION', 'Manage site configuration'), 'category' => _t( 'SilverStripe\\Security\\Permission.PERMISSIONS_CATEGORY', 'Roles and access permissions' ), 'help' => _t( self::class . '.EDIT_PERMISSION_HELP', 'Ability to edit global access settings/top-level page permissions.' ), 'sort' => 400 ] ]; } /** * Can a user create pages in the root of this site? * * @param Member $member * @return boolean */ public function canCreateTopLevel($member = null) { if (!$member) { $member = Security::getCurrentUser(); } if ($member && Permission::checkMember($member, "ADMIN")) { return true; } $extended = $this->extendedCan('canCreateTopLevel', $member); if ($extended !== null) { return $extended; } // check for any logged-in users with CMS permission if ($this->CanCreateTopLevelType === 'LoggedInUsers' && Permission::checkMember($member, $this->config()->get('required_permission')) ) { return true; } // check for specific groups if ($this->CanCreateTopLevelType === 'OnlyTheseUsers' && $member && $member->inGroups($this->CreateTopLevelGroups()) ) { return true; } return false; } /** * Add $SiteConfig to all SSViewers */ public static function get_template_global_variables() { return [ 'SiteConfig' => 'current_site_config', ]; } } |