{"id":17323073,"url":"https://github.com/danieleds/simpleroles","last_synced_at":"2025-04-14T16:16:37.538Z","repository":{"id":3148100,"uuid":"4177702","full_name":"danieleds/SimpleRoles","owner":"danieleds","description":"A simple and lightweight PHP authorization library","archived":false,"fork":false,"pushed_at":"2014-06-14T16:41:57.000Z","size":114,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-22T19:42:07.152Z","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/danieleds.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":"2012-04-29T21:35:27.000Z","updated_at":"2023-04-20T16:38:41.000Z","dependencies_parsed_at":"2022-08-02T22:45:20.904Z","dependency_job_id":null,"html_url":"https://github.com/danieleds/SimpleRoles","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieleds%2FSimpleRoles","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieleds%2FSimpleRoles/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieleds%2FSimpleRoles/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danieleds%2FSimpleRoles/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danieleds","download_url":"https://codeload.github.com/danieleds/SimpleRoles/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241664318,"owners_count":19999464,"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-10-15T13:45:14.839Z","updated_at":"2025-03-03T12:30:33.227Z","avatar_url":"https://github.com/danieleds.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Getting started\n----------\nFirst, you have to create a new class where you will define all your rules. Let's start with this:\n\n```php\n\u003c?php\nrequire_once \"SimpleRoles.php\";\n\nclass Ability extends SimpleRoles {\n    \n    function __construct($user) {\n        parent::__construct();\n\n        // Here is where you'll define your rules\n    }\n\n}\n```\n\nWe place our rules inside the `__construct` function.\n\n```php\n\u003c?php\nfunction __construct($user) {\n    parent::__construct();\n\n    // We put the current user roles in $user_roles.\n    // Please note that you can use whatever method you\n    // prefer for checking role membership.\n    $user_roles = array();\n    if($user != null)\n        $user_roles = $user-\u003egetRoles();\n\n    if(in_array('admin', $user_roles)) {\n        $this-\u003ecan('read_article');\n        $this-\u003ecan('update_article');\n        $this-\u003ecan('delete_article');\n    }\n\n    if(in_array('moderator', $user_roles)) {\n        $this-\u003ecan('read_article');\n        $this-\u003ecan('update_article');\n    }\n\n    if(in_array('guest', $user_roles)) {\n        $this-\u003ecannot('all');\n        $this-\u003ecan('read_article');\n    }\n}\n```\n\nThe code is self-explanatory. `all` is a special keyword which will match every rule.\nNow, we just have to add code to our main application.\nFor example:\n\n```php\n\u003c?php\n...\n$a = new Ability($session_current_user);\n...\nif(isset($_POST['update'])) {\n    $a-\u003eauthorize('update_article');\n    // ... We're authorized. Update the article.\n} else if(isset($_POST['delete'])) {\n    $a-\u003eauthorize('delete_article');\n    // ... We're authorized. Delete the article.\n} else {\n    $a-\u003eauthorize('read_article');\n    // ... We're authorized. Display the article.\n}\n```\n\n`$session_current_user` is an instance of some User model, representing the current logged in user. In the Ability class, we called `$user-\u003egetRoles()` to get an array of enabled roles for the current user.\n\nNow, if the user is not authorized to complete the action, an exception will be raised. You can override this behavior by adding a special function called *unauthorized* to your Ability class, for example:\n\n```php\n\u003c?php\nprotected function unauthorized($action) {\n  echo \"You are not authorized to view this page\\n\";\n  // ... Redirect to home page\n  exit;\n}\n```\n\nIf you just want to check if an user is authorized or not to do an action, without doing extra jobs, just call `$a-\u003eis_authorized('your action')`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieleds%2Fsimpleroles","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanieleds%2Fsimpleroles","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanieleds%2Fsimpleroles/lists"}