From 79659ae1dc086075b9362ebe0c63001b451445fc Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Sat, 13 Jan 2018 18:11:21 +0100 Subject: [PATCH] Add ability to import user to django cauth example This has been in used around a number of community sites already, backpatch it into the upstream master. --- tools/communityauth/sample/django/auth.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tools/communityauth/sample/django/auth.py b/tools/communityauth/sample/django/auth.py index b1c3503b..1cc094fb 100644 --- a/tools/communityauth/sample/django/auth.py +++ b/tools/communityauth/sample/django/auth.py @@ -208,3 +208,28 @@ def user_search(searchterm=None, userid=None): j = json.loads(s) return j + +# Import a user into the local authentication system. Will initially +# make a search for it, and if anything other than one entry is returned +# the import will fail. +# Import is only supported based on userid - so a search should normally +# be done first. This will result in multiple calls to the upstream +# server, but they are cheap... +# The call to this function should normally be wrapped in a transaction, +# and this function itself will make no attempt to do anything about that. +def user_import(uid): + u = user_search(userid=uid) + if len(u) != 1: + raise Exception("Internal error, duplicate or no user found") + + u = u[0] + + if User.objects.filter(username=u['u']).exists(): + raise Exception("User already exists") + + User(username=u['u'], + first_name=u['f'], + last_name=u['l'], + email=u['e'], + password='setbypluginnotsha1', + ).save() -- 2.11.4.GIT