55from cloudharness_model .models import ApplicationConfig
66from cloudharness import log
77
8+
89class KCAttributeNode :
910 def __init__ (self , name , attrs ):
1011 self .attrs = attrs
@@ -26,6 +27,7 @@ def _filter_attrs(attrs, valid_keys_map):
2627 valid_attrs .update ({key : attrs [key ][0 ]})
2728 return valid_attrs
2829
30+
2931def _construct_attribute_tree (groups , valid_keys_map ) -> KCAttributeNode :
3032 """Construct a tree of attributes from the user groups"""
3133 root = KCAttributeNode ("root" , {})
@@ -56,6 +58,7 @@ def _construct_attribute_tree(groups, valid_keys_map) -> KCAttributeNode:
5658class UserNotFound (Exception ):
5759 pass
5860
61+
5962def _compute_attributes_from_tree (node : KCAttributeNode , transform_value_fn = lambda x : x ):
6063 """Recursively traverse the tree and find the attributes per level
6164 the lower leafs overrule parent leafs values
@@ -100,7 +103,6 @@ def _compute_attributes_from_tree(node: KCAttributeNode, transform_value_fn=lamb
100103 return node .attrs
101104
102105
103-
104106def get_user_attributes (user_id : str = None , valid_keys_map = {}, default_attributes = {}, transform_value_fn = lambda x : x ) -> dict :
105107 """Get the user attributes from Keycloak recursively from the user attributes and groups
106108
@@ -115,7 +117,7 @@ def get_user_attributes(user_id: str = None, valid_keys_map={}, default_attribut
115117 Example:
116118 {'quota-ws-maxcpu': 1000, 'quota-ws-open': 10, 'quota-ws-max': 8}
117119 """
118-
120+
119121 try :
120122 auth_client = AuthClient ()
121123 if not user_id :
@@ -125,7 +127,6 @@ def get_user_attributes(user_id: str = None, valid_keys_map={}, default_attribut
125127 log .warning ("Quotas not available: error retrieving user: %s" , user_id )
126128 raise UserNotFound ("User not found" ) from e
127129
128-
129130 group_quotas = _compute_attributes_from_tree (
130131 _construct_attribute_tree (
131132 user ["userGroups" ],
@@ -137,4 +138,4 @@ def get_user_attributes(user_id: str = None, valid_keys_map={}, default_attribut
137138 for key in default_attributes :
138139 if key not in user_attrs :
139140 user_attrs .update ({key : transform_value_fn (default_attributes [key ])})
140- return user_attrs
141+ return user_attrs
0 commit comments