Skip to content

Persistent ldap_user object #9

@alexgrjd

Description

@alexgrjd

We have a custom AbstractUser model which looks like this:

class User(AbstractUser):
    organization = models.CharField(max_length=64)
    organization_cgkprimarykey = models.PositiveIntegerField()

    def clean(self):
        ldap_prefix_1, ldap_prefix_2 = 'AUTH_LDAP_1_', 'AUTH_LDAP_2_'
        ldap_user_object = self.ldap_user
        usd = UsdDBActions()
        username = self.username

        if ldap_user_object.backend.settings_prefix == ldap_prefix_1:
            # do something

        elif ldap_user_object.backend.settings_prefix == ldap_prefix_2:
            # do something else

    def save(self, *args, **kwargs):
        self.clean()
        super(User, self).save(*args, **kwargs)

Now, we use the multi-config to authenticate the user against 2 LDAP backends which works great.

The problem comes when we try to update the permissions of the user via shell (after the user first logged in to the app):

from backend.apps.base.models import User

u = User.objects.first()
u.is_staff = True
u.is_superuser = True
u.save()

The above throws the following error when the save() method is called:

AttributeError                            Traceback (most recent call last)
<ipython-input-4-36670e5dc5d1> in <module>()
----> 1 u.save()

~/main/apps/Order/backend/apps/base/models.py in save(self, *args, **kwargs)
     48 
     49     def save(self, *args, **kwargs):
---> 50         self.clean()
     51         super(User, self).save(*args, **kwargs)

~/main/apps/Order/backend/apps/base/models.py in clean(self)
     21     def clean(self):
     22         ldap_prefix_1, ldap_prefix_2 = 'AUTH_LDAP_1_', 'AUTH_LDAP_2_'
---> 23         ldap_user_object = self.ldap_user
     24         usd = UsdDBActions()
     25         username = self.username

AttributeError: 'User' object has no attribute 'ldap_user'

I'm not sure if we did anything wrong but IMO it looks like the self.ldap_user object is persistent only when the login is being made. Did we miss anything? Any ideas on how can we come around this issue?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions