分类

链接

2025 年 8 月
 123
45678910
11121314151617
18192021222324
25262728293031

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Python > 正文
共享办公室出租
authentic如何把roles返回给sso客户端
Python 暂无评论 阅读(3)
在 authentik 中,Role 并不是直接挂在 User 对象上的,而是:
User → Group → Role
所以要想拿到“用户真正拥有的所有 Role”,必须先遍历用户所在的 Group,再把每个 Group 关联的 Role 收集起来并去重。
把你的映射改成下面这样即可(已用你当前可用的变量名 userrequest.user 做了兼容):
# 取用户所在的所有组的所有角色,去重后返回
roles = set()
for group in request.user.ak_groups.all():
    roles.update(group.roles.all())

return {
    "_debug_all_attrs": list(user.attributes.keys()),  # 调试用,可删
    "employee_id": user.attributes.get("employee_id", ""),
    "employeeid": user.attributes.get("employeeid", ""),
    "department":  user.attributes.get("department", ""),
    "roles": [r.name for r in roles],   # 角色名称列表
}

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:authentic如何把roles返回给sso客户端 | Bruce's Blog

发表评论

留言无头像?