Giter Club home page Giter Club logo

easyswoole-permission's Introduction

easyswoole-permission

easyswoole-permission is an authorization library for the easyswoole framework.

Build Status Coverage Status Latest Stable Version Total Downloads License

Chinese Version

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 easyswoole project. This will download the package.

$ composer require

Or in the root directory of your easyswoole application, you can use the following composer command to install this package directly .

$ composer require casbin/easyswoole-permission:dev-master

Usage

Database settings

add mysql configuration to dev.php:

/*################ MYSQL CONFIG ##################*/

'MYSQL'  => [
    'host'          => '127.0.0.1',
    'port'          => 3306,
    'user'          => 'root',
    'password'      => 'root',
    'database'      => 'easyswoole',
    'timeout'       => 5,
    'charset'       => 'utf8mb4',
]

add mysql configuration to EasySwooleEvent.php:

use EasySwoole\ORM\Db\Connection;
use EasySwoole\ORM\DbManager;

public static function initialize()
{
  ...
  $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
  DbManager::getInstance()->addConnection(new Connection($config));
}

Create corresponding data table

Before using it, you need to create a table named casbin_rules for Casbin to store the policy.

Take mysql as an example:

CREATE TABLE  if not exists  `casbin_rules` (
  `id` BigInt(20) unsigned NOT NULL AUTO_INCREMENT,
  `ptype` varchar(255) DEFAULT NULL,
  `v0` varchar(255) DEFAULT NULL,
  `v1` varchar(255) DEFAULT NULL,
  `v2` varchar(255) DEFAULT NULL,
  `v3` varchar(255) DEFAULT NULL,
  `v4` varchar(255) DEFAULT NULL,
  `v5` varchar(255) DEFAULT NULL,
  `create_time` timestamp NULL DEFAULT NULL,
  `update_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4;

Quick start

Then you can start like this:

use EasySwoole\Permission\Casbin;
use EasySwoole\Permission\Config;

$config = new Config();
$casbin = new Casbin($config);

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

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

// to check if a user has permission
if ($casbin->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:

First create an instance of the enforcer class, and the following operations are based on this instance:

$config = new Config();
$casbin = new Casbin($config);
$enforcer = $casbin->enforcer();

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.

Thinks

Casbin in Easyswoole. You can find the full documentation of Casbin on the website.

License

This project is licensed under the Apache 2.0 license.

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.