Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set user roles #318

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@
"mypy-type-checker.importStrategy": "fromEnvironment",
"isort.importStrategy": "fromEnvironment",
"black-formatter.importStrategy": "fromEnvironment",
"workbench.colorCustomizations": {
"activityBar.background": "#4D1C3B",
"titleBar.activeBackground": "#6B2752",
"titleBar.activeForeground": "#FDF8FB"
},
}
1 change: 1 addition & 0 deletions descope/management/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MgmtV1:
user_update_name_path = "/v1/mgmt/user/update/name"
user_update_picture_path = "/v1/mgmt/user/update/picture"
user_update_custom_attribute_path = "/v1/mgmt/user/update/customAttribute"
user_set_role_path = "/v1/mgmt/user/update/role/set"
user_add_role_path = "/v1/mgmt/user/update/role/add"
user_remove_role_path = "/v1/mgmt/user/update/role/remove"
user_set_password_path = "/v1/mgmt/user/password/set"
Expand Down
57 changes: 57 additions & 0 deletions descope/management/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,34 @@ def update_custom_attribute(
)
return response.json()

def set_roles(
self,
login_id: str,
role_names: List[str],
) -> dict:
"""
Set roles to a user without tenant association. Use set_tenant_roles
for users that are part of a multi-tenant project.

Args:
login_id (str): The login ID of the user to update.
role_names (List[str]): A list of roles to set to a user without tenant association.

Return value (dict):
Return dict in the format
{"user": {}}
Containing the updated user information.

Raise:
AuthException: raised if the operation fails
"""
response = self._auth.do_post(
MgmtV1.user_set_role_path,
{"loginId": login_id, "roleNames": role_names},
pswd=self._auth.management_key,
)
return response.json()

def add_roles(
self,
login_id: str,
Expand Down Expand Up @@ -850,6 +878,35 @@ def remove_tenant(
)
return response.json()

def set_tenant_roles(
self,
login_id: str,
tenant_id: str,
role_names: List[str],
) -> dict:
"""
Set roles to a user in a specific tenant.

Args:
login_id (str): The login ID of the user to update.
tenant_id (str): The ID of the user's tenant.
role_names (List[str]): A list of roles to set on the user.

Return value (dict):
Return dict in the format
{"user": {}}
Containing the updated user information.

Raise:
AuthException: raised if the operation fails
"""
response = self._auth.do_post(
MgmtV1.user_set_role_path,
{"loginId": login_id, "tenantId": tenant_id, "roleNames": role_names},
pswd=self._auth.management_key,
)
return response.json()

def add_tenant_roles(
self,
login_id: str,
Expand Down
76 changes: 76 additions & 0 deletions tests/management/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,42 @@ def test_update_custom_attribute(self):
timeout=DEFAULT_TIMEOUT_SECONDS,
)

def test_set_roles(self):
# Test failed flows
with patch("requests.post") as mock_post:
mock_post.return_value.ok = False
self.assertRaises(
AuthException,
self.client.mgmt.user.set_roles,
"valid-id",
["foo", "bar"],
)

# Test success flow
with patch("requests.post") as mock_post:
network_resp = mock.Mock()
network_resp.ok = True
network_resp.json.return_value = json.loads("""{"user": {"id": "u1"}}""")
mock_post.return_value = network_resp
resp = self.client.mgmt.user.set_roles("valid-id", ["foo", "bar"])
user = resp["user"]
self.assertEqual(user["id"], "u1")
mock_post.assert_called_with(
f"{common.DEFAULT_BASE_URL}{MgmtV1.user_set_role_path}",
headers={
**common.default_headers,
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}",
},
params=None,
json={
"loginId": "valid-id",
"roleNames": ["foo", "bar"],
},
allow_redirects=False,
verify=True,
timeout=DEFAULT_TIMEOUT_SECONDS,
)

def test_add_roles(self):
# Test failed flows
with patch("requests.post") as mock_post:
Expand Down Expand Up @@ -1083,6 +1119,46 @@ def test_remove_tenant(self):
timeout=DEFAULT_TIMEOUT_SECONDS,
)

def test_set_tenant_roles(self):
# Test failed flows
with patch("requests.post") as mock_post:
mock_post.return_value.ok = False
self.assertRaises(
AuthException,
self.client.mgmt.user.set_tenant_roles,
"valid-id",
"tid",
["foo", "bar"],
)

# Test success flow
with patch("requests.post") as mock_post:
network_resp = mock.Mock()
network_resp.ok = True
network_resp.json.return_value = json.loads("""{"user": {"id": "u1"}}""")
mock_post.return_value = network_resp
resp = self.client.mgmt.user.set_tenant_roles(
"valid-id", "tid", ["foo", "bar"]
)
user = resp["user"]
self.assertEqual(user["id"], "u1")
mock_post.assert_called_with(
f"{common.DEFAULT_BASE_URL}{MgmtV1.user_set_role_path}",
headers={
**common.default_headers,
"Authorization": f"Bearer {self.dummy_project_id}:{self.dummy_management_key}",
},
params=None,
json={
"loginId": "valid-id",
"tenantId": "tid",
"roleNames": ["foo", "bar"],
},
allow_redirects=False,
verify=True,
timeout=DEFAULT_TIMEOUT_SECONDS,
)

def test_add_tenant_roles(self):
# Test failed flows
with patch("requests.post") as mock_post:
Expand Down
Loading