portal_wizard.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. import logging
  4. from odoo.tools.translate import _
  5. from odoo.tools import email_normalize
  6. from odoo.exceptions import UserError
  7. from odoo import api, fields, models, Command
  8. _logger = logging.getLogger(__name__)
  9. class PortalWizard(models.TransientModel):
  10. """
  11. A wizard to manage the creation/removal of portal users.
  12. """
  13. _name = 'portal.wizard'
  14. _description = 'Grant Portal Access'
  15. def _default_partner_ids(self):
  16. partner_ids = self.env.context.get('default_partner_ids', []) or self.env.context.get('active_ids', [])
  17. contact_ids = set()
  18. for partner in self.env['res.partner'].sudo().browse(partner_ids):
  19. contact_partners = partner.child_ids.filtered(lambda p: p.type in ('contact', 'other')) | partner
  20. contact_ids |= set(contact_partners.ids)
  21. return [Command.link(contact_id) for contact_id in contact_ids]
  22. partner_ids = fields.Many2many('res.partner', string='Partners', default=_default_partner_ids)
  23. user_ids = fields.One2many('portal.wizard.user', 'wizard_id', string='Users', compute='_compute_user_ids', store=True, readonly=False)
  24. welcome_message = fields.Text('Invitation Message', help="This text is included in the email sent to new users of the portal.")
  25. @api.depends('partner_ids')
  26. def _compute_user_ids(self):
  27. for portal_wizard in self:
  28. portal_wizard.user_ids = [
  29. Command.create({
  30. 'partner_id': partner.id,
  31. 'email': partner.email,
  32. })
  33. for partner in portal_wizard.partner_ids
  34. ]
  35. @api.model
  36. def action_open_wizard(self):
  37. """Create a "portal.wizard" and open the form view.
  38. We need a server action for that because the one2many "user_ids" records need to
  39. exist to be able to execute an a button action on it. If they have no ID, the
  40. buttons will be disabled and we won't be able to click on them.
  41. That's why we need a server action, to create the records and then open the form
  42. view on them.
  43. """
  44. portal_wizard = self.create({})
  45. return portal_wizard._action_open_modal()
  46. def _action_open_modal(self):
  47. """Allow to keep the wizard modal open after executing the action."""
  48. return {
  49. 'name': _('Portal Access Management'),
  50. 'type': 'ir.actions.act_window',
  51. 'res_model': 'portal.wizard',
  52. 'view_type': 'form',
  53. 'view_mode': 'form',
  54. 'res_id': self.id,
  55. 'target': 'new',
  56. }
  57. class PortalWizardUser(models.TransientModel):
  58. """
  59. A model to configure users in the portal wizard.
  60. """
  61. _name = 'portal.wizard.user'
  62. _description = 'Portal User Config'
  63. wizard_id = fields.Many2one('portal.wizard', string='Wizard', required=True, ondelete='cascade')
  64. partner_id = fields.Many2one('res.partner', string='Contact', required=True, readonly=True, ondelete='cascade')
  65. email = fields.Char('Email')
  66. user_id = fields.Many2one('res.users', string='User', compute='_compute_user_id', compute_sudo=True)
  67. login_date = fields.Datetime(related='user_id.login_date', string='Latest Authentication')
  68. is_portal = fields.Boolean('Is Portal', compute='_compute_group_details')
  69. is_internal = fields.Boolean('Is Internal', compute='_compute_group_details')
  70. email_state = fields.Selection([
  71. ('ok', 'Valid'),
  72. ('ko', 'Invalid'),
  73. ('exist', 'Already Registered')],
  74. string='Status', compute='_compute_email_state', default='ok')
  75. @api.depends('email')
  76. def _compute_email_state(self):
  77. portal_users_with_email = self.filtered(lambda user: email_normalize(user.email))
  78. (self - portal_users_with_email).email_state = 'ko'
  79. normalized_emails = [email_normalize(portal_user.email) for portal_user in portal_users_with_email]
  80. existing_users = self.env['res.users'].with_context(active_test=False).sudo().search_read([('login', 'in', normalized_emails)], ['id', 'login'])
  81. for portal_user in portal_users_with_email:
  82. if next((user for user in existing_users if user['login'] == email_normalize(portal_user.email) and user['id'] != portal_user.user_id.id), None):
  83. portal_user.email_state = 'exist'
  84. else:
  85. portal_user.email_state = 'ok'
  86. @api.depends('partner_id')
  87. def _compute_user_id(self):
  88. for portal_wizard_user in self:
  89. user = portal_wizard_user.partner_id.with_context(active_test=False).user_ids
  90. portal_wizard_user.user_id = user[0] if user else False
  91. @api.depends('user_id', 'user_id.groups_id')
  92. def _compute_group_details(self):
  93. for portal_wizard_user in self:
  94. user = portal_wizard_user.user_id
  95. if user and user._is_internal():
  96. portal_wizard_user.is_internal = True
  97. portal_wizard_user.is_portal = False
  98. elif user and user.has_group('base.group_portal'):
  99. portal_wizard_user.is_internal = False
  100. portal_wizard_user.is_portal = True
  101. else:
  102. portal_wizard_user.is_internal = False
  103. portal_wizard_user.is_portal = False
  104. def action_grant_access(self):
  105. """Grant the portal access to the partner.
  106. If the partner has no linked user, we will create a new one in the same company
  107. as the partner (or in the current company if not set).
  108. An invitation email will be sent to the partner.
  109. """
  110. self.ensure_one()
  111. self._assert_user_email_uniqueness()
  112. if self.is_portal or self.is_internal:
  113. raise UserError(_('The partner "%s" already has the portal access.', self.partner_id.name))
  114. group_portal = self.env.ref('base.group_portal')
  115. group_public = self.env.ref('base.group_public')
  116. self._update_partner_email()
  117. user_sudo = self.user_id.sudo()
  118. if not user_sudo:
  119. # create a user if necessary and make sure it is in the portal group
  120. company = self.partner_id.company_id or self.env.company
  121. user_sudo = self.sudo().with_company(company.id)._create_user()
  122. if not user_sudo.active or not self.is_portal:
  123. user_sudo.write({'active': True, 'groups_id': [(4, group_portal.id), (3, group_public.id)]})
  124. # prepare for the signup process
  125. user_sudo.partner_id.signup_prepare()
  126. self.with_context(active_test=True)._send_email()
  127. return self.action_refresh_modal()
  128. def action_revoke_access(self):
  129. """Remove the user of the partner from the portal group.
  130. If the user was only in the portal group, we archive it.
  131. """
  132. self.ensure_one()
  133. if not self.is_portal:
  134. raise UserError(_('The partner "%s" has no portal access or is internal.', self.partner_id.name))
  135. group_portal = self.env.ref('base.group_portal')
  136. group_public = self.env.ref('base.group_public')
  137. self._update_partner_email()
  138. # Remove the sign up token, so it can not be used
  139. self.partner_id.sudo().signup_token = False
  140. user_sudo = self.user_id.sudo()
  141. # remove the user from the portal group
  142. if user_sudo and user_sudo.has_group('base.group_portal'):
  143. user_sudo.write({'groups_id': [(3, group_portal.id), (4, group_public.id)], 'active': False})
  144. return self.action_refresh_modal()
  145. def action_invite_again(self):
  146. """Re-send the invitation email to the partner."""
  147. self.ensure_one()
  148. self._assert_user_email_uniqueness()
  149. if not self.is_portal:
  150. raise UserError(_('You should first grant the portal access to the partner "%s".', self.partner_id.name))
  151. self._update_partner_email()
  152. self.with_context(active_test=True)._send_email()
  153. return self.action_refresh_modal()
  154. def action_refresh_modal(self):
  155. """Refresh the portal wizard modal and keep it open. Used as fallback action of email state icon buttons,
  156. required as they must be non-disabled buttons to fire mouse events to show tooltips on email state."""
  157. return self.wizard_id._action_open_modal()
  158. def _create_user(self):
  159. """ create a new user for wizard_user.partner_id
  160. :returns record of res.users
  161. """
  162. return self.env['res.users'].with_context(no_reset_password=True)._create_user_from_template({
  163. 'email': email_normalize(self.email),
  164. 'login': email_normalize(self.email),
  165. 'partner_id': self.partner_id.id,
  166. 'company_id': self.env.company.id,
  167. 'company_ids': [(6, 0, self.env.company.ids)],
  168. })
  169. def _send_email(self):
  170. """ send notification email to a new portal user """
  171. self.ensure_one()
  172. # determine subject and body in the portal user's language
  173. template = self.env.ref('portal.mail_template_data_portal_welcome')
  174. if not template:
  175. raise UserError(_('The template "Portal: new user" not found for sending email to the portal user.'))
  176. lang = self.user_id.sudo().lang
  177. partner = self.user_id.sudo().partner_id
  178. portal_url = partner.with_context(signup_force_type_in_url='', lang=lang)._get_signup_url_for_action()[partner.id]
  179. partner.signup_prepare()
  180. template.with_context(dbname=self._cr.dbname, portal_url=portal_url, lang=lang).send_mail(self.id, force_send=True)
  181. return True
  182. def _assert_user_email_uniqueness(self):
  183. """Check that the email can be used to create a new user."""
  184. self.ensure_one()
  185. if self.email_state == 'ko':
  186. raise UserError(_('The contact "%s" does not have a valid email.', self.partner_id.name))
  187. if self.email_state == 'exist':
  188. raise UserError(_('The contact "%s" has the same email as an existing user', self.partner_id.name))
  189. def _update_partner_email(self):
  190. """Update partner email on portal action, if a new one was introduced and is valid."""
  191. email_normalized = email_normalize(self.email)
  192. if self.email_state == 'ok' and email_normalize(self.partner_id.email) != email_normalized:
  193. self.partner_id.write({'email': email_normalized})