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.

177 lines
5.5 KiB

7 years ago
  1. ###################################################################################
  2. #
  3. # Copyright (C) 2017 MuK IT GmbH
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU Affero General Public License as
  7. # published by the Free Software Foundation, either version 3 of the
  8. # License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU Affero General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Affero General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. ###################################################################################
  19. from odoo import models, fields, api
  20. class AccessGroups(models.Model):
  21. _name = 'muk_security.groups'
  22. _description = "Access Groups"
  23. _inherit = 'muk_utils.model'
  24. _parent_store = True
  25. _parent_name = "parent_group"
  26. _parent_order = 'parent_left'
  27. _order = 'parent_left'
  28. #----------------------------------------------------------
  29. # Database
  30. #----------------------------------------------------------
  31. name = fields.Char(
  32. string="Group Name",
  33. required=True)
  34. parent_group = fields.Many2one(
  35. comodel_name='muk_security.groups',
  36. string='Parent Group',
  37. ondelete='cascade',
  38. auto_join=True,
  39. index=True)
  40. child_groups = fields.One2many(
  41. comodel_name='muk_security.groups',
  42. inverse_name='parent_group',
  43. string='Child Groups')
  44. parent_left = fields.Integer(
  45. string='Left Parent',
  46. index=True)
  47. parent_right = fields.Integer(
  48. string='Right Parent',
  49. index=True)
  50. perm_read = fields.Boolean(
  51. string='Read Access')
  52. perm_create = fields.Boolean(
  53. string='Create Access')
  54. perm_write = fields.Boolean(
  55. string='Write Access')
  56. perm_unlink = fields.Boolean(
  57. string='Unlink Access')
  58. groups = fields.Many2many(
  59. comodel_name='res.groups',
  60. relation='muk_groups_groups_rel',
  61. column1='gid',
  62. column2='rid',
  63. string='Groups')
  64. explicit_users = fields.Many2many(
  65. comodel_name='res.users',
  66. relation='muk_groups_explicit_users_rel',
  67. column1='gid',
  68. column2='uid',
  69. string='Explicit Users')
  70. users = fields.Many2many(
  71. comodel_name='res.users',
  72. relation='muk_groups_users_rel',
  73. column1='gid',
  74. column2='uid',
  75. string='Users',
  76. compute='_compute_users',
  77. store=True)
  78. count_users = fields.Integer(
  79. compute='_compute_count_users',
  80. string="Users")
  81. _sql_constraints = [
  82. ('name_uniq', 'unique (name)', 'The name of the group must be unique!')
  83. ]
  84. #----------------------------------------------------------
  85. # Functions
  86. #----------------------------------------------------------
  87. def trigger_computation_up(self, fields):
  88. parent_group = self.parent_group
  89. if parent_group:
  90. parent_group.trigger_computation(fields)
  91. def trigger_computation_down(self, fields):
  92. for child in self.child_groups:
  93. child.with_context(is_subnode=True).trigger_computation(fields)
  94. def trigger_computation(self, fields):
  95. values = {}
  96. if "users" in fields:
  97. values.update(self._compute_users(write=False))
  98. if values:
  99. self.write(values);
  100. if "users" in fields:
  101. self.trigger_computation_down(fields)
  102. #----------------------------------------------------------
  103. # Read, View
  104. #----------------------------------------------------------
  105. @api.model
  106. def check_user_values(self, values):
  107. if any(field in values for field in ['parent_group', 'groups', 'explicit_users']):
  108. return True
  109. return False
  110. @api.multi
  111. def get_users(self):
  112. self.ensure_one()
  113. users = self.env['res.users']
  114. if self.parent_group:
  115. users |= self.parent_group.users
  116. users |= self.groups.mapped('users')
  117. users |= self.explicit_users
  118. return users
  119. def _compute_users(self, write=True):
  120. if write:
  121. for record in self:
  122. record.users = record.get_users()
  123. else:
  124. self.ensure_one()
  125. return {'users': [(6, 0, self.get_users().mapped('id'))]}
  126. @api.depends('users')
  127. def _compute_count_users(self):
  128. for record in self:
  129. record.count_users = len(record.users)
  130. #----------------------------------------------------------
  131. # Create, Write, Delete
  132. #----------------------------------------------------------
  133. def _after_create(self, vals):
  134. record = super(AccessGroups, self)._after_create(vals)
  135. record._check_recomputation(vals)
  136. return record
  137. def _after_write_record(self, vals):
  138. vals = super(AccessGroups, self)._after_write_record(vals)
  139. self._check_recomputation(vals)
  140. return vals
  141. def _check_recomputation(self, values):
  142. fields = []
  143. if self.check_user_values(values):
  144. fields.extend(['users'])
  145. if fields:
  146. self.trigger_computation(fields)