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.

75 lines
2.4 KiB

  1. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  2. from odoo import api, fields, models
  3. class ResUsers(models.Model):
  4. _inherit = "res.users"
  5. def _get_default_profile(self):
  6. return self.env.ref(
  7. "base_user_role_profile.default_profile", raise_if_not_found=False
  8. )
  9. profile_id = fields.Many2one(
  10. "res.users.profile",
  11. "Current profile",
  12. default=lambda self: self._get_default_profile(),
  13. )
  14. profile_ids = fields.Many2many(
  15. "res.users.profile",
  16. string="Currently allowed profiles",
  17. )
  18. def _get_action_root_menu(self):
  19. # used JS-side. Reload the client; open the first available root menu
  20. menu = self.env["ir.ui.menu"].search([("parent_id", "=", False)])[:1]
  21. return {
  22. "type": "ir.actions.client",
  23. "tag": "reload",
  24. "params": {"menu_id": menu.id},
  25. }
  26. def action_profile_change(self, vals):
  27. self.write(vals)
  28. return self._get_action_root_menu()
  29. @api.model
  30. def create(self, vals):
  31. new_record = super().create(vals)
  32. if vals.get("role_line_ids"):
  33. new_record.sudo()._compute_profile_ids()
  34. return new_record
  35. def write(self, vals):
  36. # inspired by base/models/res_users.py l. 491
  37. if self == self.env.user and vals.get("profile_id"):
  38. self.sudo().write({"profile_id": vals["profile_id"]})
  39. del vals["profile_id"]
  40. res = super().write(vals)
  41. if vals.get("profile_id") or vals.get("role_line_ids"):
  42. self.sudo()._compute_profile_ids()
  43. return res
  44. def _get_enabled_roles(self):
  45. res = super()._get_enabled_roles()
  46. res = res.filtered(
  47. lambda r: not r.profile_id or (r.profile_id.id == r.user_id.profile_id.id)
  48. )
  49. return res
  50. def _update_profile_id(self):
  51. default_profile = self._get_default_profile()
  52. if not self.profile_ids:
  53. if self.profile_id != default_profile:
  54. self.profile_id = default_profile
  55. elif self.profile_id not in self.profile_ids:
  56. self.write({"profile_id": self.profile_ids[0].id})
  57. def _compute_profile_ids(self):
  58. for rec in self:
  59. role_lines = rec.role_line_ids
  60. profiles = role_lines.mapped("profile_id")
  61. rec.profile_ids = profiles
  62. # set defaults in case applicable profile changes
  63. rec._update_profile_id()