casdoor/authz/authz.go

132 lines
3.9 KiB
Go
Raw Normal View History

2022-02-13 23:39:27 +08:00
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
2021-02-28 20:23:50 +08:00
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package authz
import (
"github.com/casbin/casbin/v2"
"github.com/casbin/casbin/v2/model"
xormadapter "github.com/casbin/xorm-adapter/v2"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/conf"
2021-02-28 20:23:50 +08:00
stringadapter "github.com/qiangmzsx/string-adapter/v2"
)
var Enforcer *casbin.Enforcer
func InitAuthz() {
var err error
tableNamePrefix := conf.GetConfigString("tableNamePrefix")
a, err := xormadapter.NewAdapterWithTableName(conf.GetConfigString("driverName"), conf.GetBeegoConfDataSourceName()+conf.GetConfigString("dbName"), "casbin_rule", tableNamePrefix, true)
2021-02-28 20:23:50 +08:00
if err != nil {
panic(err)
}
modelText := `
[request_definition]
2021-02-28 23:14:48 +08:00
r = subOwner, subName, method, urlPath, objOwner, objName
2021-02-28 20:23:50 +08:00
[policy_definition]
2021-02-28 23:14:48 +08:00
p = subOwner, subName, method, urlPath, objOwner, objName
2021-02-28 20:23:50 +08:00
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
2021-04-27 18:08:21 +08:00
m = (r.subOwner == p.subOwner || p.subOwner == "*") && \
(r.subName == p.subName || p.subName == "*" || r.subName != "anonymous" && p.subName == "!anonymous") && \
(r.method == p.method || p.method == "*") && \
(r.urlPath == p.urlPath || p.urlPath == "*") && \
(r.objOwner == p.objOwner || p.objOwner == "*") && \
(r.objName == p.objName || p.objName == "*") || \
2022-03-01 22:37:23 +08:00
(r.subOwner == r.objOwner && r.subName == r.objName)
2021-02-28 20:23:50 +08:00
`
m, err := model.NewModelFromString(modelText)
if err != nil {
panic(err)
}
Enforcer, err = casbin.NewEnforcer(m, a)
if err != nil {
panic(err)
}
Enforcer.ClearPolicy()
2021-02-28 23:14:48 +08:00
//if len(Enforcer.GetPolicy()) == 0 {
if true {
2021-02-28 20:23:50 +08:00
ruleText := `
2021-02-28 23:14:48 +08:00
p, built-in, *, *, *, *, *
p, app, *, *, *, *, *
2021-04-27 22:47:44 +08:00
p, *, *, POST, /api/signup, *, *
p, *, *, POST, /api/get-email-and-phone, *, *
2021-02-28 23:14:48 +08:00
p, *, *, POST, /api/login, *, *
2021-03-21 00:38:00 +08:00
p, *, *, GET, /api/get-app-login, *, *
2021-02-28 23:14:48 +08:00
p, *, *, POST, /api/logout, *, *
p, *, *, GET, /api/get-account, *, *
p, *, *, GET, /api/userinfo, *, *
2021-03-20 22:34:22 +08:00
p, *, *, POST, /api/login/oauth/access_token, *, *
p, *, *, POST, /api/login/oauth/refresh_token, *, *
p, *, *, GET, /api/login/oauth/logout, *, *
2021-02-28 23:14:48 +08:00
p, *, *, GET, /api/get-application, *, *
2021-03-06 00:40:11 +08:00
p, *, *, GET, /api/get-user, *, *
p, *, *, GET, /api/get-user-application, *, *
2021-09-06 00:49:10 +08:00
p, *, *, GET, /api/get-resources, *, *
2022-03-13 11:51:33 +08:00
p, *, *, GET, /api/get-product, *, *
2022-03-13 16:25:54 +08:00
p, *, *, POST, /api/buy-product, *, *
p, *, *, GET, /api/get-payment, *, *
2022-03-13 11:51:33 +08:00
p, *, *, GET, /api/get-providers, *, *
2021-04-27 20:42:19 +08:00
p, *, *, POST, /api/unlink, *, *
2021-05-16 21:04:26 +08:00
p, *, *, POST, /api/set-password, *, *
p, *, *, POST, /api/send-verification-code, *, *
p, *, *, GET, /api/get-human-check, *, *
2021-05-23 23:38:38 +08:00
p, *, *, POST, /api/reset-email-or-phone, *, *
2021-08-28 11:13:38 +08:00
p, *, *, POST, /api/upload-resource, *, *
p, *, *, GET, /.well-known/openid-configuration, *, *
p, *, *, *, /.well-known/jwks, *, *
p, *, *, GET, /api/get-saml-login, *, *
p, *, *, POST, /api/acs, *, *
p, *, *, *, /cas, *, *
2021-02-28 20:23:50 +08:00
`
sa := stringadapter.NewAdapter(ruleText)
// load all rules from string adapter to enforcer's memory
err := sa.LoadPolicy(Enforcer.GetModel())
if err != nil {
panic(err)
}
// save all rules from enforcer's memory to Xorm adapter (DB)
// same as:
// a.SavePolicy(Enforcer.GetModel())
err = Enforcer.SavePolicy()
if err != nil {
panic(err)
}
}
}
2021-02-28 23:14:48 +08:00
func IsAllowed(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
res, err := Enforcer.Enforce(subOwner, subName, method, urlPath, objOwner, objName)
2021-02-28 20:23:50 +08:00
if err != nil {
panic(err)
}
return res
}