From f7191963dc34aaad64efb01284a0affb9c41a871 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Fri, 20 Mar 2020 17:23:02 +0100 Subject: [PATCH] Improve handling of complex constraint checks --- compose/service/event/module.go | 9 ++++++--- compose/service/event/namespace.go | 6 +++--- compose/service/event/page.go | 9 ++++++--- compose/service/event/record.go | 10 +++++++--- messaging/service/event/channel.go | 6 +++--- messaging/service/event/channel_member.go | 5 ++++- messaging/service/event/command.go | 5 ++++- messaging/service/event/message.go | 5 ++++- pkg/eventbus/constraints.go | 10 ++++++++++ system/service/event/application.go | 6 +++--- system/service/event/auth.go | 2 +- system/service/event/mail.go | 6 +++--- system/service/event/role.go | 6 +++--- system/service/event/role_member.go | 5 ++++- system/service/event/sink.go | 6 +----- system/service/event/user.go | 6 +++--- 16 files changed, 65 insertions(+), 37 deletions(-) diff --git a/compose/service/event/module.go b/compose/service/event/module.go index 2ef88c8dd..bf8e235c5 100644 --- a/compose/service/event/module.go +++ b/compose/service/event/module.go @@ -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 } diff --git a/compose/service/event/namespace.go b/compose/service/event/namespace.go index f6888e1c2..c9a81cc8e 100644 --- a/compose/service/event/namespace.go +++ b/compose/service/event/namespace.go @@ -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 } diff --git a/compose/service/event/page.go b/compose/service/event/page.go index 94c53972f..d88cae10f 100644 --- a/compose/service/event/page.go +++ b/compose/service/event/page.go @@ -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 } diff --git a/compose/service/event/record.go b/compose/service/event/record.go index 932ecbc25..17ee2b6e8 100644 --- a/compose/service/event/record.go +++ b/compose/service/event/record.go @@ -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 } diff --git a/messaging/service/event/channel.go b/messaging/service/event/channel.go index 96e6d779e..027f2ee33 100644 --- a/messaging/service/event/channel.go +++ b/messaging/service/event/channel.go @@ -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 } diff --git a/messaging/service/event/channel_member.go b/messaging/service/event/channel_member.go index 0f016a277..4a5f5850f 100644 --- a/messaging/service/event/channel_member.go +++ b/messaging/service/event/channel_member.go @@ -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 diff --git a/messaging/service/event/command.go b/messaging/service/event/command.go index ecc1b40a8..bdbf78bb2 100644 --- a/messaging/service/event/command.go +++ b/messaging/service/event/command.go @@ -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 diff --git a/messaging/service/event/message.go b/messaging/service/event/message.go index f1cba384f..418a2e9a3 100644 --- a/messaging/service/event/message.go +++ b/messaging/service/event/message.go @@ -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 diff --git a/pkg/eventbus/constraints.go b/pkg/eventbus/constraints.go index 09702f4fa..e203a5d26 100644 --- a/pkg/eventbus/constraints.go +++ b/pkg/eventbus/constraints.go @@ -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 +} diff --git a/system/service/event/application.go b/system/service/event/application.go index a8d6d0ad6..c42257182 100644 --- a/system/service/event/application.go +++ b/system/service/event/application.go @@ -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 } diff --git a/system/service/event/auth.go b/system/service/event/auth.go index 86cc83371..8436bcad8 100644 --- a/system/service/event/auth.go +++ b/system/service/event/auth.go @@ -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) } diff --git a/system/service/event/mail.go b/system/service/event/mail.go index 36ec87bbb..643e8ad96 100644 --- a/system/service/event/mail.go +++ b/system/service/event/mail.go @@ -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 { diff --git a/system/service/event/role.go b/system/service/event/role.go index 896136464..68b4e96f9 100644 --- a/system/service/event/role.go +++ b/system/service/event/role.go @@ -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 } diff --git a/system/service/event/role_member.go b/system/service/event/role_member.go index 1eb1e3d78..85deebaec 100644 --- a/system/service/event/role_member.go +++ b/system/service/event/role_member.go @@ -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) }, + ) } diff --git a/system/service/event/sink.go b/system/service/event/sink.go index b8329f9f8..c14a71444 100644 --- a/system/service/event/sink.go +++ b/system/service/event/sink.go @@ -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 diff --git a/system/service/event/user.go b/system/service/event/user.go index 78a4e856c..442ca6c80 100644 --- a/system/service/event/user.go +++ b/system/service/event/user.go @@ -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 }