Add a createuser callback for cauth django plugin
authorMagnus Hagander <magnus@hagander.net>
Sat, 13 Jan 2018 17:14:13 +0000 (18:14 +0100)
committerMagnus Hagander <magnus@hagander.net>
Sat, 13 Jan 2018 17:14:13 +0000 (18:14 +0100)
This callback can prevent new users from being created based on specific
criteria. The plugin is defined in settings.py, so it can be any python
code. For example, it can look up in a specific table if this user is
supposed to be allowed to log in at all.

tools/communityauth/sample/django/auth.py

index 1cc094fb015642754380368013fd0240e9c7559f..6cf2c808d2177c7f0f99b3e8ac9bca983b8b7df6 100644 (file)
@@ -142,6 +142,18 @@ for you.
 We apologize for the inconvenience.
 """ % (data['e'][0], data['u'][0]), content_type='text/plain')
 
+               if hasattr(settings, 'PGAUTH_CREATEUSER_CALLBACK'):
+                       res = getattr(settings, 'PGAUTH_CREATEUSER_CALLBACK')(
+                               data['u'][0],
+                               data['e'][0],
+                               ['f'][0],
+                               data['l'][0],
+                       )
+                       # If anything is returned, we'll return that as our result.
+                       # If None is returned, it means go ahead and create the user.
+                       if res:
+                               return res
+
                user = User(username=data['u'][0],
                                        first_name=data['f'][0],
                                        last_name=data['l'][0],