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