You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (C) 2017 Marcos Pereira <marcospereira.mpj@gmail.com>
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. # this software and associated documentation files (the "Software"), to deal in
  9. # the Software without restriction, including without limitation the rights to
  10. # use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. # the Software, and to permit persons to whom the Software is furnished to do so,
  12. # subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in all
  15. # copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. # FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. # COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. """Keycloak authorization Permission module."""
  24. class Permission:
  25. """Base permission class.
  26. Consider this simple and very common permission:
  27. A permission associates the object being protected with the policies that must be evaluated to
  28. determine whether access is granted.
  29. X CAN DO Y ON RESOURCE Z
  30. where
  31. - X represents one or more users, roles, or groups, or a combination of them. You can
  32. also use claims and context here.
  33. - Y represents an action to be performed, for example, write, view, and so on.
  34. - Z represents a protected resource, for example, "/accounts".
  35. https://keycloak.gitbooks.io/documentation/authorization_services/topics/permission/overview.html
  36. """
  37. def __init__(self, name, type, logic, decision_strategy):
  38. """Init method."""
  39. self.name = name
  40. self.type = type
  41. self.logic = logic
  42. self.decision_strategy = decision_strategy
  43. self.resources = []
  44. self.scopes = []
  45. def __repr__(self):
  46. """Repr method."""
  47. return "<Permission: %s (%s)>" % (self.name, self.type)
  48. def __str__(self):
  49. """Str method."""
  50. return "Permission: %s (%s)" % (self.name, self.type)
  51. @property
  52. def name(self):
  53. """Get name."""
  54. return self._name
  55. @name.setter
  56. def name(self, value):
  57. self._name = value
  58. @property
  59. def type(self):
  60. """Get type."""
  61. return self._type
  62. @type.setter
  63. def type(self, value):
  64. self._type = value
  65. @property
  66. def logic(self):
  67. """Get logic."""
  68. return self._logic
  69. @logic.setter
  70. def logic(self, value):
  71. self._logic = value
  72. @property
  73. def decision_strategy(self):
  74. """Get decision strategy."""
  75. return self._decision_strategy
  76. @decision_strategy.setter
  77. def decision_strategy(self, value):
  78. self._decision_strategy = value
  79. @property
  80. def resources(self):
  81. """Get resources."""
  82. return self._resources
  83. @resources.setter
  84. def resources(self, value):
  85. self._resources = value
  86. @property
  87. def scopes(self):
  88. """Get scopes."""
  89. return self._scopes
  90. @scopes.setter
  91. def scopes(self, value):
  92. self._scopes = value