Skip to content

Commit 63fbec9

Browse files
committed
TT-94 feat: add method to update role
1 parent 0e89e9f commit 63fbec9

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

time_tracker_api/users/users_namespace.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def get(self):
6060
return AzureConnection().users()
6161

6262

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

7879

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

108111

109112
@ns.route('/<string:user_id>/roles/<string:role_id>/revoke')
110113
@ns.param('user_id', 'The user identifier')
111114
@ns.param('role_id', 'The role name identifier')
112115
class RevokeRole(Resource):
113116
@ns.doc('revoke_role')
117+
@ns.marshal_with(user_response_fields)
114118
def post(self, user_id, role_id):
115119
"""Revoke role to user"""
116-
return [], HTTPStatus.OK
120+
return AzureConnection().update_role(user_id, role_id, is_grant=False)

utils/azure_users.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def users(self) -> List[AzureUser]:
8383
assert 'value' in response.json()
8484
return [self.to_azure_user(item) for item in response.json()['value']]
8585

86+
# TODO : DEPRECATE OR UPDATE
8687
def update_user_role(self, id, role):
8788
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
8889
endpoint=self.config.ENDPOINT, user_id=id
@@ -110,3 +111,36 @@ def to_azure_user(self, item) -> AzureUser:
110111
email = item['otherMails'][0] if there_is_email else ''
111112
role = item[self.role_field] if there_is_role else None
112113
return AzureUser(id, name, email, role)
114+
115+
def update_role(self, user_id, role_id, is_grant):
116+
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
117+
endpoint=self.config.ENDPOINT, user_id=user_id
118+
)
119+
120+
data = self.get_role_data(role_id, is_grant)
121+
response = requests.patch(
122+
endpoint,
123+
auth=BearerAuth(self.access_token),
124+
data=json.dumps(data),
125+
headers=HTTP_PATCH_HEADERS,
126+
)
127+
assert 204 == response.status_code
128+
129+
response = requests.get(endpoint, auth=BearerAuth(self.access_token))
130+
assert 200 == response.status_code
131+
132+
return self.to_azure_user(response.json())
133+
134+
def get_role_data(self, role_id, is_grant=True):
135+
ROLE_VALUES = {
136+
'admin': (
137+
'extension_1d76efa96f604499acc0c0ee116a1453_role',
138+
'time_tracker_admin',
139+
),
140+
'test': ('waitforrealvalue', 'waitforrealvalue'),
141+
}
142+
field_name, field_value = ROLE_VALUES[role_id]
143+
if is_grant:
144+
return {field_name: field_value}
145+
else:
146+
return {field_name: None}

0 commit comments

Comments
 (0)