|
| 1 | +# Copyright 2026 Therp BV <https://therp.nl>. |
| 2 | +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). |
| 3 | +"""We override the standard functionality that only supports one Outlook connection. |
| 4 | +
|
| 5 | +To support each outgoing mail server to have its own connection to Outlook, |
| 6 | +we provide for the connection parameters to be part of the server definition itself. |
| 7 | +
|
| 8 | +If there is no connection information in the server record, we will still fall back to |
| 9 | +the default configuration using system parameters. |
| 10 | +""" |
| 11 | +from odoo import api, fields, models |
| 12 | + |
| 13 | + |
| 14 | +class IrMailServer(models.Model): |
| 15 | + _inherit = "ir.mail_server" |
| 16 | + |
| 17 | + microsoft_outlook_client_identifier = fields.Char( |
| 18 | + "Outlook Client Id", |
| 19 | + help="Specific client_id for this server", |
| 20 | + ) |
| 21 | + microsoft_outlook_client_secret = fields.Char( |
| 22 | + "Outlook Client Secret", |
| 23 | + help="Specific client_secret for this server", |
| 24 | + ) |
| 25 | + |
| 26 | + @api.depends( |
| 27 | + "use_microsoft_outlook_service", |
| 28 | + "microsoft_outlook_client_identifier", |
| 29 | + "microsoft_outlook_client_secret", |
| 30 | + ) |
| 31 | + def _compute_is_microsoft_outlook_configured(self): |
| 32 | + """Check using this record, else fallback to default. |
| 33 | +
|
| 34 | + The field microsoft_outlook_configured should be set to |
| 35 | + true when client identifier and secret have been set. The |
| 36 | + values for these fields must be given by the person who |
| 37 | + registered the Odoo tenant in the Azure directory. |
| 38 | + """ |
| 39 | + # Cannot depend on trick to override get_param as original |
| 40 | + # method might fail on Singleton error. |
| 41 | + for this in self: |
| 42 | + if not this.use_microsoft_outlook_service: |
| 43 | + # The super method also returns True for any record, whether configured |
| 44 | + # to use Outlook or not, if system parameters for outlook have been set. |
| 45 | + # This is pure madness, so we override this. |
| 46 | + self.is_microsoft_outlook_configured = False |
| 47 | + elif this._has_id_and_secret(): |
| 48 | + self.is_microsoft_outlook_configured = True |
| 49 | + else: |
| 50 | + super(IrMailServer, this)._compute_is_microsoft_outlook_configured() |
| 51 | + |
| 52 | + @api.depends("is_microsoft_outlook_configured") |
| 53 | + def _compute_outlook_uri(self): |
| 54 | + """The outlook_uri contains the information that is needed activate the client. |
| 55 | +
|
| 56 | + After client id and secret have been set, the user must click on the |
| 57 | + "Connect Your Outlook account" button. This will open a Microsoft |
| 58 | + webpage containing the needed information. Microsoft will then |
| 59 | + generate an authorization token and call back a controller that sets |
| 60 | + this token, and a refresh token and expiration on the server record. |
| 61 | + """ |
| 62 | + record = self._get_preset_record() if self._has_id_and_secret() else self |
| 63 | + return super(IrMailServer, record)._compute_outlook_uri() |
| 64 | + |
| 65 | + def _fetch_outlook_refresh_token(self, authorization_code): |
| 66 | + record = self._get_preset_record() if self._has_id_and_secret() else self |
| 67 | + return super(IrMailServer, record).__fetch_outlook_refresh_token( |
| 68 | + authorization_code |
| 69 | + ) |
| 70 | + |
| 71 | + def _smtp_login(self, connection, smtp_user, smtp_password): |
| 72 | + record = self._get_preset_record() if self._has_id_and_secret() else self |
| 73 | + return super(IrMailServer, record)._smtp_login( |
| 74 | + connection, smtp_user, smtp_password |
| 75 | + ) |
| 76 | + |
| 77 | + def _has_id_and_secret(self): |
| 78 | + """Check whether called on record with specific client ID and Secret.""" |
| 79 | + if ( |
| 80 | + len(self) == 1 |
| 81 | + and self.microsoft_outlook_client_identifier |
| 82 | + and self.microsoft_outlook_client_secret |
| 83 | + ): |
| 84 | + return True |
| 85 | + return False |
| 86 | + |
| 87 | + def _get_preset_record(self): |
| 88 | + """Return record with context filled with preset client id and secret.""" |
| 89 | + self.ensure_one() |
| 90 | + return self.with_context( |
| 91 | + preset_microsoft_outlook_client_id=self.microsoft_outlook_client_identifier, |
| 92 | + preset_microsoft_outlook_client_secret=self.microsoft_outlook_client_secret, |
| 93 | + ) |
0 commit comments