grpcApiActions.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package grpcapi
  2. import (
  3. "crypto/md5"
  4. "fmt"
  5. pb "github.com/OliveTin/OliveTin/gen/grpc"
  6. acl "github.com/OliveTin/OliveTin/internal/acl"
  7. config "github.com/OliveTin/OliveTin/internal/config"
  8. )
  9. func actionsCfgToPb(cfgActions []config.Action, user *acl.AuthenticatedUser) *pb.GetDashboardComponentsResponse {
  10. res := &pb.GetDashboardComponentsResponse{}
  11. for _, action := range cfgActions {
  12. if !acl.IsAllowedView(cfg, user, &action) {
  13. continue
  14. }
  15. btn := actionCfgToPb(action, user)
  16. res.Actions = append(res.Actions, btn)
  17. }
  18. return res
  19. }
  20. func actionCfgToPb(action config.Action, user *acl.AuthenticatedUser) *pb.Action {
  21. btn := pb.Action{
  22. Id: fmt.Sprintf("%x", md5.Sum([]byte(action.Title))),
  23. Title: action.Title,
  24. Icon: action.Icon,
  25. CanExec: acl.IsAllowedExec(cfg, user, &action),
  26. }
  27. for _, cfgArg := range action.Arguments {
  28. pbArg := pb.ActionArgument{
  29. Name: cfgArg.Name,
  30. Title: cfgArg.Title,
  31. Type: cfgArg.Type,
  32. Description: cfgArg.Description,
  33. DefaultValue: cfgArg.Default,
  34. Choices: buildChoices(cfgArg.Choices),
  35. }
  36. btn.Arguments = append(btn.Arguments, &pbArg)
  37. }
  38. return &btn
  39. }
  40. func buildChoices(choices []config.ActionArgumentChoice) []*pb.ActionArgumentChoice {
  41. ret := []*pb.ActionArgumentChoice{}
  42. for _, cfgChoice := range choices {
  43. pbChoice := pb.ActionArgumentChoice{
  44. Value: cfgChoice.Value,
  45. Title: cfgChoice.Title,
  46. }
  47. ret = append(ret, &pbChoice)
  48. }
  49. return ret
  50. }