fiper

package module
v0.0.0-...-4eccbda Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2023 License: MIT Imports: 8 Imported by: 0

README


Go JWT



FiPer - Fiber Permits

FiPer is a library that provides Fiber with Role Based Access Control (RBAC) using JWT and with database persistence. Two ORM libraries are supported: Gorm and Bun.

     

Install

go get gitlab.com/rodrigoodhin/fiper
import (
	"gitlab.com/rodrigoodhin/fiper/pkgs/fp"
	"gitlab.com/rodrigoodhin/fiper/pkgs/fgorm"
	"gitlab.com/rodrigoodhin/fiper/pkgs/fbun"
)

     

Usage

To initiate FiPer you need to pass two variables for the newfunction. The first one is the bun instance, the second is the option. Option is a type and contains the follow variables:

Name Description
UserTableName User table name
UserRoleField Role field name on User table
UserIdField Id field name on User table
jwtLocals Name configured for Fiber to store
Create FiPer Instance

Create a Bun RBAC instance

r = fiper.Bun(App.DB, &fp.Options{
    UserTableName: "users",
    UserPermitField: "role",
    UserIdField: "id",
    JwtLocals: "user",
})

Create a Gorm RBAC instance

r = fiper.Gorm(App.DB, &fp.Options{
    UserTableName: "users",
    UserPermitField: "role",
    UserIdField: "id",
    JwtLocals: "user",
})
Declaring Models

For GORM, the permit field must be created like this:

type User struct {
    ...
    Permits  datatypes.JSON  `json:"permits"`
    ...
}

For BUN, the permit field must be created like this:

type User struct {
    ...
    Permits  []string  `json:"permits" bun:",array"`
    ...
}

     

Features

Fiber Handler
r.CheckFiPer([]string{"admin", "role:listAll"})
Roles

Get Role

roles, err = r.GetRoles()

Add Role

r.AddRole(fgorm.FiperRole{
    Name: "admin",
})

r.AddRole(fbun.FiperRole{
    Name: "admin",
})

Delete Role

r.DeleteRole("admin")
Permissions

Get Permission

permissions, err = r.GetPermissions()

Add Permission

r.AddPermission(fgorm.FiperPermission{
    Model:  "user",
    Action: "list",
})

r.AddPermission(fbun.FiperPermission{
    Model:  "user",
    Action: "list",
})

Delete Permission

r.DeletePermission("user", "list")
Role Permissions

Get Role Permission

rolePermissions, err = r.GetRolePermissions()

Add Role Permission

r.AddRolePermission(fgorm.AddRolePermission{
    "role": "admin",
    "permissionModel": "user",
    "permissionAction": "list"
})

r.AddRolePermission(fbun.AddRolePermission{
    "role": "admin",
    "permissionModel": "user",
    "permissionAction": "list"
})

Delete Role Permission

r.DeleteRolePermission("admin", "user", "list")
Users

Get User Permits

r.GetUserPermits(userId)

Grant User Role

r.GrantUserRole(userId, roleName)

Grant User Permission

r.GrantUserPermission(userId, permissionModel, permissionAction)

Revoke User Permit

r.RevokeUserPermit(userId, permit)

     

MIT License

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMigrationTables = errors.New("cannot migration tables")
)

Functions

func Bun

func Bun(db *bun.DB, options *fp.Options) (r *fbun.FiPer)

New initiates Bun FiPer

func Gorm

func Gorm(db *gorm.DB, options *fp.Options) (r *fgorm.FiPer)

New initiates Gorm FiPer

Types

This section is empty.

Directories

Path Synopsis
examples
bun
pkgs
fp

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL