Add ability to import user to django cauth example
authorMagnus Hagander <magnus@hagander.net>
Sat, 13 Jan 2018 17:11:21 +0000 (18:11 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sat, 13 Jan 2018 17:11:21 +0000 (18:11 +0100)
This has been in used around a number of community sites already,
backpatch it into the upstream master.

tools/communityauth/sample/django/auth.py

index b1c3503b1291135534f776a9f3e08569ea84ab83..1cc094fb015642754380368013fd0240e9c7559f 100644 (file)
@@ -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()