Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: TT-156 add group fields in model
  • Loading branch information
Angeluz-07 committed Feb 26, 2021
commit ae854de83188d78c17419c88ef8f643c4724cf46
14 changes: 12 additions & 2 deletions time_tracker_api/users/users_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,23 @@
title='Roles',
description='List of the roles assigned to the user by the tenant',
),
example=Faker().words(
3, ['time-tracker-admin', 'test-user', 'guest',], unique=True
),
),
'groups': fields.List(
fields.String(
title='Groups',
description='List of the groups the user belongs to, assigned by the tenant',
),
example=Faker().words(
3,
[
'time-tracker-admin',
'test-user',
'guest',
'time-tracker-tester',
'time-tracker-guest',
],
unique=True,
),
),
},
Expand Down
29 changes: 27 additions & 2 deletions utils/azure_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ def __call__(self, r):


class AzureUser:
def __init__(self, id, name, email, roles):
def __init__(self, id, name, email, roles, groups):
self.id = id
self.name = name
self.email = email
self.roles = roles
self.groups = groups if groups else []


HTTP_PATCH_HEADERS = {
Expand Down Expand Up @@ -115,7 +116,14 @@ def to_azure_user(self, item) -> AzureUser:
for (field_name, field_value) in ROLE_FIELD_VALUES.values()
if field_name in item
]
return AzureUser(id, name, email, roles)

groups_and_users = self.get_groups_and_users()
groups = [
item['group_name']
for item in groups_and_users
if id in item['user_ids']
]
return AzureUser(id, name, email, roles, groups)

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

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

def get_groups_and_users(self):
endpoint = "{endpoint}/groups?api-version=1.6&$select=displayName,members&$expand=members".format(
endpoint=self.config.ENDPOINT
)
response = requests.get(endpoint, auth=BearerAuth(self.access_token))
assert 200 == response.status_code

result = []
for item in response.json()['value']:
new_item = {}
new_item['group_name'] = item['displayName']
user_ids = [member['objectId'] for member in item['members']]
new_item['user_ids'] = user_ids
result.append(new_item)

return result

def is_user_in_group(self, user_id, data: dict):
group_id = self.get_group_id_by_group_name(
group_name=data['group_name']
Expand Down