Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend django Groups and Permissions

I've been searching Google and Stackoverflow for the last 3 days now and couldn't find anything similar. I'd like to extend or use the django groups and permissions.

Let's say we got some Projects with different Teams and Users.

class Project(models.Model):
    owner = models.ForeignKey(User)
    name = models.CharField(max_length=100)

class Team(models.Model):
    project = models.ForeignKey(Project)
    members = models.ManyToManyField(User)
    name = models.CharField(max_length=100)
    permission = models.ManyToManyField(Permission)

Now - everything's fine. Now what I want is to extend the existing Auth.Group delivered by django so I could use request.user.has_perm... the direct way.

So, let's change the Team-Model into

class Team(Group):
    project = models.ForeignKey(Project)
    members = models.ManyToManyField(User)

This extends the existing Group. The name and permissions field comes direct from the Group-Model. If I create a new Team, there will be a Group added, too. That's great.

With a custom save method I could also add, that the user listed in members, will be added to the created Group and gets the permissions granted to the Team/Group.

Now my problem is, the name field of the Group is unique - so I couldn't add an Admins-Team for each Project.

I thought about adding another name-field for the frontend and generate a unique string for the name field of the Group-Model. But hmmm....is there a better solution for multiple custom Groups? Maybe one got an idea? Thank you in advance!

like image 555
Thomas Schwärzl Avatar asked Oct 15 '12 09:10

Thomas Schwärzl


People also ask

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.

How do permissions work in django?

By default, Django automatically gives add, change, and delete permissions to all models, which allow users with the permissions to perform the associated actions via the admin site. You can define your own permissions to models and grant them to specific users.


1 Answers

You need to make group names unique. So one way that you found out is having another name field, making it unique for all subgroups.

Another way would be to make use of existing name field and add special chars of yours. For example group name with admin#project1, members#project1 etc. (Note: I'm not sure '#' is allowed in group name, you can choose any special character that is allowed).

So whenever you create Team you update the name field with suffix #<project_name> in Team models save() method.

To display it more sanely, you can add __unicode__() method to return only first part of the group name. Example:

class Team(Group):
    ...
    def __unicode__(self):
        try:
            return self.name.split('#')[0]
        except:
            #something wrong!
            return self.name
like image 123
Rohan Avatar answered Oct 10 '22 01:10

Rohan