1. Benoit Chesneau
  2. django-authopenid
Issue #36 open

Automatically generate a profile when a user registers for the first time.

Noah Campbell
created an issue

I would like to have all openid logins be consider first class citizens so when a new user comes to the site, the do not need to create a new username/password or associate with an existing one.

There are two options, one is replace the django.contrib.auth or auto-create the user when an unauthenticated, but authenticated OpenID, account lands on the accounts page.

Comments (4)

  1. prem

    It would be nice to have this feature, but instead of replacing contrib.auth....

    When user logs in thru openid, if username provided in persona/profile would be used, else user would be promoted only for a username ( not password ).

    If persona has a username which is already in use, then user is prompted for a unique username.

  2. Éric Veiras Galisson

    I think I have found how to solve this problem. Assuming the AUTH_PROFILE_MODULE have a field named 'user' which is a ForeignKey(django.contrib.auth.models.User) and it's the only field required and not autofiled (via the save() for example) the following patch seems to work:

    diff -r 8fa4626ed8d7 django_authopenid/views.py
    --- a/django_authopenid/views.py        Wed Aug 26 17:27:01 2009 +0200
    +++ b/django_authopenid/views.py        Fri Nov 27 21:20:09 2009 +0100
    @@ -14,6 +14,7 @@
     # limitations under the License.
     #
     from django.conf import settings
    +from django.db.models import get_model
     from django.contrib.auth import REDIRECT_FIELD_NAME
     from django.contrib.auth.forms import *
     from django.contrib.auth.models import User
    @@ -316,6 +317,11 @@
         """ create an account """
         user = User.objects.create_user(form.cleaned_data['username'], 
                                 form.cleaned_data['email'])
    +    auth_profile_module = getattr(settings, 'AUTH_PROFILE_MODULE', None)
    +    if auth_profile_module:
    +        auth_profile_model = get_model(*auth_profile_module.split('.'))
    +        auth_profile_instance = auth_profile_model(user=user)
    +        auth_profile_instance.save()
         user.backend = "django.contrib.auth.backends.ModelBackend"
         oid_register.send(sender=user, openid=_openid)
         return user
    

    Maybe it can be improved by using a sort of self-inspection of the model to retrieve the field name which points to User.

  3. Éric Veiras Galisson

    As I have seen that you're in the process of updating this app to incorpore some available patches, I have reworked to make this patch really 'dynamic' (i.e. not depending on the FK to be named 'user'). The limitations about required fields still exists but can not, i think, been solved.

    So this is my new patch

    diff -r 8fa4626ed8d7 django_authopenid/views.py
    --- a/django_authopenid/views.py        Wed Aug 26 17:27:01 2009 +0200
    +++ b/django_authopenid/views.py        Sat Feb 06 19:38:54 2010 +0100
    @@ -14,6 +14,8 @@
     # limitations under the License.
     #
     from django.conf import settings
    +from django.db.models import get_model
    +from django.db.models.fields.related import ForeignKey
     from django.contrib.auth import REDIRECT_FIELD_NAME
     from django.contrib.auth.forms import *
     from django.contrib.auth.models import User
    @@ -316,6 +318,15 @@
         """ create an account """
         user = User.objects.create_user(form.cleaned_data['username'], 
                                 form.cleaned_data['email'])
    +    auth_profile_module = getattr(settings, 'AUTH_PROFILE_MODULE', None)
    +    if auth_profile_module:
    +        auth_profile_model = get_model(*auth_profile_module.split('.'))
    +        for field in auth_profile_model._meta.fields:
    +            if isinstance(field, ForeignKey) and (field.rel.to == User):
    +                auth_profile_fk_to_user = field.name
    +        auth_profile_instance = auth_profile_model()
    +        setattr(auth_profile_instance, auth_profile_fk_to_user, user)
    +        auth_profile_instance.save()
         user.backend = "django.contrib.auth.backends.ModelBackend"
         oid_register.send(sender=user, openid=_openid)
         return user
    

    Please review this patch to see if it corresponds to what you want, and if someone can test it on its own project, please do it and comment about it.

  4. Log in to comment