implement script policy
This commit is contained in:
parent
33ea38d35f
commit
bc6613f235
@ -35,6 +35,7 @@ type Scripts interface {
|
|||||||
|
|
||||||
type ScriptPolicy interface {
|
type ScriptPolicy interface {
|
||||||
Get(ctx context.Context, id int64) (model.ScriptPolicy, error)
|
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)
|
Create(ctx context.Context, smap model.ScriptPolicy) (model.ScriptPolicy, error)
|
||||||
Update(ctx context.Context, id int64, values OptionalMap) error
|
Update(ctx context.Context, id int64, values OptionalMap) error
|
||||||
Delete(ctx context.Context, id int64) error
|
Delete(ctx context.Context, id int64) error
|
||||||
|
@ -23,6 +23,16 @@ func (env *ScriptPolicy) Get(ctx context.Context, id int64) (model.ScriptPolicy,
|
|||||||
return mdl, nil
|
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) {
|
func (env *ScriptPolicy) Create(ctx context.Context, smap model.ScriptPolicy) (model.ScriptPolicy, error) {
|
||||||
if err := env.db.Create(&smap).Error; err != nil {
|
if err := env.db.Create(&smap).Error; err != nil {
|
||||||
return smap, err
|
return smap, err
|
||||||
|
@ -2,7 +2,12 @@ package service
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
"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.
|
// CreateScriptPolicy implements createScriptPolicy operation.
|
||||||
@ -11,7 +16,35 @@ import (
|
|||||||
//
|
//
|
||||||
// POST /script-policy
|
// POST /script-policy
|
||||||
func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolicyCreate) (*api.ID, error){
|
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.
|
// DeleteScriptPolicy implements deleteScriptPolicy operation.
|
||||||
//
|
//
|
||||||
@ -19,7 +52,16 @@ func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolic
|
|||||||
//
|
//
|
||||||
// DELETE /script-policy/id/{ScriptPolicyID}
|
// DELETE /script-policy/id/{ScriptPolicyID}
|
||||||
func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScriptPolicyParams) error{
|
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.
|
// GetScriptPolicy implements getScriptPolicy operation.
|
||||||
//
|
//
|
||||||
@ -27,7 +69,24 @@ func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScr
|
|||||||
//
|
//
|
||||||
// GET /script-policy/id/{ScriptPolicyID}
|
// GET /script-policy/id/{ScriptPolicyID}
|
||||||
func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPolicyParams) (*api.ScriptPolicy, error){
|
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.
|
// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation.
|
||||||
//
|
//
|
||||||
@ -35,7 +94,30 @@ func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPol
|
|||||||
//
|
//
|
||||||
// GET /script-policy/hash/{FromScriptHash}
|
// GET /script-policy/hash/{FromScriptHash}
|
||||||
func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetScriptPolicyFromHashParams) (*api.ScriptPolicy, error){
|
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.
|
// UpdateScriptPolicy implements updateScriptPolicy operation.
|
||||||
//
|
//
|
||||||
@ -43,5 +125,28 @@ func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetS
|
|||||||
//
|
//
|
||||||
// PATCH /script-policy/id/{ScriptPolicyID}
|
// PATCH /script-policy/id/{ScriptPolicyID}
|
||||||
func (svc *Service) UpdateScriptPolicy(ctx context.Context, req *api.ScriptPolicyUpdate, params api.UpdateScriptPolicyParams) error{
|
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)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user