Skip to content

Commit

Permalink
Merge pull request #5 from brunopacheco1/master
Browse files Browse the repository at this point in the history
Get object of model.User from helpers.process_user
  • Loading branch information
tino097 authored Oct 13, 2023
2 parents 5acdc7d + 2d7e0fb commit 63a6ee0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 38 deletions.
27 changes: 13 additions & 14 deletions ckanext/keycloak/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import ckan.model as model
import ckan.plugins.toolkit as tk
import ckan.lib.dictization.model_dictize as model_dictize


log = logging.getLogger(__name__)
Expand Down Expand Up @@ -36,26 +35,16 @@ def ensure_unique_username_from_email(email):
return cleaned_localpart

def process_user(userinfo):
context = {
u'ignore_auth': True,
}
return _get_user_by_email(userinfo.get('email')) or tk.get_action(
u'user_create'
)(context, userinfo)
return _get_user_by_email(userinfo.get('email')) or _create_user(userinfo)

def _get_user_by_email(email):
context = {
u'keep_email': True,
u'model': model,
}
user = model.User.by_email(email)
if user and isinstance(user, list):
user = user[0]

activate_user_if_deleted(user)

return model_dictize.user_dictize(user, context) if user else False


return user

def activate_user_if_deleted(user):
u'''Reactivates deleted user.'''
Expand All @@ -65,3 +54,13 @@ def activate_user_if_deleted(user):
user.activate()
user.commit()
log.info(u'User {} reactivated'.format(user.name))

def _create_user(userinfo):
context = {
u'ignore_auth': True,
}
created_user_dict = tk.get_action(
u'user_create'
)(context, userinfo)

return _get_user_by_email(created_user_dict['email'])
34 changes: 10 additions & 24 deletions ckanext/keycloak/views.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import logging
from flask import Blueprint, session
from flask import Blueprint
from ckan.plugins import toolkit as tk
import ckan.lib.helpers as h
import ckan.model as model
from ckan.common import g
from ckan.views.user import set_repoze_user, RequestResetView
from ckanext.keycloak.keycloak import KeycloakClient
import ckanext.keycloak.helpers as helpers
from urllib.parse import urlencode
import ckan.lib.dictization as dictization
from os import environ

log = logging.getLogger(__name__)
Expand All @@ -24,16 +22,6 @@

client = KeycloakClient(server_url, client_id, realm_name, client_secret_key)

def _create_user(user_dict):
context = {
u'ignore_auth': True,
}
try:
return tk.get_action(u'user_create')(context, user_dict)
except tk.ValidationError as e:
error_message = (e.error_summary or e.message or e.error_dict)
tk.abort(400, error_message)

def _log_user_into_ckan(resp):
""" Log the user into different CKAN versions.
CKAN 2.10 introduces flask-login and login_user method.
Expand All @@ -43,16 +31,16 @@ def _log_user_into_ckan(resp):
"""
if tk.check_ckan_version(min_version="2.10"):
from ckan.common import login_user
login_user(g.userobj)
login_user(g.user_obj)
return

if tk.check_ckan_version(min_version="2.9.6"):
user_id = "{},1".format(g.userobj.get('id'))
user_id = "{},1".format(g.user_obj.id)
else:
user_id = g.userobj['name']
user_id = g.user
set_repoze_user(user_id, resp)

log.info(u'User {0}<{1}> logged in successfully'.format(g.userobj['name'], g.userobj['email']))
log.info(u'User {0}<{1}> logged in successfully'.format(g.user_obj.name, g.user_obj.email))

def sso():
log.info("SSO Login")
Expand Down Expand Up @@ -80,14 +68,12 @@ def sso_login():
}
}
context = {"model": model, "session": model.Session}
user = helpers.process_user(user_dict)
g.user = user
g.userobj = user
context['user'] = user
context['auth_user_obj'] = g.userobj
user_id = "{}".format(user.get('name'))
g.user_obj = helpers.process_user(user_dict)
g.user = g.user_obj.name
context['user'] = g.user
context['auth_user_obj'] = g.user_obj

response = tk.redirect_to(tk.url_for('user.me',context))
response = tk.redirect_to(tk.url_for('user.me', context))

_log_user_into_ckan(response)
log.info("Logged in success")
Expand Down

0 comments on commit 63a6ee0

Please sign in to comment.