feat(tester): extend GetContestResponse

This commit is contained in:
Vyacheslav1557 2025-03-02 00:29:31 +05:00
parent e6088953b9
commit 81d7aa2366
17 changed files with 539 additions and 238 deletions

View file

@ -3,16 +3,16 @@ package models
import "time"
type Problem struct {
Id *int32 `db:"id"`
Title *string `db:"title"`
Legend *string `db:"legend"`
InputFormat *string `db:"input_format"`
OutputFormat *string `db:"output_format"`
Notes *string `db:"notes"`
Tutorial *string `db:"tutorial"`
LatexSummary *string `db:"latex_summary"`
TimeLimit *int32 `db:"time_limit"`
MemoryLimit *int32 `db:"memory_limit"`
CreatedAt *time.Time `db:"created_at"`
UpdatedAt *time.Time `db:"updated_at"`
Id int32 `db:"id"`
Title string `db:"title"`
Legend string `db:"legend"`
InputFormat string `db:"input_format"`
OutputFormat string `db:"output_format"`
Notes string `db:"notes"`
Tutorial string `db:"tutorial"`
LatexSummary string `db:"latex_summary"`
TimeLimit int32 `db:"time_limit"`
MemoryLimit int32 `db:"memory_limit"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}

162
internal/models/session.go Normal file
View file

@ -0,0 +1,162 @@
package models
import (
"context"
"errors"
"github.com/google/uuid"
"github.com/open-policy-agent/opa/v1/rego"
)
type JWT struct {
SessionId string `json:"session_id"`
UserId int32 `json:"user_id"`
Role Role `json:"role"`
ExpiresAt int64 `json:"exp"`
IssuedAt int64 `json:"iat"`
NotBefore int64 `json:"nbf"`
Permissions []grant `json:"permissions"`
}
func (j JWT) Valid() error {
if uuid.Validate(j.SessionId) != nil {
return errors.New("invalid session id")
}
if j.UserId == 0 {
return errors.New("empty user id")
}
if j.ExpiresAt == 0 {
return errors.New("empty expires at")
}
if j.IssuedAt == 0 {
return errors.New("empty issued at")
}
if j.NotBefore == 0 {
return errors.New("empty not before")
}
if len(j.Permissions) == 0 {
return errors.New("empty permissions")
}
return nil
}
type Role int32
const (
RoleGuest Role = -1
RoleStudent Role = 0
RoleTeacher Role = 1
RoleAdmin Role = 2
)
func (r Role) String() string {
switch r {
case RoleGuest:
return "guest"
case RoleStudent:
return "student"
case RoleTeacher:
return "teacher"
case RoleAdmin:
return "admin"
}
panic("invalid role")
}
type Action string
const (
Create Action = "create"
Read Action = "read"
Update Action = "update"
Delete Action = "delete"
)
type Resource string
const (
ResourceAnotherUser Resource = "another-user"
ResourceMeUser Resource = "me-user"
ResourceListUser Resource = "list-user"
ResourceOwnSession Resource = "own-session"
)
type grant struct {
Action Action `json:"action"`
Resource Resource `json:"resource"`
}
var Grants = map[string][]grant{
RoleGuest.String(): {},
RoleStudent.String(): {
{Read, ResourceAnotherUser},
{Read, ResourceMeUser},
{Update, ResourceOwnSession},
{Delete, ResourceOwnSession},
},
RoleTeacher.String(): {
{Create, ResourceAnotherUser},
{Read, ResourceAnotherUser},
{Read, ResourceMeUser},
{Read, ResourceListUser},
{Update, ResourceOwnSession},
{Delete, ResourceOwnSession},
},
RoleAdmin.String(): {
{Create, ResourceAnotherUser},
{Read, ResourceAnotherUser},
{Read, ResourceMeUser},
{Read, ResourceListUser},
{Update, ResourceAnotherUser},
{Update, ResourceOwnSession},
{Delete, ResourceAnotherUser},
{Delete, ResourceOwnSession},
},
}
const module = `package app.rbac
default allow := false
allow if {
some grant in input.role_grants[input.role]
input.action == grant.action
input.resource == grant.resource
}
`
var query rego.PreparedEvalQuery
func (r Role) HasPermission(action Action, resource Resource) bool {
ctx := context.TODO()
input := map[string]interface{}{
"action": action,
"resource": resource,
"role": r.String(),
"role_grants": Grants,
}
results, err := query.Eval(ctx, rego.EvalInput(input))
if err != nil {
panic(err)
}
return results.Allowed()
}
func init() {
var err error
ctx := context.TODO()
query, err = rego.New(
rego.Query("data.app.rbac.allow"),
rego.Module("ms-auth.rego", module),
).PrepareForEval(ctx)
if err != nil {
panic(err)
}
}

View file

@ -1,13 +1,17 @@
package models
//type Solution struct {
// Id *int32 `db:"id"`
// TaskId *int32 `db:"task_id"`
// ParticipantId *int32 `db:"participant_id"`
// State *int32 `db:"state"`
// Score *int32 `db:"score"`
// Penalty *int32 `db:"penalty"`
// TotalScore *int32 `db:"total_score"`
// Language *int32 `db:"language"`
// CreatedAt *time.Time `db:"created_at"`
//}
import "time"
type Solution struct {
Id int32 `db:"id"`
TaskId int32 `db:"task_id"`
ParticipantId int32 `db:"participant_id"`
Solution string `db:"solution"`
State int32 `db:"state"`
Score int32 `db:"score"`
Penalty int32 `db:"penalty"`
TotalScore int32 `db:"total_score"`
Language int32 `db:"language"`
UpdatedAt time.Time `db:"updated_at"`
CreatedAt time.Time `db:"created_at"`
}

View file

@ -1,10 +1,24 @@
package models
//type Task struct {
// Id *int32 `db:"id"`
// ProblemId *int32 `db:"problem_id"`
// ContestId *int32 `db:"contest_id"`
// Position *int32 `db:"position"`
// CreatedAt *time.Time `db:"created_at"`
// UpdatedAt *time.Time `db:"updated_at"`
//}
import "time"
type Task struct {
Id int32 `db:"id"`
ProblemId int32 `db:"problem_id"`
ContestId int32 `db:"contest_id"`
Position int32 `db:"position"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
type RichTask struct {
Id int32 `db:"id"`
ProblemId int32 `db:"problem_id"`
ContestId int32 `db:"contest_id"`
Position int32 `db:"position"`
Title string `db:"title"`
MemoryLimit int32 `db:"memory_limit"`
TimeLimit int32 `db:"time_limit"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}