GithubHelp home page GithubHelp logo

php-casbin / database-adapter Goto Github PK

View Code? Open in Web Editor NEW
28.0 6.0 4.0 45 KB

Database adapter for PHP-Casbin, Casbin is a powerful and efficient open-source access control library.

License: Apache License 2.0

PHP 100.00%
database-adapter orm php-casbin casbin permissions

database-adapter's Introduction

Database adapter for php-casbin

Build Status Coverage Status Latest Stable Version Total Downloads License

Database adapter for PHP-Casbin.

The current supported databases are:

type database
mysql MySQL
pgsql PostgreSQL
sqlite SQLite
sqlsrv SqlServer

Installation

Use Composer

composer require casbin/database-adapter

Usage

require_once './vendor/autoload.php';

use Casbin\Enforcer;
use Casbin\Util\Log;
use CasbinAdapter\Database\Adapter as DatabaseAdapter;

$config = [
    'type'     => 'mysql', // mysql,pgsql,sqlite,sqlsrv
    'hostname' => '127.0.0.1',
    'database' => 'test',
    'username' => 'root',
    'password' => 'abc-123',
    'hostport' => '3306',
];

$adapter = DatabaseAdapter::newAdapter($config);

$e = new Enforcer('path/to/model.conf', $adapter);

$sub = "alice"; // the user that wants to access a resource.
$obj = "data1"; // the resource that is going to be accessed.
$act = "read"; // the operation that the user performs on the resource.

if ($e->enforce($sub, $obj, $act) === true) {
    // permit alice to read data1
} else {
    // deny the request, show an error
}

Getting Help

License

This project is licensed under the Apache 2.0 license.

database-adapter's People

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

database-adapter's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


Missing package.json file.

A package.json file at the root of your project is required to release on npm.

Please follow the npm guideline to create a valid package.json file.


Good luck with your project ✨

Your semantic-release bot 📦🚀

Please implement FilterAdapter

Becasue every request will query all data from casbin_rule, If i have many domain and user , the result will be very large.

I think FilteredAdapter may fix the problem , but I can not find any example for php version.
Anyone who has implemented it for mysql( or any database ) ? please provide it , thanks.

Errors when trying to connect postgresql DB

Hi! I was going to use php-casbin together with this adapter, but I faced some troubles with it right at the beginning. I figure out how to fix them, but I don't think this is the right way to do it.

So, the first one: Fatal error: Class 'TechOne\Database\Connectors\PDO' not found, when trying to use Postgres connector. It appears that there is no use PDO; in techone/database/src/Connectors/Pgsql.php, though it is in techone/database/src/Connectors/Connector.php.

The second one is sql for creating the casbin table (casbin/database-adapter/src/Adapter.php, initTable function). Postgre was throwing syntax errors, so I had to change it on this:

CREATE TABLE IF NOT EXISTS $this->casbinRuleTableName (
  id bigserial NOT NULL,
  ptype varchar(255) NOT 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,
  PRIMARY KEY (id)
);

I hope, this is helpfull and it's possible to fix that errors.

Bug custom rule table name

I see in code that the initTable method is invoked in the constructor.

This means the db table is created (with the default name) before the casbinRuleTableName parameter can be set.

This also means the default table is -always- created, when you choose to use a custom table name.

I think it's better to remove the initTable invokation from the constructor, and let the developer use this method manually to create the tables.

Duplicate entries (again)

Hi all, this is a followup back again - now with a different model and policies, but with the same result as #2 (sorry for not replying earlier, I had to move to different projects unexpectedly). The problem is possibly related to php-casbin/php-casbin#61 ... When using the file adapter, i get i.e. 47 lines, when I use the database, I get 2013 lines. For example, with

$rules = [
            'p' => [ 
                // admininstration role
                [ 'admin', '0', '*', 'c' ],
                [ 'admin', '0', '*', 'r' ],
                [ 'admin', '0', '*', 'u' ],
                [ 'admin', '0', '*', 'd' ],
                // usage role
                [ 'usage', '0', '/ui/worklog', 'r' ],
                [ 'usage', '0', '/ui/core/accounts/self', 'r' ],
                [ 'usage', '0', '/ui/core/profiles/self', 'r' ],
                [ 'usage', '0', '/ui/stor', 'r' ],
            ],
            // The `g` policy assigns a {role} in {domain} to a {user}
            // Per model definition, `g = user, role, domain`.
            'g' => [
                [ '1', 'admin', '0' ],
            ],
];

and model

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _
g2 = _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) && g2(r.dom, p.dom) && keyMatch2(r.obj, p.obj) && r.act == p.act

... During my testing of casbin, I first noted that I must envelope the addPolicy with a hasPolicy method, otherwise I get duplocate data.

if (!$m->hasPolicy('g', 'g2', $rule)) {
$m->addPolicy('g', 'g2', $rule);
$e->savePolicy();
}

I also noticed, that integers in the $rule array (where strings are expected, but not type-enforced) cause another set of duplication. Upon writing a wrapper function, that casts all $rule elements to string. the problem with duplicate data disappeared when using the file adapter, but remains with the database adapter.

Either there's a difference in how data are handled by the adapters before storing them, or its the difference between mysql and a textfile. The sql table uses varchar, so that should be fine, but it inherits from the database the default charset (in my case utf8mb4_0900_ai_ci) , which means accent and case insensitive. I can't imagine how numbers could be a problem, but there's a lot to consider.

I think php-casbin and the database-adapter need a stricter type checking and comparing to avoid these problems.

Support for 3.x

Hey!

Thought I'd ask this project going to support the 3.0 branch of php-casbin or will it be deprecated?

Thanks!

Duplicate entries

Hi,

I noticed that

$e = new Enforcer('casbin_model.conf', $adapter);
$e->addPermissionForUser('eve', 'data3', 'read');

run more then once will add duplicate database entries. I don't think this should be possible, shouldn't the function skip adding the permission in case it already exists? Same for addPolicy and possibly other functions.

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.