Skip to content

Commit 58168f7

Browse files
committed
refactor(backend): 重构用户和组的修改接口并优化相关服务
- 修改 LdapClient 中的 Modify 方法名称为 ModifyAttributes,以更清晰地表示其功能 - 在 User 结构体中添加 LoginShell 字段,用于存储用户的登录 Shell - 更新 RepositoryGroup 和 RepositoryUser 中的 Modify 方法为 ModifyAttributes - 调整 ServiceGroup 和 ServiceUser 中的相关调用,使用新的 ModifyAttributes 方法 - 在 ServiceManager 中为新注册的用户设置默认的 LoginShell 为 /bin/bash - 优化 LDAP 属性解析逻辑,跳过 dnAttr 字段的处理
1 parent f20bcad commit 58168f7

8 files changed

Lines changed: 18 additions & 17 deletions

File tree

backend/pkg/client/ldap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (c *LdapClient) Add(dn string, objectClass []string, attributes map[string]
149149
})
150150
}
151151

152-
func (c *LdapClient) Modify(dn string, addAttrs, delAttrs, replaceAttrs map[string][]string) error {
152+
func (c *LdapClient) ModifyAttributes(dn string, addAttrs, delAttrs, replaceAttrs map[string][]string) error {
153153
return c.withConnection(func(conn *ldap.Conn) error {
154154
modifyReq := ldap.NewModifyRequest(dn, nil)
155155
for attr, values := range addAttrs {

backend/pkg/entity/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ type User struct {
1111
HomeDirectory string `ldap:"homeDirectory" json:"homeDirectory"`
1212
Mail string `ldap:"mail" json:"mail"`
1313
UserPassword string `ldap:"userPassword" json:"userPassword"`
14+
LoginShell string `ldap:"loginShell" json:"loginShell"`
1415
}

backend/pkg/repository/group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ func (r *RepositoryGroup) FindAllByOuAndMemberUid(ou string, uid string) ([]*ent
8080
return r.find(fmt.Sprintf("ou=%s", ou), fmt.Sprintf("memberUid=%s", uid))
8181
}
8282

83-
func (r *RepositoryGroup) Modify(dn string, addAttrs map[string][]string, delAttrs map[string][]string, replaceAttrs map[string][]string) error {
84-
return r.client.Modify(dn, addAttrs, delAttrs, replaceAttrs)
83+
func (r *RepositoryGroup) ModifyAttributes(dn string, addAttrs map[string][]string, delAttrs map[string][]string, replaceAttrs map[string][]string) error {
84+
return r.client.ModifyAttributes(dn, addAttrs, delAttrs, replaceAttrs)
8585
}

backend/pkg/repository/user.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"asynclab.club/asynx/backend/pkg/config"
88
"asynclab.club/asynx/backend/pkg/entity"
99
"asynclab.club/asynx/backend/pkg/transfer"
10+
11+
1012
)
1113

1214
type RepositoryUser struct {
@@ -92,13 +94,13 @@ func (r *RepositoryUser) Create(user *entity.User) error {
9294
return r.client.Add(r.BuildDn(user), config.UserObjectClasses, attributes)
9395
}
9496

95-
func (r *RepositoryUser) Modify(user *entity.User) error {
97+
func (r *RepositoryUser) ModifyAttributes(user *entity.User) error {
9698
attributes, err := transfer.ParseToLdapAttributes(user)
9799
if err != nil {
98100
return err
99101
}
100102

101-
return r.client.Modify(r.BuildDn(user), nil, nil, attributes)
103+
return r.client.ModifyAttributes(r.BuildDn(user), nil, nil, attributes)
102104
}
103105

104106
func (r *RepositoryUser) ModifyDn(user *entity.User, newRDN, newSuperior string) error {

backend/pkg/service/group.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,12 @@ func (s *ServiceGroup) RevokeRoleByUid(uid string) error {
7171
return err
7272
}
7373
for _, group := range roleGroups {
74-
return s.repositoryGroup.Modify(s.repositoryGroup.BuildDn(group), nil, attr, nil)
74+
return s.repositoryGroup.ModifyAttributes(s.repositoryGroup.BuildDn(group), nil, attr, nil)
7575
}
7676
return nil
7777
}
7878

79+
// WARN: 千万不要用replaceAttrs在多值属性上,否则会清空其他不相干值
7980
func (s *ServiceGroup) GrantRoleByUid(uid string, newRole security.Role) error {
8081
oldRole, err := s.GetRoleByUid(uid)
8182
if err != nil {
@@ -110,7 +111,7 @@ func (s *ServiceGroup) GrantRoleByUid(uid string, newRole security.Role) error {
110111

111112
// 如果用户之前没有角色(直接添加)
112113
if oldRole == security.RoleAnonymous {
113-
return s.repositoryGroup.Modify(s.repositoryGroup.BuildDn(newGroup), attr, nil, nil) // 添加用户
114+
return s.repositoryGroup.ModifyAttributes(s.repositoryGroup.BuildDn(newGroup), attr, nil, nil) // 添加用户
114115
}
115116

116117
// 如果是角色切换:先从旧组移除,再添加到新组
@@ -121,14 +122,14 @@ func (s *ServiceGroup) GrantRoleByUid(uid string, newRole security.Role) error {
121122
}
122123

123124
if !oldNotFound {
124-
if err := s.repositoryGroup.Modify(s.repositoryGroup.BuildDn(oldGroup), nil, attr, nil); err != nil {
125+
if err := s.repositoryGroup.ModifyAttributes(s.repositoryGroup.BuildDn(oldGroup), nil, attr, nil); err != nil {
125126
return err
126127
}
127128
}
128-
if err := s.repositoryGroup.Modify(s.repositoryGroup.BuildDn(newGroup), attr, nil, nil); err != nil {
129+
if err := s.repositoryGroup.ModifyAttributes(s.repositoryGroup.BuildDn(newGroup), attr, nil, nil); err != nil {
129130
// 回滚
130131
if !oldNotFound {
131-
if err = s.repositoryGroup.Modify(s.repositoryGroup.BuildDn(oldGroup), attr, nil, nil); err != nil {
132+
if err = s.repositoryGroup.ModifyAttributes(s.repositoryGroup.BuildDn(oldGroup), attr, nil, nil); err != nil {
132133
logrus.Warningf("Failed to rollback group modification when grant role: %v", err)
133134
}
134135
}

backend/pkg/service/manager.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (s *ServiceManager) Register(username, surName, givenName, mail, category,
104104
HomeDirectory: fmt.Sprintf("/home/%s", username),
105105
Mail: mail,
106106
UserPassword: password,
107+
LoginShell: "/bin/bash",
107108
}
108109

109110
if err := s.serviceUser.Create(user); err != nil {

backend/pkg/service/user.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ func (s *ServiceUser) Create(user *entity.User) error {
5454
return s.repositoryUser.Create(user)
5555
}
5656

57-
func (s *ServiceUser) Modify(user *entity.User) error {
58-
return s.repositoryUser.Modify(user)
59-
}
60-
6157
func (s *ServiceUser) ModifyPassword(user *entity.User, newPassword string) error {
6258
return s.repositoryUser.ModifyPassword(user, newPassword)
6359
}

backend/pkg/transfer/ldap.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,10 @@ func ParseToLdapAttributes[T any](item *T) (map[string][]string, error) {
148148
continue
149149
}
150150

151-
isDnField, isTransient, attrName, _, _ := parseTag(ldapTag)
151+
isDnField, isTransient, attrName, dnAttr, _ := parseTag(ldapTag)
152152

153-
// 跳过 transient 字段和 DN 字段(DN字段通常在创建条目时单独处理
154-
if isTransient || isDnField {
153+
// 跳过 transient、DN、dnAttr
154+
if isTransient || isDnField || dnAttr != "" {
155155
continue
156156
}
157157

0 commit comments

Comments
 (0)