{"id":18212006,"url":"https://github.com/bedus-creation/role-permission","last_synced_at":"2025-04-02T18:32:00.299Z","repository":{"id":42026419,"uuid":"239930555","full_name":"bedus-creation/role-permission","owner":"bedus-creation","description":"Laravel Role Permission package.","archived":false,"fork":false,"pushed_at":"2024-02-09T20:38:45.000Z","size":8733,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-17T11:50:28.654Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bedus-creation.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-02-12T04:57:45.000Z","updated_at":"2023-10-08T03:12:47.000Z","dependencies_parsed_at":"2024-02-09T21:42:56.752Z","dependency_job_id":null,"html_url":"https://github.com/bedus-creation/role-permission","commit_stats":{"total_commits":27,"total_committers":2,"mean_commits":13.5,"dds":0.03703703703703709,"last_synced_commit":"dc0bcc855b21c0ba4db056146ddb2af2bd19d8d9"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Frole-permission","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Frole-permission/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Frole-permission/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bedus-creation%2Frole-permission/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bedus-creation","download_url":"https://codeload.github.com/bedus-creation/role-permission/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246870010,"owners_count":20847226,"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":[],"created_at":"2024-11-03T15:04:23.248Z","updated_at":"2025-04-02T18:32:00.013Z","avatar_url":"https://github.com/bedus-creation.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Introduction and Core Concepts\n\n\n### Content\n1. [Introduction](https://github.com/bedus-creation/role-permission#introduction)\n2. [Installation](https://github.com/bedus-creation/role-permission#installation)\n\n\n## Introduction \n### Role\nRole can be used to define a group of permission. If a user has a editor role, he/she can edit, delete, publish articles. I prefer to use role in most of the cases to allow a group of action.\n```\n$user-\u003eaddRole('admin');\n```  \n* Roles are **case insensetive**. ```$user-\u003eaddRole('admin');``` and ```$user-\u003eaddRole('Admin');``` has same meaning.\n* Roles not need to create explicitly. ```$user-\u003eaddRole('admin');``` This function creats a new role **admin** if the given role is not created yet in the database, and then the given role is assign to the given user.  \n#### To add permission to role\n```\n$role-\u003eaddPermission('read article');\n// or \n$role-\u003eaddPermission(['read article','update article']);\n```\nTo attach a CRUD of permission to Role.\n```\n$role-\u003eaddResourcePermission('article');\n```\nIt will add create article, read article, update article, delete article permission to the given role. \n\n### Permission\nPermission can be used to **deny** a particular action. I assume in most of the cases the actions are associated with roles. So, if read, write, delete article action is associated with a Editor role, then you can deny Editor to delete article by:\n```  \n$user-\u003eremovePermission('delete article');\n```\n### Middleware\nUse either permission or role as a middleware to protect the resources. Use `|` to use multiple role or permission in a  middleware. If both role and permission middleware are defined both middleware should passed to access the resources. Here, you can deny to publish a article even he has got a editor role.\n##### Add middlewire in the route middlewire section. ```App\\Http\\Kernel.php```\n```\n    protected $routeMiddleware = [\n        'role' =\u003e \\Aammui\\RolePermission\\Middleware\\Role::class,\n    ]\n```\n##### Use Middlewire in anywhere\n```\nRoute::group(['middleware' =\u003e ['role:system admin|database admin','permission:read article']], function () {\n    //\n});\n```\nabove can interpret as user should have sytem admin or database admin role **and** read article permission **is not** denied.\n\n## Installation\n```\ncomposer require aammui/role-permission\n```\n\n##### Laravel Compatibility \n| Laravel Version | Role Permission Version | Installation |\n|-----------------|-------------------------| --- |\n| 9.x             | 3.0.0 | ```composer require aammui/role-permission:3.0.0``` |\n| 8.x             | 2.0.0 | ```composer require aammui/role-permission:2.0.0``` |\n| 7.x             | 1.0.0 | ```composer require aammui/role-permission:1.0.0``` |\n| 6.x, 5.x        | 0.7   | ```composer require aammui/role-permission:0.7``` |\n\n\n#### Publish the assests and run migrations \n```\nphp artisan vendor:publish --provider=\"Aammui\\RolePermission\\RolePermissionServiceProvider\"\nphp artisan migrate\n```\n\n\n## Uses\nUse a trait ```HasRole``` to your user model.\n```\nuse Aammui\\RolePermission\\Traits\\HasRole;\n\nclass User extends Authenticatable\n{\n    use Notifiable, HasRole;\n}\n```\nand then following api are available to you.\n* ```public function addRole($role): void ```  \nThis **sync** the roles, if a user has admin role and then you send only editor, it will remove admin role and then user will only have editor role. Send all roles to update the roles.\n* ```public function getRoles(): array```  \nIt returns roles in array.\n* ```public function hasGotRole(array $roles): bool```\n\n## Exception\nIt throws following exception as below.\n| Exception | Remarks |\n| --- | --- |\n| ```Aammui\\RolePermission\\Exception\\UserNotLoginException``` | User is not logged in yet. |\n| ```Aammui\\RolePermission\\Exception\\RoleDoesNotExistException``` | A function or route is protected by a role, and logged in user doesn't have that role yet. |\n\n#### UseCase: Exception uses for user redirection.\nSuppose we want to redirect not logged in user to login page, which can be done using handling exception in ```app\\Exceptions\\Handler.php``` class. The purpose of this exception make available is to support full customization. For example you may want to redirect to login page for that user whom don't have right role, or you simply only want to show 403 page.\n```php\n// App\\Exceptions\\Handler.php;\nuse Aammui\\RolePermission\\Exception\\UserNotLoginException;\nuse Aammui\\RolePermission\\Exception\\RoleDoesNotExistException;\n\n....\n\npublic function render($request, Throwable $exception)\n{\n    if ($exception instanceof UserNotLoginException) {\n        return redirect('/login')\n            -\u003ewith('error', $exception-\u003egetMessage());\n    }\n    \n    if ($exception instanceof RoleDoesNotExistException) {\n        return redirect('/login')\n            -\u003ewith('error', $exception-\u003egetMessage());\n    }\n\n    return parent::render($request, $exception);\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Frole-permission","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbedus-creation%2Frole-permission","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbedus-creation%2Frole-permission/lists"}