permission_parameters.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # Copyright 2019 The FATE Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. from ._base import BaseEntity
  18. class PermissionParameters(BaseEntity):
  19. def __init__(self, **kwargs):
  20. self.party_id = None
  21. self.component = None
  22. self.dataset = {}
  23. self.is_delete = False
  24. for k, v in kwargs.items():
  25. if hasattr(self, k):
  26. setattr(self, k, v)
  27. def to_dict(self):
  28. d = {}
  29. for k, v in self.__dict__.items():
  30. if v is None:
  31. continue
  32. d[k] = v
  33. return d
  34. class DataSet(BaseEntity):
  35. def __init__(self, namespace, name, **kwargs):
  36. self.namespace = namespace
  37. self.name = name
  38. def to_dict(self):
  39. d = {}
  40. for k, v in self.__dict__.items():
  41. if v is None:
  42. continue
  43. d[k] = v
  44. return d
  45. @property
  46. def value(self):
  47. return json.dumps(self.to_dict(), sort_keys=True)
  48. @property
  49. def casbin_value(self):
  50. return json.dumps(self.to_dict(), sort_keys=True, separators=(';', '-'))
  51. @staticmethod
  52. def load_casbin_value(value):
  53. return json.loads(value.replace(";", ",").replace("-", ":"))
  54. def check(self):
  55. if not self.name or not self.namespace:
  56. raise ValueError(f"name {self.name} or namespace {self.namespace} is null")
  57. class CheckReturn:
  58. SUCCESS = 0
  59. NO_ROLE_PERMISSION = 1
  60. NO_COMPONENT_PERMISSION = 2
  61. NO_DATASET_PERMISSION = 3