承接 casbin/codeigniter-permission 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

casbin/codeigniter-permission

Composer 安装命令:

composer require casbin/codeigniter-permission

包简介

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

README 文档

README

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.

casbin/codeigniter-permission 适用场景与选型建议

casbin/codeigniter-permission 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.1k 次下载、GitHub Stars 达 45, 最近一次更新时间为 2019 年 09 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「authorization」 「codeigniter」 「acl」 「permission」 「rbac」 「access control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 casbin/codeigniter-permission 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 casbin/codeigniter-permission 我们能提供哪些服务?
定制开发 / 二次开发

基于 casbin/codeigniter-permission 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.1k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 46
  • 点击次数: 25
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 45
  • Watchers: 13
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2019-09-12