fixed region fetching in user_attributes
This commit is contained in:
@@ -86,9 +86,17 @@ func GetUserAttributes(userID string) (map[string]string, error) {
|
||||
SELECT attribute_name, attribute_value
|
||||
FROM uess_user_management.user_attributes
|
||||
WHERE users_id = ?
|
||||
|
||||
UNION
|
||||
|
||||
SELECT 'region' AS attribute_name, o.reg AS attribute_value
|
||||
FROM uess_reference.office o
|
||||
LEFT JOIN uess_user_management.users u
|
||||
ON o.id = u.office_id
|
||||
WHERE u.users_id = ?
|
||||
`
|
||||
|
||||
rows, err := db.DB.Query(query, userID)
|
||||
rows, err := db.DB.Query(query, userID, userID)
|
||||
if err != nil {
|
||||
log.Printf("[Repository] ✗ Database error querying user attributes: %v", err)
|
||||
return nil, fmt.Errorf("error querying user attributes: %w", err)
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestGetPolicyAttributesByPermissionSuccess(t *testing.T) {
|
||||
AddRow(1, "department", "user", "=", "engineering", 1).
|
||||
AddRow(2, "level", "user", ">=", "5", 1)
|
||||
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM uess_user_management.policy_attributes WHERE permission_id = \\?").
|
||||
mock.ExpectQuery("SELECT id, attribute_name, attribute_type, comparison, attribute_value, permission_id FROM uess_user_management.policy_attributes WHERE permission_id = ?").
|
||||
WithArgs(1).
|
||||
WillReturnRows(rows)
|
||||
|
||||
|
||||
@@ -60,9 +60,17 @@ func compare(actual, expected, operator string) bool {
|
||||
|
||||
switch operator {
|
||||
case "=":
|
||||
// Special logic for region: allow '1' and '01' to match
|
||||
if isRegionComparison(actual, expected) {
|
||||
return normalizeRegion(actual) == normalizeRegion(expected)
|
||||
}
|
||||
return actual == expected // case-sensitive
|
||||
|
||||
case "!=":
|
||||
// Special logic for region: allow '1' and '01' to match
|
||||
if isRegionComparison(actual, expected) {
|
||||
return normalizeRegion(actual) != normalizeRegion(expected)
|
||||
}
|
||||
return actual != expected // case-sensitive
|
||||
|
||||
case ">":
|
||||
@@ -94,6 +102,31 @@ func compare(actual, expected, operator string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// Checks if the comparison is for region attribute
|
||||
func isRegionComparison(actual, expected string) bool {
|
||||
// Only trigger for region values that are numeric or zero-padded numeric
|
||||
// This is a heuristic: if both are digits or zero-padded digits, treat as region
|
||||
return isRegionValue(actual) && isRegionValue(expected)
|
||||
}
|
||||
|
||||
func isRegionValue(val string) bool {
|
||||
val = strings.TrimLeft(val, "0")
|
||||
return len(val) > 0 && isDigits(val)
|
||||
}
|
||||
|
||||
func isDigits(val string) bool {
|
||||
for _, r := range val {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func normalizeRegion(val string) string {
|
||||
return strings.TrimLeft(val, "0")
|
||||
}
|
||||
|
||||
func numericCompare(actual, expected string, compareFn func(float64, float64) bool) bool {
|
||||
actualNum, err1 := strconv.ParseFloat(actual, 64)
|
||||
expectedNum, err2 := strconv.ParseFloat(expected, 64)
|
||||
|
||||
Reference in New Issue
Block a user