feat: add demo mode (#1097)

* feat: add demo mode

* feat: add demo mode

* Update app.conf

* Update authz.go

* Update authz.go

Co-authored-by: Yang Luo <hsluoyz@qq.com>
This commit is contained in:
cofecatt 2022-09-04 21:20:19 +08:00 committed by GitHub
parent 35676455bc
commit 7599e2715a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 1 deletions

View File

@ -128,6 +128,12 @@ p, *, *, GET, /api/get-release, *, *
} }
func IsAllowed(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool { func IsAllowed(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
if conf.IsDemoMode() {
if !isAllowedInDemoMode(subOwner, subName, method, urlPath, objOwner, objName) {
return false
}
}
res, err := Enforcer.Enforce(subOwner, subName, method, urlPath, objOwner, objName) res, err := Enforcer.Enforce(subOwner, subName, method, urlPath, objOwner, objName)
if err != nil { if err != nil {
panic(err) panic(err)
@ -135,3 +141,22 @@ func IsAllowed(subOwner string, subName string, method string, urlPath string, o
return res return res
} }
func isAllowedInDemoMode(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
if method == "POST" {
if urlPath == "/api/login" || urlPath == "/api/logout" || urlPath == "/api/signup" || urlPath == "/api/send-verification-code" {
return true
} else if urlPath == "/api/update-user" {
// Allow ordinary users to update their own information
if subOwner == objOwner && subName == objName && !(subOwner == "built-in" && subName == "admin") {
return true
}
return false
} else {
return false
}
}
// If method equals GET
return true
}

View File

@ -17,3 +17,4 @@ initScore = 2000
logPostOnly = true logPostOnly = true
origin = origin =
staticBaseUrl = "https://cdn.casbin.org" staticBaseUrl = "https://cdn.casbin.org"
isDemoMode = false

View File

@ -80,3 +80,7 @@ func GetBeegoConfDataSourceName() string {
return dataSourceName return dataSourceName
} }
func IsDemoMode() bool {
return strings.ToLower(GetConfigString("isDemoMode")) == "true"
}