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
Prev Previous commit
Next Next commit
feat: WIP users endpoint role field #230
  • Loading branch information
Angeluz-07 committed Nov 11, 2020
commit c733a3d0c222126d5bf916d89656c718cfe9a08e
31 changes: 7 additions & 24 deletions time_tracker_api/users/users_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
from flask_restplus import fields, Resource
from flask_restplus._http import HTTPStatus

from time_tracker_api.activities.activities_model import create_dao
from time_tracker_api.api import (
common_fields,
api,
remove_required_constraint,
NullableString,
)
from time_tracker_api.api import common_fields, api

faker = Faker()

Expand All @@ -31,24 +25,17 @@
description='Email of the user that belongs to the tenant',
example=faker.email(),
),
'role': fields.String(
title="User's Role",
max_length=50,
description='Role assigned to the user by the tenant',
example=faker.word(['admin']),
),
},
)

user_response_fields.update(common_fields)

"""
activity_response_fields = {}
activity_response_fields.update(common_fields)

activity = ns.inherit(
'Activity',
#activity_input,
activity_response_fields
)

activity_dao = create_dao()
"""


@ns.route('')
class Users(Resource):
Expand All @@ -59,10 +46,7 @@ def get(self):
from utils.azure_users import AzureConnection

azure_connection = AzureConnection()
for u in azure_connection.users():
print(u.name, '|', u.email)
return azure_connection.users()
# return activity_dao.get_all()


@ns.route('/<string:id>')
Expand All @@ -75,4 +59,3 @@ class User(Resource):
def get(self, id):
"""Get an user"""
return {}
# return activity_dao.get(id)
29 changes: 17 additions & 12 deletions utils/azure_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def __call__(self, r):


class AzureUser:
def __init__(self, id, name, email):
def __init__(self, id, name, email, role):
self.id = id
self.name = name
self.email = email
self.role = role


class AzureConnection:
Expand All @@ -66,22 +67,26 @@ def get_token(self):
def users(self) -> List[AzureUser]:
def to_azure_user(item) -> AzureUser:
there_is_email = len(item['otherMails']) > 0
there_is_role = (
'extension_1d76efa96f604499acc0c0ee116a1453_role' in item
)

id = item['objectId']
name = item['displayName']
email = item['otherMails'][0] if there_is_email else ''
return AzureUser(id, name, email)

endpoint = f"{self.config.ENDPOINT}/users?api-version=1.6&$select=displayName,otherMails,objectId"
role = (
item['extension_1d76efa96f604499acc0c0ee116a1453_role']
if there_is_role
else None
)
return AzureUser(id, name, email, role)

endpoint = "{endpoint}/users?api-version=1.6&$select=displayName,otherMails,objectId,{role_field}".format(
endpoint=self.config.ENDPOINT,
role_field='extension_1d76efa96f604499acc0c0ee116a1453_role',
)
response = requests.get(endpoint, auth=BearerAuth(self.access_token))

assert 200 == response.status_code
assert 'value' in response.json()

_id = ""
_endpoint = f"{self.config.ENDPOINT}/users/{_id}/appRoleAssignments?api-version=1.6"
_response = requests.get(_endpoint, auth=BearerAuth(self.access_token))
from pprint import pprint

pprint(_response.json())

return [to_azure_user(item) for item in response.json()['value']]