Thanks. That makes sense. I'll give it a try.
Also as a related question.
How would I make users with "IT - Help Desk" in descriptions
automatically get the is_staff flag? Actually I guess I could just
add it to the function you wrote, right?
thanks again,
-Greg
On Mar 29, 2:59 pm, Peter Sagerson <
psag...@ignorare.net> wrote:
> Settings is not generally a good place to put nontrivial code. In general, I think the recommend place to put this sort of thing is in one of your models.py. This module will always get imported fairly early.
>
> The User model has a many-to-many relationship with the Group model, which you can manipulate like any other.[1][2] Here's a slightly more fleshed out example. This assumes that the target group already exists.
>
> from django.contrib.auth.models import Group
>
> def update_groups(sender, user=None, ldap_user=None, **kwargs):
> # Remember that every attribute maps to a list of values
> descriptions = ldap_user.attrs.get("description", [])
>
> group = Group.objects.get(name="help_desk")
> if "IT - Help Desk" in descriptions:
> user.groups.add(group)
> else:
> user.groups.remove(group)
>
> [1]
http://docs.djangoproject.com/en/1.3/topics/auth/#methods
> [2]
http://docs.djangoproject.com/en/1.3/topics/db/queries/#many-to-many-...
>
> On Mar 29, 2011, at 11:43 AM, LDAP Warrior wrote:
>
> > I asked a question[1] where the answer to me was to create a function
> > and register it like this:
>
> > import django_auth_ldap.backend
>
> > def update_groups(sender, user=None, ldap_user=None, **kwargs):
> > # Remember that every attribute maps to a list of values
> > descriptions = ldap_user.attrs.get("description", [])
>
> > if "IT - Help Desk" in descriptions:
> > # Add user to group
> > else:
> > # Remove user from group
>
> > django_auth_ldap.backend.populate_user.connect(update_groups)
>
> > My first question is where to place this code? I tried adding it to
> > settings.py but I'm getting this error:
> > ImportError: Settings cannot be imported, because environment variable
> > DJANGO_SETTINGS_MODULE is undefined.
>
> > Secondly, what do I write in the function above where it says "#add
> > user to group". Am I altering the user object somehow? Do I return
> > the user object?
>
> > Any help is much appreciated!
>
> > [1]
http://stackoverflow.com/questions/5463366/django-ldap-how-to-map-des...