{"id":15296384,"url":"https://github.com/alexyaframework/roles","last_synced_at":"2026-02-11T22:52:32.042Z","repository":{"id":56944938,"uuid":"93307712","full_name":"AlexyaFramework/Roles","owner":"AlexyaFramework","description":"Alexya's Role Based Access System utilities","archived":false,"fork":false,"pushed_at":"2017-06-06T20:32:38.000Z","size":24,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-27T11:48:04.615Z","etag":null,"topics":["library","php","php-framework","php-library","php7","php71","rbac","role","roles"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlexyaFramework.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":null,"security":null,"support":null}},"created_at":"2017-06-04T11:01:50.000Z","updated_at":"2022-02-12T14:13:20.000Z","dependencies_parsed_at":"2022-08-21T07:20:19.645Z","dependency_job_id":null,"html_url":"https://github.com/AlexyaFramework/Roles","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/AlexyaFramework/Roles","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexyaFramework%2FRoles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexyaFramework%2FRoles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexyaFramework%2FRoles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexyaFramework%2FRoles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlexyaFramework","download_url":"https://codeload.github.com/AlexyaFramework/Roles/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlexyaFramework%2FRoles/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":274289534,"owners_count":25257979,"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","status":"online","status_checked_at":"2025-09-09T02:00:10.223Z","response_time":80,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["library","php","php-framework","php-library","php7","php71","rbac","role","roles"],"created_at":"2024-09-30T18:10:20.036Z","updated_at":"2026-02-11T22:52:32.002Z","avatar_url":"https://github.com/AlexyaFramework.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Roles\n=====\n\nAlexya's Role Based Access System utilities.\n\nContents\n--------\n - [Creating permissions](#creating_permissions)\n - [Creating roles](#creating_roles)\n - [Adding permissions to roles](#adding_permissions_to_roles)\n - [Creating users](#creating_users)\n \n\u003ca name=\"creating_permissions\"\u003e\u003c/a\u003e\nCreating permissions\n--------------------\nPermissions are the way to authorized users to perform certain actions.\n\nThe class `\\Alexya\\Roles\\Permission` represents a permission that can be assigned to a specific role.\n\nA permission consists of an identifier, a title and a status flag.\n   \nYou can easily extend this class to provide more functionality such as alternative names, ranks...\n\nFor example, in a file system, each user should have permissions to read/write to certain files:\n\n```php\n\u003c?php\nnamespace Application\\RBAC\\Permissions;\n\nuse Alexya\\Roles\\Permission;\n\nclass Read extends Permission\n{\n    /**\n     * Constructor.\n     */\n    public function __construct()\n    {\n        parent::__construct(1, \"read\", Permission::STATUS_INHERITED);\n    }\n}\n\nclass Write extends Permission\n{\n    /**\n     * Constructor.\n     */\n    public function __construct()\n    {\n        parent::__construct(2, \"write\", Permission::STATUS_INHERITED);\n    }\n}\n```\n\n\u003ca name=\"creating_roles\"\u003e\u003c/a\u003e\nCreating roles\n--------------\n\nRoles are the containers of various permissions.\n\nThe class `\\Alexya\\Roles\\Role` represents a role by itself.\n\nIt has an identifier for the role, a title and an array of `Permission` with the granted permissions for this role.\nAlternatively, a role can have a parent role for inheritance.\n\nIt also has an integer representing the priority of the role, the bigger the number, the most priority it has.\n\nThe method `hasPermission` accepts an identifier of a permission or an instance of the permission and checks that this role has been granted with the permission.\n\nFor a shorter version, you can use `can`, 'cuz shorter \u003e longer.\n\nExample:\n\n```php\nnamespace Application\\RBAC\\Roles;\n\nuse Alexya\\Roles\\Role;\n\nclass CanRead extends Role\n{\n    /**\n     * Constructor\n     */\n    public function __construct()\n    {\n        parent::__construct(1, \"can_read\", 1);\n    }\n}\n\nclass CanWrite extends Role\n{\n    /**\n     * Constructor\n     */\n    public function __construct()\n    {\n        parent::__construct(2, \"can_write\", 1);\n    }\n}\n\nclass CanReadAndWrite extends Role\n{\n    /**\n     * Constructor.\n     */\n    public function __construct()\n    {\n        parent::__construct(3, \"can_read_and_write\", 2);\n    }\n}\n```\n\n\u003ca name=\"adding_permissions_to_roles\"\u003e\u003c/a\u003e\nAdding permissions to roles\n---------------------------\n\nOnce you have the permissions and roles you need to specify which roles have granted which permissions.\n\nA permission can be enabled, disabled or inherited. If a permission is inherited the role will check the status\nof that permission in the parent role, if there's no parent role it will be treated as disabled.\n\nAdding permissions to roles is as easy as calling the `addPermission` method with the permission to add.\n\nExample:\n```php\n$canRead = new CanRead();\n$canRead-\u003eaddPermission(new Read());\n\n$canWrite = new CanWrite();\n$canWrite-\u003eaddPermission(new Write());\n\n$canReadAndWrite = new CanReadAndWrite();\n$canReadAndWrite-\u003eaddPermission(new Read());\n$canReadAndWrite-\u003eaddPermission(new Write());\n\n// Alternatively you could have set the parent role like this:\n// $canReadAndWrite = new CanReadAndWrite();\n// $canReadAndWrite-\u003eparent = $canRead;\n// $canReadAndWrite-\u003eaddPermission(new Write());\n```\n\nTo check if a role has granted a certain permission you can use the `hasPermission` or `can` methods:\n\n```php\n$canRead-\u003ecan(\"read\"); // true\n$canRead-\u003ecan(2); // false (Write permission has ID 2)\n$canRead-\u003ecan(new Read()); // true\n```\n\nThe method `getPermission` returns a permission from the role:\n\n```php\n$read  = $canRead-\u003egetPermission(new Read()); // The Read permission sent through `addPermission`\n$write = $canRead-\u003egetPermission(\"write\");    // null;\n```\n\n\u003ca name=\"creating_users\"\u003e\u003c/a\u003e\nCreating users\n--------------\n\nNow that we have the roles and permissions we need users to assign them.\n\nThe class `\\Alexya\\Roles\\User` represents an user, where the roles are assigned.\n\nIt's the class that you should extend in order to add roles to your users as it provides the `hasPermission` and `can` methods for checking if this user has any permission granted.\n\nIt also has the methods `addRole` and `removeRole` to add/remove roles.\n\nExample:\n\n```php\n$user = new User();\n\n$user-\u003eaddRole(2);\n\n$user-\u003ecan(new Read());  // false\n$user-\u003ecan(new Write()); // true\n\n$user-\u003eaddRole(new CanRead());\n\n$user-\u003ecan(\"read\");  // true\n$user-\u003ecan(new Write()); // true\n\n$user-\u003egetRole(\"can_write\")\n     -\u003egetPermission(\"write\")\n     -\u003estatus = Permission::STATUS_DISABLED;\n\n$user-\u003ecan(new Read());  // true\n$user-\u003ecan(new Write()); // false\n\n$user-\u003eaddRole(new CanReadAndWrite());\n\n$user-\u003ecan(new Read());  // true\n$user-\u003ecan(new Write()); // true because the priority of `CanReadAndWrite` is bigger than the `CanWrite` priority.\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexyaframework%2Froles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexyaframework%2Froles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexyaframework%2Froles/lists"}