diff --git a/src/uds/core/util/auth.py b/src/uds/core/util/auth.py index f8b1ea19a..27b04ab58 100644 --- a/src/uds/core/util/auth.py +++ b/src/uds/core/util/auth.py @@ -108,8 +108,10 @@ def get_attributes_regex_field(field: "ui.gui.TextField|str") -> set[str]: # If attributes concateated with +, add all if "+" in attr: res.update(attr.split("+")) - elif ":" in attr: # lower precedence than + - res.add(attr.split(":")[0]) + elif "**" in attr: # lower precedence than +; "attr**prefix" — request the actual attribute + res.add(attr.split("**", 1)[0]) + elif ":" in attr: # lower precedence than +; "prefix:attr" — request the actual attribute + res.add(attr.split(":", 1)[1]) else: # If not, add the attribute res.add(attr) @@ -145,6 +147,10 @@ def _get_attr(attr_name: str) -> list[str]: # Prepend the value after ** to attribute value before ** attr, prependable = attr_name.split("**") val = [prependable + a for a in ensure.as_list(attributes.get(attr, []))] + elif ":" in attr_name: + # "prefix:attr" — look up attr, prepend prefix to each value (e.g. alumno:alum + "S" -> "alumnoS") + prefix, attr = attr_name.split(":", 1) + val = [prefix + a for a in ensure.as_list(attributes.get(attr, []))] else: val = ensure.as_list(attributes.get(attr_name, [])) return val diff --git a/tests/core/util/test_auth_regex.py b/tests/core/util/test_auth_regex.py new file mode 100644 index 000000000..0760ebb6a --- /dev/null +++ b/tests/core/util/test_auth_regex.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- +from tests.utils.test import UDSTestCase + + +class TestProcessRegexField(UDSTestCase): + def test_plain_attr(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field("group", {"group": ["a", "b"]}), + ["a", "b"], + ) + + def test_plus_concat(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field("a+b", {"a": ["x"], "b": ["y"]}), + ["x", "y"], + ) + + def test_colon_prefix_rename(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field( + "alumno:alumno\npas:pas\npdi:pdi", + {"alumno": ["S"], "pas": ["S"], "pdi": ["N"]}, + ), + ["alumnoS", "pasS", "pdiN"], + ) + + def test_colon_self_rename(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field("grupo2:grupo2", {"grupo2": ["foo"]}), + ["grupo2foo"], + ) + + def test_double_star_prefix(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field("val**pre", {"val": ["S"]}), + ["preS"], + ) + + def test_regex_with_groups(self) -> None: + from uds.core.util.auth import process_regex_field + + self.assertEqual( + process_regex_field("dn=ou=([^,]+)", {"dn": ["ou=staff,dc=uca"]}), + ["staff"], + ) + + def test_get_attributes_regex_field_colon(self) -> None: + from uds.core.util.auth import get_attributes_regex_field + + self.assertEqual(get_attributes_regex_field("alumno:alumno\npas:pas"), {"alumno", "pas"})