feat: add permission check

This commit is contained in:
Vyacheslav1557 2024-08-23 14:56:52 +05:00
parent f778100bc9
commit bf99d521a8
4 changed files with 64 additions and 34 deletions

View file

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models" "git.sch9.ru/new_gate/ms-tester/internal/models"
) )
@ -14,36 +15,43 @@ type ContestStorage interface {
type ContestService struct { type ContestService struct {
contestStorage ContestStorage contestStorage ContestStorage
permissionService IPermissionService
} }
func NewContestService( func NewContestService(
contestStorage ContestStorage, contestStorage ContestStorage,
permissionService IPermissionService,
) *ContestService { ) *ContestService {
return &ContestService{ return &ContestService{
contestStorage: contestStorage, contestStorage: contestStorage,
permissionService: permissionService,
} }
} }
func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) { func (service *ContestService) CreateContest(ctx context.Context, contest *models.Contest) (int32, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
panic("access control is not implemented yet") return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.contestStorage.CreateContest(ctx, contest) return service.contestStorage.CreateContest(ctx, contest)
} }
func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) { func (service *ContestService) ReadContestById(ctx context.Context, id int32) (*models.Contest, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
panic("access control is not implemented yet") return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.contestStorage.ReadContestById(ctx, id) return service.contestStorage.ReadContestById(ctx, id)
} }
func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error { func (service *ContestService) UpdateContest(ctx context.Context, contest *models.Contest) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.contestStorage.UpdateContest(ctx, contest) return service.contestStorage.UpdateContest(ctx, contest)
} }
func (service *ContestService) DeleteContest(ctx context.Context, id int32) error { func (service *ContestService) DeleteContest(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.contestStorage.DeleteContest(ctx, id) return service.contestStorage.DeleteContest(ctx, id)
} }

View file

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models" "git.sch9.ru/new_gate/ms-tester/internal/models"
) )
@ -14,36 +15,43 @@ type ParticipantStorage interface {
type ParticipantService struct { type ParticipantService struct {
participantStorage ParticipantStorage participantStorage ParticipantStorage
permissionService IPermissionService
} }
func NewParticipantService( func NewParticipantService(
participantStorage ParticipantStorage, participantStorage ParticipantStorage,
permissionService IPermissionService,
) *ParticipantService { ) *ParticipantService {
return &ParticipantService{ return &ParticipantService{
participantStorage: participantStorage, participantStorage: participantStorage,
permissionService: permissionService,
} }
} }
func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) { func (service *ParticipantService) CreateParticipant(ctx context.Context, participant *models.Participant) (int32, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
panic("access control is not implemented yet") return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.participantStorage.CreateParticipant(ctx, participant) return service.participantStorage.CreateParticipant(ctx, participant)
} }
func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) { func (service *ParticipantService) ReadParticipantById(ctx context.Context, id int32) (*models.Participant, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
panic("access control is not implemented yet") return nil, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.participantStorage.ReadParticipantById(ctx, id) return service.participantStorage.ReadParticipantById(ctx, id)
} }
func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error { func (service *ParticipantService) UpdateParticipant(ctx context.Context, participant *models.Participant) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.participantStorage.UpdateParticipant(ctx, participant) return service.participantStorage.UpdateParticipant(ctx, participant)
} }
func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error { func (service *ParticipantService) DeleteParticipant(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.participantStorage.DeleteParticipant(ctx, id) return service.participantStorage.DeleteParticipant(ctx, id)
} }

View file

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models" "git.sch9.ru/new_gate/ms-tester/internal/models"
) )
@ -14,36 +15,43 @@ type SolutionStorage interface {
type SolutionService struct { type SolutionService struct {
solutionStorage SolutionStorage solutionStorage SolutionStorage
permissionService IPermissionService
} }
func NewSolutionService( func NewSolutionService(
solutionStorage SolutionStorage, solutionStorage SolutionStorage,
permissionService IPermissionService,
) *SolutionService { ) *SolutionService {
return &SolutionService{ return &SolutionService{
solutionStorage: solutionStorage, solutionStorage: solutionStorage,
permissionService: permissionService,
} }
} }
func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) { func (service *SolutionService) CreateSolution(ctx context.Context, solution models.Solution) (int32, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
panic("access control is not implemented yet") return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.solutionStorage.CreateSolution(ctx, solution) return service.solutionStorage.CreateSolution(ctx, solution)
} }
func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) { func (service *SolutionService) ReadSolutionById(ctx context.Context, id int32) (models.Solution, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "read") {
panic("access control is not implemented yet") return models.Solution{}, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.solutionStorage.ReadSolutionById(ctx, id) return service.solutionStorage.ReadSolutionById(ctx, id)
} }
func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error { func (service *SolutionService) RejudgeSolution(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "update") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.solutionStorage.RejudgeSolution(ctx, id) return service.solutionStorage.RejudgeSolution(ctx, id)
} }
func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error { func (service *SolutionService) DeleteSolution(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.solutionStorage.DeleteSolution(ctx, id) return service.solutionStorage.DeleteSolution(ctx, id)
} }

View file

@ -2,6 +2,7 @@ package services
import ( import (
"context" "context"
"git.sch9.ru/new_gate/ms-tester/internal/lib"
"git.sch9.ru/new_gate/ms-tester/internal/models" "git.sch9.ru/new_gate/ms-tester/internal/models"
) )
@ -12,24 +13,29 @@ type TaskStorage interface {
type TaskService struct { type TaskService struct {
taskStorage TaskStorage taskStorage TaskStorage
permissionService IPermissionService
} }
func NewTaskService( func NewTaskService(
taskStorage TaskStorage, taskStorage TaskStorage,
permissionService IPermissionService,
) *TaskService { ) *TaskService {
return &TaskService{ return &TaskService{
taskStorage: taskStorage, taskStorage: taskStorage,
permissionService: permissionService,
} }
} }
func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) { func (service *TaskService) CreateTask(ctx context.Context, task models.Task) (int32, error) {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "create") {
panic("access control is not implemented yet") return 0, lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.taskStorage.CreateTask(ctx, task) return service.taskStorage.CreateTask(ctx, task)
} }
func (service *TaskService) DeleteTask(ctx context.Context, id int32) error { func (service *TaskService) DeleteTask(ctx context.Context, id int32) error {
//userId := ctx.Value("user_id").(int32) if !service.permissionService.Allowed(ctx, extractUser(ctx), "delete") {
panic("access control is not implemented yet") return lib.ServiceError(nil, lib.ErrNoPermission, "permission denied")
}
return service.taskStorage.DeleteTask(ctx, id) return service.taskStorage.DeleteTask(ctx, id)
} }