优化
This commit is contained in:
+19
-15
@@ -63,7 +63,11 @@ type UserRepo interface {
|
||||
FillDepartmentNamePaths(context.Context, *User) error
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) Login(ctx context.Context, username, password string) (*User, error) {
|
||||
type UserUsecase struct{ repo UserRepo }
|
||||
|
||||
func NewUserUsecase(repo UserRepo) *UserUsecase { return &UserUsecase{repo: repo} }
|
||||
|
||||
func (uc *UserUsecase) Login(ctx context.Context, username, password string) (*User, error) {
|
||||
u, err := uc.repo.FindUserByUsername(ctx, username)
|
||||
if err != nil || bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password)) != nil {
|
||||
return nil, ErrInvalidCredentials
|
||||
@@ -72,7 +76,7 @@ func (uc *SystemUsecase) Login(ctx context.Context, username, password string) (
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) User(ctx context.Context, id uint) (*User, error) {
|
||||
func (uc *UserUsecase) User(ctx context.Context, id uint) (*User, error) {
|
||||
user, err := uc.repo.FindUserByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -82,7 +86,7 @@ func (uc *SystemUsecase) User(ctx context.Context, id uint) (*User, error) {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) fallbackDefaultRouter(ctx context.Context, user *User) {
|
||||
func (uc *UserUsecase) fallbackDefaultRouter(ctx context.Context, user *User) {
|
||||
if user == nil || user.Authority.DefaultRouter == "" {
|
||||
return
|
||||
}
|
||||
@@ -101,14 +105,14 @@ func menuNameExists(menus []*Menu, name string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) Menus(ctx context.Context, authorityID uint) ([]*Menu, error) {
|
||||
func (uc *UserUsecase) Menus(ctx context.Context, authorityID uint) ([]*Menu, error) {
|
||||
return uc.repo.MenusByAuthority(ctx, authorityID)
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) ListUsers(ctx context.Context, page, pageSize int, filter *UserListFilter) ([]*User, int64, error) {
|
||||
func (uc *UserUsecase) ListUsers(ctx context.Context, page, pageSize int, filter *UserListFilter) ([]*User, int64, error) {
|
||||
return uc.repo.ListUsers(ctx, page, pageSize, filter)
|
||||
}
|
||||
func (uc *SystemUsecase) CreateUser(ctx context.Context, user *User, authorityIDs []uint) (*User, error) {
|
||||
func (uc *UserUsecase) CreateUser(ctx context.Context, user *User, authorityIDs []uint) (*User, error) {
|
||||
if user.AuthorityID == 0 && len(authorityIDs) > 0 {
|
||||
user.AuthorityID = authorityIDs[0]
|
||||
}
|
||||
@@ -120,12 +124,12 @@ func (uc *SystemUsecase) CreateUser(ctx context.Context, user *User, authorityID
|
||||
user.Password = string(hash)
|
||||
return uc.repo.CreateUserWithAuthorities(ctx, user, authorityIDs)
|
||||
}
|
||||
func (uc *SystemUsecase) UpdateUser(ctx context.Context, user *User, authorityIDs []uint) error {
|
||||
func (uc *UserUsecase) UpdateUser(ctx context.Context, user *User, authorityIDs []uint) error {
|
||||
authorityIDs = includeAuthority(authorityIDs, user.AuthorityID)
|
||||
return uc.repo.UpdateUserWithAuthorities(ctx, user, authorityIDs)
|
||||
}
|
||||
|
||||
func (uc *SystemUsecase) UpdateSelfUser(ctx context.Context, user *User) error {
|
||||
func (uc *UserUsecase) UpdateSelfUser(ctx context.Context, user *User) error {
|
||||
return uc.repo.UpdateSelfUser(ctx, user)
|
||||
}
|
||||
|
||||
@@ -140,17 +144,17 @@ func includeAuthority(ids []uint, primary uint) []uint {
|
||||
}
|
||||
return append(ids, primary)
|
||||
}
|
||||
func (uc *SystemUsecase) DeleteUser(ctx context.Context, id uint) error {
|
||||
func (uc *UserUsecase) DeleteUser(ctx context.Context, id uint) error {
|
||||
return uc.repo.DeleteUser(ctx, id)
|
||||
}
|
||||
func (uc *SystemUsecase) ResetPassword(ctx context.Context, id uint, password string) error {
|
||||
func (uc *UserUsecase) ResetPassword(ctx context.Context, id uint, password string) error {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return uc.repo.UpdatePassword(ctx, id, string(hash), false)
|
||||
}
|
||||
func (uc *SystemUsecase) ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error {
|
||||
func (uc *UserUsecase) ChangePassword(ctx context.Context, id uint, oldPassword, newPassword string) error {
|
||||
user, err := uc.repo.FindUserByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -164,18 +168,18 @@ func (uc *SystemUsecase) ChangePassword(ctx context.Context, id uint, oldPasswor
|
||||
}
|
||||
return uc.repo.UpdatePassword(ctx, id, string(hash), true)
|
||||
}
|
||||
func (uc *SystemUsecase) Authorities(ctx context.Context) ([]*Authority, error) {
|
||||
func (uc *UserUsecase) Authorities(ctx context.Context) ([]*Authority, error) {
|
||||
return uc.repo.ListAuthorities(ctx)
|
||||
}
|
||||
func (uc *SystemUsecase) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
||||
func (uc *UserUsecase) SetUserAuthorities(ctx context.Context, id uint, authorityIDs []uint) error {
|
||||
if len(authorityIDs) == 0 {
|
||||
return ErrInvalidCredentials
|
||||
}
|
||||
return uc.repo.SetUserAuthorities(ctx, id, authorityIDs)
|
||||
}
|
||||
func (uc *SystemUsecase) SetUserAuthority(ctx context.Context, id, authorityID uint) error {
|
||||
func (uc *UserUsecase) SetUserAuthority(ctx context.Context, id, authorityID uint) error {
|
||||
return uc.repo.SetUserAuthority(ctx, id, authorityID)
|
||||
}
|
||||
func (uc *SystemUsecase) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
|
||||
func (uc *UserUsecase) SetUserSetting(ctx context.Context, id uint, setting map[string]any) error {
|
||||
return uc.repo.SetUserSetting(ctx, id, setting)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user