{"id":18486194,"url":"https://github.com/mathsgod/light-rbac","last_synced_at":"2025-05-13T22:13:17.630Z","repository":{"id":232930602,"uuid":"785517034","full_name":"mathsgod/light-rbac","owner":"mathsgod","description":null,"archived":false,"fork":false,"pushed_at":"2024-04-15T03:34:33.000Z","size":52,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-13T09:03:24.962Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mathsgod.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,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-04-12T03:23:09.000Z","updated_at":"2024-04-12T07:13:34.000Z","dependencies_parsed_at":"2024-04-15T05:01:57.857Z","dependency_job_id":"3e33c642-df8d-45cb-82a3-db672048cdf9","html_url":"https://github.com/mathsgod/light-rbac","commit_stats":null,"previous_names":["mathsgod/light-rbac"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Flight-rbac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Flight-rbac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Flight-rbac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mathsgod%2Flight-rbac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mathsgod","download_url":"https://codeload.github.com/mathsgod/light-rbac/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254036843,"owners_count":22003654,"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-06T12:48:17.118Z","updated_at":"2025-05-13T22:13:12.616Z","avatar_url":"https://github.com/mathsgod.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Light RBAC\n\nLight RBAC is a simple Role-Based Access Control (RBAC) system implemented in PHP.\n\n## Class: Rbac\n\nThe `Rbac` class is the main class of the system. It manages roles and users.\n\n### Properties\n\n- `$roles`: An instance of `RoleManager` that manages all roles in the system.\n- `$users`: An instance of `UserManager` that manages all users in the system.\n\n### Methods\n\n- `addUser(string $name, array $roles = []): User`: Adds a user with the given name and roles to the system. If the user already exists, it adds the roles to the existing user.\n- `addRole(string $name)`: Adds a role with the given name to the system. If the role already exists, it returns the existing role.\n- `getRole(string $name)`: Returns the role with the given name.\n- `removeRole(string $name)`: Removes the role with the given name from the system.\n- `getUser(string $name)`: Returns the user with the given name. \n- `removeUser(string $name)`: Removes the user with the given name from the system.\n- `getPermissions(): array`: Returns all permissions in the system.\n\n\n## Usage\n\nFirst, create an instance of the `Rbac` class. Then, use the `addUser` and `addRole` methods to add users and roles to the system. Use the `getRole` method to retrieve a role by its name.\n\n```php\n$rbac = new \\Light\\Rbac\\Rbac();\n$rbac-\u003eaddRole('admin');\n$rbac-\u003eaddUser('John Doe', ['admin']);\n\n$admin = $rbac-\u003egetRole('admin');\n```\n\n\n### Permissions\n\nPermissions can be assigned to roles. A permission is a string that represents a certain action or resource. For example, `post:read`, `post:write`, `post:delete`, etc.\n\n```php\n$role = $rbac-\u003eaddRole('admin');\n$role-\u003eaddPermission('post:read');\n$role-\u003eaddPermission('post:write');\n```\n\n### Checking Permissions\n\nTo check if a user has a certain permission, use the `can` method of the `User` class.\n\n```php\n$user = $rbac-\u003eaddUser('John Doe', ['admin']);\nif ($user-\u003ecan('post:read')) {\n    echo 'John Doe can read posts.';\n}\n```\n\n### Asterisk Permission\n\nYou can use the asterisk `*` to represent all permissions.\n\n```php\n$role = $rbac-\u003eaddRole('admin');\n$role-\u003eaddPermission('*');\n\nif ($role-\u003ecan('post:read')) {\n    echo 'Admin can read posts.';\n}\n```\n\nYou can also use the asterisk 'resource:*' to represent all permissions for a specific resource.\n\n```php\n$role = $rbac-\u003eaddRole('admin');\n$role-\u003eaddPermission('post:*');\n\nif ($role-\u003ecan('post:read')) {\n    echo 'Admin can read posts.';\n}\n```\n\n\n### Checking Roles\n\nTo check if a user has a certain role, use the `hasRole` method of the `User` class.\n\n```php\n$user = $rbac-\u003eaddUser('John Doe', ['admin']);\nif ($user-\u003ehasRole('admin')) {\n    echo 'John Doe is an admin.';\n}\n```\n\n### Hierarchical Roles\n\n```php\n$admin = $rbac-\u003eaddRole('admin');\n$admin-\u003eaddChild('editor');\n\n$rbac-\u003egetRole('editor')-\u003eaddPermission('post:read');\n\nif($admin-\u003ecan('post:read')) {\n    echo 'Admin can read posts.';\n}\n\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathsgod%2Flight-rbac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathsgod%2Flight-rbac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathsgod%2Flight-rbac/lists"}