{"id":15277433,"url":"https://github.com/php-casbin/codeigniter-permission","last_synced_at":"2025-04-06T03:09:15.204Z","repository":{"id":53000481,"uuid":"157478230","full_name":"php-casbin/codeigniter-permission","owner":"php-casbin","description":"Associate users with roles and permissions, use Casbin in CodeIgniter4 Web Framework.","archived":false,"fork":false,"pushed_at":"2024-12-18T16:38:39.000Z","size":31,"stargazers_count":44,"open_issues_count":0,"forks_count":9,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-03-30T02:06:36.582Z","etag":null,"topics":["access-control","acl","authorization","codeigniter","permissions","rbac","restful"],"latest_commit_sha":null,"homepage":"https://github.com/php-casbin/php-casbin","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/php-casbin.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,"publiccode":null,"codemeta":null}},"created_at":"2018-11-14T02:27:52.000Z","updated_at":"2025-03-13T18:38:45.000Z","dependencies_parsed_at":"2024-10-31T01:01:57.974Z","dependency_job_id":"fc57c993-8062-4327-8578-f93c4dcbbca2","html_url":"https://github.com/php-casbin/codeigniter-permission","commit_stats":{"total_commits":10,"total_committers":2,"mean_commits":5.0,"dds":0.09999999999999998,"last_synced_commit":"9a26e4e23484d5538e6145d92a11f530f361d44c"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-casbin%2Fcodeigniter-permission","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-casbin%2Fcodeigniter-permission/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-casbin%2Fcodeigniter-permission/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/php-casbin%2Fcodeigniter-permission/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/php-casbin","download_url":"https://codeload.github.com/php-casbin/codeigniter-permission/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247427006,"owners_count":20937201,"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":["access-control","acl","authorization","codeigniter","permissions","rbac","restful"],"created_at":"2024-09-30T11:06:16.219Z","updated_at":"2025-04-06T03:09:15.174Z","avatar_url":"https://github.com/php-casbin.png","language":"PHP","readme":"\u003ch1 align=\"center\"\u003e\r\n    CodeIgniter Permission\r\n\u003c/h1\u003e\r\n\r\n\u003cp align=\"center\"\u003e\r\n    \u003cstrong\u003eCodeIgniter Permission is an authorization library for the CodeIgniter4 framework.\u003c/strong\u003e    \r\n\u003c/p\u003e\r\n\r\n\u003cp align=\"center\"\u003e\r\n    \u003ca href=\"https://github.com/php-casbin/codeigniter-permission/actions/workflows/phpunit.yml\"\u003e\r\n        \u003cimg src=\"https://github.com/php-casbin/codeigniter-permission/actions/workflows/phpunit.yml/badge.svg\" alt=\"Build Status\"\u003e\r\n    \u003c/a\u003e\r\n    \u003ca href=\"https://coveralls.io/github/php-casbin/codeigniter-permission\"\u003e\r\n        \u003cimg src=\"https://coveralls.io/repos/github/php-casbin/codeigniter-permission/badge.svg\" alt=\"Coverage Status\"\u003e\r\n    \u003c/a\u003e\r\n    \u003ca href=\"https://packagist.org/packages/casbin/codeigniter-permission\"\u003e\r\n        \u003cimg src=\"https://poser.pugx.org/casbin/codeigniter-permission/v/stable\" alt=\"Latest Stable Version\"\u003e\r\n    \u003c/a\u003e\r\n     \u003ca href=\"https://packagist.org/packages/casbin/codeigniter-permission\"\u003e\r\n        \u003cimg src=\"https://poser.pugx.org/casbin/codeigniter-permission/downloads\" alt=\"Total Downloads\"\u003e\r\n    \u003c/a\u003e\r\n    \u003ca href=\"https://packagist.org/packages/casbin/codeigniter-permission\"\u003e\r\n        \u003cimg src=\"https://poser.pugx.org/casbin/codeigniter-permission/license\" alt=\"License\"\u003e\r\n    \u003c/a\u003e\r\n\u003c/p\u003e\r\n\r\nIt's based on [Casbin](https://github.com/php-casbin/php-casbin), an authorization library that supports access control models like ACL, RBAC, ABAC.\r\n\r\nAll you need to learn to use `Casbin` first.\r\n\r\n* [Installation](#installation)\r\n* [Usage](#usage)\r\n  * [Quick start](#quick-start)\r\n  * [Using Enforcer Api](#using-enforcer-api)  \r\n  * [Multiple enforcers](#multiple-enforcers)  \r\n  * [Cache](#using-cache)\r\n* [Thinks](#thinks)\r\n* [License](#license)\r\n\r\n## Installation\r\n\r\nRequire this package in the `composer.json` of your `CodeIgniter 4` project. This will download the package.\r\n\r\n```\r\ncomposer require casbin/codeigniter-permission\r\n```\r\n\r\nTo migrate the migrations, run the migrate command:\r\n\r\n```\r\nphp spark migrate -n \"Casbin\\CodeIgniter\"\r\n```\r\n\r\nThis will create a new table named `rules`\r\n\r\n\r\n## Usage\r\n\r\n### Quick start\r\n\r\nOnce installed you can do stuff like this:\r\n\r\n```php\r\n\r\n$enforcer = \\Config\\Services::enforcer();\r\n\r\n// adds permissions to a user\r\n$enforcer-\u003eaddPermissionForUser('eve', 'articles', 'read');\r\n// adds a role for a user.\r\n$enforcer-\u003eaddRoleForUser('eve', 'writer');\r\n// adds permissions to a rule\r\n$enforcer-\u003eaddPolicy('writer', 'articles','edit');\r\n\r\n```\r\n\r\nYou can check if a user has a permission like this:\r\n\r\n```php\r\n// to check if a user has permission\r\nif ($enforcer-\u003eenforce(\"eve\", \"articles\", \"edit\")) {\r\n    // permit eve to edit articles\r\n} else {\r\n    // deny the request, show an error\r\n}\r\n\r\n```\r\n\r\n### Using Enforcer Api\r\n\r\nIt provides a very rich api to facilitate various operations on the Policy:\r\n\r\nGets all roles:\r\n\r\n```php\r\n$enforcer-\u003egetAllRoles(); // ['writer', 'reader']\r\n```\r\n\r\nGets all the authorization rules in the policy.:\r\n\r\n```php\r\n$enforcer-\u003egetPolicy();\r\n```\r\n\r\nGets the roles that a user has.\r\n\r\n```php\r\n$enforcer-\u003egetRolesForUser('eve'); // ['writer']\r\n```\r\n\r\nGets the users that has a role.\r\n\r\n```php\r\n$enforcer-\u003egetUsersForRole('writer'); // ['eve']\r\n```\r\n\r\nDetermines whether a user has a role.\r\n\r\n```php\r\n$enforcer-\u003ehasRoleForUser('eve', 'writer'); // true or false\r\n```\r\n\r\nAdds a role for a user.\r\n\r\n```php\r\n$enforcer-\u003eaddRoleForUser('eve', 'writer');\r\n```\r\n\r\nAdds a permission for a user or role.\r\n\r\n```php\r\n// to user\r\n$enforcer-\u003eaddPermissionForUser('eve', 'articles', 'read');\r\n// to role\r\n$enforcer-\u003eaddPermissionForUser('writer', 'articles','edit');\r\n```\r\n\r\nDeletes a role for a user.\r\n\r\n```php\r\n$enforcer-\u003edeleteRoleForUser('eve', 'writer');\r\n```\r\n\r\nDeletes all roles for a user.\r\n\r\n```php\r\n$enforcer-\u003edeleteRolesForUser('eve');\r\n```\r\n\r\nDeletes a role.\r\n\r\n```php\r\n$enforcer-\u003edeleteRole('writer');\r\n```\r\n\r\nDeletes a permission.\r\n\r\n```php\r\n$enforcer-\u003edeletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).\r\n```\r\n\r\nDeletes a permission for a user or role.\r\n\r\n```php\r\n$enforcer-\u003edeletePermissionForUser('eve', 'articles', 'read');\r\n```\r\n\r\nDeletes permissions for a user or role.\r\n\r\n```php\r\n// to user\r\n$enforcer-\u003edeletePermissionsForUser('eve');\r\n// to role\r\n$enforcer-\u003edeletePermissionsForUser('writer');\r\n```\r\n\r\nGets permissions for a user or role.\r\n\r\n```php\r\n$enforcer-\u003egetPermissionsForUser('eve'); // return array\r\n```\r\n\r\nDetermines whether a user has a permission.\r\n\r\n```php\r\n$enforcer-\u003ehasPermissionForUser('eve', 'articles', 'read');  // true or false\r\n```\r\n\r\nSee [Casbin API](https://casbin.org/docs/en/management-api) for more APIs.\r\n\r\n### Multiple enforcers\r\n\r\nIf you need multiple permission controls in your project, you can configure multiple enforcers.\r\n\r\nIn the `Config\\Enforcer.php` file, it should be like this:\r\n\r\n```php\r\n\r\nnamespace Config;\r\n\r\nuse Casbin\\CodeIgniter\\Config\\Enforcer as BaseConfig;\r\nuse Casbin\\CodeIgniter\\Adapters\\DatabaseAdapter;\r\n\r\nclass Enforcer extends BaseConfig\r\n{\r\n    /*\r\n     * Default Enforcer driver\r\n     *\r\n     * @var string\r\n     */\r\n    public $default = 'basic';\r\n\r\n    public $basic = [\r\n        /*\r\n        * Casbin model setting.\r\n        */\r\n        'model' =\u003e [\r\n            // Available Settings: \"file\", \"text\"\r\n            'config_type' =\u003e 'file',\r\n\r\n            'config_file_path' =\u003e __DIR__.'/rbac-model.conf',\r\n\r\n            'config_text' =\u003e '',\r\n        ],\r\n\r\n        /*\r\n        * Casbin adapter .\r\n        */\r\n        'adapter' =\u003e DatabaseAdapter::class,\r\n\r\n        /*\r\n        * Database setting.\r\n        */\r\n        'database' =\u003e [\r\n            // Database connection for following tables.\r\n            'connection' =\u003e '',\r\n\r\n            // Rule table name.\r\n            'rules_table' =\u003e 'rules',\r\n        ],\r\n\r\n        'log' =\u003e [\r\n            // changes whether Casbin will log messages to the Logger.\r\n            'enabled' =\u003e false,\r\n\r\n            // Casbin Logger\r\n            'logger' =\u003e \\Casbin\\CodeIgniter\\Logger::class,\r\n        ],\r\n\r\n        'cache' =\u003e [\r\n            // changes whether Casbin will cache the rules.\r\n            'enabled' =\u003e false,\r\n\r\n            // cache Key\r\n            'key' =\u003e 'rules',\r\n\r\n            // ttl int|null\r\n            'ttl' =\u003e 24 * 60,\r\n        ],\r\n    ];\r\n\r\n    public $second = [\r\n        'model' =\u003e [\r\n            // ...\r\n        ],\r\n\r\n        'adapter' =\u003e DatabaseAdapter::class,\r\n        // ...\r\n    ];\r\n}\r\n\r\n```\r\n\r\nThen you can choose which enforcers to use.\r\n\r\n```php\r\n$enforcer-\u003eguard('second')-\u003eenforce(\"eve\", \"articles\", \"edit\");\r\n```\r\n\r\n### Using cache\r\n\r\nAuthorization rules are cached to speed up performance. The default is off.\r\n\r\nSets your own cache configs in `Config\\Enforcer.php`. \r\n\r\n```php\r\n'cache' =\u003e [\r\n    // changes whether Casbin will cache the rules.\r\n    'enabled' =\u003e false,\r\n    // cache Key\r\n    'key' =\u003e 'rules',\r\n    // ttl int|null\r\n    'ttl' =\u003e 24 * 60,\r\n]\r\n```\r\n\r\n## Thinks\r\n\r\n[PHP-Casbin](https://github.com/php-casbin/php-casbin). You can find the full documentation of Casbin [on the website](https://casbin.org/).\r\n\r\n## License\r\n\r\nThis project is licensed under the [Apache 2.0 license](LICENSE).\r\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-casbin%2Fcodeigniter-permission","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphp-casbin%2Fcodeigniter-permission","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphp-casbin%2Fcodeigniter-permission/lists"}