2017 © Pedro Peláez
 

symfony-bundle static-roles-bundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

image

becklyn/static-roles-bundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  • Thursday, February 15, 2018
  • by becklyn
  • Repository
  • 2 Watchers
  • 3 Stars
  • 705 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 2 Open issues
  • 5 Versions
  • 5 % Grown

The README.md

BecklynStaticRolesBundle

As user roles are directly coupled to the application code and we would like to configure our roles using an existing VCS (instead of the DB) this bundle implements a simple role system., (*1)

You define your roles including the hierarchy in your security.yml and the system provides ways to validated that, list them and for you to select them., (*2)

Installation

You can install it via composer:, (*3)

$ composer require becklyn/static-roles-bundle

Afterwards, you need to activate the bundle in your app/AppKernel.php:, (*4)

public function registerBundles()
{
    $bundles = array(
        // ...
        new \Becklyn\StaticRolesBundle\BecklynStaticRolesBundle(),
        // ...
    );

    // ...
}

Configuration

Open up app/config/security.yml and first remove the section role_hierarchy that is automatically provided by symfony., (*5)

Then add your own role configuration on top of the file:, (*6)

becklyn_static_roles:
    roles:
        ROLE_ADMIN:
            title: "Admin"
            included_roles: [ROLE_USER]
        ROLE_USER:
            title: "User"
            description: "The default frontend user"
            tags: [tag1, tag2]

Assigning roles to a user entity

The bundle provides a form type to be used in user forms:, (*7)

$builder
    ->add("roles", "static_role", [
        "label" => "User roles",
        "multiple" => true,
        "expanded" => true,
    ]);

You will receive an array of roles in the entity as values: ["ROLE_ADMIN", "ROLE_USER"]., (*8)

The mapping of these values can be done using the simple_array type of doctrine. You need to set it nullable to properly support a user without any roles., (*9)

class User implements UserInterface
{
    // ...

    /**
     * @var string[]
     *
     * @ORM\Column(name="roles", type="simple_array", nullable=true)
     *
     */
    private $userRoles = null;


    // ...


    /**
     * @inheritdoc
     */
    public function getRoles ()
    {
        return $this->roles;
    }



    /**
     * @param string[]|null $roles
     */
    public function setRoles (array $roles = null)
    {
        $this->roles = $roles;
    }

    // ...
}

Hidden roles

If you are using roles, that should be used internally, but shouldn't be presented in the form type, you can add hidden: true to the role definition:, (*10)

becklyn_static_roles:
    roles:
        ROLE_ADMIN:
            title: "Admin"
            included_roles: [ROLE_ALLOWED_TO_SWITCH]
        ROLE_ALLOWED_TO_SWITCH:
            title: "Internal: The user is allowed to switch roles"
            hidden: true

In this example, only ROLE_ADMIN will be selectable by the user., (*11)

Tagging

You can tag roles. This is a way to filter the visible roles in the form type. All roles that have at least one of your defined tags will be included., (*12)

$builder
    ->add("roles", "static_role", [
        "label" => "User roles",
        "multiple" => true,
        "expanded" => true,
        "roles_with_tags" => ["tag1", "tag2"], // only include roles with either "tag1" or "tag2"
    ]);
becklyn_static_roles:
    roles:
        ROLE_USER_1:
            title: "User 1"
            tags: [tag1, tag3] # will be included, as it has at least one of the defined roles
        ROLE_USER_2:
            title: "User 2"
            tags: [tag3, tag4] # will not be included, as it has none of the defined roles

If you don't define any tags in your form, all roles will be included., (*13)

$builder
    ->add("roles", "static_role", [
        "label" => "User roles",
        "multiple" => true,
        "expanded" => true,
        "roles_with_tags" => [], // includes all roles
    ]);

// This is also the default value, so you can omit it:
$builder
  ->add("roles", "static_role", [
      "label" => "User roles",
      "multiple" => true,
      "expanded" => true,
  ]);

Twig helper functions

staticRoleTitle(key)

Returns the role title by key. Returns null if the role key is not found., (*14)

Note

If you are transforming sensitive data, please keep in mind that updating the roles of the user entity won't automatically update the roles of the authenticated user token. You need to refresh this token., (*15)

You can fix this issue by adding this configuration in your app/config/security.yml:, (*16)

security:
    always_authenticate_before_granting: true

The Versions

15/02 2018

dev-master

9999999-dev https://github.com/Becklyn/BecklynStaticRolesBundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  Sources   Download

BSD-3-Clause

The Requires

 

The Development Requires

security symfony

11/04 2016

1.0.0

1.0.0.0 https://github.com/Becklyn/BecklynStaticRolesBundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  Sources   Download

BSD-3-Clause

The Requires

 

security symfony

09/07 2015

1.0.0-alpha.3

1.0.0.0-alpha3 https://github.com/Becklyn/BecklynStaticRolesBundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  Sources   Download

BSD-3-Clause

The Requires

 

security symfony

09/07 2015

1.0.0-alpha.2

1.0.0.0-alpha2 https://github.com/Becklyn/BecklynStaticRolesBundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  Sources   Download

BSD-3-Clause

The Requires

 

security symfony

09/07 2015

1.0.0-alpha.1

1.0.0.0-alpha1 https://github.com/Becklyn/BecklynStaticRolesBundle

This bundle provides a simple way to define all available roles and their hierarchy in your configuration

  Sources   Download

BSD-3-Clause

The Requires

 

security symfony