Giter Site home page Giter Site logo

php-casbin / codeigniter-permission Goto Github PK

View Code? Open in Web Editor NEW
44.0 15.0 9.0 25 KB

Associate users with roles and permissions, use Casbin in CodeIgniter4 Web Framework.

Home Page: https://github.com/php-casbin/php-casbin

License: Apache License 2.0

PHP 100.00%
codeigniter permissions rbac authorization acl access-control restful

codeigniter-permission's Introduction

CodeIgniter Permission

CodeIgniter Permission is an authorization library for the CodeIgniter4 framework.

Build Status Coverage Status Latest Stable Version Total Downloads License

It's based on Casbin, an authorization library that supports access control models like ACL, RBAC, ABAC.

All you need to learn to use Casbin first.

Installation

Require this package in the composer.json of your CodeIgniter 4 project. This will download the package.

composer require casbin/codeigniter-permission

To migrate the migrations, run the migrate command:

php spark migrate -n "Casbin\CodeIgniter"

This will create a new table named rules

Usage

Quick start

Once installed you can do stuff like this:

$enforcer = \Config\Services::enforcer();

// adds permissions to a user
$enforcer->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$enforcer->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$enforcer->addPolicy('writer', 'articles','edit');

You can check if a user has a permission like this:

// to check if a user has permission
if ($enforcer->enforce("eve", "articles", "edit")) {
    // permit eve to edit articles
} else {
    // deny the request, show an error
}

Using Enforcer Api

It provides a very rich api to facilitate various operations on the Policy:

Gets all roles:

$enforcer->getAllRoles(); // ['writer', 'reader']

Gets all the authorization rules in the policy.:

$enforcer->getPolicy();

Gets the roles that a user has.

$enforcer->getRolesForUser('eve'); // ['writer']

Gets the users that has a role.

$enforcer->getUsersForRole('writer'); // ['eve']

Determines whether a user has a role.

$enforcer->hasRoleForUser('eve', 'writer'); // true or false

Adds a role for a user.

$enforcer->addRoleForUser('eve', 'writer');

Adds a permission for a user or role.

// to user
$enforcer->addPermissionForUser('eve', 'articles', 'read');
// to role
$enforcer->addPermissionForUser('writer', 'articles','edit');

Deletes a role for a user.

$enforcer->deleteRoleForUser('eve', 'writer');

Deletes all roles for a user.

$enforcer->deleteRolesForUser('eve');

Deletes a role.

$enforcer->deleteRole('writer');

Deletes a permission.

$enforcer->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).

Deletes a permission for a user or role.

$enforcer->deletePermissionForUser('eve', 'articles', 'read');

Deletes permissions for a user or role.

// to user
$enforcer->deletePermissionsForUser('eve');
// to role
$enforcer->deletePermissionsForUser('writer');

Gets permissions for a user or role.

$enforcer->getPermissionsForUser('eve'); // return array

Determines whether a user has a permission.

$enforcer->hasPermissionForUser('eve', 'articles', 'read');  // true or false

See Casbin API for more APIs.

Multiple enforcers

If you need multiple permission controls in your project, you can configure multiple enforcers.

In the Config\Enforcer.php file, it should be like this:

namespace Config;

use Casbin\CodeIgniter\Config\Enforcer as BaseConfig;
use Casbin\CodeIgniter\Adapters\DatabaseAdapter;

class Enforcer extends BaseConfig
{
    /*
     * Default Enforcer driver
     *
     * @var string
     */
    public $default = 'basic';

    public $basic = [
        /*
        * Casbin model setting.
        */
        'model' => [
            // Available Settings: "file", "text"
            'config_type' => 'file',

            'config_file_path' => __DIR__.'/rbac-model.conf',

            'config_text' => '',
        ],

        /*
        * Casbin adapter .
        */
        'adapter' => DatabaseAdapter::class,

        /*
        * Database setting.
        */
        'database' => [
            // Database connection for following tables.
            'connection' => '',

            // Rule table name.
            'rules_table' => 'rules',
        ],

        'log' => [
            // changes whether Casbin will log messages to the Logger.
            'enabled' => false,

            // Casbin Logger
            'logger' => \Casbin\CodeIgniter\Logger::class,
        ],

        'cache' => [
            // changes whether Casbin will cache the rules.
            'enabled' => false,

            // cache Key
            'key' => 'rules',

            // ttl int|null
            'ttl' => 24 * 60,
        ],
    ];

    public $second = [
        'model' => [
            // ...
        ],

        'adapter' => DatabaseAdapter::class,
        // ...
    ];
}

Then you can choose which enforcers to use.

$enforcer->guard('second')->enforce("eve", "articles", "edit");

Using cache

Authorization rules are cached to speed up performance. The default is off.

Sets your own cache configs in Config\Enforcer.php.

'cache' => [
    // changes whether Casbin will cache the rules.
    'enabled' => false,
    // cache Key
    'key' => 'rules',
    // ttl int|null
    'ttl' => 24 * 60,
]

Thinks

PHP-Casbin. You can find the full documentation of Casbin on the website.

License

This project is licensed under the Apache 2.0 license.

codeigniter-permission's People

Contributors

basakest avatar hsluoyz avatar leeqvip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

codeigniter-permission's Issues

Error in Composer

Hi, I'm getting these error when trying to install via composer:

Problem 1
     - Installation request for casbin/codeigniter-permission dev-master -> satisfiable by casbin/codeigniter-permission[dev-master].
     - casbin/codeigniter-permission dev-master requires codeigniter4/framework ^4@rc -> satisfiable by codeigniter4/framework[v4.0.0-alpha.3, v4.0.0-alpha.4, v4.0.0-alpha.5, v4.0.0-beta.1, v4.0.0-beta.2, v4.0.0-beta.3, v4.0.0-beta.4, v4.0.0-rc.1, v4.0.0-rc.2, v4.0.0-rc.2.1, v4.0.0-rc.3] but these conflict with your requirements or minimum-stability.

The CodeIgniter version that I'm using is 4.0.0.-rc3

Can you please help me?

AddNamedPolicies() not add authorization rules to the current named policy

I have rules in array, E.g.:
$rules = [ ["editor", "/admin/dashboard/*", "GET"], ["editor", "/admin/users/edit*", "GET"] ];

Even though the function from ManagementApi $e->addPolicies($rules) returns True, does not add any rules.

For completion only:
If I use E.g. $e->addPolicy("editor", "/admin/dashboard/*", "GET"), the function returns True and the rule is added properly.

outdated casbin

Looks like codeigniter-permission is still uses outdated casbin ^2.0 this leads to error on php 8.1
is it possible to update composer to follow latest version of casbin 3.x?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.