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.

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