Giving default permissions or a default group to new users

jozxyqk picture jozxyqk · Jul 10, 2015 · Viewed 6.9k times · Source

Default behaviour seems to be new users have no permissions and no groups. However I'd like not to have to manually grant every single new users basic permissions, and I'd assume they'd like not to have to wait for me to do so.

How should I assign default permissions for new users?

Some similar questions have been asked but with no clear answer for the general case:

I'm using python-social-auth so I don't have my own create-user form and view which I guess is where everyone else sets default permissions. I assume I need an on-user-create hook of some sort but not sure what the cleanest approach is.

This is unrelated to creating the default possible permissions that may be granted to users, although for reference,

Answer

kmmbvnr picture kmmbvnr · Jul 10, 2015

You can subscribe to post_save signal on User model and put newly created user to desired group or add permissions.

from django.contrib.auth.models import Group

def add_to_default_group(sender, **kwargs):
    user = kwargs["instance"]
    if kwargs["created"]:
        group = Group.objects.get(name='groupname')
        user.groups.add(group)

And on django 1.8+ put following code into your AppConfig.ready()

from django.conf import settings

post_save.connect(add_to_default_group, sender=settings.AUTH_USER_MODEL)