45 lines
879 B
Go
45 lines
879 B
Go
package scope
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// Split 将空格分隔的 scope 拆成列表。
|
|
func Split(scope string) []string {
|
|
if strings.TrimSpace(scope) == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Fields(scope)
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
p = strings.TrimSpace(p)
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// Contains 判断 scope 字符串是否包含指定权限标记。
|
|
func Contains(scope, want string) bool {
|
|
if want == "" {
|
|
return true
|
|
}
|
|
for _, s := range Split(scope) {
|
|
if s == want {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// HasAPIAccess 约定含 `api` 或 `api.*` 前缀即表示可访问业务 API(可与 IAM 菜单权限组合使用)。
|
|
func HasAPIAccess(scope string) bool {
|
|
for _, s := range Split(scope) {
|
|
if s == "api" || strings.HasPrefix(s, "api.") {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|