Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] password_security: use ir.config_parameter #1

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions password_security/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2015 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from .post_install import init_config_parameters
from . import controllers, models
3 changes: 2 additions & 1 deletion password_security/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{
"name": "Password Security",
"summary": "Allow admin to set password security requirements.",
"version": "17.0.1.0.0",
"version": "17.0.2.0.0",
"author": "LasLabs, "
"Onestein, "
"Kaushal Prajapati, "
Expand All @@ -28,5 +28,6 @@
"demo": [
"demo/res_users.xml",
],
"post_init_hook": "init_config_parameters",
"installable": True,
}
27 changes: 27 additions & 0 deletions password_security/migrations/17.0.2.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
env.cr.execute(
f"SELECT {openupgrade.get_legacy_name('password_expiration')}, "
f"{openupgrade.get_legacy_name('password_minimum')}, "
f"{openupgrade.get_legacy_name('password_history')}, "
f"{openupgrade.get_legacy_name('password_lower')}, "
f"{openupgrade.get_legacy_name('password_upper')}, "
f"{openupgrade.get_legacy_name('password_numeric')}, "
f"{openupgrade.get_legacy_name('password_special')} "
"FROM res_company ORDER BY id LIMIT 1"
)
res = env.cr.fetchone()
env["ir.config_parameter"].set_param("password_security.expiration_days", res[0])
env["ir.config_parameter"].set_param("password_security.minimum_hours", res[1])
env["ir.config_parameter"].set_param("password_security.history", res[2])
env["ir.config_parameter"].set_param("password_security.lower", res[3])
env["ir.config_parameter"].set_param("password_security.upper", res[4])
env["ir.config_parameter"].set_param("password_security.numeric", res[5])
env["ir.config_parameter"].set_param("password_security.special", res[6])
57 changes: 57 additions & 0 deletions password_security/migrations/17.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Copyright 2024 Akretion France (http://www.akretion.com/)
# @author: Alexis de Lattre <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).


from openupgradelib import openupgrade


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_fields(
env,
[
(
"res.company",
"res_company",
"password_expiration",
openupgrade.get_legacy_name("password_expiration"),
),
(
"res.company",
"res_company",
"password_lower",
openupgrade.get_legacy_name("password_lower"),
),
(
"res.company",
"res_company",
"password_upper",
openupgrade.get_legacy_name("password_upper"),
),
(
"res.company",
"res_company",
"password_numeric",
openupgrade.get_legacy_name("password_numeric"),
),
(
"res.company",
"res_company",
"password_special",
openupgrade.get_legacy_name("password_special"),
),
(
"res.company",
"res_company",
"password_history",
openupgrade.get_legacy_name("password_history"),
),
(
"res.company",
"res_company",
"password_minimum",
openupgrade.get_legacy_name("password_minimum"),
),
],
)
1 change: 0 additions & 1 deletion password_security/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2015 LasLabs Inc.
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).

from . import res_company
from . import res_config_settings
from . import res_users
from . import res_users_pass_history
46 changes: 0 additions & 46 deletions password_security/models/res_company.py

This file was deleted.

52 changes: 45 additions & 7 deletions password_security/models/res_config_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,58 @@
class ResConfigSettings(models.TransientModel):
_inherit = "res.config.settings"

# Imagine that the ir.config_parameter password_security.numeric has a default value of 1.
# If the user sets the value to 0 on the config page, the ir.config_parameter is deleted... but
# when the ir.config_parameter is not present in the database, Odoo displays the default value
# on the config page => Odoo displays 1 !
# So, when the users sets the value of 0 on the config page, he will see 1
# after saving the page !!!
# If the default value is 0 (like auth_password_policy.minlength in the
# module auth_password_policy of the official addons), there is no problem.
# So the solution to avoid this problem and have a non-null default value:
# 1) define the ir.config_parameter fields on res.config.settings with default=0
# 2) initialize the ir.config_parameter with a default value in the init script
# So the default value of the fields below are written in post_install.py
password_expiration = fields.Integer(
related="company_id.password_expiration", readonly=False
string="Days",
default=0,
config_parameter="password_security.expiration_days",
help="How many days until passwords expire",
)
password_minimum = fields.Integer(
related="company_id.password_minimum", readonly=False
string="Minimum Hours",
default=0,
config_parameter="password_security.minimum_hours",
help="Number of hours until a user may change password again",
)
password_history = fields.Integer(
related="company_id.password_history", readonly=False
string="History",
default=0,
config_parameter="password_security.history",
help="Disallow reuse of this many previous passwords - use negative "
"number for infinite, or 0 to disable",
)
password_lower = fields.Integer(
string="Lowercase",
default=0,
config_parameter="password_security.lower",
help="Require number of lowercase letters",
)
password_upper = fields.Integer(
string="Uppercase",
default=0,
config_parameter="password_security.upper",
help="Require number of uppercase letters",
)
password_lower = fields.Integer(related="company_id.password_lower", readonly=False)
password_upper = fields.Integer(related="company_id.password_upper", readonly=False)
password_numeric = fields.Integer(
related="company_id.password_numeric", readonly=False
string="Numeric",
default=0,
config_parameter="password_security.numeric",
help="Require number of numeric digits",
)
password_special = fields.Integer(
related="company_id.password_special", readonly=False
string="Special",
default=0,
config_parameter="password_security.special",
help="Require number of unique special characters",
)
Loading