|
1 | 1 | from dataclasses import dataclass, field, asdict, MISSING, fields |
2 | | -from typing import Dict, Any, Optional, Iterable, Union, Tuple |
| 2 | +from typing import Dict, Any, Optional, Iterable, Union, Tuple, List |
3 | 3 |
|
4 | 4 | from django.core.exceptions import ImproperlyConfigured |
5 | 5 |
|
6 | 6 | # domain name to use as a fallback setting for domain missing from WAUTH_DOMAINS |
7 | 7 | DEFAULT_DOMAIN_SETTING = "__default__" |
8 | 8 |
|
9 | 9 |
|
| 10 | +def _get_group_list(value) -> List[str]: |
| 11 | + if value is None: |
| 12 | + return [] |
| 13 | + elif isinstance(value, str): |
| 14 | + return [value] |
| 15 | + else: |
| 16 | + return value |
| 17 | + |
| 18 | + |
10 | 19 | @dataclass() |
11 | 20 | class LDAPSettings: |
12 | 21 | # connection settings |
@@ -39,7 +48,9 @@ class LDAPSettings: |
39 | 48 | SUPERUSER_GROUPS: Optional[Union[str, Iterable[str]]] = "Domain Admins" |
40 | 49 | STAFF_GROUPS: Optional[Union[str, Iterable[str]]] = "Administrators" |
41 | 50 | ACTIVE_GROUPS: Optional[Union[str, Iterable[str]]] = None |
42 | | - GROUP_MAP: Dict[str, str] = field(default_factory=dict) |
| 51 | + PROPAGATE_GROUPS: bool = True |
| 52 | + GROUP_MAP: Dict[str, Union[str, Iterable[str]]] = field(default_factory=dict) |
| 53 | + FLAG_MAP: Dict[str, Union[str, Iterable[str]]] = field(default_factory=dict) |
43 | 54 |
|
44 | 55 | @classmethod |
45 | 56 | def for_domain(cls, domain: str): |
@@ -72,3 +83,30 @@ def for_domain(cls, domain: str): |
72 | 83 | for setting, value in merged_settings.items() |
73 | 84 | if setting in cls_fields |
74 | 85 | }) |
| 86 | + |
| 87 | + def get_superuser_groups(self): |
| 88 | + return _get_group_list(self.SUPERUSER_GROUPS) |
| 89 | + |
| 90 | + def get_staff_groups(self): |
| 91 | + if self.PROPAGATE_GROUPS: |
| 92 | + return list({*_get_group_list(self.STAFF_GROUPS), *self.get_superuser_groups()}) |
| 93 | + else: |
| 94 | + return _get_group_list(self.STAFF_GROUPS) |
| 95 | + |
| 96 | + def get_active_groups(self): |
| 97 | + if self.PROPAGATE_GROUPS: |
| 98 | + return list({*_get_group_list(self.ACTIVE_GROUPS), *self.get_staff_groups()}) |
| 99 | + else: |
| 100 | + return _get_group_list(self.ACTIVE_GROUPS) |
| 101 | + |
| 102 | + def get_flag_map(self): |
| 103 | + return { |
| 104 | + "is_superuser": self.get_superuser_groups(), |
| 105 | + "is_staff": self.get_staff_groups(), |
| 106 | + "is_active": self.get_active_groups(), |
| 107 | + **{ |
| 108 | + k: _get_group_list(v) |
| 109 | + for k, v in self.FLAG_MAP.items() |
| 110 | + } |
| 111 | + } |
| 112 | + |
0 commit comments