diff --git a/system/service/permissions.go b/system/service/permissions.go index d8d5031f2..a9e866e74 100644 --- a/system/service/permissions.go +++ b/system/service/permissions.go @@ -6,38 +6,9 @@ import ( "github.com/pkg/errors" ) -func validateResource(resource string) error { - delimiter := ":" - - resources := map[string]bool{ - "messaging:channel": true, - } - - if result := resources[resource]; result { - return nil - } - - resourceParts := strings.Split(resource, delimiter) - if len(resourceParts) != 3 { - return errors.Errorf("Invalid resource format, expected 3, got %d", len(resourceParts)) - } - - for prefix := range resources { - prefix = prefix + delimiter - if len(resource) < len(prefix)+1 { - continue - } - if resource[:len(prefix)] == prefix { - return nil - } - } - - return errors.Errorf("Invalid resource name, '%s'", resource) -} - -func validatePermission(service string, resource string, operation string) error { +func validatePermission(resource string, operation string) error { var services = map[string]map[string]bool{ - "messaging": map[string]bool{ + "messaging:channel": map[string]bool{ "manage.webhooks": false, "message.send": true, "message.embed": true, @@ -47,11 +18,24 @@ func validatePermission(service string, resource string, operation string) error "message.react": true, }, } - if service, ok := services[service]; ok { + + delimiter := ":" + resourceParts := strings.Split(resource, delimiter) + if len(resourceParts) < 2 { + return errors.Errorf("Invalid resource format, expected >= 2, got %d", len(resourceParts)) + } + + resourceName := resourceParts[0] + delimiter + resourceParts[1] + if service, ok := services[resourceName]; ok { if op := service[operation]; op { - return validateResource(resource) + if len(resourceParts) == 3 { + if val := resourceParts[2]; val != "" { + return nil + } + } + return errors.Errorf("Invalid resource format, missing resource ID") } return errors.Errorf("Unknown operation: '%s'", operation) } - return errors.Errorf("Unknown service name: '%s'", service) + return errors.Errorf("Unknown resource name: '%s'", resourceName) } diff --git a/system/service/permissions_test.go b/system/service/permissions_test.go index 38ed94b2b..11988782f 100644 --- a/system/service/permissions_test.go +++ b/system/service/permissions_test.go @@ -7,9 +7,9 @@ import ( ) func TestPermissionsValidation(t *testing.T) { - test.Error(t, validatePermission("bogus", "bogus", "bogus"), "expected error") - test.Error(t, validatePermission("messaging", "bogus", "bogus"), "expected error") - test.Error(t, validatePermission("messaging", "messaging:channel", "bogus"), "expected error") - test.Error(t, validatePermission("messaging", "messaging:channel:", "message.send"), "expected error") - test.NoError(t, validatePermission("messaging", "messaging:channel:1", "message.send"), "expected valid response") + test.Error(t, validatePermission("bogus", "bogus"), "expected error") + test.Error(t, validatePermission("bogus", "bogus"), "expected error") + test.Error(t, validatePermission("messaging:channel", "bogus"), "expected error") + test.Error(t, validatePermission("messaging:channel:", "message.send"), "expected error") + test.NoError(t, validatePermission("messaging:channel:1", "message.send"), "expected valid response") }