Giter Club home page Giter Club logo

gin-casbin's Introduction

Overview

This is a super simple middleware integrating with Gin and Casbin to implement RBAC in Gin apps.

Installation

$ go get github.com/maxwellhertz/gin-casbin

Usage

See examples.

SubjectFn

You will have to provide a custom function to look up the current subject in runtime when you initialize this middleware.

// SubjectFn is used to look up current subject in runtime.
// If it can not find anything, just return an empty string.
type SubjectFn func(c *gin.Context) string

I am pretty sure there are many ways to do this. I provided two examples using JWT and session respectively.

RequiresPermissions

This is one of the two core functionalities. You can use it to filter requests if the subjects don't have the required permissions.

func (am *CasbinMiddleware) RequiresPermissions(permissions []string, opts ...Option) gin.HandlerFunc

The first parameter is a slice of formatted strings representing required permissions. For example, "book:read" stands for the permission to read a book. Note that if you pass in an illegal string such as "bookread" or ":", it will abort immediately and respond HTTP 500.

RequiresRoles

This is the other core functionality. It is a little simpler than RequiresPermissions since you just need to specify what roles you expect the subjects to have.

func (am *CasbinMiddleware) RequiresRoles(requiredRoles []string, opts ...Option) gin.HandlerFunc

gin-casbin's People

Contributors

kilosonc avatar kirinse avatar maxwellhertz 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.