sky/internal/platform/rbac/rbac.go
root 6ed55254a0 chore: initial import of sky project
Go + PostgreSQL + Valkey backend (cmd/internal/migrations),
uni-app mobile frontend, PC web (web/), admin console (admin/),
Android WebView container (android_project/).
Build artifacts, node_modules, local env and debug screenshots excluded.
2026-11-09 22:22:44 +08:00

130 lines
3.4 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.

package rbac
// Role 与权限模型。继承链:super_admin > operator > reviewer > author > user > guest。
type Role string
const (
RoleGuest Role = "guest"
RoleUser Role = "user"
RoleAuthor Role = "author"
RoleReviewer Role = "reviewer"
RoleOperator Role = "operator"
RoleSuperAdmin Role = "super_admin"
)
// weight 越大权限越高,用于 minRole 比较
var weight = map[Role]int{
RoleGuest: 0,
RoleUser: 10,
RoleAuthor: 20,
RoleReviewer: 30,
RoleOperator: 40,
RoleSuperAdmin: 100,
}
func (r Role) Weight() int {
if w, ok := weight[r]; ok {
return w
}
return -1
}
// AtLeast 判断当前角色是否达到要求
func (r Role) AtLeast(min Role) bool { return r.Weight() >= min.Weight() }
// 权限点
const (
PermArticleRead = "article:read"
PermArticleCreate = "article:create"
PermArticleEditOwn = "article:edit_own"
PermArticleDeleteOwn = "article:delete_own"
PermArticlePublish = "article:publish"
PermArticleReview = "article:review"
PermArticleManageAll = "article:manage_all"
PermCommentCreate = "comment:create"
PermCommentReview = "comment:review"
PermCommentManage = "comment:manage_all"
PermCategoryRead = "category:read"
PermCategoryManage = "category:manage"
PermTagRead = "tag:read"
PermTagManage = "tag:manage"
PermLike = "article:like"
PermFavorite = "article:favorite"
PermFollow = "user:follow"
PermReportMake = "report:create"
PermReportRead = "report:read"
PermReportHandle = "report:handle"
PermNotifRead = "notification:read"
PermWalletRead = "wallet:read"
PermWalletWithdraw = "wallet:withdraw"
PermStatsOwn = "stats:read_own"
PermStatsAll = "stats:read_all"
PermUserManage = "user:manage"
PermSystemSetting = "system:setting"
PermTopicManage = "topic:manage"
PermTemplateManage = "template:manage"
)
// grants 各角色的直接授权。查权限时从当前角色向上累积(继承链语义)。
var grants = map[Role][]string{
RoleGuest: {PermArticleRead, PermCategoryRead, PermTagRead},
RoleUser: {
PermArticleRead, PermCategoryRead, PermTagRead,
PermLike, PermFavorite, PermFollow, PermReportMake, PermNotifRead,
PermCommentCreate,
},
RoleAuthor: {
PermArticleCreate, PermArticleEditOwn, PermArticleDeleteOwn, PermArticlePublish,
PermWalletRead, PermWalletWithdraw, PermStatsOwn,
},
RoleReviewer: {PermArticleReview, PermCommentReview, PermReportRead},
RoleOperator: {
PermArticleManageAll, PermCommentManage, PermCategoryManage, PermTagManage,
PermReportHandle, PermStatsAll, PermTopicManage, PermTemplateManage,
},
RoleSuperAdmin: {PermUserManage, PermSystemSetting},
}
// 继承链:低角色自动拥有高角色不需要的那些权限?
// 本设计的语义是「向上累积」:reviewer 同时拥有 author 与 user 的能力。
var chain = map[Role][]Role{
RoleUser: {RoleGuest},
RoleAuthor: {RoleUser},
RoleReviewer: {RoleAuthor},
RoleOperator: {RoleReviewer},
RoleSuperAdmin: {RoleOperator},
}
// Has 判断角色是否拥有某权限点
func Has(r Role, perm string) bool {
seen := map[Role]bool{}
var walk func(Role) bool
walk = func(cur Role) bool {
if seen[cur] {
return false
}
seen[cur] = true
for _, p := range grants[cur] {
if p == perm {
return true
}
}
for _, parent := range chain[cur] {
if walk(parent) {
return true
}
}
return false
}
return walk(r)
}