Skip to content

issue #7 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions permissions/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
# -*- coding: utf-8 -*-
from django.contrib import admin

from permissions.models import ObjectPermission
admin.site.register(ObjectPermission)
from permissions.models import (
ObjectPermission, Permission, Role, PrincipalRoleRelation
)

from permissions.models import Permission
admin.site.register(Permission)

from permissions.models import Role
admin.site.register(Role)
class ObjectPermissionAdmin(admin.ModelAdmin):
list_display = ('permission', 'content_type', 'content_id', 'role')
list_filter = ('content_type', 'role',)

from permissions.models import PrincipalRoleRelation
admin.site.register(PrincipalRoleRelation)
admin.site.register(ObjectPermission, ObjectPermissionAdmin)


class PermissionAdmin(admin.ModelAdmin):
list_display = ('name', 'codename')
search_fields = ('name', 'codename')
list_filter = ('content_types',)

admin.site.register(Permission, PermissionAdmin)


class RoleAdmin(admin.ModelAdmin):
list_display = ('name',)
search_fields = ('name',)

admin.site.register(Role, RoleAdmin)


class PrincipalRoleRelationAdmin(admin.ModelAdmin):
list_display = ('role', 'user', 'group', 'content_type', 'content_id')
list_filter = ('role', 'content_type',)

admin.site.register(PrincipalRoleRelation, PrincipalRoleRelationAdmin)
11 changes: 8 additions & 3 deletions permissions/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# django imports
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
# from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -32,6 +33,7 @@ class Permission(models.Model):

class Meta:
app_label = "permissions"
ordering = ('name',)

def __unicode__(self):
return "%s (%s)" % (self.name, self.codename)
Expand Down Expand Up @@ -159,7 +161,7 @@ class PrincipalRoleRelation(models.Model):
content
The content object which gets the local role (optional).
"""
user = models.ForeignKey(User, verbose_name=_(u"User"), blank=True, null=True)
user = models.ForeignKey(settings.AUTH_USER_MODEL, verbose_name=_(u"User"), blank=True, null=True)
group = models.ForeignKey(Group, verbose_name=_(u"Group"), blank=True, null=True)
role = models.ForeignKey(Role, verbose_name=_(u"Role"))
content_type = models.ForeignKey(ContentType, verbose_name=_(u"Content type"), blank=True, null=True)
Expand All @@ -183,8 +185,11 @@ def get_principal(self):
return self.user or self.group

def set_principal(self, principal):
"""Sets the principal.
"""
Sets the principal.
"""
from django.contrib.auth import get_user_model
User = get_user_model()
if isinstance(principal, User):
self.user = principal
else:
Expand Down
13 changes: 7 additions & 6 deletions permissions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ def get_roles(user, obj=None):
"""
# Cached roles
obj_id = "0"

if obj:
ctype = ContentType.objects.get_for_model(obj)
obj_id = "{}|{}".format(obj.id, ctype.id)
Expand All @@ -213,11 +214,12 @@ def get_roles(user, obj=None):
pass

groups = user.groups.all()
groups_ids_str = ", ".join([str(g.id) for g in groups])
#groups_ids = ", ".join([str(g.id) for g in groups])
groups_ids = [g.id for g in groups]

if groups_ids_str:
if groups_ids:
prrs = PrincipalRoleRelation.objects.filter(
Q(user_id=user.id) | Q(group_id__in=groups_ids_str), content_id=None
Q(user_id=user.id) | Q(group_id__in=groups_ids), content_id=None
).values("role_id")
else:
prrs = PrincipalRoleRelation.objects.filter(user_id=user.id, content_id=None).values("role_id")
Expand All @@ -229,9 +231,9 @@ def get_roles(user, obj=None):
while obj:
ctype = ContentType.objects.get_for_model(obj)

if groups_ids_str:
if groups_ids:
prrs = PrincipalRoleRelation.objects.filter(
Q(user_id=user.id) | Q(group_id__in=groups_ids_str), content_id=obj.id, content_type_id=ctype.id
Q(user_id=user.id) | Q(group_id__in=groups_ids), content_id=obj.id, content_type_id=ctype.id
).values("role_id")
else:
prrs = PrincipalRoleRelation.objects.filter(
Expand All @@ -255,7 +257,6 @@ def get_roles(user, obj=None):

return roles


def get_global_roles(principal):
"""Returns *direct* global roles of passed principal (user or group).
"""
Expand Down