feat: завершён этап 2 - Аутентификация Core Service
- Реализована JWT аутентификация с organization-scope - Добавлено хеширование паролей через bcrypt - Созданы репозитории для организаций и пользователей - Реализован AuthService с бизнес-логикой - Добавлен AuthMiddleware для проверки токенов - Созданы handlers для регистрации и входа - Обновлён API сервер для использования аутентификации Готово для этапа 3 - API структура
This commit is contained in:
88
core-service/internal/repository/organizations.go
Normal file
88
core-service/internal/repository/organizations.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type OrganizationRepository interface {
|
||||
Create(ctx context.Context, org *models.Organization) error
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error)
|
||||
Update(ctx context.Context, org *models.Organization) error
|
||||
}
|
||||
|
||||
type organizationRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewOrganizationRepository(db *sql.DB) OrganizationRepository {
|
||||
return &organizationRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *organizationRepository) Create(ctx context.Context, org *models.Organization) error {
|
||||
query := `
|
||||
INSERT INTO organizations (id, name, type, settings, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, org.Settings, org.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create organization: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *organizationRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.Organization, error) {
|
||||
query := `
|
||||
SELECT id, name, type, settings, created_at
|
||||
FROM organizations
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
org := &models.Organization{}
|
||||
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
||||
&org.ID,
|
||||
&org.Name,
|
||||
&org.Type,
|
||||
&org.Settings,
|
||||
&org.CreatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("organization not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get organization: %w", err)
|
||||
}
|
||||
|
||||
return org, nil
|
||||
}
|
||||
|
||||
func (r *organizationRepository) Update(ctx context.Context, org *models.Organization) error {
|
||||
query := `
|
||||
UPDATE organizations
|
||||
SET name = $2, type = $3, settings = $4
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
result, err := r.db.ExecContext(ctx, query, org.ID, org.Name, org.Type, org.Settings)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to update organization: %w", err)
|
||||
}
|
||||
|
||||
rowsAffected, err := result.RowsAffected()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get rows affected: %w", err)
|
||||
}
|
||||
|
||||
if rowsAffected == 0 {
|
||||
return fmt.Errorf("organization not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
99
core-service/internal/repository/users.go
Normal file
99
core-service/internal/repository/users.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"erp-mvp/core-service/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, user *models.User, password string) error
|
||||
GetByEmail(ctx context.Context, email string) (*models.User, error)
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*models.User, error)
|
||||
}
|
||||
|
||||
type userRepository struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewUserRepository(db *sql.DB) UserRepository {
|
||||
return &userRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *userRepository) Create(ctx context.Context, user *models.User, password string) error {
|
||||
query := `
|
||||
INSERT INTO users (id, organization_id, email, password_hash, role, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
`
|
||||
|
||||
_, err := r.db.ExecContext(ctx, query,
|
||||
user.ID,
|
||||
user.OrganizationID,
|
||||
user.Email,
|
||||
password,
|
||||
user.Role,
|
||||
user.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create user: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByEmail(ctx context.Context, email string) (*models.User, error) {
|
||||
query := `
|
||||
SELECT id, organization_id, email, password_hash, role, created_at
|
||||
FROM users
|
||||
WHERE email = $1
|
||||
`
|
||||
|
||||
user := &models.User{}
|
||||
err := r.db.QueryRowContext(ctx, query, email).Scan(
|
||||
&user.ID,
|
||||
&user.OrganizationID,
|
||||
&user.Email,
|
||||
&user.PasswordHash,
|
||||
&user.Role,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *userRepository) GetByID(ctx context.Context, id uuid.UUID) (*models.User, error) {
|
||||
query := `
|
||||
SELECT id, organization_id, email, password_hash, role, created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
user := &models.User{}
|
||||
err := r.db.QueryRowContext(ctx, query, id).Scan(
|
||||
&user.ID,
|
||||
&user.OrganizationID,
|
||||
&user.Email,
|
||||
&user.PasswordHash,
|
||||
&user.Role,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("user not found")
|
||||
}
|
||||
return nil, fmt.Errorf("failed to get user: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
Reference in New Issue
Block a user