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 | |
---|
13 | |
---|
14 | class PolicyComponent(object): |
---|
15 | """Base class for Policy and Policy subelements""" |
---|
16 | XACML_2_0_XMLNS = "urn:oasis:names:tc:xacml:2.0:policy:schema:os" |
---|
17 | |
---|
18 | __slots__ = ('__xmlns', '__reader', '__writer') |
---|
19 | |
---|
20 | ELEMENT_LOCAL_NAME = None |
---|
21 | |
---|
22 | def __init__(self): |
---|
23 | self.__xmlns = PolicyComponent.XACML_2_0_XMLNS |
---|
24 | self.__reader = None |
---|
25 | self.__writer = None |
---|
26 | |
---|
27 | def _getXmlns(self): |
---|
28 | return self.__xmlns |
---|
29 | |
---|
30 | def _setXmlns(self, value): |
---|
31 | if not isinstance(value, basestring): |
---|
32 | raise TypeError('Expecting string type for "xmlns" ' |
---|
33 | 'attribute; got %r' % type(value)) |
---|
34 | self.__xmlns = value |
---|
35 | |
---|
36 | xmlns = property(_getXmlns, _setXmlns, |
---|
37 | doc="XML Namespace for policy the document") |
---|
38 | |
---|
39 | @property |
---|
40 | def isValidXmlns(self): |
---|
41 | return self.xmlns in PolicyComponent.XMLNS |
---|
42 | |
---|
43 | def read(self, obj): |
---|
44 | """Read using callable assinged to reader property""" |
---|
45 | if self.__reader is None: |
---|
46 | raise AttributeError('No reader set for %r' % self.__class__) |
---|
47 | |
---|
48 | self.__reader(self, obj) |
---|
49 | |
---|
50 | @classmethod |
---|
51 | def Read(cls, obj): |
---|
52 | """Construct a new Policy""" |
---|
53 | xacmlObj = cls() |
---|
54 | xacmlObj.read(obj) |
---|
55 | return xacmlObj |
---|
56 | |
---|
57 | def write(self, obj): |
---|
58 | """Read using callable assinged to reader property""" |
---|
59 | if self.__writer is None: |
---|
60 | raise AttributeError('No writer set for %r' % self.__class__) |
---|
61 | |
---|
62 | self.__writer(self, obj) |
---|
63 | |
---|
64 | |
---|
65 | class RequestPropertyBase(PolicyComponent): |
---|
66 | """Base type for Subject, Resource, Action and Environment types""" |
---|
67 | MATCH_TYPE = None |
---|
68 | |
---|
69 | __slots__ = ('__matches', ) |
---|
70 | |
---|
71 | def __init__(self): |
---|
72 | # Derived types can specify the type for matches via the MATCH_TYPE |
---|
73 | # class variable |
---|
74 | self.__matches = TypedList(self.__class__.MATCH_TYPE) |
---|
75 | |
---|
76 | @property |
---|
77 | def matches(self): |
---|
78 | return self.__matches |
---|