GithubHelp home page GithubHelp logo

memwey / casbin-sqlx-adapter Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 10.0 23 KB

sqlx adapter for Casbin https://github.com/casbin/casbin

License: Apache License 2.0

Go 100.00%
casbin casbin-adapter sqlx

casbin-sqlx-adapter's Introduction

casbin-sqlx-adapter

sqlx adapter for Casbin https://github.com/casbin/casbin

Based on sqlx, and tested in MySQL.

Installation

go get github.com/memwey/casbin-sqlx-adapter

NOTICE

The v2 version of Casbin has some break change, check for the detail. If you are still using v1 version, use 0.1.x of this project. And if you have been using the package before, and wanting to upgrade, check the following massage.

The adapter maintained by Casbin team introduce a BREAKING CHANGE by changing the column name from p_type to ptype, see the issue in gorm-adapter and xorm-adapter. Thought I think the adapter layer is able to imply its own storage detail, I follow this BREAKING CHANGE in version 0.3.0 in case of developers may port to different adapters.

The implement is kind of different from the official one. In this implement you should create the database and table on your own.

In my opinion, in a general PRODUCTION environment, the business code is only allow to do DML but not DDL.

The filtered adapter feature has been added in some adapter implements such as Xorm Adapter. However, it's not yet documented in the doc. I will add it after it's documented. See this issue.

Usage example

opts := &AdapterOptions{
    DriverName: "mysql",
    DataSourceName: "root:1234@tcp(127.0.0.1:3306)/yourdb",
    TableName: "casbin_rule",
    // or reuse an existing connection:
    // DB: myDBConn,
}

a := NewAdapterFromOptions(opts)
// Casbin v2 may return an error
e, err := casbin.NewEnforcer("examples/rbac_model.conf", a)
if err != nil {
    panic(err)
}

Thank

Special thanks to Casbin. They provide a superb authorization library.

Special thanks to sqlx. It provides a brilliant set of extensions on go's standard database/sql library.

And this project is inspected by Xorm Adapter, Gorm Adapter, Beego ORM Adapter and MySQL adapter . Thanks all of them.

Others

This is a very first opensource of me and if this project violates any of the opensource guidelines, please contact me. The project is far from perfect, issues and pull requesets are very welcome.

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

casbin-sqlx-adapter's People

Contributors

hmdsefi avatar memwey avatar tobias-kuendig avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar

casbin-sqlx-adapter's Issues

Panic with using pgx

The second line in code causes panic:

opts := &sqlxadapter.AdapterOptions{
		DriverName: "pgx",
		DataSourceName: v.GetString("dsn"),
		TableName: "casbin_rule",
	}
a := sqlxadapter.NewAdapterFromOptions(opts)
panic: ERROR: syntax error at or near "`" (SQLSTATE 42601)

goroutine 1 [running]:
github.com/memwey/casbin-sqlx-adapter.(*Adapter).ensureTable(0xc000372820)
        /Users/vivek/work/pkg/mod/github.com/memwey/[email protected]/adapter.go:99 +0x115
github.com/memwey/casbin-sqlx-adapter.NewAdapterFromOptions(0xc000217f18, 0x18a9083)
        /Users/vivek/work/pkg/mod/github.com/memwey/[email protected]/adapter.go:168 +0x93
main.main()

The sql for casbin_rule table is not sqlite friendly.

The sql in here, breaks when trying to use it in sqlite3.

Below is the smallest change.

CREATE TABLE IF NOT EXISTS `casbin_rule` (
    `id` INTEGER,
    `p_type` VARCHAR(32) NOT NULL DEFAULT '',
    `v0` VARCHAR(255) NOT NULL DEFAULT '',
    `v1` VARCHAR(255) NOT NULL DEFAULT '',
    `v2` VARCHAR(255) NOT NULL DEFAULT '',
    `v3` VARCHAR(255) NOT NULL DEFAULT '',
    `v4` VARCHAR(255) NOT NULL DEFAULT '',
    `v5` VARCHAR(255) NOT NULL DEFAULT '',
    PRIMARY KEY (`id`)
);

Also, out the box, I have now learnt, sqlite doesnt care about VARCHAR and it is always TEXT.

Here is an example with sqlite checking

CREATE TABLE IF NOT EXISTS `casbin_rule` (
    `id` INTEGER,
    `p_type` VARCHAR(32) NOT NULL DEFAULT ''
    CHECK(
      typeof("p_type") = "text" AND
      length("p_type") <= 32
    ),
    `v0` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v0") = "text" AND
      length("v0") <= 255
    ),
    `v1` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v1") = "text" AND
      length("v1") <= 255
    ),
    `v2` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v2") = "text" AND
      length("v2") <= 255
    ),
    `v3` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v3") = "text" AND
      length("v3") <= 255
    ),
    `v4` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v4") = "text" AND
      length("v4") <= 255
    ),
    `v5` VARCHAR(255) NOT NULL DEFAULT ''
    CHECK(
      typeof("v5") = "text" AND
      length("v5") <= 255
    ),
    PRIMARY KEY (`id`)
);

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.