Skip to content
Discussion options

You must be logged in to vote
module main

// 定义 RBAC 核心结构
struct RBAC {
mut:
	// 存储角色-权限映射 (角色 -> 资源-操作列表)
	role_permissions map[string][]Permission
	// 存储用户-角色映射 (用户 -> 角色列表)
	user_roles map[string][]string
}

// 权限结构:资源 + 操作
struct Permission {
	resource string
	action   string
}

// 创建新 RBAC 实例
pub fn new_rbac() &RBAC {
	return &RBAC{
		role_permissions: map[string][]Permission{}
		user_roles:       map[string][]string{}
	}
}

// 添加角色权限
pub fn (mut r RBAC) add_permission(role string, resource string, action string) {
	perm := Permission{resource, action}

	// 简化检查逻辑,避免未使用变量
	if role in r.role_permissions {
		r.role_permissions[role] << perm
	} else {
		r.role_permissions[role] = [perm]
	}
}

// 分配用户角色(
pub fn (mut…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by Jengro777
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant