提交 91b0f386 编写于 作者: MELF晓宇's avatar MELF晓宇 🤤

Add casbin

上级 500d847a
......@@ -23,6 +23,7 @@ type GlobalConfig struct {
Database DatabaseConfig `json:"Database"`
Redis RedisConfig `json:"Redis"`
Self SelfConfig `json:"Self"`
Company CompanyConfig `json:"Company"`
}
type DatabaseConfig struct {
......@@ -49,6 +50,10 @@ type SelfConfig struct {
Port string `json:"RouterPort"`
}
type CompanyConfig struct {
ID int64 `json:"ID"`
}
var (
globalConfig *GlobalConfig
configMux sync.RWMutex
......
......@@ -19,5 +19,8 @@
"Self": {
"RouterHost": "0.0.0.0",
"RouterPort": "2048"
},
"Company": {
"ID": 1
}
}
\ No newline at end of file
[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
\ No newline at end of file
......@@ -66,6 +66,7 @@ func InitDB() (err error) {
// 设置了连接可复用的最大时间
theDB.SetConnMaxLifetime(time.Hour * time.Duration(config.Database.ConnMaxLifetime))
InitDBForCasbin()
//测试连通性
return nil
}
/**
* @Time :2021/11/17 11:10
* @Author :MELF晓宇
* @Email :xyzh.melf@petalmail.com
* @FileName:DatabaseForCasbin.go
* @Project :gin-start
* @Blog :https://blog.csdn.net/qq_29537269
* @Guide :https://guide.melf.space
* @Information:
*
*/
package connection
import (
"fmt"
Config "gin-start/config"
"github.com/jinzhu/gorm"
)
import _ "github.com/go-sql-driver/mysql"
var (
DBForCasbin *gorm.DB
)
func InitDBForCasbin() {
var err error
// 获取配置文件
config := Config.Config()
// 拼接数据库连接字符串
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Database.DbUser, config.Database.DbPassword, config.Database.DbHost, config.Database.DbPort, config.Database.DbName,
)
DBForCasbin, err = gorm.Open("mysql", dsn)
if err != nil {
fmt.Println("初始化DB For Casbin失败")
panic(err)
}
}
......@@ -3,6 +3,9 @@ module gin-start
go 1.17
require (
github.com/bwmarrin/snowflake v0.3.0
github.com/casbin/casbin/v2 v2.39.0
github.com/casbin/gorm-adapter/v3 v3.4.5
github.com/gin-contrib/sessions v0.0.4
github.com/gin-gonic/gin v1.7.4
github.com/go-redis/redis/v8 v8.11.4
......@@ -11,29 +14,48 @@ require (
)
require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
github.com/casbin/casbin v1.9.1 // indirect
github.com/casbin/gorm-adapter v1.0.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/denisenkom/go-mssqldb v0.11.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.13.0 // indirect
github.com/go-playground/universal-translator v0.17.0 // indirect
github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/go-sql-driver/mysql v1.6.0 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomodule/redigo v2.0.0+incompatible // indirect
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/securecookie v1.1.1 // indirect
github.com/gorilla/sessions v1.2.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.10.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.1.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.8.1 // indirect
github.com/jackc/pgx/v4 v4.13.0 // indirect
github.com/jinzhu/gorm v1.9.16 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.2 // indirect
github.com/json-iterator/go v1.1.9 // indirect
github.com/leodido/go-urn v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/sys v0.0.0-20210423082822-04245dca01da // indirect
google.golang.org/protobuf v1.26.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.10.3 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
golang.org/x/crypto v0.0.0-20211115234514-b4de73f9ece8 // indirect
golang.org/x/sys v0.0.0-20211116061358-0a5406a5449c // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gorm.io/driver/postgres v1.1.2 // indirect
gorm.io/driver/sqlserver v1.0.9 // indirect
gorm.io/plugin/dbresolver v1.1.0 // indirect
)
此差异已折叠。
......@@ -43,6 +43,7 @@ func main() {
// 数据表数据初始化
models.InitModel()
// 初始化路由
r := routers.InitRouter()
if errRouter := r.Run(); errRouter != nil {
......
/**
* @Time :2021/11/12 15:05
* @Author :MELF晓宇
* @Email :xyzh.melf@petalmail.com
* @FileName:CasbinRule.go
* @Project :gin-start
* @Blog :https://blog.csdn.net/qq_29537269
* @Guide :https://guide.melf.space
* @Information:
*
*/
package models
type CasbinRule struct {
ID uint `gorm:"primaryKey;autoIncrement"`
Ptype string `gorm:"size:512;uniqueIndex:unique_index"`
V0 string `gorm:"size:512;uniqueIndex:unique_index"`
V1 string `gorm:"size:512;uniqueIndex:unique_index"`
V2 string `gorm:"size:512;uniqueIndex:unique_index"`
V3 string `gorm:"size:512;uniqueIndex:unique_index"`
V4 string `gorm:"size:512;uniqueIndex:unique_index"`
V5 string `gorm:"size:512;uniqueIndex:unique_index"`
}
//func (casbinRule *CasbinRule) BeforeCreate(tx *gorm.DB) (err error) {
// casbinRule.ID = uint(ID.CreateId())
// return
//}
//// TableName 自定义表名
//func (CasbinRule) TableName() string {
// return "casbin_rule"
//}
/**
* @Time :2021/11/12 14:18
* @Author :MELF晓宇
* @Email :xyzh.melf@petalmail.com
* @FileName:Role.go
* @Project :gin-start
* @Blog :https://blog.csdn.net/qq_29537269
* @Guide :https://guide.melf.space
* @Information:
*
*/
package models
import (
"gin-start/pkg/ID"
"gorm.io/gorm"
"time"
)
type Role struct {
ID int64 `gorm:"primaryKey;comment:角色ID"`
CreatedAt time.Time `gorm:"comment:创建时间"`
UpdatedAt time.Time `gorm:"comment:更新时间"`
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间"`
RoleName string `gorm:"type:varchar(20);not null;comment:角色名称"`
}
func (role *Role) BeforeCreate(tx *gorm.DB) (err error) {
role.ID = ID.CreateId()
return
}
// TableName 自定义表名
func (Role) TableName() string {
return "role"
}
/**
* @Time :2021/11/12 14:04
* @Author :MELF晓宇
* @Email :xyzh.melf@petalmail.com
* @FileName:User.go
* @Project :gin-start
* @Blog :https://blog.csdn.net/qq_29537269
* @Guide :https://guide.melf.space
* @Information:
*
*/
package models
import (
"gin-start/pkg/ID"
"gorm.io/gorm"
"time"
)
type User struct {
ID int64 `gorm:"primaryKey;comment:用户ID"`
CreatedAt time.Time `gorm:"comment:创建时间"`
UpdatedAt time.Time `gorm:"comment:更新时间"`
DeletedAt gorm.DeletedAt `gorm:"index;comment:删除时间"`
Name string `gorm:"type:varchar(10);not null;comment:姓名"`
Account string `gorm:"type:varchar(20);not null;comment:账号"`
Password string `gorm:"type:varchar(255);not null;comment:密码"`
Phone string `gorm:"type:varchar(255);not null;comment:电话"`
Sex int8 `gorm:"default:1;comment:性别"`
LastLoginAt *time.Time `gorm:"comment:上传登录时间"`
LastLoginIP *string `gorm:"comment:上传登录IP"`
IsFirst bool `gorm:"default:false;comment:是否初次登录"`
}
func (user *User) BeforeCreate(tx *gorm.DB) (err error) {
user.ID = ID.CreateId()
return
}
// TableName 自定义表名
func (User) TableName() string {
return "user"
}
......@@ -15,7 +15,11 @@ package models
import (
"errors"
conn "gin-start/connection"
"github.com/casbin/casbin"
"github.com/casbin/gorm-adapter"
"log"
)
import _ "github.com/go-sql-driver/mysql"
// DBAutoMigrate
/**
......@@ -25,7 +29,8 @@ import (
func DBAutoMigrate() (err error) {
// 自动迁移
err = conn.DB.AutoMigrate(
// 实体
&User{}, // 用户
&Role{}, // 角色
)
if err != nil {
return errors.New("自动迁移失败:" + err.Error())
......@@ -38,5 +43,10 @@ func DBAutoMigrate() (err error) {
* @Description: 初始化数据表
*/
func InitModel() {
adapter := gormadapter.NewAdapterByDB(conn.DBForCasbin)
Enforcer := casbin.NewEnforcer("config\\rbac_model.conf", adapter)
err3 := Enforcer.LoadPolicy()
if err3 != nil {
log.Fatalln(err3)
}
}
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && keyMatch2(r.obj, p.obj) && regexMatch(r.act, p.act)
[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
\ No newline at end of file
/**
* @Time :2021/11/12 11:11
* @Author :MELF晓宇
* @Email :xyzh.melf@petalmail.com
* @FileName:DistributedID.go
* @Project :gin-start
* @Blog :https://blog.csdn.net/qq_29537269
* @Guide :https://guide.melf.space
* @Information:
*
*/
package ID
import (
"fmt"
Config "gin-start/config"
"github.com/bwmarrin/snowflake"
)
func CreateId() int64 {
// 获取配置文件
config := Config.Config()
node, err := snowflake.NewNode(config.Company.ID)
if err != nil {
fmt.Println(err)
return 0
}
id := node.Generate().Int64()
fmt.Println("id is:", id)
return id
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册