chore: initial commit of ai site platform
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
66
platform/internal/logic/applogic/members.go
Normal file
66
platform/internal/logic/applogic/members.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package applogic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"aijianzhan/platform/internal/authx"
|
||||
"aijianzhan/platform/internal/userstore"
|
||||
)
|
||||
|
||||
type MemberLogic struct {
|
||||
*AuthLogic
|
||||
}
|
||||
|
||||
func NewMemberLogic(l *AuthLogic) *MemberLogic {
|
||||
return &MemberLogic{AuthLogic: l}
|
||||
}
|
||||
|
||||
func (l *MemberLogic) List() ([]userstore.User, error) {
|
||||
tid := authx.TenantID(l.ctx)
|
||||
if tid <= 0 {
|
||||
return nil, fmt.Errorf("missing tenant")
|
||||
}
|
||||
return l.svcCtx.Users.ListMembers(l.ctx, tid)
|
||||
}
|
||||
|
||||
// Create 创建成员:用户名始终随机唯一;password 为空则随机初始密码(用户登录后可自行修改)。
|
||||
func (l *MemberLogic) Create(password, displayName, role string, orgUnitID int64) (*userstore.User, string, error) {
|
||||
tid := authx.TenantID(l.ctx)
|
||||
if tid <= 0 {
|
||||
return nil, "", fmt.Errorf("missing tenant")
|
||||
}
|
||||
if role == "" {
|
||||
role = authx.Role编辑
|
||||
}
|
||||
uname, err := userstore.AllocUniqueUsername(l.ctx, l.svcCtx.Users.UsernameExists)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
u, plain, err := l.svcCtx.Users.CreateMember(l.ctx, tid, uname, password, displayName, role, orgUnitID)
|
||||
if err != nil {
|
||||
// 极罕见冲突:再试一次
|
||||
if strings.Contains(err.Error(), "already exists") {
|
||||
uname2, e2 := userstore.AllocUniqueUsername(l.ctx, l.svcCtx.Users.UsernameExists)
|
||||
if e2 != nil {
|
||||
return nil, "", e2
|
||||
}
|
||||
return l.svcCtx.Users.CreateMember(l.ctx, tid, uname2, password, displayName, role, orgUnitID)
|
||||
}
|
||||
return nil, "", err
|
||||
}
|
||||
return u, plain, nil
|
||||
}
|
||||
|
||||
func (l *MemberLogic) Update(userID int64, role string, orgUnitID int64, status string) (*userstore.User, error) {
|
||||
tid := authx.TenantID(l.ctx)
|
||||
if tid <= 0 {
|
||||
return nil, fmt.Errorf("missing tenant")
|
||||
}
|
||||
if userID == authx.UserID(l.ctx) && role != "" && authx.NormalizeRole(role) != authx.Role管理员 {
|
||||
if authx.IsCompanyAdmin(authx.Role(l.ctx)) {
|
||||
return nil, fmt.Errorf("不能修改自己的角色,请由其他管理员操作")
|
||||
}
|
||||
}
|
||||
return l.svcCtx.Users.UpdateMember(l.ctx, tid, userID, role, orgUnitID, status)
|
||||
}
|
||||
Reference in New Issue
Block a user