Members page previously only displayed phone status; add bind/replace UI and admin member update phone support for login and sync identity.
83 lines
2.4 KiB
Go
83 lines
2.4 KiB
Go
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)
|
||
}
|
||
|
||
// SetPhone 管理员为本公司成员绑定/更换/清空手机号(空字符串解绑)。
|
||
func (l *MemberLogic) SetPhone(userID int64, phone string) (*userstore.User, error) {
|
||
tid := authx.TenantID(l.ctx)
|
||
if tid <= 0 {
|
||
return nil, fmt.Errorf("missing tenant")
|
||
}
|
||
u, err := l.svcCtx.Users.GetByID(l.ctx, userID)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
if u.TenantID != tid {
|
||
return nil, fmt.Errorf("成员不属于本公司")
|
||
}
|
||
return l.svcCtx.Users.BindPhone(l.ctx, userID, phone)
|
||
}
|