Skip to content

Commit ae854de

Browse files
committed
feat: TT-156 add group fields in model
1 parent 87894dd commit ae854de

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

time_tracker_api/users/users_namespace.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,23 @@
2929
title='Roles',
3030
description='List of the roles assigned to the user by the tenant',
3131
),
32+
example=Faker().words(
33+
3, ['time-tracker-admin', 'test-user', 'guest',], unique=True
34+
),
35+
),
36+
'groups': fields.List(
37+
fields.String(
38+
title='Groups',
39+
description='List of the groups the user belongs to, assigned by the tenant',
40+
),
3241
example=Faker().words(
3342
3,
3443
[
3544
'time-tracker-admin',
36-
'test-user',
37-
'guest',
45+
'time-tracker-tester',
46+
'time-tracker-guest',
3847
],
48+
unique=True,
3949
),
4050
),
4151
},

utils/azure_users.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ def __call__(self, r):
3434

3535

3636
class AzureUser:
37-
def __init__(self, id, name, email, roles):
37+
def __init__(self, id, name, email, roles, groups):
3838
self.id = id
3939
self.name = name
4040
self.email = email
4141
self.roles = roles
42+
self.groups = groups if groups else []
4243

4344

4445
HTTP_PATCH_HEADERS = {
@@ -115,7 +116,14 @@ def to_azure_user(self, item) -> AzureUser:
115116
for (field_name, field_value) in ROLE_FIELD_VALUES.values()
116117
if field_name in item
117118
]
118-
return AzureUser(id, name, email, roles)
119+
120+
groups_and_users = self.get_groups_and_users()
121+
groups = [
122+
item['group_name']
123+
for item in groups_and_users
124+
if id in item['user_ids']
125+
]
126+
return AzureUser(id, name, email, roles, groups)
119127

120128
def update_role(self, user_id, role_id, is_grant):
121129
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
@@ -181,6 +189,23 @@ def get_group_id_by_group_name(self, group_name):
181189

182190
return response.json()['value'][0]['objectId']
183191

192+
def get_groups_and_users(self):
193+
endpoint = "{endpoint}/groups?api-version=1.6&$select=displayName,members&$expand=members".format(
194+
endpoint=self.config.ENDPOINT
195+
)
196+
response = requests.get(endpoint, auth=BearerAuth(self.access_token))
197+
assert 200 == response.status_code
198+
199+
result = []
200+
for item in response.json()['value']:
201+
new_item = {}
202+
new_item['group_name'] = item['displayName']
203+
user_ids = [member['objectId'] for member in item['members']]
204+
new_item['user_ids'] = user_ids
205+
result.append(new_item)
206+
207+
return result
208+
184209
def is_user_in_group(self, user_id, data: dict):
185210
group_id = self.get_group_id_by_group_name(
186211
group_name=data['group_name']

0 commit comments

Comments
 (0)