Commit c45fac1f authored by Markus Scheidgen's avatar Markus Scheidgen
Browse files

Added username normalization script. [skip-ci]

parent f0bdb9cb
Pipeline #67118 skipped
......@@ -295,11 +295,11 @@ class Keycloak():
created=datetime.fromtimestamp(keycloak_user['createdTimestamp'] / 1000),
**kwargs)
def search_user(self, query: str = None, **kwargs):
def search_user(self, query: str = None, max=1000, **kwargs):
if query is not None:
kwargs['query'] = dict(search=query)
kwargs['query'] = dict(search=query, max=max)
else:
kwargs['query'] = dict(max=1000)
kwargs['query'] = dict(max=max)
try:
keycloak_results = self._admin_client.get_users(**kwargs)
except Exception as e:
......
import re
import unidecode
from nomad import infrastructure
infrastructure.setup_logging()
existing = set()
for user in infrastructure.keycloak.search_user(max=2000):
if not re.match(r'^[a-zA-Z0-9_\-\.]+$', user.username):
# need to replace username
if user.first_name is not None and user.last_name is not None:
user.username = '%s%s' % (user.first_name[:1], user.last_name)
elif user.last_name is not None:
user.username = user.last_name
elif '@' in user.username:
user.username = user.username.split('@')[0]
user.username = unidecode.unidecode(user.username.lower())
user.username = re.sub('[^0-9a-zA-Z_\-\.]+', '', user.username)
index = 1
while user.username in existing:
user.username += '%d' % index
index += 1
existing.add(user.username)
infrastructure.keycloak._admin_client.update_user(
user_id=user.user_id, payload=dict(username=user.username))
print(user.username)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment