diff --git a/Checker/ActionManager.php b/Checker/ActionManager.php index d56b9b4..50cf88f 100644 --- a/Checker/ActionManager.php +++ b/Checker/ActionManager.php @@ -139,7 +139,7 @@ public function editAction(Request $request, Action $action, int $id) $process = $this->processForm($request, $entityObject); return new Data(array( "success" => $process["success"], - "redirect" => true, + "redirect" => $process["success"], "form" => $process["form"]->createView(), "flash" => $process["flash"]) ); diff --git a/Core/EntityInfo.php b/Core/EntityInfo.php index bbbda78..eff4522 100644 --- a/Core/EntityInfo.php +++ b/Core/EntityInfo.php @@ -194,8 +194,8 @@ private function computePermissionsForObject($obj, Action $currentAction = null) foreach ($this->actions as $action) { $action->object = $obj; if ($action == $currentAction) - $currentPerm = $action->isCheckAuthorize(true); - $permissions[$action->id] = $action->environment == Conf::ENV_OBJECT && $action->isCheckAuthorize(true); + $currentPerm = $action->isFullAuthorize(true); + $permissions[$action->id] = $action->environment == Conf::ENV_OBJECT && $action->isFullAuthorize(true); } return array( 'current' => $currentPerm, diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index 69e8d7d..c2c49ae 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -67,8 +67,8 @@ private function addMethodsSection(ArrayNodeDefinition $node) { $node ->children() - ->arrayNode('methods') - ->canBeUnset() + ->arrayNode('methods')->isRequired()->cannotBeEmpty() + //->canBeUnset() ->children() ->scalarNode('service')->isRequired()->cannotBeEmpty()->end() ->arrayNode('content') diff --git a/FQTDBCoreManagerBundle.php b/FQTDBCoreManagerBundle.php index 1c4a582..438bc63 100644 --- a/FQTDBCoreManagerBundle.php +++ b/FQTDBCoreManagerBundle.php @@ -4,6 +4,11 @@ use Symfony\Component\HttpKernel\Bundle\Bundle; +// TODO : Support no custom methods +// TODO : If custom support methods is define, isRequired "content" +// TODO : Test access property +// TODO : Name of entity not key but create property +// TODO : Test custom global action class FQTDBCoreManagerBundle extends Bundle { } diff --git a/README.md b/README.md index 2d596c0..3c9c026 100644 --- a/README.md +++ b/README.md @@ -1 +1,240 @@ -# FQTDBCoreManagerBundle \ No newline at end of file + +# FQT Database Core Manager + +DBCManager (DBCM) is a core that help you to implement a database manager on your website. + +It can be use with : +* [DBManagerBundle](https://github.com/hugo082/DBManagerBundle) : Implement web interface +* FQTDBRestManager (Coming soon) + +Features include: +* Action control on entity + * Default (List | Add | Edit | Remove) + * Custom +* Access control + * By roles + * Custom + +`v1.0` `15 MAI 17` + +## Installation + +### Step 1: Composer requirement + +Add repositories to your `composer.json` + + "repositories" : [ + { + "type" : "vcs", + "url" : "https://github.com/hugo082/FQTDBCoreManagerBundle.git", + "no-api": true + } + ] + +Add requirement : + + "require": { + "fqt/db-core-managerbundle": "1.0.*", + //... + }, + +Update your requirements with `composer update` command. + +### Step 2: Bundle configuration + +Enable the bundle in the kernel : + + +For example, if you want that the entity is accessible only to admin users, you can specify the `access` config + + DisplayName: + access: ROLE_ADMIN + #... + +You can also defined multi-roles : + + DisplayName: + access: [ ROLE_ADMIN, ROLE_REDACTOR ] + #... + +If you want that users can list and so access to entity information but admins can execute actions on this entity, you +you can defined the parameter `access_details`. This parameter **must** defined roles for all actions : + + DisplayName: + #... + access_details: + - { method: list, roles: [ ROLE_REDACTOR, ROLE_ADMIN ]} + - { method: edit, roles: [ ROLE_ADMIN ]} + - { method: remove, roles: [ ROLE_SUPER_ADMIN ]} + +**WARNING** : if you defined the access_details property, this parameter override access +and so access is no longer taken into consideration.
+ + +#### Custom constraints + +You can implement an actionMethod to process a custom constraint. Your method will call for each entity and must return +a boolean (`true` to allow access and `false` to prevent). + + access_details: + - { method: myCustomAction, check: myCustomCheckMethod } + #... + +This method is call on the service specified of your custom action (more information below). + +When DBM list your entity, you can also choose your method repository. By default, DBManager use `findAll()` but you can +override this easily : + + Flight: + fullName: AppBundle:Flight + listingMethod: myRepositoryMethod + + +#### Events + +For add, edit and remove actions, events are called. You can listen them and execute a custom process : + + + class ActionSubscriber implements EventSubscriberInterface { + + //... + + public static function getSubscribedEvents() { + return array( + DBManagerEvents::ACTION_REMOVE_BEFORE => 'beforeRemove', + //... + ); + } + + public function beforeRemove(ActionEvent $event) { + $e = $event->getEntityObject(); + if ($e instanceof Flight) { + if ($e->getId() == 13) { + $event->setExecuted(true); // DBM default action ignored + $event->setFlash('ERROR', 'You want remove VIP Flight'); + } else + $event->setFlash('SUCCESS', 'Your Flight have been removed'); + } + } + } + +At the end of your process, if `executed` property of event are set to true, DBCM will ignore the default action. By default, +the `executed` property is set to false.
+Of course you must register your subscriber in your services. + +### Actions + +#### Default actions + +By default, DBCM implement 4 actions (`list`, `add`, `edit`, `remove`). This methods can't be overrided and so you can't +specify a custom check method. However, you can implement roles access. + +#### Custom actions + +All custom actions mus be defined in `methods.content` property of configuration file. + + methods: + service: 'my.processor' + content: + methodID: + method: 'multipleBillMargin' + environment: 'object' # object | global + fullName: 'Method Name' # Optionnal + service: 'my.other.processor' # Optionnal + +For each action, you must define `method` property. This property define the method will be call to execute the action with +object in parameter (if environment is `object`) or null (if environment is `global`). +This method must return a `Data` object (more information below). + +For each action, you must define the `environment` property. This property is used to define if action is applicable to an +entity object (like `edit`) or global (like `add`). + +By default, DBCM call method of your action in `methods.service` but you can override this service for each action with +property `service`. + +By default, DBCM name your method `methodID` but you can override this name with `fullName` property. + +#### Data object + +Data object is an object of class `FQT\DBCoreManagerBundle\Core\Data`. You can init this class with an array parameter. +This object is a parameter in result of execution and is accessible in your template. + +You can add customs parameters and defaults parameters. Defaults parameters are used by DBCM to execute actions. + +For example, you can indicate if action have succeed, add flash message, and redirect result. You can also send custom parameter +like a form. + + return new Data(array( + "success" => true, + "redirect" => true, + "form" => $form->createView(), + "flash" => array( + array("type" => 'success', "message" => 'Marge multiplié par ' . $data['value']) + )) + ); + +The redirect parameter can be a boolean or an array for more precision : + + "redirect" => array( + "route_name" => "my.route.name", + "data" => array() + ) \ No newline at end of file diff --git a/composer.json b/composer.json index a84e6db..6506e44 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "Database manager", "Core manager" ], "require" : { - "php" : ">=5.6.0", + "php" : ">=7.0.0", "symfony/framework-bundle" : ">=3.0", "twig/twig" : "*", "doctrine/doctrine-bundle" : "*"