casdoor/authz/authz.go

157 lines
4.6 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 (
2022-09-09 01:53:21 +08:00
"strings"
2021-02-28 20:23:50 +08:00
"github.com/casbin/casbin/v2"
2022-01-20 14:11:46 +08:00
"github.com/casdoor/casdoor/conf"
2022-10-07 15:59:23 +08:00
"github.com/casdoor/casdoor/object"
2023-05-19 14:26:32 +08:00
"github.com/casdoor/casdoor/util"
2021-02-28 20:23:50 +08:00
stringadapter "github.com/qiangmzsx/string-adapter/v2"
)
var Enforcer *casbin.Enforcer
func InitApi() {
2021-02-28 20:23:50 +08:00
var err error
e, err := object.GetEnforcer(util.GetId("built-in", "api-enforcer-built-in"))
2021-02-28 20:23:50 +08:00
if err != nil {
panic(err)
}
Enforcer, err = e.InitEnforcer()
2021-02-28 20:23:50 +08:00
if err != nil {
panic(err)
}
Enforcer.ClearPolicy()
// if len(Enforcer.GetPolicy()) == 0 {
2021-02-28 23:14:48 +08:00
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, *, *, GET, /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/logout, *, *
2021-02-28 23:14:48 +08:00
p, *, *, GET, /api/get-account, *, *
p, *, *, GET, /api/userinfo, *, *
p, *, *, GET, /api/user, *, *
2023-05-16 21:47:34 +08:00
p, *, *, GET, /api/health, *, *
p, *, *, POST, /api/webhook, *, *
p, *, *, GET, /api/get-webhook-event, *, *
p, *, *, GET, /api/get-captcha-status, *, *
p, *, *, *, /api/login/oauth, *, *
2021-02-28 23:14:48 +08:00
p, *, *, GET, /api/get-application, *, *
p, *, *, GET, /api/get-organization-applications, *, *
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-09-18 16:16:45 +08:00
p, *, *, GET, /api/get-records, *, *
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-04-27 01:06:54 +08:00
p, *, *, POST, /api/update-payment, *, *
p, *, *, POST, /api/invoice-payment, *, *
2022-07-17 14:29:06 +08:00
p, *, *, POST, /api/notify-payment, *, *
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-captcha, *, *
p, *, *, POST, /api/verify-captcha, *, *
p, *, *, POST, /api/verify-code, *, *
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, *, *, GET, /api/saml/metadata, *, *
p, *, *, *, /cas, *, *
p, *, *, *, /api/webauthn, *, *
p, *, *, GET, /api/get-release, *, *
p, *, *, GET, /api/get-default-application, *, *
2023-04-25 16:06:09 +08:00
p, *, *, GET, /api/get-prometheus-info, *, *
p, *, *, *, /api/metrics, *, *
p, *, *, GET, /api/get-pricing, *, *
p, *, *, GET, /api/get-plan, *, *
2023-05-27 19:02:54 +08:00
p, *, *, GET, /api/get-organization-names, *, *
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 {
if conf.IsDemoMode() {
if !isAllowedInDemoMode(subOwner, subName, method, urlPath, objOwner, objName) {
return false
}
}
user, err := object.GetUser(util.GetId(subOwner, subName))
if err != nil {
panic(err)
}
if user != nil && user.IsAdmin && (subOwner == objOwner || (objOwner == "admin")) {
2022-10-07 15:59:23 +08:00
return true
}
2021-02-28 23:14:48 +08:00
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
}
func isAllowedInDemoMode(subOwner string, subName string, method string, urlPath string, objOwner string, objName string) bool {
if method == "POST" {
2023-03-02 21:40:27 +08:00
if strings.HasPrefix(urlPath, "/api/login") || urlPath == "/api/logout" || urlPath == "/api/signup" || urlPath == "/api/send-verification-code" || urlPath == "/api/send-email" || urlPath == "/api/verify-captcha" {
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
}