mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Added support for custom permissions in configuration
This commit is contained in:
parent
5b5ef98495
commit
7574195ca2
53
system/config/permissions.yaml
Normal file
53
system/config/permissions.yaml
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
actions:
|
||||
site:
|
||||
type: access
|
||||
label: Site
|
||||
admin:
|
||||
type: access
|
||||
label: Admin
|
||||
admin.pages:
|
||||
type: access
|
||||
label: Pages
|
||||
admin.users:
|
||||
type: access
|
||||
label: User Accounts
|
||||
|
||||
types:
|
||||
default:
|
||||
type: access
|
||||
|
||||
crud:
|
||||
type: compact
|
||||
letters:
|
||||
c:
|
||||
action: create
|
||||
label: PLUGIN_ADMIN.CREATE
|
||||
r:
|
||||
action: read
|
||||
label: PLUGIN_ADMIN.READ
|
||||
u:
|
||||
action: update
|
||||
label: PLUGIN_ADMIN.UPDATE
|
||||
d:
|
||||
action: delete
|
||||
label: PLUGIN_ADMIN.DELETE
|
||||
|
||||
crudp:
|
||||
type: crud
|
||||
letters:
|
||||
p:
|
||||
action: publish
|
||||
label: PLUGIN_ADMIN.PUBLISH
|
||||
|
||||
crudl:
|
||||
type: crud
|
||||
letters:
|
||||
l:
|
||||
action: list
|
||||
label: PLUGIN_ADMIN.LIST
|
||||
|
||||
crudpl:
|
||||
type: crud
|
||||
use:
|
||||
- crudp
|
||||
- crudl
|
||||
|
|
@ -16,8 +16,9 @@ use Grav\Common\Page\Header;
|
|||
use Grav\Common\Page\Interfaces\PageInterface;
|
||||
use Grav\Common\User\DataUser;
|
||||
use Grav\Common\User\User;
|
||||
use Grav\Framework\Acl\Events\RegisterPermissionsEvent;
|
||||
use Grav\Events\RegisterPermissionsEvent;
|
||||
use Grav\Framework\Acl\Permissions;
|
||||
use Grav\Framework\Acl\PermissionsReader;
|
||||
use Grav\Framework\Flex\Flex;
|
||||
use Grav\Framework\Flex\FlexDirectory;
|
||||
use Pimple\Container;
|
||||
|
|
@ -30,7 +31,18 @@ class AccountsServiceProvider implements ServiceProviderInterface
|
|||
public function register(Container $container)
|
||||
{
|
||||
$container['permissions'] = static function (Grav $container) {
|
||||
/** @var Config $config */
|
||||
$config = $container['config'];
|
||||
|
||||
$permissions = new Permissions();
|
||||
$permissions->addTypes($config->get('permissions.types', []));
|
||||
|
||||
$array = $config->get('permissions.actions');
|
||||
if (is_array($array)) {
|
||||
$actions = PermissionsReader::fromArray($array, $permissions->getTypes());
|
||||
$permissions->addActions($actions);
|
||||
}
|
||||
|
||||
$event = new RegisterPermissionsEvent($permissions);
|
||||
$container->dispatchEvent($event);
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @package Grav\Framework\Acl
|
||||
* @package Grav\Events
|
||||
*
|
||||
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
|
||||
* @license MIT License; see LICENSE file for details.
|
||||
*/
|
||||
|
||||
namespace Grav\Framework\Acl\Events;
|
||||
namespace Grav\Events;
|
||||
|
||||
use Grav\Framework\Acl\Permissions;
|
||||
|
||||
|
|
@ -15,6 +15,8 @@ class Permissions implements \ArrayAccess, \Countable, \IteratorAggregate
|
|||
protected $instances = [];
|
||||
/** @var Action[] */
|
||||
protected $actions = [];
|
||||
/** @var array */
|
||||
protected $types = [];
|
||||
|
||||
/**
|
||||
* @return array
|
||||
|
|
@ -24,6 +26,10 @@ class Permissions implements \ArrayAccess, \Countable, \IteratorAggregate
|
|||
return $this->instances;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAction(string $name): bool
|
||||
{
|
||||
return isset($this->instances[$name]);
|
||||
|
|
@ -77,6 +83,48 @@ class Permissions implements \ArrayAccess, \Countable, \IteratorAggregate
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasType(string $name): bool
|
||||
{
|
||||
return isset($this->types[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Action|null
|
||||
*/
|
||||
public function getType(string $name): ?Action
|
||||
{
|
||||
return $this->types[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $type
|
||||
*/
|
||||
public function addType(string $name, array $type): void
|
||||
{
|
||||
$this->types[$name] = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getTypes(): array
|
||||
{
|
||||
return $this->types;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $types
|
||||
*/
|
||||
public function addTypes(array $types): void
|
||||
{
|
||||
$this->types = array_replace($this->types, $types);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|null $access
|
||||
* @return Access
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user