1 | """NDG XACML core package |
---|
2 | |
---|
3 | NERC DataGrid Project |
---|
4 | """ |
---|
5 | __author__ = "P J Kershaw" |
---|
6 | __date__ = "16/03/10" |
---|
7 | __copyright__ = "(C) 2010 Science and Technology Facilities Council" |
---|
8 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
9 | __license__ = "BSD - see LICENSE file in top-level directory" |
---|
10 | __contact__ = "Philip.Kershaw@stfc.ac.uk" |
---|
11 | __revision__ = "$Id: $" |
---|
12 | from ndg.xacml.utils import TypedList |
---|
13 | |
---|
14 | |
---|
15 | class PolicyComponent(object): |
---|
16 | """Base class for Policy and Policy subelements""" |
---|
17 | XACML_2_0_XMLNS = "urn:oasis:names:tc:xacml:2.0:policy:schema:os" |
---|
18 | |
---|
19 | __slots__ = ('__xmlns', '__reader', '__writer') |
---|
20 | |
---|
21 | ELEMENT_LOCAL_NAME = None |
---|
22 | |
---|
23 | def __init__(self): |
---|
24 | self.__xmlns = PolicyComponent.XACML_2_0_XMLNS |
---|
25 | self.__reader = None |
---|
26 | self.__writer = None |
---|
27 | |
---|
28 | def _getXmlns(self): |
---|
29 | return self.__xmlns |
---|
30 | |
---|
31 | def _setXmlns(self, value): |
---|
32 | if not isinstance(value, basestring): |
---|
33 | raise TypeError('Expecting string type for "xmlns" ' |
---|
34 | 'attribute; got %r' % type(value)) |
---|
35 | self.__xmlns = value |
---|
36 | |
---|
37 | xmlns = property(_getXmlns, _setXmlns, |
---|
38 | doc="XML Namespace for policy the document") |
---|
39 | |
---|
40 | @property |
---|
41 | def isValidXmlns(self): |
---|
42 | return self.xmlns in PolicyComponent.XMLNS |
---|
43 | |
---|
44 | def read(self, obj): |
---|
45 | """Read using callable assinged to reader property""" |
---|
46 | if self.__reader is None: |
---|
47 | raise AttributeError('No reader set for %r' % self.__class__) |
---|
48 | |
---|
49 | self.__reader(self, obj) |
---|
50 | |
---|
51 | @classmethod |
---|
52 | def Read(cls, obj): |
---|
53 | """Construct a new Policy""" |
---|
54 | xacmlObj = cls() |
---|
55 | xacmlObj.read(obj) |
---|
56 | return xacmlObj |
---|
57 | |
---|
58 | def write(self, obj): |
---|
59 | """Read using callable assinged to reader property""" |
---|
60 | if self.__writer is None: |
---|
61 | raise AttributeError('No writer set for %r' % self.__class__) |
---|
62 | |
---|
63 | self.__writer(self, obj) |
---|
64 | |
---|
65 | |
---|
66 | class RequestPropertyBase(PolicyComponent): |
---|
67 | """Base type for Subject, Resource, Action and Environment types""" |
---|
68 | MATCH_TYPE = None |
---|
69 | |
---|
70 | __slots__ = ('__matches', ) |
---|
71 | |
---|
72 | def __init__(self): |
---|
73 | # Derived types can specify the type for matches via the MATCH_TYPE |
---|
74 | # class variable |
---|
75 | self.__matches = TypedList(self.__class__.MATCH_TYPE) |
---|
76 | |
---|
77 | @property |
---|
78 | def matches(self): |
---|
79 | return self.__matches |
---|