-
Notifications
You must be signed in to change notification settings - Fork 14
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
LDAP: Update user names formatting on sync #903
Merged
kpsherva
merged 2 commits into
CERNDocumentServer:master
from
sakshamarora1:ldap/sort_by_last_names
Jun 14, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -193,17 +193,17 @@ def run_command(command, catch_exceptions=False, verbose=True): | |
|
||
# Create users | ||
run_command("users create [email protected] -a --password=123456") # ID 1 | ||
create_userprofile_for("[email protected]", "patron1", "Yannic Vilma") | ||
create_userprofile_for("[email protected]", "patron1", "VILMA, Yannic") | ||
run_command("users create [email protected] -a --password=123456") # ID 2 | ||
create_userprofile_for("[email protected]", "patron2", "Diana Adi") | ||
create_userprofile_for("[email protected]", "patron2", "ADI, Diana") | ||
run_command("users create [email protected] -a --password=123456") # ID 3 | ||
create_userprofile_for("[email protected]", "admin", "Zeki Ryoichi") | ||
create_userprofile_for("[email protected]", "admin", "RYOICHI, Zeki") | ||
run_command("users create [email protected] -a --password=123456") # ID 4 | ||
create_userprofile_for("[email protected]", "librarian", "Hector Nabu") | ||
create_userprofile_for("[email protected]", "librarian", "NABU, Hector") | ||
run_command("users create [email protected] -a --password=123456") # ID 5 | ||
create_userprofile_for("[email protected]", "patron3", "Medrod Tara") | ||
create_userprofile_for("[email protected]", "patron3", "TARA, Medrod") | ||
run_command("users create [email protected] -a --password=123456") # ID 6 | ||
create_userprofile_for("[email protected]", "patron4", "Devi Cupid") | ||
create_userprofile_for("[email protected]", "patron4", "CUPID, Devi") | ||
|
||
# Assign roles | ||
run_command("roles add [email protected] admin") | ||
|
This file contains 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 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 |
---|---|---|
|
@@ -15,7 +15,8 @@ class LdapClient(object): | |
|
||
Response example: | ||
[ | ||
{'displayName': [b'Joe Foe'], | ||
{'givenName': [b'Joe'], | ||
'sn': [b'FOE'], | ||
'department': [b'IT/CDA'], | ||
'uidNumber': [b'100000'], | ||
'mail': [b'[email protected]'], | ||
|
@@ -32,7 +33,8 @@ class LdapClient(object): | |
|
||
LDAP_USER_RESP_FIELDS = [ | ||
"mail", | ||
"displayName", | ||
"givenName", | ||
"sn", | ||
"department", | ||
"cernAccountType", | ||
"employeeID", | ||
|
This file contains 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 |
---|---|---|
|
@@ -21,7 +21,8 @@ class LdapUserImporter: | |
|
||
Expected input format for ldap users: | ||
[ | ||
{'displayName': [b'Joe Foe'], | ||
{'givenName': [b'Joe'], | ||
'sn': [b'FOE'], | ||
'department': [b'IT/CDA'], | ||
'uidNumber': [b'100000'], | ||
'mail': [b'[email protected]'], | ||
|
@@ -54,7 +55,9 @@ def create_invenio_user_identity(self, user_id, ldap_user): | |
|
||
def create_invenio_user_profile(self, user_id, ldap_user): | ||
"""Return new user profile.""" | ||
display_name = ldap_user["user_profile_full_name"] | ||
display_name = "{0}, {1}".format( | ||
ldap_user["user_profile_last_name"], ldap_user["user_profile_first_name"] | ||
) | ||
return UserProfile( | ||
user_id=user_id, | ||
_displayname="id_{}".format(user_id), | ||
|
This file contains 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 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,63 @@ | ||
# Copyright (C) 2024 CERN. | ||
# | ||
# CDS-ILS is free software; you can redistribute it and/or modify it under | ||
# the terms of the MIT License; see LICENSE file for more details. | ||
|
||
"""CDS-ILS oauth overrides.""" | ||
from flask import current_app, flash, g, redirect | ||
from invenio_oauthclient.contrib.cern_openid import get_resource | ||
from invenio_oauthclient.errors import OAuthCERNRejectedAccountError | ||
from invenio_oauthclient.handlers.rest import response_handler | ||
|
||
|
||
def _account_info(remote, resp): | ||
"""Retrieve remote account information used to find local user.""" | ||
g.oauth_logged_in_with_remote = remote | ||
resource = get_resource(remote, resp) | ||
|
||
valid_roles = current_app.config.get("OAUTHCLIENT_CERN_OPENID_ALLOWED_ROLES") | ||
cern_roles = resource.get("cern_roles") | ||
if cern_roles is None or not set(cern_roles).issubset(valid_roles): | ||
raise OAuthCERNRejectedAccountError( | ||
"User roles {0} are not one of {1}".format(cern_roles, valid_roles), | ||
remote, | ||
resp, | ||
) | ||
|
||
email = resource["email"] | ||
external_id = resource["cern_upn"] | ||
nice = resource["preferred_username"] | ||
name = "{0}, {1}".format(resource["family_name"].upper(), resource["given_name"]) | ||
|
||
return dict( | ||
user=dict(email=email.lower(), profile=dict(username=nice, full_name=name)), | ||
external_id=external_id, | ||
external_method="cern_openid", | ||
active=True, | ||
) | ||
|
||
|
||
def account_info(remote, resp): | ||
"""Retrieve remote account information used to find local user.""" | ||
try: | ||
return _account_info(remote, resp) | ||
except OAuthCERNRejectedAccountError as e: | ||
current_app.logger.warning(e.message, exc_info=True) | ||
flash(_("CERN account not allowed."), category="danger") | ||
return redirect("/") | ||
|
||
|
||
def account_info_rest(remote, resp): | ||
"""Retrieve remote account information used to find local user.""" | ||
try: | ||
return _account_info(remote, resp) | ||
except OAuthCERNRejectedAccountError as e: | ||
current_app.logger.warning(e.message, exc_info=True) | ||
remote_app_config = current_app.config["OAUTHCLIENT_REST_REMOTE_APPS"][ | ||
remote.name | ||
] | ||
return response_handler( | ||
remote, | ||
remote_app_config["error_redirect_url"], | ||
payload=dict(message="CERN account not allowed.", code=400), | ||
) |
This file contains 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't we only need some of these functions? even better, can we have configurable callable to inject our own _account_info? it would require adding this variable in the oauthclient (you will need to make sure the default callable is the one defined in oauthclient)