|
| 1 | +from diffgram.core.directory import Directory |
| 2 | + |
| 3 | + |
| 4 | +class Role: |
| 5 | + id: int |
| 6 | + name: int |
| 7 | + project_id: int |
| 8 | + permissions_list: list |
| 9 | + |
| 10 | + def __init__(self, |
| 11 | + client, |
| 12 | + role_dict = None): |
| 13 | + |
| 14 | + self.client = client |
| 15 | + |
| 16 | + if self.client.project_string_id is None: |
| 17 | + raise Exception("\n No project string id in client.") |
| 18 | + |
| 19 | + self.refresh_from_dict( |
| 20 | + role_dict = role_dict) |
| 21 | + |
| 22 | + def refresh_from_dict(self, role_dict: dict = None): |
| 23 | + |
| 24 | + if not role_dict: |
| 25 | + return |
| 26 | + |
| 27 | + for key, value in role_dict.items(): |
| 28 | + setattr(self, key, value) |
| 29 | + |
| 30 | + def __repr__(self): |
| 31 | + return f'Role<{str(self.serialize())}>' |
| 32 | + |
| 33 | + def serialize(self): |
| 34 | + |
| 35 | + return { |
| 36 | + 'id': self.id, |
| 37 | + 'name': self.name, |
| 38 | + 'project_id': self.project_id, |
| 39 | + 'permissions_list': self.permissions_list, |
| 40 | + } |
| 41 | + |
| 42 | + def new(self, name: str = None) -> 'Role': |
| 43 | + """ |
| 44 | + Creates a new Role |
| 45 | + :param name: |
| 46 | + :return: |
| 47 | + """ |
| 48 | + endpoint = "/api/v1/project/{}/roles/new".format(self.client.project_string_id) |
| 49 | + |
| 50 | + response = self.client.session.post( |
| 51 | + self.client.host + endpoint, |
| 52 | + json = {'name': name}) |
| 53 | + |
| 54 | + self.client.handle_errors(response) |
| 55 | + |
| 56 | + data = response.json() |
| 57 | + role = Role(client = self.client, role_dict = data) |
| 58 | + return role |
| 59 | + |
| 60 | + def delete(self) -> 'Role': |
| 61 | + """ |
| 62 | + Creates a new Role |
| 63 | + :param name: |
| 64 | + :return: |
| 65 | + """ |
| 66 | + if not self.id: |
| 67 | + raise Exception('Role has no ID. Cannot be deleted.') |
| 68 | + |
| 69 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/roles/{self.id}/delete" |
| 70 | + |
| 71 | + response = self.client.session.delete( |
| 72 | + self.client.host + endpoint, json = {}) |
| 73 | + |
| 74 | + self.client.handle_errors(response) |
| 75 | + |
| 76 | + data = response.json() |
| 77 | + role = Role(client = self.client, role_dict = data) |
| 78 | + return role |
| 79 | + |
| 80 | + def add_permission(self, perm: str, object_type: str): |
| 81 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/roles/{self.id}/add-perm" |
| 82 | + |
| 83 | + response = self.client.session.patch( |
| 84 | + self.client.host + endpoint, json = { |
| 85 | + 'permission': perm, |
| 86 | + 'object_type': object_type, |
| 87 | + }) |
| 88 | + |
| 89 | + self.client.handle_errors(response) |
| 90 | + |
| 91 | + data = response.json() |
| 92 | + role = Role(client = self.client, role_dict = data) |
| 93 | + self.permissions_list = role.permissions_list |
| 94 | + return role |
| 95 | + |
| 96 | + def remove_permission(self, perm: str, object_type: str): |
| 97 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/roles/{self.id}/remove-perm" |
| 98 | + |
| 99 | + response = self.client.session.patch( |
| 100 | + self.client.host + endpoint, json = { |
| 101 | + 'permission': perm, |
| 102 | + 'object_type': object_type, |
| 103 | + }) |
| 104 | + |
| 105 | + self.client.handle_errors(response) |
| 106 | + |
| 107 | + data = response.json() |
| 108 | + role = Role(client = self.client, role_dict = data) |
| 109 | + self.permissions_list = role.permissions_list |
| 110 | + return role |
| 111 | + |
| 112 | + def assign_to_member_in_object(self, member_id: int, object_id: int, object_type: str): |
| 113 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/role-member-object" |
| 114 | + |
| 115 | + if not self.id: |
| 116 | + raise Exception(f'Provide role ID') |
| 117 | + |
| 118 | + response = self.client.session.patch( |
| 119 | + self.client.host + endpoint, json = { |
| 120 | + 'member_id': member_id, |
| 121 | + 'role_id': self.id, |
| 122 | + 'object_type': object_type, |
| 123 | + 'object_id': object_id, |
| 124 | + } |
| 125 | + ) |
| 126 | + |
| 127 | + self.client.handle_errors(response) |
| 128 | + role_member_object = response.json() |
| 129 | + return role_member_object |
| 130 | + |
| 131 | + def remove_role_assignment(self, member_id: int, object_id: int, object_type: str): |
| 132 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/role-member-object/remove" |
| 133 | + |
| 134 | + if not self.id: |
| 135 | + raise Exception(f'Provide role ID') |
| 136 | + |
| 137 | + response = self.client.session.patch( |
| 138 | + self.client.host + endpoint, json = { |
| 139 | + 'member_id': member_id, |
| 140 | + 'role_id': self.id, |
| 141 | + 'object_type': object_type, |
| 142 | + 'object_id': object_id, |
| 143 | + } |
| 144 | + ) |
| 145 | + |
| 146 | + self.client.handle_errors(response) |
| 147 | + role_member_object = response.json() |
| 148 | + return role_member_object |
| 149 | + |
| 150 | + def get(self, name: str) -> list: |
| 151 | + |
| 152 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/roles" |
| 153 | + |
| 154 | + response = self.client.session.get(self.client.host + endpoint) |
| 155 | + |
| 156 | + self.client.handle_errors(response) |
| 157 | + roles = response.json() |
| 158 | + |
| 159 | + result = None |
| 160 | + for r in roles: |
| 161 | + if r.get('name') == name: |
| 162 | + result = r |
| 163 | + break |
| 164 | + if result is None: |
| 165 | + raise Exception(f'Role {name} not found') |
| 166 | + role_obj = Role(client = self.client, role_dict = result) |
| 167 | + return role_obj |
| 168 | + |
| 169 | + def list(self) -> list: |
| 170 | + |
| 171 | + endpoint = f"/api/v1/project/{self.client.project_string_id}/roles" |
| 172 | + response = self.client.session.get(self.client.host + endpoint) |
| 173 | + |
| 174 | + self.client.handle_errors(response) |
| 175 | + roles = response.json() |
| 176 | + result = [] |
| 177 | + for r in roles: |
| 178 | + role_obj = Role(client = self.client, role_dict = r) |
| 179 | + result.append(role_obj) |
| 180 | + return result |
0 commit comments