0

I have a registration method in views.py

def register_page(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            user = User.objects.create_user(username=form.cleaned_data['username'],
                                            password=form.cleaned_data['password1'],
                                            email=form.cleaned_data['email'])
            if request.POST.get("admin", ""):
                content_type = ContentType.objects.get_for_model(User)
                permission = Permission.objects.get(codename='admin_can_manage_users')
                user.user_permissions.add(permission)
            new_user = authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password1'])
            login(request, new_user)
            return redirect('payment_list')
    form = RegistrationForm()
    variables = RequestContext(request, {'form': form})
    return render_to_response('registration/register.html',variables)

I also have a register form with standart fields and additional manually-added checkboxes. I want to check if one checkbox is checked and if so add permissions to users.

<form method="post" action=".">
{{ form.as_p }}
    <input type="radio" class='radio-button' name="regular" id="regular"/><label for="regular">User</label>
    <input type="radio" class='radio-button' name="manager" id="manager"/><label for="manager">Manager</label>
    <input type="radio" class='radio-button' name="admin" id="admin"/><label for="admin">admin</label>
<input type="submit" value="register" />

</form>

Now permissions aren't assigned automatically. Previously I assigned this permission to first user and it worked but when I tried to assign it to another user got nothing.

Alex Ermolaev
  • 311
  • 2
  • 4
  • 17

2 Answers2

1

Did you save the user?

user.user_permissions.add(permission)
user.save()

You can also turn the logs on and see what queries django does to database like this

Community
  • 1
  • 1
deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • I tried to save but the same effetc: when I try `user.has_perm('tracker.admin_can_manage_users')` get False – Alex Ermolaev Jan 17 '17 at 13:48
  • The same issue when I try to assign it via shell – Alex Ermolaev Jan 17 '17 at 13:48
  • Try to turn on logs and see what queries go to database. Also if you use default user model you could step into `has_perm` method in debug and see what it exactly does. – deathangel908 Jan 17 '17 at 13:51
  • `(0.000) SELECT "django_content_type"."app_label", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "auth_user_user_permissions" ON ("auth_permission"."id" = "auth_user_user_permissions"."permission_id") INNER JOIN "django_content_type" ON ("auth_permission"."content_type_id" = "django_content_type"."id") WHERE "auth_user_user_permissions"."user_id" = 12; args=(12,)` – Alex Ermolaev Jan 17 '17 at 14:05
  • `(0.000) SELECT "django_content_type"."app_label", "auth_permission"."codename" FROM "auth_permission" INNER JOIN "auth_group_permissions" ON ("auth_permission"."id" = "auth_group_permissions"."permission_id") INNER JOIN "auth_group" ON ("auth_group_permissions"."group_id" = "auth_group"."id") INNER JOIN "auth_user_groups" ON ("auth_group"."id" = "auth_user_groups"."group_id") INNER JOIN "django_content_type" ON ("auth_permission"."content_type_id" = "django_content_type"."id") WHERE "auth_user_groups"."user_id" = 12; args=(12,)` – Alex Ermolaev Jan 17 '17 at 14:06
  • Great, now check is there any queries when you execute `user.user_permissions.add(permission)` you should see in logs some insert to `auth_permissions`. And check the database `auth_permissions` table, it should have permission for that user in the end of the day. – deathangel908 Jan 17 '17 at 14:08
  • So to summarize: 1) `user.user_permissions.add(permission)` adds permission and after it database contains the corresponding row/field. 2) Select that you wrote above returns this permissions from database. ( You can execute it strictly to DB) 3) has_permission returns `false`, if it's so you should definitely debug `has_perm` method – deathangel908 Jan 17 '17 at 14:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133394/discussion-between-deathangel908-and-alex-ermolaev). – deathangel908 Jan 17 '17 at 22:52
0

The error was because of the namespace: my permission was in auth namespace. Just changed to another (another model) and it works perfectly.

Alex Ermolaev
  • 311
  • 2
  • 4
  • 17