Files
mc-gateway/internal/adminuser/user.go
tursom f508ecc1b9
Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
docs: 补充中文代码注释
2026-06-27 20:15:29 +08:00

86 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// internal/adminuser/user.go 定义管理用户角色、校验规则、密码哈希和对外用户视图。
package adminuser
import (
"errors"
"fmt"
"strings"
)
const (
// 角色按权限从高到低排列admin 管理所有资源member 管理路由和查看运行态,
// guest 只保留基础只读能力。
RoleAdmin = "admin"
RoleMember = "member"
RoleGuest = "guest"
)
type User struct {
Username string `json:"username"`
Role string `json:"role"`
Disabled bool `json:"disabled"`
CreatedAt int64 `json:"created_at"`
UpdatedAt int64 `json:"updated_at"`
}
func ValidateUsername(username string) error {
if username == "" {
return errors.New("username is required")
}
// 用户名会出现在 URL path 和审计记录中,因此禁止空白和斜杠。
if strings.ContainsAny(username, " \t\r\n/") {
return errors.New("username must not contain whitespace or /")
}
return nil
}
func ValidateRole(role string) error {
// 所有角色必须在这里登记,避免数据库里出现管理端无法解释的角色。
switch role {
case RoleAdmin, RoleMember, RoleGuest:
return nil
default:
return fmt.Errorf("invalid role %q", role)
}
}
func ValidatePassword(password string) error {
// 当前只做非空校验;更复杂的密码策略应放在产品策略确定后再补。
if strings.TrimSpace(password) == "" {
return errors.New("password is required")
}
return nil
}
func RoleRank(role string) int {
// rank 让权限判断保持单调:高角色天然包含低角色能力。
switch role {
case RoleAdmin:
return 3
case RoleMember:
return 2
case RoleGuest:
return 1
default:
return 0
}
}
func HasRole(actual, required string) bool {
return RoleRank(actual) >= RoleRank(required)
}
// Permissions 返回前端可直接消费的权限位;后端仍以角色校验为准。
func Permissions(role string) map[string]bool {
return map[string]bool{
"read_routes": HasRole(role, RoleGuest),
"write_routes": HasRole(role, RoleMember),
"read_status": HasRole(role, RoleMember),
"read_plugins": HasRole(role, RoleMember),
"manage_plugins": HasRole(role, RoleAdmin),
"manage_users": HasRole(role, RoleAdmin),
"manage_services": HasRole(role, RoleAdmin),
}
}