124 lines
4.8 KiB
Go
124 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type UserInput struct {
|
|
ID uint
|
|
Username, Password, NickName, HeaderImg, Phone, Email string
|
|
AuthorityID uint
|
|
Enable int
|
|
AuthorityIDs []uint
|
|
}
|
|
|
|
type UserService struct {
|
|
uc *biz.UserUsecase
|
|
settings *SecurityService
|
|
}
|
|
|
|
func NewUserService(uc *biz.UserUsecase, settings *SecurityService) *UserService {
|
|
return &UserService{uc: uc, settings: settings}
|
|
}
|
|
|
|
func userInput(value *dto.UserRequest) UserInput {
|
|
return UserInput{ID: value.ID, Username: value.Username, Password: value.Password, NickName: value.NickName, HeaderImg: value.HeaderImg, AuthorityID: value.AuthorityID, AuthorityIDs: value.AuthorityIDs, Enable: value.Enable, Phone: value.Phone, Email: value.Email}
|
|
}
|
|
func (s *UserService) ListUsersRequest(ctx context.Context, value *dto.UserListRequest) ([]map[string]any, int64, error) {
|
|
return s.ListUsers(ctx, value.Page, value.PageSize, &biz.UserListFilter{Username: value.Username, NickName: value.NickName, Phone: value.Phone, Email: value.Email, OrderKey: value.OrderKey, Desc: value.Desc})
|
|
}
|
|
func (s *UserService) CreateUserRequest(ctx context.Context, value *dto.UserRequest) (map[string]any, error) {
|
|
return s.CreateUser(ctx, userInput(value))
|
|
}
|
|
func (s *UserService) UpdateUserRequest(ctx context.Context, value *dto.UserRequest) error {
|
|
return s.UpdateUser(ctx, userInput(value))
|
|
}
|
|
|
|
func (s *UserService) User(ctx context.Context, id uint) (map[string]any, error) {
|
|
value, err := s.uc.User(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertUser(value), nil
|
|
}
|
|
|
|
func (s *UserService) Menus(ctx context.Context, authorityID uint) ([]map[string]any, error) {
|
|
menus, err := s.uc.Menus(ctx, authorityID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]map[string]any, 0, len(menus))
|
|
for _, menu := range menus {
|
|
result = append(result, convertMenu(menu))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) ListUsers(ctx context.Context, page, pageSize int, filter *biz.UserListFilter) ([]map[string]any, int64, error) {
|
|
users, total, err := s.uc.ListUsers(ctx, page, pageSize, filter)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
result := make([]map[string]any, 0, len(users))
|
|
for _, user := range users {
|
|
result = append(result, convertUser(user))
|
|
}
|
|
return result, total, nil
|
|
}
|
|
|
|
func (s *UserService) Authorities(ctx context.Context) ([]map[string]any, error) {
|
|
values, err := s.uc.Authorities(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result := make([]map[string]any, 0, len(values))
|
|
for _, value := range values {
|
|
result = append(result, convertAuthority(*value))
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func (s *UserService) CreateUser(ctx context.Context, input UserInput) (map[string]any, error) {
|
|
if err := s.settings.ValidatePassword(ctx, input.Password); err != nil {
|
|
return nil, err
|
|
}
|
|
security, _ := s.settings.CurrentSecurity(ctx)
|
|
mustChange := security != nil && security.ForceNewUserChangePassword
|
|
user, err := s.uc.CreateUser(ctx, &biz.User{Username: input.Username, Password: input.Password, NickName: input.NickName, HeaderImg: input.HeaderImg, AuthorityID: input.AuthorityID, Phone: input.Phone, Email: input.Email, Enable: input.Enable, MustChangePassword: mustChange}, input.AuthorityIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return convertUser(user), nil
|
|
}
|
|
func (s *UserService) UpdateUser(ctx context.Context, input UserInput) error {
|
|
return s.uc.UpdateUser(ctx, &biz.User{ID: input.ID, NickName: input.NickName, HeaderImg: input.HeaderImg, AuthorityID: input.AuthorityID, Phone: input.Phone, Email: input.Email, Enable: input.Enable}, input.AuthorityIDs)
|
|
}
|
|
func (s *UserService) UpdateSelfUser(ctx context.Context, input UserInput) error {
|
|
return s.uc.UpdateSelfUser(ctx, &biz.User{ID: input.ID, NickName: input.NickName, HeaderImg: input.HeaderImg, Phone: input.Phone, Email: input.Email, Enable: input.Enable})
|
|
}
|
|
func (s *UserService) DeleteUser(ctx context.Context, id uint) error {
|
|
return s.uc.DeleteUser(ctx, id)
|
|
}
|
|
func (s *UserService) ResetPassword(ctx context.Context, id uint, password string) error {
|
|
if err := s.settings.ValidatePassword(ctx, password); err != nil {
|
|
return err
|
|
}
|
|
return s.uc.ResetPassword(ctx, id, password)
|
|
}
|
|
func (s *UserService) ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error {
|
|
if err := s.settings.ValidatePassword(ctx, newPassword); err != nil {
|
|
return err
|
|
}
|
|
return s.uc.ChangePassword(ctx, id, oldPassword, newPassword)
|
|
}
|
|
|
|
func (s *UserService) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
|
return s.uc.SetUserAuthorities(ctx, id, authorityIDs)
|
|
}
|
|
func (s *UserService) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
|
|
return s.uc.SetUserSetting(ctx, id, setting)
|
|
}
|