Giving default permissions or a default group to new users
Asked Answered
P

1

13

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,

Periotic answered 10/7, 2015 at 6:54 Comment(0)
C
15

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)
Collocate answered 10/7, 2015 at 7:33 Comment(3)
Thanks! In my case I think I'll also need sender=settings.AUTH_USER_MODEL. I also used group, group_created = Group.objects.get_or_create(name='Default').Periotic
I'm new to Django, which file to I add these lines?Rodrique
apps.py is the file into which the above lines go (hence AppConfig)Dejected

© 2022 - 2024 — McMap. All rights reserved.