diff --git a/pkg/decision/evaluator/matchers/exists.go b/pkg/decision/evaluator/matchers/exists.go index 37a1392cd..1b9719967 100644 --- a/pkg/decision/evaluator/matchers/exists.go +++ b/pkg/decision/evaluator/matchers/exists.go @@ -28,11 +28,5 @@ type ExistsMatcher struct { // Match returns true if the user's attribute is in the condition func (m ExistsMatcher) Match(user entities.UserContext) (bool, error) { - - _, err := user.GetStringAttribute(m.Condition.Name) - if err != nil { - return false, nil - } - - return true, nil + return user.CheckAttributeExists(m.Condition.Name), nil } diff --git a/pkg/entities/user_context.go b/pkg/entities/user_context.go index caf4c8fbc..df681988e 100644 --- a/pkg/entities/user_context.go +++ b/pkg/entities/user_context.go @@ -31,6 +31,15 @@ type UserContext struct { Attributes map[string]interface{} } +// CheckAttributeExists returns whether the specified attribute name exists in the attributes map. +func (u UserContext) CheckAttributeExists(attrName string) bool { + if value, ok := u.Attributes[attrName]; ok && value != nil { + return true + } + + return false +} + // GetStringAttribute returns the string value for the specified attribute name in the attributes map. Returns error if not found. func (u UserContext) GetStringAttribute(attrName string) (string, error) { if value, ok := u.Attributes[attrName]; ok { diff --git a/pkg/entities/user_context_test.go b/pkg/entities/user_context_test.go index eb6d37d41..da155c7ec 100644 --- a/pkg/entities/user_context_test.go +++ b/pkg/entities/user_context_test.go @@ -23,6 +23,28 @@ import ( "github.com/stretchr/testify/assert" ) +func TestUserAttributeExists(t *testing.T) { + userContext := UserContext{ + Attributes: map[string]interface{}{ + "string_foo": "foo", + "bool_true": true, + "bool_false": false, + "null_value": nil, + }, + } + + // Test happy path + assert.Equal(t, true, userContext.CheckAttributeExists("string_foo")) + assert.Equal(t, true, userContext.CheckAttributeExists("bool_true")) + assert.Equal(t, true, userContext.CheckAttributeExists("bool_false")) + + // Test non-existent attr name + assert.Equal(t, false, userContext.CheckAttributeExists("invalid")) + + // Test null value + assert.Equal(t, false, userContext.CheckAttributeExists("null_value")) +} + func TestUserAttributesGetStringAttribute(t *testing.T) { userContext := UserContext{ Attributes: map[string]interface{}{