Switched to using getField/setField for caching whether better navigator should be displayed or not

Moved the edit link permission code into the config layer so it can be overridden per-controller

Added a method (BetterNavigatorShouldDisplay) to allow overriding of the default shouldDisplay checks
This commit is contained in:
UndefinedOffset 2022-01-05 17:42:42 -04:00
parent 92416eceef
commit 8d85d5df36
No known key found for this signature in database
GPG Key ID: 59C4EE2B6468B796
2 changed files with 46 additions and 13 deletions

View File

@ -101,6 +101,34 @@ public function BetterNavigatorEditLink()
(This example uses [sunnysideup/cms_edit_link_field](https://github.com/sunnysideup/silverstripe-cms_edit_link_field) to automatically find an edit link for a specified DataObject, but you can return any URL.)
## Overriding whether better navigator should be shown or not
There may be occasions when you wish to override whether better navigator should be shown at all, for example on custom data objects. To do so simply add a `BetterNavigatorShouldDisplay()` method to your Controller, e.g.:
```php
// EventController.php
/**
* Detect whether better navigator should be displayed or not
* @return bool
*/
public function BetterNavigatorShouldDisplay()
{
return $this->dataRecord
&& $this->dataRecord instanceof Event
&& $this->dataRecord->ID > 0
&& (Director::isDev() || Permission::check('CMS_ACCESS_' . EventAdmin::class));
}
```
## Overriding the permissions required for the cms edit link
By default users are required to have at least the `CMS_ACCESS_CMSMain` permission in order to see the edit link in better navigator, you can override this by setting the `better_navigator_edit_permission` configuration option on your controller to another permission code or an array of permission codes, e.g.:
```yml
My\Namespace\EventController:
better_navigator_edit_permission: "CUSTOM_PERMISSION_CODE"
better_navigator_edit_permission_mode: "any" #Optional, but can be either "any" or "all" (defaults to "all")
## Bonus: better debugging tools
This module provide quick access to Silverstripe's built in [URL Variable Tools](http://doc.silverstripe.org/framework/en/reference/urlvariabletools) but reading their output isn't much fun. You can peek under Silverstripe's hood much more conveniently using lekoala's [Silverstripe DebugBar](https://github.com/lekoala/silverstripe-debugbar)

View File

@ -17,15 +17,11 @@ use SilverStripe\Security\Security;
use SilverStripe\Versioned\Versioned;
use SilverStripe\View\ArrayData;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
class BetterNavigatorExtension extends DataExtension
{
/**
* @var bool|null
*/
private $shouldDisplay = null;
private static $better_navigator_edit_permission = 'CMS_ACCESS_CMSMain';
private static $better_navigator_edit_permission_mode = 'any';
/**
* @param $request
@ -111,10 +107,10 @@ class BetterNavigatorExtension extends DataExtension
}
}
}
// Only show edit link if user has CMS access
$editLink = null;
if($isDev || Permission::check('CMS_ACCESS_CMSMain')) {
if($isDev || Permission::check($this->owner->config()->better_navigator_edit_permission, $this->owner->config()->better_navigator_edit_permission_mode)) {
// Check for edit link override, e.g. for a DataObject
if(method_exists($this->owner, 'BetterNavigatorEditLink')) {
$editLink = $this->owner->BetterNavigatorEditLink();
@ -127,7 +123,7 @@ class BetterNavigatorExtension extends DataExtension
}
// Is the logged in member nominated as a developer?
$member = Member::currentUser();
$member = Security::getCurrentUser();
$devs = Config::inst()->get('BetterNavigator', 'developers');
$identifierField = Member::config()->unique_identifier_field;
$isDeveloper = $member && is_array($devs) ? in_array($member->{$identifierField}, $devs) : false;
@ -165,20 +161,29 @@ class BetterNavigatorExtension extends DataExtension
*/
private function shouldDisplay()
{
if ($this->shouldDisplay !== null) {
return $this->shouldDisplay;
if ($this->owner->getField('_betterNavigatorShouldDisplay') !== null) {
return $this->owner->getField('_betterNavigatorShouldDisplay');
}
if ($this->owner->hasMethod('BetterNavigatorShouldDisplay')) {
$result = $this->owner->BetterNavigatorShouldDisplay();
$this->owner->setField('_betterNavigatorShouldDisplay', $result);
return $result;
}
// Make sure this is a page
if (!$this->isAPage() || !$this->owner->showBetterNavigator()) {
return $this->shouldDisplay = false;
$this->owner->setField('_betterNavigatorShouldDisplay', false);
return false;
}
// Only show navigator to appropriate users
$isDev = Director::isDev();
$canViewDraft = (Permission::check('VIEW_DRAFT_CONTENT') || Permission::check('CMS_ACCESS_CMSMain'));
return $this->shouldDisplay = ($isDev || $canViewDraft);
$result = ($isDev || $canViewDraft);
$this->owner->setField('_betterNavigatorShouldDisplay', $result);
return $result;
}
/**