davidcr01 / WordlePlus

Repository to store all the documentation, files and structure of my Final Degree Project (TFG in Spanish). The main goal is to develop, as full-stack web developer, a remodel of the Wordle game, including more features and functionalities using Ionic, Django REST Api and PostgreSQL.
1 stars 0 forks source link

Administrators can not see anything in the Admin site (S1) #9

Closed davidcr01 closed 1 year ago

davidcr01 commented 1 year ago

Description

It has been observed that when creating a new Administrator (is_staff is True) they can not see anything on the Admin site, but they can access the Admin site.

Is necessary to investigate this and fix it.

1

davidcr01 commented 1 year ago

Update Report

Development

This behavior has not been seen until now. It seems there are problems with the permissions of the staff users. Although they are staff, they can not see the Djapi models.

It has been observed that, if a staff user has assigned the permissions to manage these models, they appear in the Admin site.

So, it is necessary to assign permissions when creating a staff user. To perform this, a new code snippet has been created:

@receiver(post_save, sender=CustomUser)
def assign_permissions(sender, instance, created, **kwargs):
    if created and instance.is_staff:
        staff_group, created = Group.objects.get_or_create(name='Staff')

        if created:
            # Obtener los permisos necesarios para gestionar CustomUser y Player
            customuser_content_type = ContentType.objects.get(app_label='djapi', model='customuser')
            player_content_type = ContentType.objects.get(app_label='djapi', model='player')

            customuser_permissions = Permission.objects.filter(content_type=customuser_content_type)
            player_permissions = Permission.objects.filter(content_type=player_content_type)

            # Asignar los permisos al grupo "Staff"
            staff_group.permissions.set(customuser_permissions | player_permissions)

        # Agregar el usuario al grupo "Staff"
        staff_group.user_set.add(instance)

This snippet is added in the models.py, after the definition of the models.

The code is executed when a post_save signal is received from the CustomUser model. If the instance is created and is a staff member, the code creates or gets a group called Staff. If the Staff group is not created, it assigns to the group the CustomUser and Player permissions and always assigns to the staff user the group Staff.

The conditional of if created avoids assigning the permissions to the group every time a staff member is created.

Testing

There are no groups: 2

A new staff member is created and the group "Staff" is created and assigned: 3

5

And the staff members now can see the User and Player models: 6