feat(权限优化)1.AI对话中点击快捷标签问题时,明明已经配置了默认默写为什么还会提示配置?必须点击一下保存并关闭才可以这个不对,只要配置了就可以问答不需要再让用户过来点击一次!如图一、二所示
deploy-hxtesting / build-and-deploy (push) Successful in 1m20s

2.知识库中的企业培训(课件)、企业培训(试题)、配置中心、组织架构、共享空间如图三所示可查看权限归属到超级管理员权限中!如何对
This commit is contained in:
runny committed 2026-11-27 14:49:06 +08:00
1 parent 353f60a175
commit 043e0485e2
14 files changed
+368 -74

No files matched your search

+49 -28
View File
@@ -579,11 +579,10 @@ func (s *userService) finishOIDCLogin(
}
isNewUser := false
if isUserLookupNotFound(err) || user == nil {
// 老账号自愈:占位邮箱曾按 sub(可能是长 UUID)合成,规则已改为
// 工号优先。同一 Casdoor 用户下次登录时按 email 查不到,会落到
// 这里——先按 username(工号)找回老账号,把它改成新短邮箱,避免
// 重复开户。仅当命中账号确属 OIDC 开户且现邮箱是同域占位邮箱时
// 才收编。
// 老账号自愈:按 email 查不到时(占位邮箱规则变更,或导入/手建
// 账号与 IdP 邮箱前缀不一致),先按 username(工号)收编活跃老
// 账号并归一邮箱,避免重复开户分裂出 26040006-1 式账号。收编
// 条件见 adoptOIDCUserByUsername。
user = s.adoptOIDCUserByUsername(ctx, userInfo)
if user == nil {
user, err = s.provisionOIDCUser(ctx, userInfo, provisioning)
@@ -2188,27 +2187,22 @@ func (s *userService) provisionOIDCUser(
return user, nil
}
// adoptOIDCUserByUsername 收编按旧规则开户的 OIDC 老账号。占位邮箱曾按
// sub(可能是长 UUID)合成;规则改为工号优先后,同一 Casdoor 用户下次
// 登录按 email 查不到,这里按 username(工号)找回老账号并改写为短邮箱,
// 避免重复开户。收编条件全部满足才生效:
// adoptOIDCUserByUsername 工号收编:按 email 查不到时(占位邮箱规则变更,
// 或导入/手建账号与 IdP 邮箱前缀不一致),按 username(工号)找回本地老
// 账号,根治同一工号分裂出 "26040006-1" 式重复账号——OIDC 首登自动开户
// 撞上已占用工号后会顺延 -1/-2 后缀。收编条件全部满足才生效:
// 1. username 精确命中本地账号且账号活跃;
// 2. 该账号是 OIDC 开户(OidcOnlyLogin);
// 3. 现邮箱是同兜底域的占位邮箱;
// 4. 新邮箱(本次登录刚按新规则合成,查询未命中)必然未被占用。
// 2. 本地邮箱可信:是同兜底域的占位邮箱,或与 IdP 邮箱同域(同一企业
// 的两种邮箱形态,如 26040006@corp ↔ sun.kai@corp);完全无关的
// 外部邮箱不收编;
// 3. 新邮箱(本次登录 IdP 返回/合成,finishOIDCLogin 已查询未命中)必然
// 未被占用,直接改写本地邮箱完成归一。
//
// 返回 nil 表示无可收编账号,调用方按新用户正常开户。失败仅告警,不阻塞登录。
// 收编不再要求账号是 OIDC 开户(OidcOnlyLogin):导入开户与手工开户的
// 工号账号同样认回。返回 nil 表示无可收编账号,调用方按新用户正常开户。
// 失败仅告警,不阻塞登录。
func (s *userService) adoptOIDCUserByUsername(ctx context.Context, info *types.OIDCUserInfo) *types.User {
if s.config == nil || s.config.OIDCAuth == nil || s.userRepo == nil {
return nil
}
fallbackDomain := strings.ToLower(strings.TrimPrefix(strings.TrimSpace(s.config.OIDCAuth.EmailFallbackDomain), "@"))
if fallbackDomain == "" {
return nil
}
if strings.Contains(info.Email, "@") &&
!strings.HasSuffix(strings.ToLower(info.Email), "@"+fallbackDomain) {
// IdP 返回了真实邮箱,不涉及占位邮箱改写。
if s.userRepo == nil {
return nil
}
candidate := sanitizeUsernameCandidate(info.Username)
@@ -2219,10 +2213,11 @@ func (s *userService) adoptOIDCUserByUsername(ctx context.Context, info *types.O
if err != nil || existing == nil || !existing.IsActive {
return nil
}
if existing.Preferences.OidcOnlyLogin == nil || !*existing.Preferences.OidcOnlyLogin {
return nil
fallbackDomain := ""
if s.config != nil && s.config.OIDCAuth != nil {
fallbackDomain = strings.ToLower(strings.TrimPrefix(strings.TrimSpace(s.config.OIDCAuth.EmailFallbackDomain), "@"))
}
if !strings.HasSuffix(strings.ToLower(existing.Email), "@"+fallbackDomain) {
if !adoptableOIDCEmail(existing.Email, info.Email, fallbackDomain) {
return nil
}
if info.Email == "" || info.Email == existing.Email {
@@ -2234,13 +2229,39 @@ func (s *userService) adoptOIDCUserByUsername(ctx context.Context, info *types.O
existing.DisplayName = displayName
}
if err := s.userRepo.UpdateUser(ctx, existing); err != nil {
logger.Warnf(ctx, "OIDC placeholder-email adoption failed for user %s: %v", existing.ID, err)
logger.Warnf(ctx, "OIDC username adoption failed for user %s: %v", existing.ID, err)
return nil
}
logger.Infof(ctx, "OIDC placeholder email rewritten: user %s email → %s", existing.ID, existing.Email)
logger.Infof(ctx, "OIDC account adopted by username: user %s email → %s", existing.ID, existing.Email)
return existing
}
// adoptableOIDCEmail 判断本地账号邮箱是否可信、可被 OIDC 工号收编:
// - 同兜底域的占位邮箱(OIDC/导入开户按 <工号>@兜底域 合成);或
// - 与 IdP 邮箱同域。
//
// 其余(无关外部邮箱)不收编,避免把陌生账号错认给登录者。
func adoptableOIDCEmail(existingEmail, idpEmail, fallbackDomain string) bool {
existingDomain := emailDomain(existingEmail)
if existingDomain == "" {
return false
}
if fallbackDomain != "" && existingDomain == fallbackDomain {
return true
}
return existingDomain == emailDomain(idpEmail)
}
// emailDomain 取小写域名;无 @、@ 顶头/收尾视为畸形,返回空串。
func emailDomain(value string) string {
value = strings.ToLower(strings.TrimSpace(value))
at := strings.LastIndex(value, "@")
if at <= 0 || at == len(value)-1 {
return ""
}
return value[at+1:]
}
// refreshOIDCDisplayName 让姓名跟随 IdP:登录时若 claim 姓名变化则更新。
// 失败仅告警,不阻塞登录。
func (s *userService) refreshOIDCDisplayName(ctx context.Context, user *types.User, info *types.OIDCUserInfo) {
@@ -457,3 +457,158 @@ func TestFinishOIDCLoginRefreshesDisplayName(t *testing.T) {
t.Fatalf("UpdateUser calls = %d, want 1 (refresh persisted)", len(repo.updated))
}
}
// Regression for the 192-environment duplicate: a roster-imported 工号 account
// (real email, NOT OIDC-marked) whose email prefix differs from the IdP email
// must be adopted on first SSO login — not split into a fresh 26040006-1.
func TestFinishOIDCLoginAdoptsImportedAccountWithSameDomainEmail(t *testing.T) {
imported := &types.User{
ID: "26040006",
Username: "26040006",
Email: "26040006@hxzh.com", // 导入开户的真实邮箱,前缀与 IdP 不同
TenantID: 6,
IsActive: true,
}
repo := &sharedSpaceUserRepo{
getUserByEmail: nil, // IdP 邮箱按 email 查未命中
byUsername: map[string]*types.User{"26040006": imported},
}
memberSvc := &sharedSpaceMemberSvc{membersByUser: []*types.TenantMember{
{UserID: "26040006", TenantID: 6, Role: types.TenantRoleOwner, Status: types.TenantMemberStatusActive},
}}
svc := newSharedSpaceService(t, false, repo, memberSvc, &sharedSpaceInvitationSvc{}, &sharedSpaceTenantSvc{})
svc.config = oidcTestConfig()
svc.tokenRepo = &stubAuthTokenRepo{}
resp, err := svc.finishOIDCLogin(context.Background(), &types.OIDCUserInfo{
Username: "26040006",
Email: "sun.kai@hxzh.com", // IdP 真实邮箱,同域不同前缀
DisplayName: "孙凯",
}, types.TenantProvisioningCreatePersonal)
if err != nil {
t.Fatalf("finishOIDCLogin: %v", err)
}
if !resp.Success || resp.User == nil || resp.User.ID != "26040006" {
t.Fatalf("expected imported account adoption, got resp=%+v", resp)
}
if resp.IsNewUser {
t.Fatal("adoption must not be reported as a new user (26040006-1 regression)")
}
if resp.User.Email != "sun.kai@hxzh.com" {
t.Fatalf("email not normalized to IdP value: %s", resp.User.Email)
}
if len(repo.updated) == 0 {
t.Fatal("adoption must persist the rewritten account")
}
}
func TestAdoptOIDCUserByUsernamePlaceholderWithoutOidcMark(t *testing.T) {
// 占位邮箱但从未标过 OidcOnlyLogin(早期手工开户):工号命中即可收编。
existing := &types.User{
ID: "26040006", Username: "26040006",
Email: "26040006@casdoor.local", IsActive: true,
}
repo := &sharedSpaceUserRepo{
byUsername: map[string]*types.User{"26040006": existing},
}
svc := &userService{userRepo: repo, config: oidcTestConfig()}
adopted := svc.adoptOIDCUserByUsername(context.Background(), &types.OIDCUserInfo{
Username: "26040006",
Email: "sun.kai@hxzh.com",
})
if adopted == nil || adopted.ID != "26040006" {
t.Fatalf("expected adoption, got %+v", adopted)
}
if len(repo.updated) != 1 || repo.updated[0].Email != "sun.kai@hxzh.com" {
t.Fatalf("email rewrite not persisted: %+v", repo.updated)
}
}
func TestAdoptOIDCUserByUsernameRejectsUnrelatedEmail(t *testing.T) {
// 本地邮箱是完全无关的外部邮箱(不同域、非占位):不收编,避免把
// 陌生账号错认给登录者。
existing := &types.User{
ID: "26040006", Username: "26040006",
Email: "someone@gmail.com", IsActive: true,
}
repo := &sharedSpaceUserRepo{
byUsername: map[string]*types.User{"26040006": existing},
}
svc := &userService{userRepo: repo, config: oidcTestConfig()}
adopted := svc.adoptOIDCUserByUsername(context.Background(), &types.OIDCUserInfo{
Username: "26040006",
Email: "sun.kai@hxzh.com",
})
if adopted != nil {
t.Fatalf("unrelated email must not be adopted, got %+v", adopted)
}
if len(repo.updated) != 0 {
t.Fatalf("no writes expected, got %+v", repo.updated)
}
}
func TestAdoptOIDCUserByUsernameRejectsInactiveAccount(t *testing.T) {
existing := &types.User{
ID: "26040006", Username: "26040006",
Email: "26040006@hxzh.com", IsActive: false,
}
repo := &sharedSpaceUserRepo{
byUsername: map[string]*types.User{"26040006": existing},
}
svc := &userService{userRepo: repo, config: oidcTestConfig()}
adopted := svc.adoptOIDCUserByUsername(context.Background(), &types.OIDCUserInfo{
Username: "26040006",
Email: "sun.kai@hxzh.com",
})
if adopted != nil {
t.Fatalf("inactive account must not be adopted, got %+v", adopted)
}
}
func TestAdoptOIDCUserByUsernameSameDomainWithoutFallbackDomain(t *testing.T) {
// 未配置兜底域时占位邮箱分支不可用,但同域收编仍然生效。
existing := &types.User{
ID: "26040006", Username: "26040006",
Email: "26040006@hxzh.com", IsActive: true,
}
repo := &sharedSpaceUserRepo{
byUsername: map[string]*types.User{"26040006": existing},
}
svc := &userService{userRepo: repo} // config 为 nil
adopted := svc.adoptOIDCUserByUsername(context.Background(), &types.OIDCUserInfo{
Username: "26040006",
Email: "sun.kai@hxzh.com",
})
if adopted == nil || adopted.ID != "26040006" {
t.Fatalf("same-domain adoption must work without fallback domain, got %+v", adopted)
}
}
func TestAdoptableOIDCEmail(t *testing.T) {
cases := []struct {
name string
existingEmail string
idpEmail string
fallback string
want bool
}{
{"placeholder in fallback domain", "26040006@casdoor.local", "sun.kai@hxzh.com", "casdoor.local", true},
{"same domain as idp", "26040006@hxzh.com", "sun.kai@hxzh.com", "casdoor.local", true},
{"same domain without fallback config", "26040006@hxzh.com", "sun.kai@hxzh.com", "", true},
{"unrelated external email", "someone@gmail.com", "sun.kai@hxzh.com", "casdoor.local", false},
{"malformed existing email", "not-an-email", "sun.kai@hxzh.com", "casdoor.local", false},
{"empty existing email", "", "sun.kai@hxzh.com", "casdoor.local", false},
{"case-insensitive domain match", "26040006@HXZH.com", "sun.kai@hxzh.com", "", true},
}
for _, tc := range cases {
if got := adoptableOIDCEmail(tc.existingEmail, tc.idpEmail, tc.fallback); got != tc.want {
t.Errorf("%s: adoptableOIDCEmail(%q, %q, %q) = %v, want %v",
tc.name, tc.existingEmail, tc.idpEmail, tc.fallback, got, tc.want)
}
}
}
@@ -405,9 +405,10 @@ func TestResolveOIDCUserInfo_VerifiedIDTokenUsedWhenUserinfoFails(t *testing.T)
}
}
// 工号式 IdP(邮箱选填):id_token 无 email claim 时,配置了兜底域则用已验签
// 的 sub 合成稳定占位邮箱,工号(name claim)继续作为 username。
func TestResolveOIDCUserInfo_EmailFallbackSynthesizedFromSub(t *testing.T) {
// 工号式 IdP(邮箱选填):id_token 无 email claim 时,配置了兜底域则合成
// 稳定占位邮箱。合成 local part 工号(username claim)优先、sub 仅在
// username 为空时回落——工号比 Casdoor 可能的长 UUID sub 更短更稳定。
func TestResolveOIDCUserInfo_EmailFallbackSynthesizedFromUsername(t *testing.T) {
withOIDCSSRFWhitelist(t, "127.0.0.1")
key, err := rsa.GenerateKey(rand.Reader, 2048)
@@ -430,8 +431,8 @@ func TestResolveOIDCUserInfo_EmailFallbackSynthesizedFromSub(t *testing.T) {
if err != nil {
t.Fatalf("resolveOIDCUserInfo: %v", err)
}
if info.Email != "built-in_10001@casdoor.local" {
t.Fatalf("email = %q, want synthesized from sub", info.Email)
if info.Email != "10001@casdoor.local" {
t.Fatalf("email = %q, want synthesized from 工号 username", info.Email)
}
if info.Username != "10001" {
t.Fatalf("username = %q, want 工号 from name claim", info.Username)