Giter Site home page Giter Site logo

goravel-authz's Introduction

Goravel Authorization

Goravel-authz is an authorization extension library for Goravel. It is built upon Casbin, which supports various access control models such as ACL (Access Control List), RBAC (Role-Based Access Control), and ABAC (Attribute-Based Access Control).

Before using Goravel-authz, you should first familiarize yourself with all aspects of working with Casbin.

中文文档

Installation

Install the Go package:

go get -u github.com/wcz0/goravel-authz

Register the provider in your config/app.go file:

// config/app.go
import "github.com/wcz0/goravel-authz"

// ...

"providers": []foundation.ServiceProvider{
    // ...
    &authz.ServiceProvider{},
},

Execute the command to publish resources:

go run . artisan vendor:publish --package=github.com/wcz0/goravel-authz

Run the migration command for the database:

go run . artisan migrate

Usage

Accessing policies through the facade:

facades.Enforcer().GetPolicy()

import authz "github.com/wcz-/goravel-authz/facades"

e := authz.Enforcer()
// Second enforcer
e2 := authz.Enforcer("second")

e.AddPolicy("admin", "/admin-api/users", "GET")
e2.AddPolicy("admin", "/api/users", "GET")
policy := e.GetPolicy()

Multiple Enforcers

casbin.go

//...
config.Add("casbin", map[string]any{
    "default": "basic", // Casbin default
    "models": map[string]any{
        "basic": models.NewRule(),
        "second": "", // Second adapter
    },
})

For the second model, the key is second. The model needs to inherit from models.Rule.

import "github.com/wcz0/goravel-authz/models"

type AdminRule struct {
    *models.Rule
}

func NewAdminRule() *AdminRule {
    return &AdminRule{
        Rule: &models.Rule{},
    }
}

func (r *Rule) TableName() string {
    return "casbin_rules"
}

func (r *Rule) Connection() string {
    return "mysql"
}

// Cache configuration
func (r *Rule) Cache() (bool, string, string) {
    return true, "memory", "casbin-key"
    // To disable cache, return false, "", ""
}

// Casbin model configuration
func (r *Rule) Model() (string, string) {
    return "file", "casbin-rbac-model.conf"
    // To use text model, return "text", `[request_definition]
    // r = sub, obj, act
    // [policy_definition]
    // p = sub, obj, act
    // [role_definition]
    // g = _, _
    // [policy_effect]
    // e = some(where (p.eft == allow))
    // [matchers]
    // m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act`
}

Future Developments:

  • Command-line creation of policies
  • Middleware implementation
  • Logging handling
  • Unit test caes

goravel-authz's People

Contributors

wcz0 avatar

Watchers

 avatar

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.