|
4 | 4 | import logging |
5 | 5 | import random |
6 | 6 |
|
7 | | -from odoo import api, models |
| 7 | +from odoo import models |
| 8 | +from odoo.tools import safe_eval |
| 9 | + |
| 10 | +from odoo.addons.auth_saml.models.ir_config_parameter import ALLOW_SAML_UID_AND_PASSWORD |
8 | 11 |
|
9 | 12 | _logger = logging.getLogger(__name__) |
10 | 13 | s = "abcdefghijklmnopqrstuvwxyz034567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" |
|
14 | 17 | class ResUsers(models.Model): |
15 | 18 | _inherit = "res.users" |
16 | 19 |
|
17 | | - @api.multi |
18 | | - def _auth_saml_signin(self, provider, validation, saml_response): |
19 | | - saml_uid = validation["user_id"] |
20 | | - user_ids = self.search( |
21 | | - [("saml_uid", "=", saml_uid), ("saml_provider_id", "=", provider)] |
22 | | - ) |
23 | | - if self.check_if_create_user(provider) and not user_ids: |
24 | | - self.create_user(saml_uid, provider) |
25 | | - return super()._auth_saml_signin(provider, validation, saml_response) |
26 | | - |
27 | 20 | def check_if_create_user(self, provider): |
28 | 21 | return self.env["auth.saml.provider"].browse(provider).create_user |
29 | 22 |
|
30 | 23 | def create_user(self, saml_uid, provider): |
31 | | - _logger.debug('Creating new Odoo user "%s" from SAML' % saml_uid) |
| 24 | + _logger.debug(f"Creating new Odoo user {saml_uid} from SAML") |
32 | 25 | SudoUser = self.env["res.users"].sudo() |
33 | | - new_user = SudoUser.create( |
34 | | - { |
35 | | - "name": saml_uid, |
36 | | - "login": saml_uid, |
37 | | - "saml_provider_id": provider, |
38 | | - "password": "".join(random.sample(s, passlen)), |
39 | | - "company_id": self.env["res.company"].sudo().browse(1).id, |
40 | | - } |
| 26 | + values = { |
| 27 | + "name": saml_uid, |
| 28 | + "login": saml_uid, |
| 29 | + "saml_ids": [ |
| 30 | + (0, 0, {"saml_provider_id": provider, "saml_uid": saml_uid}), |
| 31 | + ], |
| 32 | + "company_id": self.env["res.company"].sudo().browse(1).id, |
| 33 | + } |
| 34 | + allow_saml_password = ( |
| 35 | + self.env["ir.config_parameter"] |
| 36 | + .sudo() |
| 37 | + .get_param(ALLOW_SAML_UID_AND_PASSWORD, "False") |
| 38 | + ) |
| 39 | + if safe_eval.safe_eval(allow_saml_password): |
| 40 | + values["password"] = "".join(random.sample(s, passlen)) |
| 41 | + res = SudoUser.create(values) |
| 42 | + return res |
| 43 | + |
| 44 | + def _auth_saml_signin(self, provider: int, validation: dict, saml_response) -> str: |
| 45 | + """ |
| 46 | + Overload to auto create a new user if configured to allow it. |
| 47 | + """ |
| 48 | + saml_uid = validation["user_id"] |
| 49 | + user_ids = self.env["res.users.saml"].search( |
| 50 | + [("saml_uid", "=", saml_uid), ("saml_provider_id", "=", provider)] |
41 | 51 | ) |
42 | | - new_user.write({"saml_uid": saml_uid}) |
| 52 | + if self.check_if_create_user(provider) and not user_ids: |
| 53 | + self.create_user(saml_uid, provider) |
| 54 | + return super()._auth_saml_signin(provider, validation, saml_response) |
0 commit comments