Skip to content

Commit

Permalink
[init]Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
xuybin committed Dec 13, 2017
1 parent 52756b4 commit 17e8a44
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
29 changes: 27 additions & 2 deletions enforcer/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,15 @@ func NewCasbinEnforcer(connStr string) *casbin.Enforcer {
}else {
Adapter := xormadapter.NewAdapter("mysql", connStr, true)
enforcer := casbin.NewEnforcer(casbin.NewModel(CasbinConf), Adapter)
return enforcer

err:=insertData(connStr)
if err!=nil{
panic(err)
}else {
return enforcer
}
}

}

func createMysqlDatabase( dataSourceName string) (err error) {
Expand All @@ -32,8 +39,26 @@ func createMysqlDatabase( dataSourceName string) (err error) {
}
defer engine.Close()
_, err = engine.Exec("CREATE DATABASE IF NOT EXISTS "+dbName)
_, err = engine.Exec("INSERT INTO `"+dbName+"`.`casbin_rule`(`p_type`,`v0`,`v1`,`v2`) VALUES ('p','admin','/policy/*','(GET)|(POST)|(PUT)|(DELETE)')")
//_, err = engine.Exec("INSERT INTO `"+dbName+"`.`casbin_rule`(`p_type`,`v0`,`v1`,`v2`) VALUES ('p','admin','/policy/*','(GET)|(POST)|(PUT)|(DELETE)')")
}else {
err=fmt.Errorf("dataSourceName:%s doesn't exist dbName",dataSourceName)
}
return
}

func insertData(dataSourceName string) (err error) {
result:=strings.LastIndex(dataSourceName,"/")
if result >= 0 && result+1<len(dataSourceName){
var engine *xorm.Engine
dbName:=string([]byte(dataSourceName)[result+1:len(dataSourceName)])
dataSourceName= string([]byte(dataSourceName)[0:result+1])
engine, err = xorm.NewEngine("mysql", dataSourceName)

if err != nil {
return err
}
defer engine.Close()
_, err = engine.Exec("INSERT INTO `"+dbName+"`.`casbin_rule`(`p_type`,`v0`,`v1`,`v2`) VALUES ('p','admin','/policy/*','(GET)|(POST)|(PUT)|(DELETE)')")
}else {
err=fmt.Errorf("dataSourceName:%s doesn't exist dbName",dataSourceName)
}
Expand Down
3 changes: 2 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func NewGatewayServer(connStr string, resourceHostStr string, defaultRole ...str
s.Echo.HideBanner = true
// load casbin policy from db
s.Enforcer.LoadPolicy()
s.Enforcer.EnableAutoSave(true)
return
}

Expand Down Expand Up @@ -369,7 +370,7 @@ func initSwaggerJSON() (s *spec.Swagger){
"/policy/metadata/":{PathItemProps:spec.PathItemProps{Head:NewOperation(
metadataTag,
fmt.Sprintf("从DB加载最新的元数据"),
fmt.Sprintf("策略更库后,,如需立即生效,则使用当前api"),
fmt.Sprintf("策略更后,会立即生效,但手工变更数据库除外,此时应使用当前api"),
[]spec.Parameter{},
fmt.Sprintf("无返回"),
&spec.Schema{
Expand Down

0 comments on commit 17e8a44

Please sign in to comment.