|
| 1 | +import xml.etree.ElementTree as ET |
| 2 | + |
| 3 | + |
| 4 | +class ItemWithPermissions(object): |
| 5 | + pass |
| 6 | + |
| 7 | + |
| 8 | +class Permission: |
| 9 | + class Type: |
| 10 | + Datasource = 'datasource' |
| 11 | + Workbook = 'workbook' |
| 12 | + Project = 'project' |
| 13 | + |
| 14 | + class GranteeType: |
| 15 | + User = 'user' |
| 16 | + Group = 'group' |
| 17 | + |
| 18 | + class CapabilityMode: |
| 19 | + Allow = 'Allow' |
| 20 | + Deny = 'Deny' |
| 21 | + |
| 22 | + class DatasourceCapabilityType: |
| 23 | + ChangePermissions = 'ChangePermissions' |
| 24 | + Connect = 'Connect' |
| 25 | + Delete = 'Delete' |
| 26 | + ExportXml = 'ExportXml' |
| 27 | + Read = 'Read' |
| 28 | + Write = 'Write' |
| 29 | + |
| 30 | + class WorkbookCapabilityType: |
| 31 | + AddComment = 'AddComment' |
| 32 | + ChangeHierarchy = 'ChangeHierarchy' |
| 33 | + ChangePermissions = 'ChangePermissions' |
| 34 | + Delete = 'Delete' |
| 35 | + ExportData = 'ExportData' |
| 36 | + ExportImage = 'ExportImage' |
| 37 | + ExportXml = 'ExportXml' |
| 38 | + Filter = 'Filter' |
| 39 | + Read = 'Read' |
| 40 | + ShareView = 'ShareView' |
| 41 | + ViewComments = 'ViewComments' |
| 42 | + ViewUnderlyingData = 'ViewUnderlyingData' |
| 43 | + WebAuthoring = 'WebAuthoring' |
| 44 | + Write = 'Write' |
| 45 | + |
| 46 | + class ProjectCapabilityType: |
| 47 | + ProjectLeader = 'ProjectLeader' |
| 48 | + Read = 'Read' |
| 49 | + Write = 'Write' |
| 50 | + |
| 51 | + |
| 52 | +class GranteeCapabilityItem(object): |
| 53 | + def __init__(self, grantee_type=None, grantee_id=None, capabilities=None): |
| 54 | + self._grantee_type = grantee_type |
| 55 | + self._grantee_id = grantee_id |
| 56 | + self._capabilities = capabilities |
| 57 | + |
| 58 | + def _set_values(self, grantee_type, grantee_id, capabilities): |
| 59 | + self._grantee_type = grantee_type |
| 60 | + self._grantee_id = grantee_id |
| 61 | + self._capabilities = capabilities |
| 62 | + |
| 63 | + @property |
| 64 | + def grantee_type(self): |
| 65 | + return self._grantee_type |
| 66 | + |
| 67 | + @property |
| 68 | + def grantee_id(self): |
| 69 | + return self._grantee_id |
| 70 | + |
| 71 | + @property |
| 72 | + def capabilities(self): |
| 73 | + return self._capabilities |
| 74 | + |
| 75 | + |
| 76 | +class PermissionsItem(object): |
| 77 | + def __init__(self): |
| 78 | + self._type = None |
| 79 | + self._item_id = None |
| 80 | + self._grantee_capabilities = None |
| 81 | + |
| 82 | + def _set_values(self, type, item_id, grantee_capabilities): |
| 83 | + self._type = type |
| 84 | + self._item_id = item_id |
| 85 | + self._grantee_capabilities = grantee_capabilities |
| 86 | + |
| 87 | + @property |
| 88 | + def type(self): |
| 89 | + return self._type |
| 90 | + |
| 91 | + @property |
| 92 | + def item_id(self): |
| 93 | + return self._item_id |
| 94 | + |
| 95 | + @property |
| 96 | + def grantee_capabilities(self): |
| 97 | + return self._grantee_capabilities |
| 98 | + |
| 99 | + @property |
| 100 | + def is_user_permission(self): |
| 101 | + return self._user_id is not None |
| 102 | + |
| 103 | + @property |
| 104 | + def is_group_permission(self): |
| 105 | + return self._group_id is not None |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def from_response(cls, resp, ns=None): |
| 109 | + permissions = PermissionsItem() |
| 110 | + parsed_response = ET.fromstring(resp) |
| 111 | + |
| 112 | + for option in ('workbook', 'datasource', 'project'): |
| 113 | + try: |
| 114 | + item_id = parsed_response.find('.//t:{0}'.format(option), namespaces=ns).get('id') |
| 115 | + permission_type = option |
| 116 | + break |
| 117 | + except AttributeError: |
| 118 | + pass |
| 119 | + |
| 120 | + all_xml = parsed_response.findall('.//t:granteeCapabilities', namespaces=ns) |
| 121 | + |
| 122 | + grantee_capabilities = [] |
| 123 | + for grantee_capability_xml in all_xml: |
| 124 | + grantee_capability = GranteeCapabilityItem() |
| 125 | + |
| 126 | + try: |
| 127 | + grantee_id = grantee_capability_xml.find('.//t:group', namespaces=ns).get('id') |
| 128 | + grantee_type = Permission.GranteeType.Group |
| 129 | + except AttributeError: |
| 130 | + pass |
| 131 | + |
| 132 | + try: |
| 133 | + grantee_id = grantee_capability_xml.find('.//t:user', namespaces=ns).get('id') |
| 134 | + grantee_type = Permission.GranteeType.User |
| 135 | + except AttributeError: |
| 136 | + pass |
| 137 | + |
| 138 | + assert grantee_id is not None |
| 139 | + |
| 140 | + capabilities = {} |
| 141 | + for capability_xml in grantee_capability_xml.findall('.//t:capabilities/t:capability', namespaces=ns): |
| 142 | + name = capability_xml.get('name') |
| 143 | + mode = capability_xml.get('mode') |
| 144 | + |
| 145 | + capabilities[name] = mode |
| 146 | + |
| 147 | + grantee_capability._set_values(grantee_type, grantee_id, capabilities) |
| 148 | + grantee_capabilities.append(grantee_capability) |
| 149 | + |
| 150 | + permissions._set_values(permission_type, item_id, grantee_capabilities) |
| 151 | + return permissions |
0 commit comments