Skip to content

Commit 4d129a9

Browse files
committed
feat: update user role #229
1 parent 3ca1c39 commit 4d129a9

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

time_tracker_api/users/users_namespace.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@
3636

3737
user_response_fields.update(common_fields)
3838

39+
user_input_fields = ns.model(
40+
'UserInput',
41+
{
42+
'role': fields.String(
43+
title="User's Role",
44+
required=True,
45+
max_length=50,
46+
description='Role assigned to the user by the tenant',
47+
example=faker.word(['admin']),
48+
),
49+
},
50+
)
51+
3952

4053
@ns.route('')
4154
class Users(Resource):
@@ -59,3 +72,16 @@ class User(Resource):
5972
def get(self, id):
6073
"""Get an user"""
6174
return {}
75+
76+
@ns.doc('update_user')
77+
@ns.expect(user_input_fields)
78+
@ns.response(
79+
HTTPStatus.BAD_REQUEST, 'Invalid format or structure of the user'
80+
)
81+
@ns.marshal_with(user_response_fields)
82+
def put(self, id):
83+
"""Update an user"""
84+
from utils.azure_users import AzureConnection
85+
86+
azure_connection = AzureConnection()
87+
return azure_connection.update_user_role(id, ns.payload['role'])

utils/azure_users.py

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import msal
22
import os
33
import requests
4+
import json
45
from typing import List
56

67

@@ -65,22 +66,6 @@ def get_token(self):
6566
raise ValueError(error_info)
6667

6768
def users(self) -> List[AzureUser]:
68-
def to_azure_user(item) -> AzureUser:
69-
there_is_email = len(item['otherMails']) > 0
70-
there_is_role = (
71-
'extension_1d76efa96f604499acc0c0ee116a1453_role' in item
72-
)
73-
74-
id = item['objectId']
75-
name = item['displayName']
76-
email = item['otherMails'][0] if there_is_email else ''
77-
role = (
78-
item['extension_1d76efa96f604499acc0c0ee116a1453_role']
79-
if there_is_role
80-
else None
81-
)
82-
return AzureUser(id, name, email, role)
83-
8469
endpoint = "{endpoint}/users?api-version=1.6&$select=displayName,otherMails,objectId,{role_field}".format(
8570
endpoint=self.config.ENDPOINT,
8671
role_field='extension_1d76efa96f604499acc0c0ee116a1453_role',
@@ -89,4 +74,42 @@ def to_azure_user(item) -> AzureUser:
8974

9075
assert 200 == response.status_code
9176
assert 'value' in response.json()
92-
return [to_azure_user(item) for item in response.json()['value']]
77+
return [self.to_azure_user(item) for item in response.json()['value']]
78+
79+
def update_user_role(self, id, role):
80+
headers = {
81+
'Content-type': 'application/json',
82+
'Accept': 'application/json',
83+
}
84+
endpoint = "{endpoint}/users/{user_id}?api-version=1.6".format(
85+
endpoint=self.config.ENDPOINT, user_id=id
86+
)
87+
data = {'extension_1d76efa96f604499acc0c0ee116a1453_role': role}
88+
response = requests.patch(
89+
endpoint,
90+
auth=BearerAuth(self.access_token),
91+
data=json.dumps(data),
92+
headers=headers,
93+
)
94+
assert 204 == response.status_code
95+
96+
response = requests.get(endpoint, auth=BearerAuth(self.access_token))
97+
assert 200 == response.status_code
98+
99+
return self.to_azure_user(response.json())
100+
101+
def to_azure_user(self, item) -> AzureUser:
102+
there_is_email = len(item['otherMails']) > 0
103+
there_is_role = (
104+
'extension_1d76efa96f604499acc0c0ee116a1453_role' in item
105+
)
106+
107+
id = item['objectId']
108+
name = item['displayName']
109+
email = item['otherMails'][0] if there_is_email else ''
110+
role = (
111+
item['extension_1d76efa96f604499acc0c0ee116a1453_role']
112+
if there_is_role
113+
else None
114+
)
115+
return AzureUser(id, name, email, role)

0 commit comments

Comments
 (0)