Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
Doc + minor bug + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hugo082 committed May 17, 2017
1 parent 336a0b8 commit 20dd75e
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Checker/ActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
);
Expand Down
4 changes: 2 additions & 2 deletions Core/EntityInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
5 changes: 5 additions & 0 deletions FQTDBCoreManagerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
241 changes: 240 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,240 @@
# FQTDBCoreManagerBundle

# 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 :

<?php
// app/AppKernel.php

public function registerBundles()
{
$bundles = array(
// ...
new FQT\DBCoreManagerBundle\FQTDBCoreManagerBundle()
);
}

Set up your `config.yml` :

fqtdb_core_manager:
methods:
service: 'my.custom.processor.db.action'

## About

DBManagerBundle is a FOUQUET initiative.
See also the [creator](https://github.com/hugo082).

## License

This bundle is under the MIT license. See the complete license [in the bundle](LICENSE)

## Documentation

### Add an entity

DBCM load your entities with your configuration file. You can specify an entity to follow by adding it in your config.yml

fqtdb_core_manager:
entities:
DisplayName:
fullName: YourBundle:RealName
methods: [ list, add, edit, remove ]

You can configure different actions on each entity :

DisplayName:
fullName: YourBundle:RealName
methods: [ action1, action2 ]
formType: MyCustomFormType # Optional
fullFormType: AnotherBundle\Form\MyCustomFormType # Optional

By default, DBCM load your entity in `YourBundle\Entity\RealName`, name the form with `RealNameType` and load your form type in
`YourBundle\Form\FormType` (so `YourBundle\Form\RealNameType`)
- `DisplayName` is used by DBM for display on template and in url, you can enter the same name of RealName.
- `methods` are actions that user can be execute on your entity. You have 4 default actions :
- `list`
- `add`
- `edit`
- `remove`

### Entity access

#### Role access

You can setup, for each entity, roles that are necessary to execute specific action or access to a specific information.<br>
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 ]}

<span style="color:#FFC107">**WARNING** :</span> if you defined the access_details property, this parameter override access
and so access is no longer taken into consideration.<br>


#### 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.<br>
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()
)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" : "*"
Expand Down

0 comments on commit 20dd75e

Please sign in to comment.