2024-12-06 00:47:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-12-07 02:19:26 +00:00
|
|
|
"strconv"
|
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/api"
|
2024-12-07 02:19:26 +00:00
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/datastore"
|
|
|
|
"git.itzana.me/strafesnet/maps-service/pkg/model"
|
2024-12-06 00:47:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// CreateScriptPolicy implements createScriptPolicy operation.
|
|
|
|
//
|
|
|
|
// Create a new script policy.
|
|
|
|
//
|
|
|
|
// POST /script-policy
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) CreateScriptPolicy(ctx context.Context, req *api.ScriptPolicyCreate) (*api.ID, error) {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
from_script, err := svc.DB.Scripts().Get(ctx, req.FromScriptID)
|
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
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),
|
|
|
|
})
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ID{
|
2024-12-12 22:29:20 +00:00
|
|
|
ID: script.ID,
|
2024-12-07 02:19:26 +00:00
|
|
|
}, nil
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-14 06:15:05 +00:00
|
|
|
|
|
|
|
// ListScriptPolicy implements listScriptPolicy operation.
|
|
|
|
//
|
|
|
|
// Get list of script policies.
|
|
|
|
//
|
|
|
|
// GET /script-policy
|
2024-12-14 06:43:32 +00:00
|
|
|
func (svc *Service) ListScriptPolicy(ctx context.Context, request *api.ListScriptPolicyReq) ([]api.ScriptPolicy, error) {
|
2024-12-14 06:15:05 +00:00
|
|
|
filter := datastore.Optional()
|
|
|
|
//fmt.Println(request)
|
2024-12-14 06:43:32 +00:00
|
|
|
if request.Filter.IsSet() {
|
|
|
|
filter.AddNotNil("from_script_hash", request.Filter.Value.FromScriptHash)
|
|
|
|
filter.AddNotNil("to_script_id", request.Filter.Value.ToScriptID)
|
|
|
|
filter.AddNotNil("policy", request.Filter.Value.Policy)
|
2024-12-14 06:15:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
items, err := svc.DB.ScriptPolicy().List(ctx, filter, model.Page{
|
2024-12-14 06:43:32 +00:00
|
|
|
Number: request.Page.GetPage(),
|
|
|
|
Size: request.Page.GetLimit(),
|
2024-12-14 06:15:05 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp []api.ScriptPolicy
|
|
|
|
for i := 0; i < len(items); i++ {
|
|
|
|
resp = append(resp, api.ScriptPolicy{
|
|
|
|
ID: items[i].ID,
|
2024-12-17 23:57:29 +00:00
|
|
|
FromScriptHash: model.HashFormat(items[i].FromScriptHash),
|
2024-12-14 06:15:05 +00:00
|
|
|
ToScriptID: items[i].ToScriptID,
|
|
|
|
Policy: int32(items[i].Policy),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// DeleteScriptPolicy implements deleteScriptPolicy operation.
|
|
|
|
//
|
|
|
|
// Delete the specified script policy by ID.
|
|
|
|
//
|
|
|
|
// DELETE /script-policy/id/{ScriptPolicyID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) DeleteScriptPolicy(ctx context.Context, params api.DeleteScriptPolicyParams) error {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-07 02:19:26 +00:00
|
|
|
return ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-07 02:19:26 +00:00
|
|
|
return ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
return svc.DB.ScriptPolicy().Delete(ctx, params.ScriptPolicyID)
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// GetScriptPolicy implements getScriptPolicy operation.
|
|
|
|
//
|
|
|
|
// Get the specified script policy by ID.
|
|
|
|
//
|
|
|
|
// GET /script-policy/id/{ScriptPolicyID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) GetScriptPolicy(ctx context.Context, params api.GetScriptPolicyParams) (*api.ScriptPolicy, error) {
|
2024-12-10 05:19:38 +00:00
|
|
|
_, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, ErrUserInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read permission for script policy only requires you to be logged in
|
|
|
|
|
|
|
|
policy, err := svc.DB.ScriptPolicy().Get(ctx, params.ScriptPolicyID)
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ScriptPolicy{
|
|
|
|
ID: policy.ID,
|
2024-12-17 23:57:29 +00:00
|
|
|
FromScriptHash: model.HashFormat(policy.FromScriptHash),
|
2024-12-07 02:19:26 +00:00
|
|
|
ToScriptID: policy.ToScriptID,
|
|
|
|
Policy: int32(policy.Policy),
|
|
|
|
}, nil
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// GetScriptPolicyFromHash implements getScriptPolicyFromHash operation.
|
|
|
|
//
|
|
|
|
// Get the policy for the given hash of script source code.
|
|
|
|
//
|
|
|
|
// GET /script-policy/hash/{FromScriptHash}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) GetScriptPolicyFromHash(ctx context.Context, params api.GetScriptPolicyFromHashParams) (*api.ScriptPolicy, error) {
|
2024-12-10 05:19:38 +00:00
|
|
|
_, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-07 02:19:26 +00:00
|
|
|
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)
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, err := svc.DB.ScriptPolicy().GetFromHash(ctx, hash)
|
2024-12-12 22:29:20 +00:00
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &api.ScriptPolicy{
|
|
|
|
ID: policy.ID,
|
2024-12-17 23:57:29 +00:00
|
|
|
FromScriptHash: model.HashFormat(policy.FromScriptHash),
|
2024-12-07 02:19:26 +00:00
|
|
|
ToScriptID: policy.ToScriptID,
|
|
|
|
Policy: int32(policy.Policy),
|
|
|
|
}, nil
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
|
2024-12-06 00:47:40 +00:00
|
|
|
// UpdateScriptPolicy implements updateScriptPolicy operation.
|
|
|
|
//
|
|
|
|
// Update the specified script policy by ID.
|
|
|
|
//
|
|
|
|
// PATCH /script-policy/id/{ScriptPolicyID}
|
2024-12-12 22:29:20 +00:00
|
|
|
func (svc *Service) UpdateScriptPolicy(ctx context.Context, req *api.ScriptPolicyUpdate, params api.UpdateScriptPolicyParams) error {
|
2024-12-10 05:19:38 +00:00
|
|
|
userInfo, ok := ctx.Value("UserInfo").(UserInfo)
|
2024-12-12 22:29:20 +00:00
|
|
|
if !ok {
|
2024-12-07 02:19:26 +00:00
|
|
|
return ErrUserInfo
|
|
|
|
}
|
|
|
|
|
2024-12-12 22:29:20 +00:00
|
|
|
if !userInfo.Roles.ScriptWrite {
|
2024-12-07 02:19:26 +00:00
|
|
|
return ErrPermissionDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
pmap := datastore.Optional()
|
2024-12-12 22:29:20 +00:00
|
|
|
if from_script_id, ok := req.FromScriptID.Get(); ok {
|
|
|
|
from_script, err := svc.DB.Scripts().Get(ctx, from_script_id)
|
|
|
|
if err != nil {
|
2024-12-07 02:19:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
pmap.Add("from_script_hash", from_script.Hash)
|
2024-12-07 02:19:26 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
if to_script_id, ok := req.ToScriptID.Get(); ok {
|
|
|
|
pmap.Add("to_script_id", to_script_id)
|
2024-12-07 02:19:26 +00:00
|
|
|
}
|
2024-12-12 22:29:20 +00:00
|
|
|
if policy, ok := req.Policy.Get(); ok {
|
|
|
|
pmap.Add("policy", policy)
|
2024-12-07 02:19:26 +00:00
|
|
|
}
|
|
|
|
return svc.DB.ScriptPolicy().Update(ctx, req.ID, pmap)
|
2024-12-06 00:47:40 +00:00
|
|
|
}
|