siganushka / rbac-bundle
RBAC bundle for symfony
dev-main
2021-05-13 02:57 UTC
Requires
- doctrine/orm: ^2.7
- symfony/form: ^3.4|^4.0|^5.0
- symfony/framework-bundle: ^3.4|^4.0|^5.0
- symfony/options-resolver: ^3.4|^4.0|^5.0
- symfony/security-bundle: ^3.4|^4.0|^5.0
README
尝试性项目,请勿在生产环境中使用。
DEMO
step 1
install bundle
$ composer require siganushka/rbac-bundle:dev-master
step 2
register bundle for kernel
// ./src/bundles.php <?php return [ // ... Siganushka\RBACBundle\SiganushkaRBACBundle::class => ['all' => true], ];
step 3
configure the bundle
// ./config/packages/framework.yaml // ... siganushka_rbac: // firewall pattern firewall_pattern: ^/admin // fully routes (include IS_AUTHENTICATED_FULLY to access) fully_routes: - admin_index - admin_ajax_xxx - admin_file_upload // anonymously routes (include IS_AUTHENTICATED_ANONYMOUSLY to access, e.g. login page) anonymously_routes: - admin_login
Usage
Entity
<?php namespace App\Entity; use Siganushka\RBACBundle\Model\RoleableTrait; use Siganushka\RBACBundle\Model\RoleableInterface; /** * @ORM\Entity */ class Role implements RoleableInterface { use RoleableTrait; // ... }
Form
<?php namespace App\Form; use App\Entity\Role; use Siganushka\RBACBundle\Form\Type\NodeType; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; class RoleType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder // ... ->add('nodes', NodeType::class) ; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults([ 'data_class' => Role::class, ]); } }
check permissions in controller
<?php namespace App\Controller; use Symfony\Component\Routing\Annotation\Route; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController class PostController extends AbstractController { /** * @Route("/post/new", name="post_new") */ public function new() { if ($this->isGranted('post_new')) { // ... } } }
check permissions in twig
{% if is_granted('post_new') %} <a href="{{ path('post_new') }}">Add New</a> {% endif %}
extras
Form fields
// ./config/packages/twig.yaml twig: // ... form_themes: // ... - rbac_fields.html.twig
Translations
// ./translations/siganushka_rbac.zh_CN.xlf <?xml version="1.0" encoding="utf-8"?> <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" version="1.2"> <file source-language="zh-CN" target-language="zh-CN" datatype="plaintext" original="file.ext"> <header> <tool tool-id="symfony" tool-name="Symfony"/> </header> <body> <trans-unit id="post_list"> <source>post_list</source> <target>Post / View Posts</target> </trans-unit> <trans-unit id="post_new"> <source>post_new</source> <target>Post / Add New</target> </trans-unit> <trans-unit id="post_edit"> <source>post_edit</source> <target>Post / Edit</target> </trans-unit> // ... </body> </file> </xliff>
update translation files
$ php bin/console translation:update zh_CN --force