[abac] Unicode of models defined

This commit is contained in:
Mikaël Ates 2011-08-06 09:06:00 +02:00
parent ffc54bee21
commit 59515aeeb6
1 changed files with 88 additions and 6 deletions

View File

@ -17,12 +17,15 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import re
import random
import string
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from acs.xacml.constants import XACML_DATA_TYPE, ACS_XACML_DATATYPE_STRING, \
XACML_COMPARISON_TYPE, ACS_XACML_COMPARISON_EQUALITY_STRING
from acs.xacml.constants import *
SOURCE_TYPE = (
@ -39,8 +42,7 @@ class Source(models.Model):
default = 'DIRECT')
def __unicode__(self):
if self.name:
return self.name
return 'Source %s of type %s' % (self.name, self.type_source)
class AttributeDefinition(models.Model):
@ -50,6 +52,9 @@ class AttributeDefinition(models.Model):
verbose_name = '',
default = ACS_XACML_DATATYPE_STRING)
def __unicode__(self):
return self.attribute_name
class AttributeNamespace(models.Model):
friendly_name = models.CharField(max_length = 100, unique=True)
@ -160,12 +165,22 @@ class AssertionAny(models.Model):
class AssertionDefinition(AssertionAny):
attribute_definition = models.ForeignKey(AttributeDefinition)
def __unicode__(self):
sources = AttachedSource.objects.filter(assertion=self)
return "attribute %s from %s" \
% (str(self.attribute_definition),
[str(x.source) for x in sources])
class AttachedSource(models.Model):
source = models.ForeignKey(Source)
assertion = models.ForeignKey(AssertionDefinition,
related_name = 'assertion')
def __unicode__(self):
return "Source %s attached to %s" \
% (str(self.source), str(self.assertion))
class AssertionData(AssertionAny):
profile = models.ForeignKey(UserAttributeProfile, null=True, blank=True)
@ -173,6 +188,37 @@ class AssertionData(AssertionAny):
source = models.ForeignKey(Source, null=True, blank=True)
certificate = models.ForeignKey(Certificate, null=True, blank=True)
def __unicode__(self):
values = None
definition = self.attribute_data.definition
if definition.attribute_type == ACS_XACML_DATATYPE_STRING:
values = StringM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_BOOLEAN:
values = BooleanM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_INTEGER:
values = IntegerM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_DOUBLE:
values = DoubleM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_TIME:
values = TimeM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_DATE:
values = DateM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_DATETIME:
values = DateTimeM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_RFC822NAME:
values = Rfc822NameM.objects.filter(data=self.attribute_data)
elif definition.attribute_type == ACS_XACML_DATATYPE_IPADDRESS:
values = IpAddressM.objects.filter(data=self.attribute_data)
s = "attribute %s with values %s" \
% (str(self.attribute_data.definition),
[str(x.value) for x in values])
if self.source:
s += ' (provided by %s)' % str(self.source)
if self.certificate:
s += ' (signed with %s)' % str(self.certificate)
if self.profile:
s += ' (added to profile %s)' % str(self.profile)
return s
'''
We use it to have id of any predicate unique to serve as a
@ -207,7 +253,7 @@ class PredicateRequired(Predicate):
definition = models.ForeignKey(AssertionDefinition)
def __unicode__(self):
return "%s required" % self.definition
return "Predicate required: %s" % str(self.definition)
MULTIVALUES_OPTION = (
@ -254,6 +300,24 @@ class PredicateComparison(Predicate):
verbose_name = 'How to handle multivalued attributes',
default = 'NO_MULTIVALUES')
def __unicode__(self):
operator = ''
if self.comparison_type in XACML_COMPARISON_EQUALITY:
operator = '='
elif self.comparison_type in ACS_XACML_COMPARISON_LT:
operator = '<'
elif self.comparison_type in ACS_XACML_COMPARISON_LT_OE:
operator = '<='
elif self.comparison_type in ACS_XACML_COMPARISON_GRT:
operator = '>'
elif self.comparison_type in ACS_XACML_COMPARISON_GRT_OE:
operator = '>='
return 'Predicate comparison: %s %s %s \
(multivalue management parameter: %s)' \
% (str(self.operand1.get_assertion_instance()),
operator, str(self.operand2.get_assertion_instance()),
self.multivalues)
'''
Predicate are str(id) of attribute definitions
@ -264,4 +328,22 @@ class AbacRule(models.Model):
expression = models.CharField(max_length = 2048)
def __unicode__(self):
return self.expression
predicates1 = {}
predicates2 = {}
expression = self.expression
'''
Pb if a predicate id is equal to value.
The value of the predicate will be substitue with a predicate
with that id.
Solution: make a first substitution with unique identifiers.
'''
for p in Predicate.objects.filter(rule=self):
rdm_str = ''.join(random.choice(string.ascii_uppercase) for x in range(8))
predicates1[p.id] = rdm_str
predicates2[rdm_str] = str(p.get_predicate_instance())
for key in predicates1.keys():
expression = re.sub(str(key), str(predicates1[key]),
expression)
for key in predicates2.keys():
expression = re.sub(str(key), str(predicates2[key]), expression)
return expression