Improved Flex Users: obey blueprints and allow Flex to be used in admin only

This commit is contained in:
Matias Griese
2019-10-09 15:29:09 +03:00
parent 9beefb3162
commit b561c2b47c
5 changed files with 82 additions and 74 deletions

View File

@@ -2,7 +2,9 @@
## mm/dd/2019
1. [](#new)
* Added Flex Pages to Grav core and removed Flex Objects plugin dependency
* Added `Flex Pages` to Grav core and removed Flex Objects plugin dependency
1. [](#improved)
* Improved `Flex Users`: obey blueprints and allow Flex to be used in admin only
1. [](#bugfix)
* Fixed `Page::untranslatedLanguages()` not being symmetrical to `Page::translatedLanguages()`
* Fixed `Flex Pages` not calling `onPageProcessed` event when cached

View File

@@ -0,0 +1,64 @@
title: Flex Accounts
description: Manage your User Accounts in Flex.
type: flex-objects
extends@:
type: account
context: blueprints://user
config:
admin:
menu:
list:
route: '/accounts'
title: PLUGIN_ADMIN.ACCOUNTS
icon: fa-users
authorize: ['admin.users', 'admin.accounts', 'admin.super']
priority: 6
template: grav-accounts
list:
fields:
username:
link: edit
search: true
email:
search: true
fullname:
search: true
options:
per_page: 20
order:
by: username
dir: asc
data:
object: 'Grav\Common\User\FlexUser\User'
collection: 'Grav\Common\User\FlexUser\UserCollection'
index: 'Grav\Common\User\FlexUser\UserIndex'
storage:
class: 'Grav\Common\User\FlexUser\Storage\UserFileStorage'
options:
formatter:
class: 'Grav\Framework\File\Formatter\YamlFormatter'
folder: 'account://'
pattern: '{FOLDER}/{KEY}{EXT}'
key: storage_key
indexed: true
search:
options:
contains: 1
fields:
- key
- email
form:
fields:
username:
flex-disabled@: exists
disabled: false
flex-readonly@: exists
readonly: false
validate:
required: true

View File

@@ -1,40 +0,0 @@
title: User Accounts
description: User Accounts
type: flex-objects
extends@: 'user/account'
config:
admin:
list:
fields:
username:
link: edit
search: true
email:
search: true
fullname:
search: true
options:
per_page: 20
order:
by: username
dir: asc
menu:
list:
route: '/accounts'
title: Accounts
icon: fa-users
authorize: ['admin.users', 'admin.accounts', 'admin.super']
priority: 6
form:
fields:
username:
flex-disabled@: exists
disabled: false
flex-readonly@: exists
readonly: false
validate:
required: true

View File

@@ -14,7 +14,6 @@ use Grav\Common\Debugger;
use Grav\Common\User\DataUser;
use Grav\Common\User\FlexUser;
use Grav\Common\User\User;
use Grav\Framework\File\Formatter\YamlFormatter;
use Grav\Framework\Flex\Flex;
use Grav\Framework\Flex\FlexDirectory;
use Pimple\Container;
@@ -67,27 +66,15 @@ class AccountsServiceProvider implements ServiceProviderInterface
$options = [
'enabled' => true,
'data' => [
'object' => User::class, // Use User class for backwards compatibility.
'collection' => FlexUser\UserCollection::class,
'index' => FlexUser\UserIndex::class,
'storage' => $this->getFlexStorage($config->get('system.accounts.storage', 'file')),
'search' => [
'options' => [
'contains' => 1
],
'fields' => [
'key',
'email'
]
]
]
] + ($config->get('plugins.flex-objects.object') ?: []);
$directory = new FlexDirectory('accounts', 'blueprints://user/accounts.yaml', $options);
$directory = new FlexDirectory('accounts', 'blueprints://flex/accounts.yaml', $options);
/** @var EventDispatcher $dispatcher */
$dispatcher = $container['events'];
$dispatcher->addListener('onFlexInit', function (Event $event) use ($directory) {
$dispatcher->addListener('onFlexInit', static function (Event $event) use ($directory) {
/** @var Flex $flex */
$flex = $event['flex'];
$flex->addDirectory($directory);
@@ -106,25 +93,13 @@ class AccountsServiceProvider implements ServiceProviderInterface
return [
'class' => FlexUser\Storage\UserFolderStorage::class,
'options' => [
'formatter' => ['class' => YamlFormatter::class],
'folder' => 'account://',
'file' => 'user',
'pattern' => '{FOLDER}/{KEY:2}/{KEY}/{FILE}{EXT}',
'key' => 'username',
'indexed' => true
],
];
}
return [
'class' => FlexUser\Storage\UserFileStorage::class,
'options' => [
'formatter' => ['class' => YamlFormatter::class],
'folder' => 'account://',
'pattern' => '{FOLDER}/{KEY}{EXT}',
'key' => 'storage_key',
'indexed' => true
],
];
return [];
}
}

View File

@@ -23,6 +23,7 @@ use Grav\Framework\File\Formatter\JsonFormatter;
use Grav\Framework\File\Formatter\YamlFormatter;
use Grav\Framework\Flex\FlexDirectory;
use Grav\Framework\Flex\FlexObject;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
use Grav\Framework\Flex\Storage\FileStorage;
use Grav\Framework\Flex\Traits\FlexAuthorizeTrait;
use Grav\Framework\Flex\Traits\FlexMediaTrait;
@@ -739,7 +740,7 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa
protected function doSerialize(): array
{
return [
'type' => 'accounts',
'type' => $this->getFlexType(),
'key' => $this->getKey(),
'elements' => $this->jsonSerialize(),
'storage' => $this->getStorage()
@@ -752,13 +753,19 @@ class User extends FlexObject implements UserInterface, MediaManipulationInterfa
protected function doUnserialize(array $serialized): void
{
$grav = Grav::instance();
$flex = $grav['flex_objects'] ?? null;
/** @var UserCollection $accounts */
$accounts = $grav['accounts'];
// Use Flex plugin if possible -- fixes issues if admin has flex users admin, but it is not used in the session.
if ($flex) {
$directory = $flex->getDirectory($serialized['type']);
} else {
/** @var UserCollection $accounts */
$accounts = $grav['accounts'];
$directory = $accounts instanceof FlexCollectionInterface ? $accounts->getFlexDirectory() : null;
}
$directory = $accounts->getFlexDirectory();
if (!$directory) {
throw new \InvalidArgumentException('Internal error');
throw new \RuntimeException('Internal error, please clear cache');
}
$this->setFlexDirectory($directory);