Skip to content

Commit 4cc52b4

Browse files
committed
[MIG] auth_saml_create_user: Migration to 18.0
1 parent bd92107 commit 4cc52b4

16 files changed

Lines changed: 115 additions & 323 deletions

auth_saml_create_user/README.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Contributors
7171
- Larbi Gharib <larbi.gharib@savoirfairelinux.com>
7272
- Pierre Gault <pierre.gault@savoirfairelinux.com>
7373
- William Beverly <william.beverly@savoirfairelinux.com>
74+
- Martin Deconinck <martin.deconinck@smile.fr>
75+
- Théo Martin <theo.martin@smile.fr>
7476

7577
Other credits
7678
-------------

auth_saml_create_user/__manifest__.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,26 @@
66
"summary": """
77
This module extends the functionality of Auth SAML to support
88
the automatic creation of SAML users when they don't exist in odoo.""",
9-
"author": "Savoir-faire Linux, Odoo Community Association (OCA)",
9+
"version": "18.0.1.0.0",
10+
"category": "Tools",
11+
"sequence": 20,
12+
"author": "Savoir-faire Linux, Odoo Community Association (OCA), Smile",
1013
"maintainers": ["eilst"],
14+
"description": """Auth SAML Auto create users
15+
16+
Allow to automatically create users when they authenticate with SAML.
17+
""",
1118
"website": "https://github.com/OCA/server-auth",
1219
"license": "AGPL-3",
13-
"category": "Tools",
14-
"version": "11.0.1.0.1",
15-
"depends": ["auth_saml"],
20+
"depends": [
21+
"auth_saml",
22+
],
1623
"data": [
17-
"data/auth_saml_create_user.xml",
18-
"views/auth_saml.xml",
24+
'views/auth_saml.xml',
1925
],
20-
"development_status": "Production/Stable",
26+
'demo': [],
27+
'test': [],
28+
"auto_install": False,
29+
"installable": True,
30+
"application": False,
2131
}

auth_saml_create_user/data/auth_saml_create_user.xml

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# © 2018 Savoir-faire Linux
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from . import auth_saml
4+
from . import auth_saml_provider
55
from . import res_users
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Copyright (C) 2010-2016 XCG Consulting <http://odoo.consulting>
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
from odoo import fields, models
4+
from odoo import models, fields
55

66

77
class AuthSamlProvider(models.Model):
8-
_inherit = "auth.saml.provider"
8+
_inherit = 'auth.saml.provider'
99

1010
create_user = fields.Boolean(
11-
string="Create User",
11+
string='Create User',
12+
default=True,
1213
)
Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,47 @@
11
# © 2019 Savoir-faire Linux
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4-
import logging
4+
from odoo import models
5+
from odoo.tools import safe_eval
6+
from odoo.addons.auth_saml.models.ir_config_parameter import ALLOW_SAML_UID_AND_PASSWORD
7+
58
import random
9+
import logging
610

7-
from odoo import api, models
811

912
_logger = logging.getLogger(__name__)
1013
s = "abcdefghijklmnopqrstuvwxyz034567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
1114
passlen = 16
1215

1316

1417
class ResUsers(models.Model):
15-
_inherit = "res.users"
16-
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)
18+
_inherit = 'res.users'
2619

2720
def check_if_create_user(self, provider):
28-
return self.env["auth.saml.provider"].browse(provider).create_user
21+
return self.env['auth.saml.provider'].browse(provider).create_user
2922

3023
def create_user(self, saml_uid, provider):
31-
_logger.debug('Creating new Odoo user "%s" from SAML' % saml_uid)
32-
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-
}
41-
)
42-
new_user.write({"saml_uid": saml_uid})
24+
_logger.debug("Creating new Odoo user \"%s\" from SAML" % saml_uid)
25+
SudoUser = self.env['res.users'].sudo()
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,
31+
}
32+
allow_saml_password = self.env['ir.config_parameter'].sudo().get_param(ALLOW_SAML_UID_AND_PASSWORD, 'False')
33+
if safe_eval.safe_eval(allow_saml_password):
34+
values['password'] = "".join(random.sample(s, passlen))
35+
res = SudoUser.create(values)
36+
return res
37+
38+
def _auth_saml_signin(self, provider: int, validation: dict, saml_response) -> str:
39+
"""
40+
Overload to auto create a new user if configured to allow it.
41+
"""
42+
saml_uid = validation['user_id']
43+
user_ids = self.env["res.users.saml"].search(
44+
[('saml_uid', '=', saml_uid), ('saml_provider_id', '=', provider)])
45+
if self.check_if_create_user(provider) and not user_ids:
46+
self.create_user(saml_uid, provider)
47+
return super()._auth_saml_signin(provider, validation, saml_response)

auth_saml_create_user/pyproject.toml

Lines changed: 0 additions & 3 deletions
This file was deleted.

auth_saml_create_user/readme/CONTRIBUTORS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
- Larbi Gharib \<larbi.gharib@savoirfairelinux.com\>
55
- Pierre Gault \<pierre.gault@savoirfairelinux.com\>
66
- William Beverly \<william.beverly@savoirfairelinux.com\>
7+
8+
- [SMILE] (https://smile.eu/en):
9+
- Martin DECONINCK \<martin.deconinck@smile.fr\>
10+
- Théo Martin \<theo.martin@smile.fr\>

auth_saml_create_user/security/ir.model.access.csv

Lines changed: 0 additions & 2 deletions
This file was deleted.

auth_saml_create_user/static/description/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,8 @@ <h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
419419
<li>Larbi Gharib &lt;<a class="reference external" href="mailto:larbi.gharib&#64;savoirfairelinux.com">larbi.gharib&#64;savoirfairelinux.com</a>&gt;</li>
420420
<li>Pierre Gault &lt;<a class="reference external" href="mailto:pierre.gault&#64;savoirfairelinux.com">pierre.gault&#64;savoirfairelinux.com</a>&gt;</li>
421421
<li>William Beverly &lt;<a class="reference external" href="mailto:william.beverly&#64;savoirfairelinux.com">william.beverly&#64;savoirfairelinux.com</a>&gt;</li>
422+
<li>Martin Deconinck &lt;<a class="reference external" href="mailto:martin.deconinck&#64;smile.fr">martin.deconinck&#64;smile.fr</a>&gt;</li>
423+
<li>Théo Martin &lt;<a class="reference external" href="mailto:theo.martin&#64;smile.fr">theo.martin&#64;smile.fr</a>&gt;</li>
422424
</ul>
423425
</div>
424426
<div class="section" id="other-credits">

0 commit comments

Comments
 (0)