-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
118 lines (77 loc) · 3.31 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from django.db import models
from django.utils import simplejson
# Create your models here.
#from unique_model.models import UniqueModel
"""
Role Based Access Control
=========================
This application build the essential models and function for a django site to
use rbac.
It defines several abtract models:
* Subject: A person or an automated agent.
* Role: Job function that define an authoritative level.
* Permission: An approval to access a resource in a particular mode.
* Session: A session mantain the information of Subject and Role. (django.contrib.sessions)
Subject
=======
A subject is an user or an automated agent, as a node or a gadget.
This model defines two abstract methods:
* identify: Used to identify a subject given some identification parameters. A site has to define a particular subject model that inherite from Subject and define this function, with its identification parameters.
* authenticate: Used to authenticate a subject given some authentication parameters.
Role
====
A role is just a job function or a title that define a subsets of subjects. It helps to define the responsabilities of the subjects that belong to this role.
Permission
==========
Resource
========
"""
class NonImplementedException(Exception):
pass
class ListStringField(models.TextField):
description = "A list of strings."
__metaclass__ = models.SubfieldBase
def __init__(self, *args, **kwargs):
if not ('default' in kwargs):
kwargs['default'] = simplejson.dumps([])
super(ListStringField, self).__init__(*args, **kwargs)
def to_python(self, value):
if isinstance(value, list) and all([isinstance(x, (str, unicode)) for x in value]):
return value
value = simplejson.loads(value)
if isinstance(value, list) and all([isinstance(x, (str, unicode)) for x in value]):
return value
return []
def get_prep_value(self, value):
return simplejson.dumps(value)
class Subject(models.Model):
"""
Subject
=======
This is an abstract class that define the minimum aspects
to be an agent in Role-Based Access Control.
It define a basic list of roles, this roles are basically identified with the name of the role.
"""
class Meta(object):
abstract = True
roles = ListStringField()
@classmethod
def identify(cls, *args, **kwargs):
"""
This method should receive the parameters needed to identify an agent.
In a single case wher agents are regular human beigns an username or email is fine.
"""
raise NonImplementedException("Define an identification mechanism.")
def authenticate(self, *args, **kwargs):
raise NonImplementedException(
"Define an authentication mechanism, given an agent already identified.")
def authorize(self, *args, **kwargs):
raise NonImplementedException(
"Define an authoization mechanism to perform transaction."
)
def authenticate_request(self, request, *args, **kwargs):
self.authenticate(*args, **kwargs)
request.session['_rbac_subject'] = self
def deauthenticate_request(self, request):
# request.session.pop('_rbac_subject')
request.session.flush()