go web 权限管理 简单例子 (面向对象权限 ABAC / Casbin)

发布时间:2020-08-03 12:59:13 作者:295631788
来源:网络 阅读:1710

go web 权限管理 简单例子 (面向对象权限 ABAC / Casbin)


说明

ABAC
调用 github.com/casbin/casbin


abac_model.conf

[request_definition]
r = sub, obj, act

[policy_definition]
p = sub, obj,act

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

[matchers]
m = r.sub.App == r.obj.App &&  r.sub.Type == r.obj.Type &&  r.sub.Method == r.obj.Method

main.go

package main

import (
    "fmt"
    "github.com/casbin/casbin"
)

type User struct {
    Id       int
    UserName string
    Group    []Group
}

type Group struct {
    Id       int
    Name     string
    App      string // app
    Type     string // 类型
    Method   string // 方法
    Priority int    // 优先级
}

type Obj struct {
    App    string // app
    Type   string // 类型
    Method string // 方法
}

func main() {
    e := casbin.NewEnforcer("E:\\go-test\\test\\abac\\abac_model.conf")

    group1 := Group{
        Name:     "group1",
        App:      "asset",
        Type:     "aliyun",
        Method:   "Get",
        Priority: 100,
    }

    group2 := Group{
        Name:     "group2",
        App:      "asset",
        Type:     "aliyun",
        Method:   "Get",
        Priority: 100,
    }

    //  用户 hequan  属于 group1 , group2
    user1 := User{
        UserName: "hequan",
        Group:    []Group{group1, group2},
    }

    obj := Obj{
        App:    "asset",
        Type:   "aliyun",
        Method: "Get",
    }

    var perms = false

    // 检查 用户 hequan 所有的组  是否有权限
    for _, v := range user1.Group {
        if e.Enforce(v, obj, ""){
            perms = true
            break
        }
    }
    if perms {
        fmt.Println("权限正常")
    } else {
        fmt.Println("没有权限")
    }
}

结果

权限正常
推荐阅读:
  1. go装饰模式,一个屌丝撸管的故事
  2. Vue axios与Go Frame后端框架的Options请求跨域问题详解

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

go abac 权限

上一篇:手机照片删除怎样去恢复呢?恢复的具体方法

下一篇:IO多路复用--epoll

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》