On Fri, Nov 16, 2012 at 4:23 AM, Anil Jangity <an...@me.com> wrote:
> I am trying to build a custom User model with a custom authentication backend 
> (ldap).
> Here is what I've done so far:
>
> Custom LDAP authentication backend:
>
> class LDAPBackend:
>     def get_user(self, user_id):
>         try:
>             return LDAPUser.objects.get(pk=user_id)
>         except LDAPUser.DoesNotExist:
>             return None
>
>
>     def authenticate(self, dn=None, password=None):
>         conn = DS(settings.AUTH_LDAP_SERVER)
>         conn.connect(dn, password)
>         try:
>             user = LDAPUser.objects.get_or_create(dn=dn)
>             return user
>         except LDAPUser.DoesNotExist:
>             pass
>         return None
>
> =========================
>
> Custom User model:
>
> class LDAPUserManager(BaseUserManager):
>
>     def get_or_create(self, dn):
>         user = LDAPUser(dn)
>         return user
>
>
> class LDAPUser(AbstractBaseUser):
>     dn = models.CharField(max_length=128)
>
>     is_active = models.BooleanField(default=True)
>     is_admin = models.BooleanField(default=False)
>
>     objects = LDAPUserManager()
>     USERNAME_FIELD = 'dn'
>
>     def __unicode__(self):
>         return self.dn
>     …
>
>
>
> =========================
>
>
>
> Based on that, in my view I have:
> def do_login():
>         user = authenticate(dn=dn, password=password)
>         user.is_authenticated()    <--- returns True
>         return HttpResponseRedirect("/manage")
>
> def manage():
>     print request.session['dn']
>     print request.user
>
>
> Shouldn't the request.user in manage() be my LDAPUser instance? It shows 
> AnonymousUser. Where is the session stored (if any) when I do the 
> authenticate() ?
> Sorry for these basic questions, I am new to Django.
>
> Also, I feel like I am going to end up having to customize every aspects of 
> the User model… am I going about this the wrong way?
>
> What I am really trying to do is create a LDAP based users as well as LDAP 
> based database (no SQL database) for all my data. I looked at 
> Django_auth_ldap, but that won't cut it, I need to have a little bit more 
> customization on it (I think).
>
> Thanks for any help!
> Anil
>

I'm surprised you didn't get an answer sooner. You call
authenticate(), but you do not call login(). authenticate()
authenticates a user against your auth backends, but login() is what
persists the authenticated user in the session.

This is detailed in the docs:

https://docs.djangoproject.com/en/1.4/topics/auth/#how-to-log-a-user-in

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to