justification.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package executor
  2. import (
  3. "fmt"
  4. "strings"
  5. authpublic "github.com/OliveTin/OliveTin/internal/auth/authpublic"
  6. )
  7. const (
  8. justificationCron = "Triggered by cron"
  9. justificationStartup = "Triggered by startup"
  10. justificationFileChange = "Triggered by file change"
  11. justificationCalendar = "Triggered by calendar"
  12. justificationWebhook = "Triggered by webhook"
  13. )
  14. var systemJustificationDefaults = map[string]string{
  15. "cron": justificationCron,
  16. "startup": justificationStartup,
  17. "fileindir": justificationFileChange,
  18. "calendar": justificationCalendar,
  19. "webhook": justificationWebhook,
  20. }
  21. func IsSystemExecution(user *authpublic.AuthenticatedUser) bool {
  22. if user == nil || user.Provider != "system" {
  23. return false
  24. }
  25. return user.Username != "guest"
  26. }
  27. func ResolveJustification(req *ExecutionRequest) string {
  28. provided := strings.TrimSpace(reqJustification(req))
  29. if provided != "" {
  30. return provided
  31. }
  32. if !actionRequiresJustification(req) {
  33. return ""
  34. }
  35. return defaultJustificationForRequest(req)
  36. }
  37. func actionRequiresJustification(req *ExecutionRequest) bool {
  38. return req != nil && req.Binding != nil && req.Binding.Action != nil && req.Binding.Action.Justification
  39. }
  40. func defaultJustificationForRequest(req *ExecutionRequest) string {
  41. if req.TriggerDepth > 0 && req.logEntry != nil {
  42. return fmt.Sprintf("Triggered by action: %s", req.logEntry.ActionTitle)
  43. }
  44. if req.AuthenticatedUser == nil {
  45. return ""
  46. }
  47. return systemJustificationDefaults[req.AuthenticatedUser.Username]
  48. }
  49. func reqJustification(req *ExecutionRequest) string {
  50. if req == nil {
  51. return ""
  52. }
  53. return req.Justification
  54. }