This commit is contained in:
2026-10-03 13:07:50 +07:00
parent 7a7808b4d1
commit 3aeaa09ea8
14 changed files with 98 additions and 28 deletions
+24 -6
View File
@@ -53,6 +53,7 @@ type usersRepository interface {
DeleteUser(ctx context.Context, userID int) error
}
// UsersService handles user accounts and authentication.
type UsersService struct {
usersRepo usersRepository
passwordGenerator password_generator.IPasswordGenerator
@@ -64,6 +65,7 @@ type UsersService struct {
appBaseURL string
}
// NewUsersService creates a users service.
func NewUsersService(
usersRepo usersRepository,
passwordGenerator password_generator.IPasswordGenerator,
@@ -84,14 +86,20 @@ func NewUsersService(
var (
// ErrTermsNotAccepted — регистрация невозможна без акцепта соглашений.
ErrTermsNotAccepted = errors.New("Необходимо принять Пользовательское соглашение и Политику конфиденциальности")
ErrInvalidEmail = errors.New("Некорректный email")
ErrInvalidCredentials = errors.New("Не верен email или пароль")
ErrTermsNotAccepted = errors.New("Необходимо принять Пользовательское соглашение и Политику конфиденциальности")
// ErrInvalidEmail indicates that an email address is invalid.
ErrInvalidEmail = errors.New("Некорректный email")
// ErrInvalidCredentials indicates that the supplied credentials are invalid.
ErrInvalidCredentials = errors.New("Не верен email или пароль")
// ErrInvalidAccountPassword indicates that the account password is invalid.
ErrInvalidAccountPassword = errors.New("Неверный пароль")
ErrInvalidRefreshToken = errors.New("Пользователь не найден")
ErrInvalidRole = errors.New("Недопустимая роль")
// ErrInvalidRefreshToken indicates that a refresh token is invalid.
ErrInvalidRefreshToken = errors.New("Пользователь не найден")
// ErrInvalidRole indicates that a role is invalid.
ErrInvalidRole = errors.New("Недопустимая роль")
)
// AddUser creates a user account.
func (s *UsersService) AddUser(
ctx context.Context,
username string,
@@ -192,6 +200,7 @@ func (s *UsersService) DeleteAccount(
return err
}
// RefreshPassword generates and sends a new password.
func (s *UsersService) RefreshPassword(
ctx context.Context,
email string,
@@ -252,6 +261,7 @@ func validateEmail(email string) error {
return nil
}
// Login authenticates a user and returns tokens.
func (s *UsersService) Login(
ctx context.Context,
email string,
@@ -294,6 +304,7 @@ func (s *UsersService) Login(
return accessToken, refreshToken, nil
}
// Refresh exchanges a refresh token for new tokens.
func (s *UsersService) Refresh(
ctx context.Context,
token string,
@@ -344,6 +355,7 @@ func (s *UsersService) Refresh(
return accessToken, refreshToken, nil
}
// GetUsers returns users.
func (s *UsersService) GetUsers(
ctx context.Context,
) ([]*User, error) {
@@ -354,6 +366,7 @@ func (s *UsersService) GetUsers(
return mapUsers(users), nil
}
// GetUserByID returns a user by ID.
func (s *UsersService) GetUserByID(
ctx context.Context,
userID int,
@@ -368,6 +381,7 @@ func (s *UsersService) GetUserByID(
return mapUser(user), nil
}
// AddUserRole assigns a role to a user.
func (s *UsersService) AddUserRole(
ctx context.Context,
userID int,
@@ -383,6 +397,7 @@ func (s *UsersService) AddUserRole(
return err
}
// DeleteUserRole removes a role from a user.
func (s *UsersService) DeleteUserRole(
ctx context.Context,
userID int,
@@ -399,13 +414,16 @@ func (s *UsersService) DeleteUserRole(
}
var (
// ErrPasswordChangeForbidden indicates that the password cannot be changed.
ErrPasswordChangeForbidden = errors.New("Изменять чужой пароль может только администратор")
ErrInvalidPasswordChange = errors.New("Укажите чужой положительный ID и пароль от 8 до 16 символов")
// ErrInvalidPasswordChange indicates that the password change request is invalid.
ErrInvalidPasswordChange = errors.New("Укажите чужой положительный ID и пароль от 8 до 16 символов")
// ErrUserNotFound — целевой пользователь не найден.
ErrUserNotFound = errors.New("Пользователь не найден")
)
// ChangeUserPassword сохраняет только хеш; действующие сеансы не изменяются.
// ChangeUserPassword changes a user's password subject to authorization checks.
func (s *UsersService) ChangeUserPassword(ctx context.Context, actorID, userID int, password string) error {
actor, err := s.usersRepo.GetUserByID(ctx, actorID)
if errors.Is(err, users_repo.ErrUserNotFound) {
+1
View File
@@ -3,6 +3,7 @@ package users_service
import "time"
// User — пользователь, доступный прикладным сценариям и выдаче наружу.
// User describes a user exposed by the service layer.
type User struct {
ID int
Username string