From bc6613f23589eac7603408014da52af417ce4629 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 6 Dec 2024 18:19:26 -0800 Subject: [PATCH] implement script policy --- pkg/datastore/datastore.go | 1 + pkg/datastore/gormstore/script_policy.go | 10 ++ pkg/service/script_policy.go | 115 ++++++++++++++++++++++- 3 files changed, 121 insertions(+), 5 deletions(-) diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index a58cb32..f85ab47 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -35,6 +35,7 @@ type Scripts interface { type ScriptPolicy interface { Get(ctx context.Context, id int64) (model.ScriptPolicy, error) + GetFromHash(ctx context.Context, hash uint64) (model.ScriptPolicy, error) Create(ctx context.Context, smap model.ScriptPolicy) (model.ScriptPolicy, error) Update(ctx context.Context, id int64, values OptionalMap) error Delete(ctx context.Context, id int64) error diff --git a/pkg/datastore/gormstore/script_policy.go b/pkg/datastore/gormstore/script_policy.go index d2fa1ba..9c292bc 100644 --- a/pkg/datastore/gormstore/script_policy.go +++ b/pkg/datastore/gormstore/script_policy.go @@ -23,6 +23,16 @@ func (env *ScriptPolicy) Get(ctx context.Context, id int64) (model.ScriptPolicy, return mdl, nil } +func (env *ScriptPolicy) GetFromHash(ctx context.Context, hash uint64) (model.ScriptPolicy, error) { + var mdl model.ScriptPolicy + if err := env.db.Model(&model.ScriptPolicy{}).Where("hash = ?", hash).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return mdl, datastore.ErrNotExist + } + } + return mdl, nil +} + func (env *ScriptPolicy) Create(ctx context.Context, smap model.ScriptPolicy) (model.ScriptPolicy, error) { if err := env.db.Create(&smap).Error; err != nil { return smap, err diff --git a/pkg/service/script_policy.go b/pkg/service/script_policy.go index a21b85c..228ee2a 100644 --- a/pkg/service/script_policy.go +++ b/pkg/service/script_policy.go @@ -2,7 +2,12 @@ package service import ( "context" + "fmt" + "strconv" + "git.itzana.me/strafesnet/maps-service/pkg/api" + "git.itzana.me/strafesnet/maps-service/pkg/datastore" + "git.itzana.me/strafesnet/maps-service/pkg/model" ) // CreateScriptPolicy implements createScriptPolicy operation. @@ -11,7 +16,35 @@ import ( // // POST /script-policy func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolicyCreate) (*api.ID, error){ - return nil,nil + userInfo, ok := ctx.Value("UserInfo").(*UserInfo) + if !ok{ + return nil, ErrUserInfo + } + + if !userInfo.Roles.ScriptWrite{ + return nil, ErrPermissionDenied + } + + from_script, err := svc.DB.Scripts().Get(ctx,req.FromScriptID) + if err != nil{ + return nil, err + } + + // the existence of ToScriptID does not need to be validated because it's checked by a foreign key constraint. + + script, err := svc.DB.ScriptPolicy().Create(ctx, model.ScriptPolicy{ + ID: 0, + FromScriptHash: from_script.Hash, + ToScriptID: req.ToScriptID, + Policy: model.Policy(req.Policy), + }) + if err != nil{ + return nil, err + } + + return &api.ID{ + ID:script.ID, + }, nil } // DeleteScriptPolicy implements deleteScriptPolicy operation. // @@ -19,7 +52,16 @@ func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolic // // DELETE /script-policy/id/{ScriptPolicyID} func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScriptPolicyParams) error{ - return nil + userInfo, ok := ctx.Value("UserInfo").(*UserInfo) + if !ok{ + return ErrUserInfo + } + + if !userInfo.Roles.ScriptWrite{ + return ErrPermissionDenied + } + + return svc.DB.ScriptPolicy().Delete(ctx, params.ScriptPolicyID) } // GetScriptPolicy implements getScriptPolicy operation. // @@ -27,7 +69,24 @@ func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScr // // GET /script-policy/id/{ScriptPolicyID} func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPolicyParams) (*api.ScriptPolicy, error){ - return nil,nil + _, ok := ctx.Value("UserInfo").(*UserInfo) + if !ok{ + return nil, ErrUserInfo + } + + // Read permission for script policy only requires you to be logged in + + policy, err := svc.DB.ScriptPolicy().Get(ctx, params.ScriptPolicyID) + if err != nil{ + return nil, err + } + + return &api.ScriptPolicy{ + ID: policy.ID, + FromScriptHash: fmt.Sprintf("%x",policy.FromScriptHash), + ToScriptID: policy.ToScriptID, + Policy: int32(policy.Policy), + }, nil } // GetScriptPolicyFromHash implements getScriptPolicyFromHash operation. // @@ -35,7 +94,30 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol // // GET /script-policy/hash/{FromScriptHash} func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetScriptPolicyFromHashParams) (*api.ScriptPolicy, error){ - return nil,nil + _, ok := ctx.Value("UserInfo").(*UserInfo) + if !ok{ + return nil, ErrUserInfo + } + + // Read permission for script policy only requires you to be logged in + + // parse hash from hex + hash, err := strconv.ParseUint(params.FromScriptHash, 16, 64) + if err != nil{ + return nil, err + } + + policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash) + if err != nil{ + return nil, err + } + + return &api.ScriptPolicy{ + ID: policy.ID, + FromScriptHash: fmt.Sprintf("%x",policy.FromScriptHash), + ToScriptID: policy.ToScriptID, + Policy: int32(policy.Policy), + }, nil } // UpdateScriptPolicy implements updateScriptPolicy operation. // @@ -43,5 +125,28 @@ func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetS // // PATCH /script-policy/id/{ScriptPolicyID} func (svc *Service) UpdateScriptPolicy(ctx context.Context, req *api.ScriptPolicyUpdate, params api.UpdateScriptPolicyParams) error{ - return nil + userInfo, ok := ctx.Value("UserInfo").(*UserInfo) + if !ok{ + return ErrUserInfo + } + + if !userInfo.Roles.ScriptWrite{ + return ErrPermissionDenied + } + + pmap := datastore.Optional() + if from_script_id,ok:=req.FromScriptID.Get();ok{ + from_script, err := svc.DB.Scripts().Get(ctx,from_script_id) + if err != nil{ + return err + } + pmap.Add("from_script_hash",from_script.Hash) + } + if to_script_id,ok:=req.ToScriptID.Get();ok{ + pmap.Add("to_script_id",to_script_id) + } + if policy,ok:=req.Policy.Get();ok{ + pmap.Add("policy",policy) + } + return svc.DB.ScriptPolicy().Update(ctx, req.ID, pmap) }