{"id":20253243,"url":"https://github.com/becklyn/becklynstaticrolesbundle","last_synced_at":"2026-03-09T19:38:26.705Z","repository":{"id":34789503,"uuid":"38773254","full_name":"Becklyn/BecklynStaticRolesBundle","owner":"Becklyn","description":"Config-based user role management for Symfony","archived":false,"fork":false,"pushed_at":"2023-01-12T15:22:26.000Z","size":64,"stargazers_count":4,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T19:50:57.614Z","etag":null,"topics":["php","roles","security","symfony","symfony-bundle"],"latest_commit_sha":null,"homepage":"https://github.com/Becklyn/BecklynStaticRolesBundle","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Becklyn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":".github/CODEOWNERS","security":null,"support":null}},"created_at":"2015-07-08T18:47:32.000Z","updated_at":"2022-12-12T13:01:49.000Z","dependencies_parsed_at":"2023-01-15T09:11:48.847Z","dependency_job_id":null,"html_url":"https://github.com/Becklyn/BecklynStaticRolesBundle","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Becklyn%2FBecklynStaticRolesBundle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Becklyn%2FBecklynStaticRolesBundle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Becklyn%2FBecklynStaticRolesBundle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Becklyn%2FBecklynStaticRolesBundle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Becklyn","download_url":"https://codeload.github.com/Becklyn/BecklynStaticRolesBundle/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317347,"owners_count":21083519,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["php","roles","security","symfony","symfony-bundle"],"created_at":"2024-11-14T10:22:39.204Z","updated_at":"2026-03-09T19:38:26.658Z","avatar_url":"https://github.com/Becklyn.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"BecklynStaticRolesBundle\n========================\n\nAs 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.\n\nYou 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.\n\n\nInstallation\n------------\n\nYou can install it via composer:\n\n```bash\n$ composer require becklyn/static-roles-bundle\n```\n\nAfterwards, you need to activate the bundle in your `app/AppKernel.php`:\n\n\n```php\npublic function registerBundles()\n{\n    $bundles = array(\n        // ...\n        new \\Becklyn\\StaticRolesBundle\\BecklynStaticRolesBundle(),\n        // ...\n    );\n\n    // ...\n}\n```\n\n\n\nConfiguration\n-------------\n\nOpen up `app/config/security.yml` and first remove the section `role_hierarchy` that is automatically provided by symfony.\n\nThen add your own role configuration on top of the file:\n\n```yml\nbecklyn_static_roles:\n    roles:\n        ROLE_ADMIN:\n            title: \"Admin\"\n            included_roles: [ROLE_USER]\n        ROLE_USER:\n            title: \"User\"\n            description: \"The default frontend user\"\n            tags: [tag1, tag2]\n```\n\n\nAssigning roles to a user entity\n--------------------------------\n\nThe bundle provides a form type to be used in user forms:\n\n```php\n$builder\n    -\u003eadd(\"roles\", \"static_role\", [\n        \"label\" =\u003e \"User roles\",\n        \"multiple\" =\u003e true,\n        \"expanded\" =\u003e true,\n    ]);\n```\n\nYou will receive an array of roles in the entity as values: `[\"ROLE_ADMIN\", \"ROLE_USER\"]`.\n\nThe mapping of these values can be done using the [`simple_array`][doctrine:simple-type] type of doctrine. You need to set it nullable to properly support a user without any roles.\n\n\n```php\nclass User implements UserInterface\n{\n    // ...\n\n    /**\n     * @var string[]\n     *\n     * @ORM\\Column(name=\"roles\", type=\"simple_array\", nullable=true)\n     *\n     */\n    private $userRoles = null;\n\n\n    // ...\n\n\n    /**\n     * @inheritdoc\n     */\n    public function getRoles ()\n    {\n        return $this-\u003eroles;\n    }\n\n\n\n    /**\n     * @param string[]|null $roles\n     */\n    public function setRoles (array $roles = null)\n    {\n        $this-\u003eroles = $roles;\n    }\n\n    // ...\n}\n\n```\n\n\nHidden roles\n------------\n\nIf 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:\n\n```yml\nbecklyn_static_roles:\n    roles:\n        ROLE_ADMIN:\n            title: \"Admin\"\n            included_roles: [ROLE_ALLOWED_TO_SWITCH]\n        ROLE_ALLOWED_TO_SWITCH:\n            title: \"Internal: The user is allowed to switch roles\"\n            hidden: true\n```\n\nIn this example, only `ROLE_ADMIN` will be selectable by the user.\n\n\nTagging\n-------\n\nYou 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.\n\n\n```php\n$builder\n    -\u003eadd(\"roles\", \"static_role\", [\n        \"label\" =\u003e \"User roles\",\n        \"multiple\" =\u003e true,\n        \"expanded\" =\u003e true,\n        \"roles_with_tags\" =\u003e [\"tag1\", \"tag2\"], // only include roles with either \"tag1\" or \"tag2\"\n    ]);\n```\n\n```yml\nbecklyn_static_roles:\n    roles:\n        ROLE_USER_1:\n            title: \"User 1\"\n            tags: [tag1, tag3] # will be included, as it has at least one of the defined roles\n        ROLE_USER_2:\n            title: \"User 2\"\n            tags: [tag3, tag4] # will not be included, as it has none of the defined roles\n```\n\nIf you don't define any tags in your form, all roles will be included.\n\n```php\n$builder\n    -\u003eadd(\"roles\", \"static_role\", [\n        \"label\" =\u003e \"User roles\",\n        \"multiple\" =\u003e true,\n        \"expanded\" =\u003e true,\n        \"roles_with_tags\" =\u003e [], // includes all roles\n    ]);\n\n// This is also the default value, so you can omit it:\n$builder\n  -\u003eadd(\"roles\", \"static_role\", [\n      \"label\" =\u003e \"User roles\",\n      \"multiple\" =\u003e true,\n      \"expanded\" =\u003e true,\n  ]);\n```\n\n\nTwig helper functions\n---------------------\n\n```twig\nstaticRoleTitle(key)\n```\n\nReturns the role title by key. Returns null if the role key is not found.\n\n\nNote\n----\n\nIf 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.\nYou need to refresh this token.\n\nYou can fix this issue by adding this configuration in your `app/config/security.yml`:\n\n```yml\nsecurity:\n    always_authenticate_before_granting: true\n```\n\n\n\n[doctrine:simple-type]: http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html#simple-array\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbecklyn%2Fbecklynstaticrolesbundle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbecklyn%2Fbecklynstaticrolesbundle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbecklyn%2Fbecklynstaticrolesbundle/lists"}