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.

83 lines
3.5 KiB

  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. import logging
  20. from collections import defaultdict
  21. from odoo import api, fields, models
  22. from odoo import tools, _
  23. from odoo.exceptions import ValidationError
  24. from odoo.addons.base.res import res_users
  25. from odoo.addons.muk_security.tools import helper
  26. _logger = logging.getLogger(__name__)
  27. class ResUser(models.Model):
  28. _inherit = 'res.users'
  29. #----------------------------------------------------------
  30. # Create, Update, Delete
  31. #----------------------------------------------------------
  32. @api.model
  33. def create(self, values):
  34. result = super(ResUser, self).create(values)
  35. model_recs = defaultdict(set)
  36. model_names = self.pool.descendants(['muk_utils.groups'], '_inherit', '_inherits')
  37. for model_name in model_names:
  38. model = self.env[model_name].sudo()
  39. if not model._abstract:
  40. model_recs[model_name] = model.search([['groups', 'in', self.mapped('groups_id.id')]])
  41. for tuple in model_recs.items():
  42. tuple[1].trigger_computation(['users'])
  43. return result
  44. @api.multi
  45. def write(self, vals):
  46. result = super(ResUser, self).write(vals)
  47. vals = self._remove_reified_groups(vals)
  48. if any(field in vals for field in ['groups_id']):
  49. group_ids = [command[1] for command in vals['groups_id'] if command[0] == 4 or command[0] == 3]
  50. group_ids += [id for command in vals['groups_id'] if command[0] == 6 for id in command[2]]
  51. group_ids = list(set(group_ids))
  52. if group_ids:
  53. model_recs = defaultdict(set)
  54. model_names = self.pool.descendants(['muk_utils.groups'], '_inherit', '_inherits')
  55. for model_name in model_names:
  56. model = self.env[model_name].sudo()
  57. if not model._abstract:
  58. model_recs[model_name] = model.search([['groups', 'in', group_ids]])
  59. for tuple in model_recs.items():
  60. tuple[1].trigger_computation(['users'])
  61. return result
  62. @api.multi
  63. def unlink(self):
  64. model_recs = defaultdict(set)
  65. model_names = self.pool.descendants(['muk_utils.groups'], '_inherit', '_inherits')
  66. for model_name in model_names:
  67. model = self.env[model_name].sudo()
  68. if not model._abstract:
  69. model_recs[model_name] = model.search([['groups', 'in', self.mapped('groups_id.id')]])
  70. result = super(ResUser, self).unlink()
  71. for tuple in model_recs.items():
  72. tuple[1].trigger_computation(['users'])