Files
backend/internal/service/setup_service.go
lafay 67a5660952
All checks were successful
Build Backend / build (push) Successful in 3m54s
Build Backend / build-docker (push) Successful in 1m9s
refactor: unify code formatting and improve push/search implementations
- Remove BOM from all Go source files
- Standardize import ordering and whitespace across handlers and services
- Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories
- Enhance JPush client with TLS transport, rate limit monitoring, and simplified API
- Refactor PushService to include userID in device management methods
- Add MaxDevicesPerUser limit and extract helper functions for push payload construction
2026-04-28 14:53:04 +08:00

64 lines
1.3 KiB
Go

package service
import (
"context"
apperrors "with_you/internal/errors"
"with_you/internal/model"
"with_you/internal/repository"
)
type SetupService interface {
SetupSuperAdmin(ctx context.Context, userID, setupSecret string) error
}
type setupService struct {
userRepo repository.UserRepository
roleRepo repository.RoleRepository
casbinSvc CasbinService
setupSecret string
}
func NewSetupService(
userRepo repository.UserRepository,
roleRepo repository.RoleRepository,
casbinSvc CasbinService,
setupSecret string,
) SetupService {
return &setupService{
userRepo: userRepo,
roleRepo: roleRepo,
casbinSvc: casbinSvc,
setupSecret: setupSecret,
}
}
func (s *setupService) SetupSuperAdmin(ctx context.Context, userID, setupSecret string) error {
if s.setupSecret == "" {
return apperrors.ErrSetupSecretNotConfigured
}
if setupSecret != s.setupSecret {
return apperrors.ErrInvalidSetupSecret
}
count, err := s.roleRepo.GetRoleUserCount(ctx, model.RoleSuperAdmin)
if err != nil {
return apperrors.ErrInternal
}
if count > 0 {
return apperrors.ErrSetupAlreadyCompleted
}
user, err := s.userRepo.GetByID(userID)
if err != nil {
return apperrors.ErrUserNotFound
}
if user.Status == model.UserStatusBanned {
return apperrors.ErrUserBanned
}
return s.casbinSvc.AddRoleForUser(ctx, user.ID, model.RoleSuperAdmin)
}