-
Notifications
You must be signed in to change notification settings - Fork 0
feat(accounts): account profile view backend — contact_email + PATCH /v2/users/me/ (GUNDI-5347) #427
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
Merged
Merged
feat(accounts): account profile view backend — contact_email + PATCH /v2/users/me/ (GUNDI-5347) #427
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
72fd3e6
add contact email into account model
amicavi 4ac6ae4
move keycloak out of utils to have its own module
amicavi 142362b
add workspaces and role into user details and allow parcial update
amicavi 10668a4
harden /v2/users/me/ and keycloak module (GUNDI-5347)
amicavi 9359a51
address Copilot review on PR #427
amicavi 1cfcc0d
address second Copilot review on PR #427
amicavi ddce5f9
drop dead get_password_reset_link
amicavi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import logging | ||
|
|
||
| import requests | ||
|
|
||
| from cdip_admin import settings | ||
| from core.utils import get_admin_access_token | ||
|
|
||
|
|
||
| KEYCLOAK_SERVER = settings.KEYCLOAK_SERVER | ||
| KEYCLOAK_REALM = settings.KEYCLOAK_REALM | ||
|
|
||
| KEYCLOAK_ADMIN_API = f"{KEYCLOAK_SERVER}/auth/admin/realms/{KEYCLOAK_REALM}/" | ||
|
|
||
| # (connect_timeout, read_timeout) for outbound Keycloak admin calls. | ||
| # Bounds the time a portal request can spend blocked on Keycloak. | ||
| KEYCLOAK_HTTP_TIMEOUT = (5, 10) | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def add_account(user): | ||
| """Create a user in Keycloak. | ||
|
|
||
| `user` is a dict with keys: ``email``, ``first_name``, ``last_name``. | ||
|
|
||
| Returns ``True`` if the user exists in Keycloak after the call | ||
| (newly created, already-present, or any 2xx). Returns ``False`` if | ||
| the call could not be made (no admin token) or Keycloak returned an | ||
| unexpected error. | ||
| """ | ||
| token = get_admin_access_token() | ||
| if not token: | ||
| logger.warning("Cannot get a valid Keycloak admin access_token.") | ||
| return False | ||
|
|
||
| url = KEYCLOAK_ADMIN_API + "users" | ||
| headers = { | ||
| "authorization": f"{token['token_type']} {token['access_token']}", | ||
| "Content-type": "application/json", | ||
| } | ||
| user_data = { | ||
| "email": user["email"], | ||
| "firstName": user["first_name"], | ||
| "lastName": user["last_name"], | ||
| "enabled": True, | ||
| } | ||
|
|
||
| try: | ||
| response = requests.post( | ||
| url=url, headers=headers, json=user_data, timeout=KEYCLOAK_HTTP_TIMEOUT, | ||
| ) | ||
| except requests.RequestException: | ||
| logger.exception("Keycloak add_account network error") | ||
| return False | ||
|
|
||
| if 200 <= response.status_code < 300: | ||
| logger.info("User created successfully") | ||
| return True | ||
| if response.status_code == 409: | ||
| logger.info(f'Keycloak user {user["email"]} already exists.') | ||
| return True | ||
|
|
||
| logger.error(f"Error adding account: {response.status_code}, {response.text}") | ||
| return False |
21 changes: 21 additions & 0 deletions
21
cdip_admin/accounts/migrations/0017_accountprofile_contact_email.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('accounts', '0016_alter_eula_options'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name='accountprofile', | ||
| name='contact_email', | ||
| field=models.EmailField( | ||
| blank=True, | ||
| help_text='User-controlled contact email, independent of the auth provider email.', | ||
| max_length=254, | ||
| null=True, | ||
| ), | ||
| ), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.