Skip to content

Commit

Permalink
FEAT: add permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
Iliain committed Nov 26, 2024
1 parent 8af0b79 commit 25ebcd5
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ You can call data from the Google settings on the frontend via `$GoogleConfig`,
<% end_with %>
```

## Permissions

You'll need to be either an Administrator, or have the `CMS_ACCESS_GoogleConfig` permission to access and edit the Google Config section in the CMS.

### GTM Scripts

You can render the GTM scripts in your template with the following:
Expand Down
2 changes: 1 addition & 1 deletion src/Admin/GoogleLeftAndMain.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class GoogleLeftAndMain extends LeftAndMain
/**
* @var array
*/
private static $required_permission_codes = array('EDIT_SITECONFIG');
private static $required_permission_codes = 'CMS_ACCESS_GoogleConfig';

/**
* Initialises the {@link GoogleConfig} controller.
Expand Down
23 changes: 20 additions & 3 deletions src/Model/GoogleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Iliain\GoogleConfig\Models;

use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\ORM\DB;
use SilverStripe\Forms\TabSet;
use SilverStripe\ORM\DataList;
Expand All @@ -19,20 +18,22 @@
use Iliain\GoogleConfig\Models\GooglePlace;
use SilverStripe\Forms\GridField\GridField;
use Iliain\GoogleConfig\Models\SchemaObject;
use SilverStripe\Security\PermissionProvider;
use SilverStripe\View\TemplateGlobalProvider;
use Iliain\GoogleConfig\Models\SchemaProperty;
use Iliain\GoogleConfig\Admin\GoogleLeftAndMain;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RecordEditor;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;

class GoogleConfig extends DataObject implements TemplateGlobalProvider
class GoogleConfig extends DataObject implements TemplateGlobalProvider, PermissionProvider
{
private static $table_name = 'GoogleConfig';

Expand Down Expand Up @@ -179,7 +180,7 @@ public function getCMSFields(): FieldList
*/
public function getCMSActions(): FieldList
{
if (Permission::check('ADMIN') || Permission::check('EDIT_SITECONFIG')) {
if (Permission::check('ADMIN') || Permission::check('CMS_ACCESS_GoogleConfig')) {
$actions = new FieldList(
FormAction::create(
'save_googleconfig',
Expand Down Expand Up @@ -295,4 +296,20 @@ public function getSchemaCode(): string

return $schemaStr;
}

public function canEdit($member = null): bool
{
return Permission::check('CMS_ACCESS_GoogleConfig', 'any', $member);
}

public function providePermissions()
{
return [
'CMS_ACCESS_GoogleConfig' => [
'name' => 'Access to \'Google\' section',
'category' => 'CMS Access',
'help' => 'Allow viewing, adding, and editing Google settings.',
]
];
}
}
21 changes: 21 additions & 0 deletions src/Model/GooglePlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\Forms\ToggleCompositeField;
use Iliain\GoogleConfig\Fields\GoogleMapField;
use SilverStripe\Security\Permission;

class GooglePlace extends DataObject
{
Expand Down Expand Up @@ -346,4 +347,24 @@ public function getBadge(): DBHTMLText
{
return $this->customise($this->getReviewData())->renderWith('Iliain\\GoogleConfig\\Models\\ReviewBadge');
}

public function canView($member = null): bool
{
return true;
}

public function canEdit($member = null): bool
{
return Permission::check('CMS_ACCESS_GoogleConfig', 'any', $member);
}

public function canCreate($member = null, $context = []): bool
{
return $this->canEdit($member);
}

public function canDelete($member = null): bool
{
return $this->canEdit($member);
}
}
25 changes: 23 additions & 2 deletions src/Model/SchemaObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\LiteralField;
use SilverStripe\Security\Permission;
use SilverStripe\Forms\GridField\GridField;
use Iliain\GoogleConfig\Models\GoogleConfig;
use SilverStripe\Forms\GridField\GridFieldConfig;
use SilverStripe\Forms\GridField\GridFieldButtonRow;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldDetailForm;
use Symbiote\GridFieldExtensions\GridFieldTitleHeader;
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
use SilverStripe\Forms\GridField\GridFieldToolbarHeader;
use SilverStripe\Forms\LiteralField;
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
use Symbiote\GridFieldExtensions\GridFieldAddNewInlineButton;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;

class SchemaObject extends DataObject
{
Expand Down Expand Up @@ -181,4 +182,24 @@ public function getSchemaCode(): string

return json_encode($schemaData, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}

public function canView($member = null): bool
{
return true;
}

public function canEdit($member = null): bool
{
return Permission::check('CMS_ACCESS_GoogleConfig', 'any', $member);
}

public function canCreate($member = null, $context = []): bool
{
return $this->canEdit($member);
}

public function canDelete($member = null): bool
{
return $this->canEdit($member);
}
}
21 changes: 21 additions & 0 deletions src/Model/SchemaProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\ORM\DataObject;
use SilverStripe\Forms\FieldList;
use SilverStripe\Security\Permission;

class SchemaProperty extends DataObject
{
Expand Down Expand Up @@ -70,4 +71,24 @@ public function getCMSFields(): FieldList

return $fields;
}

public function canView($member = null): bool
{
return true;
}

public function canEdit($member = null): bool
{
return Permission::check('CMS_ACCESS_GoogleConfig', 'any', $member);
}

public function canCreate($member = null, $context = []): bool
{
return $this->canEdit($member);
}

public function canDelete($member = null): bool
{
return $this->canEdit($member);
}
}

0 comments on commit 25ebcd5

Please sign in to comment.