Improved flexibility around enabling better navigator and edit link (#56)

* Added ability for extension to be applied to non-SiteTree objects by moving the `isAPage()` check in to the `showBetterNavigator()` method, which can be overidden.
* Added ability for edit link to be enabled via a permission code check, configurable on a per-controller basis.
* Switched to using getField/setField for caching whether better navigator should be displayed or not.
* Adjusted readme around using custom logic to control the display of the navigator.
This commit is contained in:
Ed Chipman 2022-07-14 17:08:08 -03:00 committed by GitHub
parent 92416eceef
commit 507632fd39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 18 deletions

View File

@ -33,10 +33,10 @@ The navigator is auto-injected into your template, and no code changes are neede
If your website uses caching, make sure BetterNavigator's output is excluded. If your website uses caching, make sure BetterNavigator's output is excluded.
## Disabling the navigator ## Custom navigator display logic
You can disable the navigator using your own custom logic by defining a `showBetterNavigator(): bool` You can customise the navigator display logic using your own custom logic by defining a `showBetterNavigator(): bool`
method in any controller with the extension applied. method in any controller with the extension applied. By default the navigator will only show on controllers that have a `dataRecord` property that is an instance of `SilverStripe\CMS\Model\SiteTree`.
```php ```php
public function showBetterNavigator() public function showBetterNavigator()
@ -101,6 +101,15 @@ 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.) (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 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 ## 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) 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\Versioned\Versioned;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\Requirements;
class BetterNavigatorExtension extends DataExtension class BetterNavigatorExtension extends DataExtension
{ {
private static $better_navigator_edit_permission = 'CMS_ACCESS_CMSMain';
/** private static $better_navigator_edit_permission_mode = 'any';
* @var bool|null
*/
private $shouldDisplay = null;
/** /**
* @param $request * @param $request
@ -70,7 +66,7 @@ class BetterNavigatorExtension extends DataExtension
*/ */
public function showBetterNavigator() public function showBetterNavigator()
{ {
return true; return $this->isAPage();
} }
/** /**
@ -111,10 +107,10 @@ class BetterNavigatorExtension extends DataExtension
} }
} }
} }
// Only show edit link if user has CMS access // Only show edit link if user has CMS access
$editLink = null; $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 // Check for edit link override, e.g. for a DataObject
if(method_exists($this->owner, 'BetterNavigatorEditLink')) { if(method_exists($this->owner, 'BetterNavigatorEditLink')) {
$editLink = $this->owner->BetterNavigatorEditLink(); $editLink = $this->owner->BetterNavigatorEditLink();
@ -127,7 +123,7 @@ class BetterNavigatorExtension extends DataExtension
} }
// Is the logged in member nominated as a developer? // Is the logged in member nominated as a developer?
$member = Member::currentUser(); $member = Security::getCurrentUser();
$devs = Config::inst()->get('BetterNavigator', 'developers'); $devs = Config::inst()->get('BetterNavigator', 'developers');
$identifierField = Member::config()->unique_identifier_field; $identifierField = Member::config()->unique_identifier_field;
$isDeveloper = $member && is_array($devs) ? in_array($member->{$identifierField}, $devs) : false; $isDeveloper = $member && is_array($devs) ? in_array($member->{$identifierField}, $devs) : false;
@ -165,20 +161,23 @@ class BetterNavigatorExtension extends DataExtension
*/ */
private function shouldDisplay() private function shouldDisplay()
{ {
if ($this->shouldDisplay !== null) { if ($this->owner->getField('_betterNavigatorShouldDisplay') !== null) {
return $this->shouldDisplay; return $this->owner->getField('_betterNavigatorShouldDisplay');
} }
// Make sure this is a page // Make sure this is a page
if (!$this->isAPage() || !$this->owner->showBetterNavigator()) { if (!$this->owner->showBetterNavigator()) {
return $this->shouldDisplay = false; $this->owner->setField('_betterNavigatorShouldDisplay', false);
return false;
} }
// Only show navigator to appropriate users // Only show navigator to appropriate users
$isDev = Director::isDev(); $isDev = Director::isDev();
$canViewDraft = (Permission::check('VIEW_DRAFT_CONTENT') || Permission::check('CMS_ACCESS_CMSMain')); $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;
} }
/** /**