优化:平台管理员账号独立管理
This commit is contained in:
@@ -7,7 +7,6 @@ import (
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type UserService struct {
|
||||
@@ -28,15 +27,6 @@ type UserListQuery struct {
|
||||
Status *int
|
||||
}
|
||||
|
||||
// MerchantMemberGroup keeps employee assignments grouped by merchant for
|
||||
// platform account management. Pagination is intentionally based on merchants
|
||||
// so one merchant's employees are never split across pages.
|
||||
type MerchantMemberGroup struct {
|
||||
MerchantID uint `json:"merchant_id"`
|
||||
Merchant *model.Merchant `json:"merchant,omitempty"`
|
||||
Members []model.MerchantMember `json:"members"`
|
||||
}
|
||||
|
||||
func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
if q.Page < 1 {
|
||||
q.Page = 1
|
||||
@@ -68,53 +58,6 @@ func (s *UserService) List(q UserListQuery) ([]model.User, int64, error) {
|
||||
return list, total, err
|
||||
}
|
||||
|
||||
// ListMerchantMembers returns employee assignments grouped by merchant for platform operations.
|
||||
func (s *UserService) ListMerchantMembers(page, size int) ([]MerchantMemberGroup, int64, error) {
|
||||
page, size = normalizePage(page, size)
|
||||
tx := s.db.Model(&model.MerchantMember{}).
|
||||
Joins("JOIN users ON users.id = merchant_members.user_id AND users.deleted_at IS NULL").
|
||||
Where("users.role = ?", model.RoleMerchant)
|
||||
var total int64
|
||||
if err := tx.Distinct("merchant_members.merchant_id").Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var merchantIDs []uint
|
||||
if err := tx.Distinct("merchant_members.merchant_id").
|
||||
Order("merchant_members.merchant_id ASC").
|
||||
Offset((page-1)*size).Limit(size).Pluck("merchant_members.merchant_id", &merchantIDs).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if len(merchantIDs) == 0 {
|
||||
return []MerchantMemberGroup{}, total, nil
|
||||
}
|
||||
var members []model.MerchantMember
|
||||
err := s.db.Model(&model.MerchantMember{}).
|
||||
Joins("JOIN users ON users.id = merchant_members.user_id AND users.deleted_at IS NULL").
|
||||
Where("users.role = ? AND merchant_members.merchant_id IN ?", model.RoleMerchant, merchantIDs).
|
||||
Preload("Merchant").Preload("User").
|
||||
Order("merchant_members.merchant_id ASC, merchant_members.id ASC").
|
||||
Find(&members).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
groupsByMerchant := make(map[uint]*MerchantMemberGroup, len(merchantIDs))
|
||||
groups := make([]MerchantMemberGroup, 0, len(merchantIDs))
|
||||
for _, merchantID := range merchantIDs {
|
||||
group := MerchantMemberGroup{MerchantID: merchantID, Members: []model.MerchantMember{}}
|
||||
groups = append(groups, group)
|
||||
groupsByMerchant[merchantID] = &groups[len(groups)-1]
|
||||
}
|
||||
for _, member := range members {
|
||||
group := groupsByMerchant[member.MerchantID]
|
||||
if group.Merchant == nil {
|
||||
group.Merchant = member.Merchant
|
||||
}
|
||||
group.Members = append(group.Members, member)
|
||||
}
|
||||
return groups, total, nil
|
||||
}
|
||||
|
||||
func (s *UserService) Create(username, password, nickname, role string, merchantID uint) (*model.User, error) {
|
||||
var count int64
|
||||
s.db.Model(&model.User{}).Where("username = ?", username).Count(&count)
|
||||
@@ -169,6 +112,82 @@ func (s *UserService) Create(username, password, nickname, role string, merchant
|
||||
return user, nil
|
||||
}
|
||||
|
||||
type UpdatePlatformAdminInput struct {
|
||||
Username string
|
||||
Nickname string
|
||||
Password string
|
||||
Status int
|
||||
}
|
||||
|
||||
func (s *UserService) ListPlatformAdmins(page, size int) ([]model.User, int64, error) {
|
||||
return s.List(UserListQuery{Page: page, Size: size, Role: model.RoleAdmin})
|
||||
}
|
||||
|
||||
// CreatePlatformAdmin creates an account with platform-only privileges. It
|
||||
// deliberately does not assign the account to any merchant.
|
||||
func (s *UserService) CreatePlatformAdmin(username, password, nickname string) (*model.User, error) {
|
||||
return s.Create(username, password, nickname, model.RoleAdmin, 0)
|
||||
}
|
||||
|
||||
func (s *UserService) UpdatePlatformAdmin(id uint, in UpdatePlatformAdminInput, actorUserID uint) (*model.User, error) {
|
||||
if id == 0 {
|
||||
return nil, errors.New("用户不存在")
|
||||
}
|
||||
if id == actorUserID {
|
||||
return nil, errors.New("当前账号请通过右上角账户菜单修改密码")
|
||||
}
|
||||
if in.Status != 0 && in.Status != 1 {
|
||||
return nil, errors.New("用户状态无效")
|
||||
}
|
||||
returnUser := &model.User{}
|
||||
err := s.db.Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.First(&user, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("用户不存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if user.Role != model.RoleAdmin {
|
||||
return errors.New("只能管理平台管理员账号")
|
||||
}
|
||||
if user.Status == 1 && in.Status == 0 {
|
||||
var admins int64
|
||||
if err := tx.Model(&model.User{}).Where("role = ? AND status = ?", model.RoleAdmin, 1).Count(&admins).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if admins <= 1 {
|
||||
return errors.New("至少保留一个启用的平台管理员")
|
||||
}
|
||||
}
|
||||
if in.Username != user.Username {
|
||||
var count int64
|
||||
if err := tx.Model(&model.User{}).Where("username = ? AND id <> ?", in.Username, user.ID).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return errors.New("用户名已存在")
|
||||
}
|
||||
}
|
||||
updates := map[string]interface{}{"username": in.Username, "nickname": in.Nickname, "status": in.Status}
|
||||
if in.Password != "" {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(in.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
updates["password_hash"] = string(hash)
|
||||
}
|
||||
if err := tx.Model(&user).Updates(updates).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.First(returnUser, user.ID).Error
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return returnUser, nil
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateStatus(id uint, status int) error {
|
||||
if status != 0 && status != 1 {
|
||||
return errors.New("用户状态无效")
|
||||
@@ -194,61 +213,9 @@ func (s *UserService) UpdateStatus(id uint, status int) error {
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateRole changes the platform-level account role. Promoted administrators are
|
||||
// also made owners of the self-operated merchant for a consistent merchant context.
|
||||
func (s *UserService) UpdateRole(id uint, role string, actorUserID uint) error {
|
||||
if id == 0 {
|
||||
return errors.New("用户不存在")
|
||||
}
|
||||
if id == actorUserID {
|
||||
return errors.New("不能修改当前登录账号的系统角色")
|
||||
}
|
||||
if role != model.RoleAdmin && role != model.RoleMerchant {
|
||||
return errors.New("无效的账号角色")
|
||||
}
|
||||
return s.db.Transaction(func(tx *gorm.DB) error {
|
||||
var user model.User
|
||||
if err := tx.First(&user, id).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return errors.New("用户不存在")
|
||||
}
|
||||
return err
|
||||
}
|
||||
if user.Role == role {
|
||||
return nil
|
||||
}
|
||||
if user.Role == model.RoleAdmin && user.Status == 1 && role != model.RoleAdmin {
|
||||
var admins int64
|
||||
if err := tx.Model(&model.User{}).Where("role = ? AND status = ?", model.RoleAdmin, 1).Count(&admins).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if admins <= 1 {
|
||||
return errors.New("至少保留一个启用的平台管理员")
|
||||
}
|
||||
}
|
||||
if err := tx.Model(&user).Update("role", role).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if role != model.RoleAdmin {
|
||||
return nil
|
||||
}
|
||||
var merchant model.Merchant
|
||||
if err := tx.Where("code = ?", model.MerchantCodeSelfOperated).First(&merchant).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
member := model.MerchantMember{MerchantID: merchant.ID, UserID: user.ID, Role: model.MemberRoleOwner, Status: user.Status, IsDefault: true}
|
||||
return tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "merchant_id"}, {Name: "user_id"}},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
"role": model.MemberRoleOwner, "status": user.Status, "is_default": true,
|
||||
}),
|
||||
}).Create(&member).Error
|
||||
})
|
||||
}
|
||||
|
||||
// Delete removes a platform account and its merchant memberships. It protects the current
|
||||
// DeletePlatformAdmin removes a platform-only account. It protects the current
|
||||
// account and the final enabled platform administrator so the platform cannot be locked out.
|
||||
func (s *UserService) Delete(id, actorUserID uint) error {
|
||||
func (s *UserService) DeletePlatformAdmin(id, actorUserID uint) error {
|
||||
if id == 0 {
|
||||
return errors.New("用户不存在")
|
||||
}
|
||||
@@ -263,6 +230,9 @@ func (s *UserService) Delete(id, actorUserID uint) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
if user.Role != model.RoleAdmin {
|
||||
return errors.New("只能删除平台管理员账号")
|
||||
}
|
||||
if user.Role == model.RoleAdmin && user.Status == 1 {
|
||||
var admins int64
|
||||
if err := tx.Model(&model.User{}).Where("role = ? AND status = ?", model.RoleAdmin, 1).Count(&admins).Error; err != nil {
|
||||
@@ -272,9 +242,6 @@ func (s *UserService) Delete(id, actorUserID uint) error {
|
||||
return errors.New("至少保留一个启用的平台管理员")
|
||||
}
|
||||
}
|
||||
if err := tx.Where("user_id = ?", id).Delete(&model.MerchantMember{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Delete(&user).Error
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user