Skip to content

Commit

Permalink
added AbstractRole to zengine.auth and made it replaceable by adding …
Browse files Browse the repository at this point in the history
…its import path as a setting

rref #5357

ref #64
  • Loading branch information
evrenesat committed Aug 10, 2016
1 parent b81f234 commit 57ff8eb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions zengine/models/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from pyoko import Model, field, ListNode, LinkProxy
from passlib.hash import pbkdf2_sha512
from zengine.lib.cache import Cache
from zengine.messaging.lib import BaseUser


Expand Down Expand Up @@ -101,10 +102,89 @@ def get_permissions(self):
users_primary_role = self.role_set[0].role
return users_primary_role.get_permissions()


class PermissionCache(Cache):
"""PermissionCache sınıfı Kullanıcıya Permission nesnelerinin
kontrolünü hızlandırmak için yetkileri cache bellekte saklamak ve
gerektiğinde okumak için oluşturulmuştur.
"""
PREFIX = 'PRM'

def __init__(self, role_id):
super(PermissionCache, self).__init__(role_id)


class AbstractRole(Model):
"""
AbstractRoles are stand as a foundation for actual roles
"""
name = field.String("Name", index=True)
read_only = field.Boolean("Archived")

class Meta:
verbose_name = "Abstract Role"
verbose_name_plural = "Abstract Roles"
search_fields = ['name']

def __unicode__(self):
return "%s" % self.name

def get_permissions(self):
"""
Soyut role ait Permission nesnelerini bulur ve code değerlerini
döner.
Returns:
list: Permission code değerleri
"""
return [p.permission.code for p in self.Permissions if p.permission.code]

def add_permission(self, perm):
"""
Soyut Role Permission nesnesi tanımlamayı sağlar.
Args:
perm (object):
"""
self.Permissions(permission=perm)
PermissionCache.flush()
self.save()

def add_permission_by_name(self, code, save=False):
"""
Soyut role Permission eklemek veya eklenebilecek Permission
nesnelerini verilen ``code`` parametresine göre listelemek olmak
üzere iki şekilde kullanılır.
Args:
code (str): Permission nesnelerini filtre etmekte kullanılır
save (bool): True ise Permission ekler, False ise Permission
listesi döner.
Returns:
list: ``save`` False ise Permission listesi döner.
"""
if not save:
return ["%s | %s" % (p.name, p.code) for p in
Permission.objects.filter(code__contains=code)]
PermissionCache.flush()
for p in Permission.objects.filter(code__contains=code):
if p not in self.Permissions:
self.Permissions(permission=p)
if p:
self.save()

class Permissions(ListNode):
permission = Permission()

class Role(Model):
"""
This model binds group of Permissions with a certain User.
"""
arole = AbstractRole()
user = User()

class Meta:
Expand Down
3 changes: 3 additions & 0 deletions zengine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
#: User model
USER_MODEL = 'zengine.models.User'

#: AbstractRole model
ABSTRACT_ROLE_MODEL = 'zengine.models.AbstractRole'

#: Role model
ROLE_MODEL = 'zengine.models.Role'

Expand Down

0 comments on commit 57ff8eb

Please sign in to comment.