Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom groups in django from group

Tags:

I'm trying to figure out how to create custom groups in Django, in way that groups are related to application and not to a Project.

For example when a user want to create a company, it should be the owner of the company:

model

class Company(models.Model):
    owner = models.ForeignKey(User)
    name = models.CharField(max_length=64, unique=True)
    description = models.TextField(max_length=512)
    created_on = models.DateTimeField(auto_now_add=timezone.now)

    class Meta:
        permissions = (
            ("erp_view_company", "Can see the company information"),
            ("erp_edit_company", "Can edit the company information"),
            ("erp_delete_company", "Can delete the company"),
        )

view

# Create your views here.
def register(request, template_name='erp/register.html'):
    if request.method == 'POST':
        company_form = CompanyRegistrationForm(request.POST)
        if company_form.is_valid():
            # Create a new company object but avoid saving it yet
            new_company = company_form.save(commit=False)
            # Set the owner
            if request.user.is_authenticated():
                new_company.owner = request.user

            # Add the user to group admin
            group = Group.objects.get_or_create(name="erp_admin")
            request.user.groups.add(group)

            # Save the User object
            new_company.save()

            template_name = 'erp/register_done.html'
            return render(request, template_name, {'new_company': new_company})

    else:
        company_form = CompanyRegistrationForm()

    return render(request, template_name, {'company_form': company_form})

Here the questions:

1) Instead of having Group.objects.get_or_create(name="erp_admin") I'd like to have CompanyGroup.objects.get_or_create(name="admin") in way that the Groups of an application are restricted to the application.

2) How I can map the permissions defined in the Meta class of each model to a group?

3) The custom groups are related to a Company, this mean each company has the group Admin, that starts with the owner. The owner can creates user like "Manager". The manager can creates groups/permissions, add user, set permission to user but can't in anyway CRUD the admin groups. So there's a kind of hierarchy in groups (I suppose that the hierarchy depend on the permissions that are added to a group).

To make it more clear here you can see a picture of the concept:  company example

So I think the main problems are:

1) How to inherit Groups to create custom groups.

2) How to map the permissions of a model to a group.

3) How groups can be restricted to a CompanyID.

I hope the problem is well defined. Let me know if I have to clarify something.

Thanks in advance for the support.

Edit 1)

For the point 3 I found this answer on SO: Extend django Groups and Permissions but the problem then is how to query for those data and how to check the permissions. And how could you override the save() method to add meta information to the property 'name'? The group name could be something like: "company_id#admin" or "company_id#manager".

like image 347
realnot Avatar asked Jul 14 '16 13:07

realnot


People also ask

How do I use user groups in django?

Django Admin Panel : In Admin Panel you will see Group in bold letter, Click on that and make 3-different group named level0, level1, level3 . Also, define the custom permissions according to the need. By Programmatically creating a group with permissions: Open python shell using python manage.py shell.

How do I give permission to groups in django?

With Django, you can create groups to class users and assign permissions to each group so when creating users, you can just assign the user to a group and, in turn, the user has all the permissions from that group. To create a group, you need the Group model from django. contrib. auth.

What is PermissionsMixin in django?

The PermissionsMixin is a mixin for models. The PermissionsMixin [Django-doc] is a mixin for Django models. If you add the mixin to one of your models, it will add fields that are specific for objects that have permissions, like is_superuser , groups , and user_permissions .


1 Answers

You may be looking for Per object permissions. Django does not support this out of the box but it is possible with the app Django-guardian

like image 135
MarkerDave Avatar answered Oct 06 '22 07:10

MarkerDave