Improve handling of complex constraint checks
This commit is contained in:
@@ -7,11 +7,14 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res moduleBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return moduleMatch(res.module, c) },
|
||||
func() bool { return namespaceMatch(res.namespace, c) },
|
||||
)
|
||||
}
|
||||
|
||||
// Handles module matchers
|
||||
func moduleMatch(r *types.Module, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func moduleMatch(r *types.Module, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "module", "module.handle":
|
||||
return c.Match(r.Handle)
|
||||
@@ -19,5 +22,5 @@ func moduleMatch(r *types.Module, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
return c.Match(r.Name)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res namespaceBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return namespaceMatch(res.namespace, c, false)
|
||||
return namespaceMatch(res.namespace, c)
|
||||
}
|
||||
|
||||
// Handles namespace matchers
|
||||
func namespaceMatch(r *types.Namespace, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func namespaceMatch(r *types.Namespace, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "namespace", "namespace.slug":
|
||||
return c.Match(r.Slug)
|
||||
@@ -19,5 +19,5 @@ func namespaceMatch(r *types.Namespace, c eventbus.ConstraintMatcher, def bool)
|
||||
return c.Match(r.Name)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -7,11 +7,14 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res pageBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return namespaceMatch(res.namespace, c, pageMatch(res.page, c, false))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return pageMatch(res.page, c) },
|
||||
func() bool { return namespaceMatch(res.namespace, c) },
|
||||
)
|
||||
}
|
||||
|
||||
// Handles namespace matchers
|
||||
func pageMatch(r *types.Page, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func pageMatch(r *types.Page, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "page", "page.handle":
|
||||
return c.Match(r.Handle)
|
||||
@@ -19,5 +22,5 @@ func pageMatch(r *types.Page, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
return c.Match(r.Title)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -13,10 +13,14 @@ const (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res recordBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return recordMatch(res.record, c, namespaceMatch(res.namespace, c, moduleMatch(res.module, c, false)))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return recordMatch(res.record, c) },
|
||||
func() bool { return moduleMatch(res.module, c) },
|
||||
func() bool { return namespaceMatch(res.namespace, c) },
|
||||
)
|
||||
}
|
||||
|
||||
func recordMatch(r *types.Record, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func recordMatch(r *types.Record, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "record.updatedAt":
|
||||
return c.Match(r.UpdatedAt.Format(time.RFC3339))
|
||||
@@ -35,5 +39,5 @@ func recordMatch(r *types.Record, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
}
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -7,13 +7,13 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res channelBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return channelMatch(res.channel, c, false)
|
||||
return channelMatch(res.channel, c)
|
||||
}
|
||||
|
||||
// Handles channel matchers
|
||||
//
|
||||
// This *match() fn uses a 3rd param to allow matcher chaining (see commands, channels)
|
||||
func channelMatch(r *types.Channel, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func channelMatch(r *types.Channel, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "channel", "channel.name":
|
||||
return r != nil && c.Match(r.Name)
|
||||
@@ -23,5 +23,5 @@ func channelMatch(r *types.Channel, c eventbus.ConstraintMatcher, def bool) bool
|
||||
return r != nil && c.Match(r.Type.String())
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -7,7 +7,10 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res channelMemberBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return channelMatch(res.channel, c, channelMemberMatch(res.member, c))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return channelMemberMatch(res.member, c) },
|
||||
func() bool { return channelMatch(res.channel, c) },
|
||||
)
|
||||
}
|
||||
|
||||
// Handles channel member matchers
|
||||
|
||||
@@ -7,7 +7,10 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res commandBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return channelMatch(res.channel, c, commandMatch(res.command, c))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return commandMatch(res.command, c) },
|
||||
func() bool { return channelMatch(res.channel, c) },
|
||||
)
|
||||
}
|
||||
|
||||
// Handles command matchers
|
||||
|
||||
@@ -7,7 +7,10 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res messageBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return channelMatch(res.channel, c, messageMatch(res.message, c))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return messageMatch(res.message, c) },
|
||||
func() bool { return channelMatch(res.channel, c) },
|
||||
)
|
||||
}
|
||||
|
||||
// Handles message matchers
|
||||
|
||||
@@ -166,3 +166,13 @@ func (c mustMatch) Match(value string) bool {
|
||||
|
||||
return c.not
|
||||
}
|
||||
|
||||
func MatchFirst(checks ...func() bool) bool {
|
||||
for _, check := range checks {
|
||||
if check() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res applicationBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return applicationMatch(res.application, c, false)
|
||||
return applicationMatch(res.application, c)
|
||||
}
|
||||
|
||||
// Handles application matchers
|
||||
func applicationMatch(r *types.Application, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func applicationMatch(r *types.Application, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
// not supported yet
|
||||
//case "application", "application.handle":
|
||||
@@ -21,5 +21,5 @@ func applicationMatch(r *types.Application, c eventbus.ConstraintMatcher, def bo
|
||||
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -4,5 +4,5 @@ import "github.com/cortezaproject/corteza-server/pkg/eventbus"
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res authBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return userMatch(res.user, c, false)
|
||||
return userMatch(res.user, c)
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@ func (res mailBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
// constraint#1 AND constraint#2 AND constraint#3 ...
|
||||
//
|
||||
// When there are multiple values, Match() can decide how to treat them (OR, AND...)
|
||||
return mailMatch(res.message, c, false)
|
||||
return mailMatch(res.message, c)
|
||||
}
|
||||
|
||||
// Handles role matchers
|
||||
func mailMatch(r *types.MailMessage, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func mailMatch(r *types.MailMessage, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "mail.header.subject":
|
||||
return c.Match(r.Subject)
|
||||
@@ -35,7 +35,7 @@ func mailMatch(r *types.MailMessage, c eventbus.ConstraintMatcher, def bool) boo
|
||||
return mailMatchAnyAddress(c, r.Header.BCC...)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
func mailMatchAnyAddress(c eventbus.ConstraintMatcher, aa ...*mail.Address) bool {
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res roleBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return roleMatch(res.role, c, false)
|
||||
return roleMatch(res.role, c)
|
||||
}
|
||||
|
||||
// Handles role matchers
|
||||
func roleMatch(r *types.Role, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func roleMatch(r *types.Role, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "role", "role.handle":
|
||||
return c.Match(r.Handle)
|
||||
@@ -19,5 +19,5 @@ func roleMatch(r *types.Role, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
return c.Match(r.Name)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -6,5 +6,8 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res roleMemberBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return userMatch(res.user, c, roleMatch(res.role, c, false))
|
||||
return eventbus.MatchFirst(
|
||||
func() bool { return userMatch(res.user, c) },
|
||||
func() bool { return roleMatch(res.role, c) },
|
||||
)
|
||||
}
|
||||
|
||||
@@ -14,11 +14,7 @@ const (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res sinkBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
if !sinkMatch(res.request, c) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
return sinkMatch(res.request, c)
|
||||
}
|
||||
|
||||
// Handles sink's URL matchers
|
||||
|
||||
@@ -7,11 +7,11 @@ import (
|
||||
|
||||
// Match returns false if given conditions do not match event & resource internals
|
||||
func (res userBase) Match(c eventbus.ConstraintMatcher) bool {
|
||||
return userMatch(res.user, c, false)
|
||||
return userMatch(res.user, c)
|
||||
}
|
||||
|
||||
// Handles user matchers
|
||||
func userMatch(r *types.User, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
func userMatch(r *types.User, c eventbus.ConstraintMatcher) bool {
|
||||
switch c.Name() {
|
||||
case "user", "user.handle":
|
||||
return r != nil && c.Match(r.Handle)
|
||||
@@ -19,5 +19,5 @@ func userMatch(r *types.User, c eventbus.ConstraintMatcher, def bool) bool {
|
||||
return r != nil && c.Match(r.Email)
|
||||
}
|
||||
|
||||
return def
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user