Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/uds/core/util/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions tests/core/util/test_auth_regex.py
Original file line number Diff line number Diff line change
@@ -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"})
Loading