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
TT-94 feat: add method to update role
  • Loading branch information
Angeluz-07 committed Jan 5, 2021
commit ca5d1354f8e06e91686222ddec5ea9f0195476dc
8 changes: 6 additions & 2 deletions time_tracker_api/users/users_namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def get(self):
return AzureConnection().users()


# TODO : DEPRECATE
@ns.route('/<string:id>/roles')
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
Expand All @@ -76,6 +77,7 @@ def post(self, id):
return AzureConnection().update_user_role(id, ns.payload['role'])


# TODO : DEPRECATE
@ns.route('/<string:user_id>/roles/<string:role_id>')
@ns.response(HTTPStatus.NOT_FOUND, 'User not found')
@ns.response(HTTPStatus.UNPROCESSABLE_ENTITY, 'The id has an invalid format')
Expand All @@ -94,6 +96,7 @@ def delete(self, user_id, role_id):
@ns.param('role_id', 'The role name identifier')
class GrantRole(Resource):
@ns.doc('grant_role')
@ns.marshal_with(user_response_fields)
def post(self, user_id, role_id):
"""
Grant role to user
Expand All @@ -103,14 +106,15 @@ def post(self, user_id, role_id):
- admin
```
"""
return [], HTTPStatus.OK
return AzureConnection().update_role(user_id, role_id, is_grant=True)


@ns.route('/<string:user_id>/roles/<string:role_id>/revoke')
@ns.param('user_id', 'The user identifier')
@ns.param('role_id', 'The role name identifier')
class RevokeRole(Resource):
@ns.doc('revoke_role')
@ns.marshal_with(user_response_fields)
def post(self, user_id, role_id):
"""Revoke role to user"""
return [], HTTPStatus.OK
return AzureConnection().update_role(user_id, role_id, is_grant=False)
34 changes: 34 additions & 0 deletions utils/azure_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def users(self) -> List[AzureUser]:
assert 'value' in response.json()
return [self.to_azure_user(item) for item in response.json()['value']]

# TODO : DEPRECATE OR UPDATE
def update_user_role(self, id, role):
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
endpoint=self.config.ENDPOINT, user_id=id
Expand Down Expand Up @@ -110,3 +111,36 @@ def to_azure_user(self, item) -> AzureUser:
email = item['otherMails'][0] if there_is_email else ''
role = item[self.role_field] if there_is_role else None
return AzureUser(id, name, email, role)

def update_role(self, user_id, role_id, is_grant):
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
endpoint=self.config.ENDPOINT, user_id=user_id
)

data = self.get_role_data(role_id, is_grant)
response = requests.patch(
endpoint,
auth=BearerAuth(self.access_token),
data=json.dumps(data),
headers=HTTP_PATCH_HEADERS,
)
assert 204 == response.status_code

response = requests.get(endpoint, auth=BearerAuth(self.access_token))
assert 200 == response.status_code

return self.to_azure_user(response.json())

def get_role_data(self, role_id, is_grant=True):
ROLE_VALUES = {
'admin': (
'extension_1d76efa96f604499acc0c0ee116a1453_role',
'time_tracker_admin',
),
'test': ('waitforrealvalue', 'waitforrealvalue'),
}
field_name, field_value = ROLE_VALUES[role_id]
if is_grant:
return {field_name: field_value}
else:
return {field_name: None}