modora 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- modora/__init__.py +12 -0
- modora/addons/__init__.py +11 -0
- modora/addons/base/__init__.py +6 -0
- modora/addons/base/__manifest__.py +22 -0
- modora/addons/base/controllers/__init__.py +7 -0
- modora/addons/base/controllers/content.py +58 -0
- modora/addons/base/controllers/export.py +51 -0
- modora/addons/base/controllers/main.py +23 -0
- modora/addons/base/controllers/report.py +36 -0
- modora/addons/base/controllers/rest.py +41 -0
- modora/addons/base/controllers/rpc.py +18 -0
- modora/addons/base/controllers/session.py +93 -0
- modora/addons/base/data/base_records.py +44 -0
- modora/addons/base/data/crons.py +22 -0
- modora/addons/base/data/menus.py +44 -0
- modora/addons/base/data/sys.lang.csv +52 -0
- modora/addons/base/data/sys_config.py +19 -0
- modora/addons/base/demo/users.py +18 -0
- modora/addons/base/models/__init__.py +75 -0
- modora/addons/base/models/auth.py +317 -0
- modora/addons/base/models/org_company.py +71 -0
- modora/addons/base/models/sys_access.py +251 -0
- modora/addons/base/models/sys_attachment.py +124 -0
- modora/addons/base/models/sys_bus.py +132 -0
- modora/addons/base/models/sys_config.py +58 -0
- modora/addons/base/models/sys_cron.py +177 -0
- modora/addons/base/models/sys_external_id.py +24 -0
- modora/addons/base/models/sys_import.py +107 -0
- modora/addons/base/models/sys_job.py +250 -0
- modora/addons/base/models/sys_lang.py +75 -0
- modora/addons/base/models/sys_model.py +72 -0
- modora/addons/base/models/sys_module.py +101 -0
- modora/addons/base/models/sys_sequence.py +158 -0
- modora/addons/base/models/sys_session.py +172 -0
- modora/addons/base/models/sys_ui.py +198 -0
- modora/addons/base/models/sys_view.py +138 -0
- modora/addons/base/security/groups.py +29 -0
- modora/addons/base/security/sys.access.csv +38 -0
- modora/addons/base/static/icons/settings.svg +7 -0
- modora/addons/contact/__init__.py +6 -0
- modora/addons/contact/__manifest__.py +17 -0
- modora/addons/contact/data/menus.py +16 -0
- modora/addons/contact/demo/contacts.py +32 -0
- modora/addons/contact/models/__init__.py +4 -0
- modora/addons/contact/models/contact.py +207 -0
- modora/addons/contact/security/sys.access.csv +4 -0
- modora/addons/contact/static/icons/app.svg +7 -0
- modora/addons/locale/__init__.py +23 -0
- modora/addons/locale/__manifest__.py +19 -0
- modora/addons/locale/data/countries.py +47 -0
- modora/addons/locale/data/currencies.py +26 -0
- modora/addons/locale/data/defaults.py +11 -0
- modora/addons/locale/data/menus.py +15 -0
- modora/addons/locale/models/__init__.py +6 -0
- modora/addons/locale/models/country.py +72 -0
- modora/addons/locale/models/currency.py +209 -0
- modora/addons/locale/models/org_company.py +29 -0
- modora/addons/locale/security/sys.access.csv +9 -0
- modora/addons/mail/__init__.py +7 -0
- modora/addons/mail/__manifest__.py +13 -0
- modora/addons/mail/data/menus.py +16 -0
- modora/addons/mail/models/__init__.py +18 -0
- modora/addons/mail/models/activity.py +101 -0
- modora/addons/mail/models/bus.py +36 -0
- modora/addons/mail/models/follower.py +21 -0
- modora/addons/mail/models/message.py +201 -0
- modora/addons/mail/models/template.py +230 -0
- modora/addons/mail/models/thread.py +283 -0
- modora/addons/mail/security/sys.access.csv +8 -0
- modora/addons/mail/transport.py +77 -0
- modora/addons/web/__init__.py +6 -0
- modora/addons/web/__manifest__.py +12 -0
- modora/addons/web/controllers/__init__.py +4 -0
- modora/addons/web/controllers/main.py +157 -0
- modora/addons/web/static/dist/THIRD-PARTY-LICENSES.txt +1356 -0
- modora/addons/web/static/dist/assets/index-0i4GsfAI.css +1 -0
- modora/addons/web/static/dist/assets/index-0i4GsfAI.css.br +0 -0
- modora/addons/web/static/dist/assets/index-0i4GsfAI.css.gz +0 -0
- modora/addons/web/static/dist/assets/index-Bk_B461G.js +1 -0
- modora/addons/web/static/dist/assets/index-CdE-3s5j.js +53 -0
- modora/addons/web/static/dist/assets/index-CdE-3s5j.js.br +0 -0
- modora/addons/web/static/dist/assets/index-CdE-3s5j.js.gz +0 -0
- modora/addons/web/static/dist/assets/index-D0nCnqYz.js +1 -0
- modora/addons/web/static/dist/assets/index-D0nCnqYz.js.br +0 -0
- modora/addons/web/static/dist/assets/index-D0nCnqYz.js.gz +0 -0
- modora/addons/web/static/dist/assets/index-cgBTXj4B.js +1 -0
- modora/addons/web/static/dist/assets/index-cgBTXj4B.js.br +0 -0
- modora/addons/web/static/dist/assets/index-cgBTXj4B.js.gz +0 -0
- modora/addons/web/static/dist/assets/jsx-runtime-u17CrQMm.js +1 -0
- modora/addons/web/static/dist/assets/sdk-DCh0ASeY.js +1 -0
- modora/addons/web/static/dist/assets/sdk-DCh0ASeY.js.br +0 -0
- modora/addons/web/static/dist/assets/sdk-DCh0ASeY.js.gz +0 -0
- modora/addons/web/static/dist/index.html +30 -0
- modora/addons/web/static/dist/modora-web.js +1 -0
- modora/addons/web/static/dist/react-dom.js +1 -0
- modora/addons/web/static/dist/react-jsx-runtime.js +1 -0
- modora/addons/web/static/dist/react.js +1 -0
- modora/addons/web/static/dist/react.js.br +0 -0
- modora/addons/web/static/dist/react.js.gz +0 -0
- modora/bus.py +195 -0
- modora/cli/__init__.py +6 -0
- modora/cli/main.py +809 -0
- modora/cli/shell.py +65 -0
- modora/cli/webclient.py +208 -0
- modora/config.py +206 -0
- modora/data.py +152 -0
- modora/db/__init__.py +7 -0
- modora/db/cursor.py +338 -0
- modora/db/errors.py +149 -0
- modora/db/functions.py +233 -0
- modora/db/listen.py +120 -0
- modora/db/manage.py +221 -0
- modora/exceptions.py +30 -0
- modora/http/__init__.py +43 -0
- modora/http/content.py +220 -0
- modora/http/errors.py +132 -0
- modora/http/request.py +256 -0
- modora/http/responses.py +57 -0
- modora/http/rest.py +377 -0
- modora/http/routing.py +310 -0
- modora/http/rpc.py +106 -0
- modora/http/serve.py +55 -0
- modora/http/server.py +601 -0
- modora/http/websocket.py +169 -0
- modora/jobs.py +400 -0
- modora/modules/__init__.py +45 -0
- modora/modules/archive.py +218 -0
- modora/modules/data_loader.py +384 -0
- modora/modules/discovery.py +277 -0
- modora/modules/loader.py +593 -0
- modora/modules/manifest.py +200 -0
- modora/modules/migrations.py +69 -0
- modora/modules/registries.py +98 -0
- modora/orm/__init__.py +9 -0
- modora/orm/api.py +95 -0
- modora/orm/client.py +391 -0
- modora/orm/commands.py +87 -0
- modora/orm/data_exchange.py +400 -0
- modora/orm/domains.py +617 -0
- modora/orm/environment.py +285 -0
- modora/orm/fields.py +1122 -0
- modora/orm/json_read.py +234 -0
- modora/orm/models.py +1861 -0
- modora/orm/registry.py +555 -0
- modora/orm/schema.py +482 -0
- modora/py.typed +0 -0
- modora/report.py +792 -0
- modora/tests/__init__.py +102 -0
- modora/tests/case.py +137 -0
- modora/tests/fixtures.py +323 -0
- modora/tests/runner.py +135 -0
- modora/tests/smtp.py +139 -0
- modora/tools/__init__.py +6 -0
- modora/tools/_py_speedups.py +48 -0
- modora/tools/expr.py +66 -0
- modora/tools/filestore.py +135 -0
- modora/tools/formatting.py +141 -0
- modora/tools/html.py +131 -0
- modora/tools/i18n.py +373 -0
- modora/tools/json.py +119 -0
- modora/tools/passwords.py +49 -0
- modora/tools/speedups.py +47 -0
- modora/tools/sql.py +32 -0
- modora/tools/tabular.py +140 -0
- modora/ui.py +329 -0
- modora/views/__init__.py +18 -0
- modora/views/definitions.py +234 -0
- modora/views/expressions.py +344 -0
- modora/views/inheritance.py +242 -0
- modora/views/validation.py +274 -0
- modora-0.1.0.dist-info/METADATA +152 -0
- modora-0.1.0.dist-info/RECORD +176 -0
- modora-0.1.0.dist-info/WHEEL +4 -0
- modora-0.1.0.dist-info/entry_points.txt +9 -0
- modora-0.1.0.dist-info/licenses/LICENSE +201 -0
- modora-0.1.0.dist-info/licenses/NOTICE +16 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Framework models of the ``base`` addon.
|
|
3
|
+
|
|
4
|
+
The module loader collects model definitions automatically; ``MODELS`` lists them in load order
|
|
5
|
+
for tests that build a registry by hand.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from modora.addons.base.models.auth import AuthGroup, AuthUser
|
|
9
|
+
from modora.addons.base.models.org_company import OrgCompany
|
|
10
|
+
from modora.addons.base.models.sys_access import SysAccess
|
|
11
|
+
from modora.addons.base.models.sys_attachment import SysAttachment
|
|
12
|
+
from modora.addons.base.models.sys_bus import SysBus
|
|
13
|
+
from modora.addons.base.models.sys_config import SysConfig
|
|
14
|
+
from modora.addons.base.models.sys_cron import SysCron
|
|
15
|
+
from modora.addons.base.models.sys_external_id import SysExternalId
|
|
16
|
+
from modora.addons.base.models.sys_import import SysImport
|
|
17
|
+
from modora.addons.base.models.sys_job import SysJob
|
|
18
|
+
from modora.addons.base.models.sys_lang import SysLang
|
|
19
|
+
from modora.addons.base.models.sys_model import SysModel, SysModelField
|
|
20
|
+
from modora.addons.base.models.sys_module import SysModule, SysModuleDependency
|
|
21
|
+
from modora.addons.base.models.sys_sequence import SysSequence
|
|
22
|
+
from modora.addons.base.models.sys_session import SysApiKey, SysSession
|
|
23
|
+
from modora.addons.base.models.sys_ui import SysAction, SysMenu
|
|
24
|
+
from modora.addons.base.models.sys_view import SysView
|
|
25
|
+
|
|
26
|
+
MODELS = [
|
|
27
|
+
AuthGroup,
|
|
28
|
+
AuthUser,
|
|
29
|
+
OrgCompany,
|
|
30
|
+
SysAccess,
|
|
31
|
+
SysAttachment,
|
|
32
|
+
SysBus,
|
|
33
|
+
SysConfig,
|
|
34
|
+
SysCron,
|
|
35
|
+
SysExternalId,
|
|
36
|
+
SysImport,
|
|
37
|
+
SysJob,
|
|
38
|
+
SysLang,
|
|
39
|
+
SysModel,
|
|
40
|
+
SysModelField,
|
|
41
|
+
SysModule,
|
|
42
|
+
SysModuleDependency,
|
|
43
|
+
SysSequence,
|
|
44
|
+
SysSession,
|
|
45
|
+
SysApiKey,
|
|
46
|
+
SysAction,
|
|
47
|
+
SysMenu,
|
|
48
|
+
SysView,
|
|
49
|
+
]
|
|
50
|
+
|
|
51
|
+
__all__ = [
|
|
52
|
+
"MODELS",
|
|
53
|
+
"AuthGroup",
|
|
54
|
+
"AuthUser",
|
|
55
|
+
"OrgCompany",
|
|
56
|
+
"SysAccess",
|
|
57
|
+
"SysAction",
|
|
58
|
+
"SysApiKey",
|
|
59
|
+
"SysAttachment",
|
|
60
|
+
"SysBus",
|
|
61
|
+
"SysConfig",
|
|
62
|
+
"SysCron",
|
|
63
|
+
"SysExternalId",
|
|
64
|
+
"SysImport",
|
|
65
|
+
"SysJob",
|
|
66
|
+
"SysLang",
|
|
67
|
+
"SysMenu",
|
|
68
|
+
"SysModel",
|
|
69
|
+
"SysModelField",
|
|
70
|
+
"SysModule",
|
|
71
|
+
"SysModuleDependency",
|
|
72
|
+
"SysSequence",
|
|
73
|
+
"SysSession",
|
|
74
|
+
"SysView",
|
|
75
|
+
]
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Users and groups.
|
|
3
|
+
|
|
4
|
+
A group may *imply* other groups (``implied_ids``): members of an administrator group are
|
|
5
|
+
members of the user group too. Access rules and field ``groups=`` see a user's groups with every
|
|
6
|
+
implied group added (:meth:`AuthUser._effective_group_ids`).
|
|
7
|
+
|
|
8
|
+
``create`` and ``write`` accept a ``password`` value: it is hashed with Argon2id into the
|
|
9
|
+
private field ``password_hash`` and never stored or returned in clear. Setting a password ends
|
|
10
|
+
the user's other sessions.
|
|
11
|
+
|
|
12
|
+
Users who may not write ``auth.user`` still edit their own preferences (:data:`SELF_WRITEABLE`)
|
|
13
|
+
through :meth:`AuthUser.update_preferences`.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from collections.abc import Mapping
|
|
19
|
+
from typing import Any
|
|
20
|
+
|
|
21
|
+
from modora import ui, view
|
|
22
|
+
from modora.exceptions import AccessError, ValidationError
|
|
23
|
+
from modora.orm import api, fields, models
|
|
24
|
+
from modora.orm.commands import Command
|
|
25
|
+
from modora.tools import passwords
|
|
26
|
+
|
|
27
|
+
#: Fields of their own user record that users may change (the profile in the web client).
|
|
28
|
+
SELF_WRITEABLE = frozenset({"name", "email", "lang", "tz", "theme"})
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class AuthGroup(models.Model):
|
|
32
|
+
_name = "auth.group"
|
|
33
|
+
_description = "User Group"
|
|
34
|
+
_order = "category, name, id"
|
|
35
|
+
|
|
36
|
+
name = fields.Char(required=True)
|
|
37
|
+
category = fields.Char(help="Groups of one category are shown together, e.g. 'Sales'.")
|
|
38
|
+
full_name = fields.Char(compute="_compute_full_name", string="Group name")
|
|
39
|
+
comment = fields.Text()
|
|
40
|
+
user_ids = fields.Many2many("auth.user", string="Users")
|
|
41
|
+
implied_ids = fields.Many2many(
|
|
42
|
+
"auth.group",
|
|
43
|
+
relation="auth_group_implied_rel",
|
|
44
|
+
column1="group_id",
|
|
45
|
+
column2="implied_id",
|
|
46
|
+
string="Implied groups",
|
|
47
|
+
help="Members of this group are members of these groups too.",
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
# memberships and implied groups decide access: the access snapshot must reload
|
|
51
|
+
def create(self, vals_list: Any) -> Any:
|
|
52
|
+
groups = super().create(vals_list)
|
|
53
|
+
_access_changed(self.env)
|
|
54
|
+
return groups
|
|
55
|
+
|
|
56
|
+
def write(self, vals: Mapping[str, Any]) -> bool:
|
|
57
|
+
if "implied_ids" in vals or "user_ids" in vals:
|
|
58
|
+
_access_changed(self.env)
|
|
59
|
+
return super().write(vals)
|
|
60
|
+
|
|
61
|
+
def unlink(self) -> bool:
|
|
62
|
+
_access_changed(self.env)
|
|
63
|
+
return super().unlink()
|
|
64
|
+
|
|
65
|
+
@api.depends("name", "category")
|
|
66
|
+
def _compute_full_name(self) -> None:
|
|
67
|
+
for group in self:
|
|
68
|
+
group.full_name = (
|
|
69
|
+
f"{group.category} / {group.name}" if group.category else (group.name or "")
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
@api.constrains("implied_ids")
|
|
73
|
+
def _check_implied_cycle(self) -> None:
|
|
74
|
+
for group in self:
|
|
75
|
+
seen: set[int] = set()
|
|
76
|
+
todo = group.implied_ids
|
|
77
|
+
while todo:
|
|
78
|
+
if group.id in todo.ids:
|
|
79
|
+
raise ValidationError(f"Group {group.full_name} implies itself.")
|
|
80
|
+
new = todo.filtered(lambda g, seen=seen: g.id not in seen)
|
|
81
|
+
seen.update(new.ids)
|
|
82
|
+
todo = new.mapped("implied_ids")
|
|
83
|
+
|
|
84
|
+
@api.private
|
|
85
|
+
def _with_implied(self) -> set[int]:
|
|
86
|
+
"""Ids of these groups and every group they imply (one query per level)."""
|
|
87
|
+
result: set[int] = set(self.ids)
|
|
88
|
+
todo = self
|
|
89
|
+
while todo:
|
|
90
|
+
implied = todo.mapped("implied_ids")
|
|
91
|
+
todo = implied.filtered(lambda g: g.id not in result)
|
|
92
|
+
result.update(todo.ids)
|
|
93
|
+
return result
|
|
94
|
+
|
|
95
|
+
@view(id="group_list", type="list")
|
|
96
|
+
def _view_list(self) -> Any:
|
|
97
|
+
return ui.list(ui.field("name"), ui.field("category"), ui.field("comment"))
|
|
98
|
+
|
|
99
|
+
@view(id="group_form", type="form")
|
|
100
|
+
def _view_form(self) -> Any:
|
|
101
|
+
return ui.form(
|
|
102
|
+
ui.sheet(
|
|
103
|
+
ui.group(ui.field("name"), ui.field("category"), ui.field("comment"), name="main"),
|
|
104
|
+
ui.notebook(
|
|
105
|
+
ui.page("Users", ui.field("user_ids", widget="many2many_tags"), name="users"),
|
|
106
|
+
ui.page("Inherited groups", ui.field("implied_ids", widget="many2many_tags"),
|
|
107
|
+
name="implied"),
|
|
108
|
+
),
|
|
109
|
+
)
|
|
110
|
+
) # fmt: skip
|
|
111
|
+
|
|
112
|
+
@view(id="group_search", type="search")
|
|
113
|
+
def _view_search(self) -> Any:
|
|
114
|
+
return ui.search(ui.field("name"), ui.field("category"), ui.group_by("category"))
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def _access_changed(env: Any) -> None:
|
|
118
|
+
if "sys.access" in env.registry:
|
|
119
|
+
env["sys.access"].sudo()._access_changed()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def _default_company(model: Any) -> Any:
|
|
123
|
+
company = model.env.company
|
|
124
|
+
return company.id if company else None
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def _default_companies(model: Any) -> Any:
|
|
128
|
+
company = model.env.company
|
|
129
|
+
return [Command.set(company.ids)] if company else []
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class AuthUser(models.Model):
|
|
133
|
+
_name = "auth.user"
|
|
134
|
+
_description = "User"
|
|
135
|
+
_order = "name, login, id"
|
|
136
|
+
|
|
137
|
+
name = fields.Char(required=True)
|
|
138
|
+
login = fields.Char(required=True, index=True)
|
|
139
|
+
email = fields.Char()
|
|
140
|
+
active = fields.Boolean(default=True)
|
|
141
|
+
lang = fields.Char(default="en_US", help="Language code of an active sys.lang record.")
|
|
142
|
+
tz = fields.Char(string="Timezone", default="UTC", help="IANA timezone, e.g. Europe/Paris.")
|
|
143
|
+
theme = fields.Selection(
|
|
144
|
+
[("system", "System"), ("light", "Light"), ("dark", "Dark")],
|
|
145
|
+
default="system",
|
|
146
|
+
help="Colour scheme of the web client; System follows the device setting.",
|
|
147
|
+
)
|
|
148
|
+
group_ids = fields.Many2many("auth.group", string="Groups")
|
|
149
|
+
company_id = fields.Many2one(
|
|
150
|
+
"org.company",
|
|
151
|
+
string="Company",
|
|
152
|
+
default=_default_company,
|
|
153
|
+
ondelete="restrict",
|
|
154
|
+
help="The company the user works in by default.",
|
|
155
|
+
)
|
|
156
|
+
company_ids = fields.Many2many(
|
|
157
|
+
"org.company",
|
|
158
|
+
string="Allowed companies",
|
|
159
|
+
default=_default_companies,
|
|
160
|
+
help="Companies whose records the user may work with.",
|
|
161
|
+
)
|
|
162
|
+
password_hash = fields.Char(private=True, copy=False)
|
|
163
|
+
|
|
164
|
+
_login_unique = models.UniqueIndex("login", message="Another user already has this login.")
|
|
165
|
+
|
|
166
|
+
@api.constrains("company_id", "company_ids")
|
|
167
|
+
def _check_company(self) -> None:
|
|
168
|
+
for user in self:
|
|
169
|
+
if user.company_id and user.company_id not in user.company_ids:
|
|
170
|
+
raise ValidationError(
|
|
171
|
+
f"User {user.name}: the default company must be one of the allowed companies."
|
|
172
|
+
)
|
|
173
|
+
|
|
174
|
+
@api.constrains("lang")
|
|
175
|
+
def _check_lang(self) -> None:
|
|
176
|
+
if "sys.lang" not in self.env:
|
|
177
|
+
return
|
|
178
|
+
codes = self.env["sys.lang"]._active_codes()
|
|
179
|
+
if not codes: # no language loaded yet (bare registries)
|
|
180
|
+
return
|
|
181
|
+
for user in self:
|
|
182
|
+
if user.lang and user.lang not in codes:
|
|
183
|
+
raise ValidationError(f"User {user.name}: language {user.lang} is not active.")
|
|
184
|
+
|
|
185
|
+
@api.constrains("tz")
|
|
186
|
+
def _check_tz(self) -> None:
|
|
187
|
+
from zoneinfo import available_timezones
|
|
188
|
+
|
|
189
|
+
zones: set[str] | None = None
|
|
190
|
+
for user in self:
|
|
191
|
+
if user.tz and user.tz != "UTC":
|
|
192
|
+
zones = zones if zones is not None else available_timezones()
|
|
193
|
+
if user.tz not in zones:
|
|
194
|
+
raise ValidationError(f"User {user.name}: unknown timezone {user.tz!r}.")
|
|
195
|
+
|
|
196
|
+
@api.private
|
|
197
|
+
def _effective_group_ids(self) -> set[int]:
|
|
198
|
+
"""Ids of the user's groups, with every implied group."""
|
|
199
|
+
self.ensure_one()
|
|
200
|
+
return self.group_ids._with_implied()
|
|
201
|
+
|
|
202
|
+
@api.private
|
|
203
|
+
def has_group(self, xml_id: str) -> bool:
|
|
204
|
+
"""Whether the user is a member (directly or through implied groups) of a group."""
|
|
205
|
+
self.ensure_one()
|
|
206
|
+
group = self.env.ref(xml_id, raise_if_not_found=False)
|
|
207
|
+
return bool(group) and group.id in self.sudo()._effective_group_ids()
|
|
208
|
+
|
|
209
|
+
# ------------------------------------------------------------ passwords
|
|
210
|
+
def create(self, vals_list: Any) -> Any:
|
|
211
|
+
secrets = [vals.get("password") for vals in vals_list]
|
|
212
|
+
if any(secrets):
|
|
213
|
+
vals_list = [{k: v for k, v in vals.items() if k != "password"} for vals in vals_list]
|
|
214
|
+
users = super().create(vals_list)
|
|
215
|
+
_access_changed(self.env)
|
|
216
|
+
for user, secret in zip(users, secrets, strict=True):
|
|
217
|
+
if secret:
|
|
218
|
+
user._set_password(secret)
|
|
219
|
+
return users
|
|
220
|
+
|
|
221
|
+
def write(self, vals: Mapping[str, Any]) -> bool:
|
|
222
|
+
if "group_ids" in vals or "active" in vals:
|
|
223
|
+
_access_changed(self.env)
|
|
224
|
+
if "password" not in vals:
|
|
225
|
+
return super().write(vals)
|
|
226
|
+
others = {k: v for k, v in vals.items() if k != "password"}
|
|
227
|
+
self.check_access("write")
|
|
228
|
+
result = super().write(others) if others else True
|
|
229
|
+
self._set_password(vals["password"])
|
|
230
|
+
return result
|
|
231
|
+
|
|
232
|
+
@api.private
|
|
233
|
+
def _set_password(self, password: str | None, keep_sessions: list[int] | None = None) -> None:
|
|
234
|
+
"""Store the hash of ``password`` (``None``/``False``: no password, so no login) and end
|
|
235
|
+
the users' sessions except ``keep_sessions``. One hash per user (each is salted)."""
|
|
236
|
+
if password is not None and password is not False and not isinstance(password, str):
|
|
237
|
+
raise ValidationError("A password must be text.")
|
|
238
|
+
for user in self:
|
|
239
|
+
hashed = passwords.hash_password(password) if password else None
|
|
240
|
+
user.sudo().write({"password_hash": hashed})
|
|
241
|
+
if "sys.session" in self.env:
|
|
242
|
+
self.env["sys.session"]._revoke(self.ids, keep=keep_sessions)
|
|
243
|
+
|
|
244
|
+
@api.private
|
|
245
|
+
def _authenticate(self, login: str, password: str) -> int:
|
|
246
|
+
"""The id of the active user with ``login`` and ``password``; raises
|
|
247
|
+
:class:`AccessError` otherwise (with the same message and timing for both cases)."""
|
|
248
|
+
user = self.sudo().search_fetch([("login", "=", login)], ["password_hash"], limit=1)
|
|
249
|
+
valid, new_hash = passwords.verify(user.password_hash if user else None, password)
|
|
250
|
+
if not (user and valid):
|
|
251
|
+
raise AccessError("Wrong login or password.")
|
|
252
|
+
if new_hash:
|
|
253
|
+
user.sudo().write({"password_hash": new_hash})
|
|
254
|
+
return user.id
|
|
255
|
+
|
|
256
|
+
@api.model
|
|
257
|
+
def update_preferences(self, values: Mapping[str, Any]) -> bool:
|
|
258
|
+
"""Change the current user's own preferences (only :data:`SELF_WRITEABLE` fields), also
|
|
259
|
+
when the user may not write users."""
|
|
260
|
+
if not isinstance(values, Mapping):
|
|
261
|
+
raise ValidationError("Preferences must be an object.")
|
|
262
|
+
refused = sorted(set(values) - SELF_WRITEABLE)
|
|
263
|
+
if refused:
|
|
264
|
+
raise AccessError(f"You cannot change {', '.join(refused)} of your user.")
|
|
265
|
+
if values:
|
|
266
|
+
self.env.user.sudo().write(dict(values))
|
|
267
|
+
return True
|
|
268
|
+
|
|
269
|
+
@api.model
|
|
270
|
+
def change_password(self, old_password: str, new_password: str) -> bool:
|
|
271
|
+
"""Change the current user's password; other sessions of the user end."""
|
|
272
|
+
user = self.env.user
|
|
273
|
+
self._authenticate(user.login, old_password)
|
|
274
|
+
if not new_password or not isinstance(new_password, str):
|
|
275
|
+
raise ValidationError("The new password cannot be empty.")
|
|
276
|
+
keep = self.env.context.get("session_id")
|
|
277
|
+
user._set_password(new_password, keep_sessions=[keep] if keep else None)
|
|
278
|
+
return True
|
|
279
|
+
|
|
280
|
+
@view(id="user_list", type="list")
|
|
281
|
+
def _view_list(self) -> Any:
|
|
282
|
+
return ui.list(
|
|
283
|
+
ui.field("name"), ui.field("login"), ui.field("email"), ui.field("company_id"),
|
|
284
|
+
default_order="name",
|
|
285
|
+
) # fmt: skip
|
|
286
|
+
|
|
287
|
+
@view(id="user_form", type="form")
|
|
288
|
+
def _view_form(self) -> Any:
|
|
289
|
+
return ui.form(
|
|
290
|
+
ui.sheet(
|
|
291
|
+
ui.group(
|
|
292
|
+
ui.group(ui.field("name"), ui.field("login"), ui.field("email"), name="identity"),
|
|
293
|
+
ui.group(ui.field("company_id"), ui.field("lang"), ui.field("tz"),
|
|
294
|
+
ui.field("theme"), ui.field("active"), name="preferences"),
|
|
295
|
+
),
|
|
296
|
+
ui.notebook(
|
|
297
|
+
ui.page("Access rights",
|
|
298
|
+
ui.group(ui.field("group_ids", widget="many2many_tags"), name="groups"),
|
|
299
|
+
name="access"),
|
|
300
|
+
ui.page("Companies",
|
|
301
|
+
ui.group(ui.field("company_ids", widget="many2many_tags"),
|
|
302
|
+
name="companies"),
|
|
303
|
+
name="companies"),
|
|
304
|
+
),
|
|
305
|
+
)
|
|
306
|
+
) # fmt: skip
|
|
307
|
+
|
|
308
|
+
@view(id="user_search", type="search")
|
|
309
|
+
def _view_search(self) -> Any:
|
|
310
|
+
return ui.search(
|
|
311
|
+
ui.field(
|
|
312
|
+
"name", filter_domain="['|', ('name', 'ilike', self), ('login', 'ilike', self)]"
|
|
313
|
+
),
|
|
314
|
+
ui.field("email"),
|
|
315
|
+
ui.filter("archived", "Archived", domain=[("active", "=", False)]),
|
|
316
|
+
ui.group_by("company_id"),
|
|
317
|
+
)
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
"""Companies: the legal entities a database works for (one or many)."""
|
|
3
|
+
|
|
4
|
+
from __future__ import annotations
|
|
5
|
+
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
from modora import ui, view
|
|
9
|
+
from modora.exceptions import ValidationError
|
|
10
|
+
from modora.orm import api, fields, models
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class OrgCompany(models.Model):
|
|
14
|
+
_name = "org.company"
|
|
15
|
+
_description = "Company"
|
|
16
|
+
_order = "sequence, name, id"
|
|
17
|
+
|
|
18
|
+
name = fields.Char(required=True)
|
|
19
|
+
active = fields.Boolean(default=True)
|
|
20
|
+
sequence = fields.Integer(default=10)
|
|
21
|
+
parent_id = fields.Many2one("org.company", string="Parent company", ondelete="restrict")
|
|
22
|
+
child_ids = fields.One2many("org.company", "parent_id", string="Branches")
|
|
23
|
+
email = fields.Char()
|
|
24
|
+
phone = fields.Char()
|
|
25
|
+
website = fields.Char()
|
|
26
|
+
company_registry = fields.Char(string="Company registry")
|
|
27
|
+
logo = fields.Binary(attachment=True)
|
|
28
|
+
user_ids = fields.Many2many("auth.user", string="Users")
|
|
29
|
+
|
|
30
|
+
_name_unique = models.UniqueIndex("name", message="Another company already has this name.")
|
|
31
|
+
|
|
32
|
+
@api.constrains("parent_id")
|
|
33
|
+
def _check_parent(self) -> None:
|
|
34
|
+
for company in self:
|
|
35
|
+
parent = company.parent_id
|
|
36
|
+
seen = {company.id}
|
|
37
|
+
while parent:
|
|
38
|
+
if parent.id in seen:
|
|
39
|
+
raise ValidationError(f"Company {company.name} cannot be its own parent.")
|
|
40
|
+
seen.add(parent.id)
|
|
41
|
+
parent = parent.parent_id
|
|
42
|
+
|
|
43
|
+
@view(id="company_list", type="list")
|
|
44
|
+
def _view_list(self) -> Any:
|
|
45
|
+
return ui.list(
|
|
46
|
+
ui.field("sequence", widget="handle"), ui.field("name"), ui.field("parent_id"),
|
|
47
|
+
ui.field("email"), ui.field("phone"),
|
|
48
|
+
) # fmt: skip
|
|
49
|
+
|
|
50
|
+
@view(id="company_form", type="form")
|
|
51
|
+
def _view_form(self) -> Any:
|
|
52
|
+
return ui.form(
|
|
53
|
+
ui.sheet(
|
|
54
|
+
ui.group(
|
|
55
|
+
ui.group(ui.field("name"), ui.field("parent_id"), ui.field("company_registry"),
|
|
56
|
+
name="main"),
|
|
57
|
+
ui.group(ui.field("logo", widget="image"), ui.field("email"),
|
|
58
|
+
ui.field("phone"), ui.field("website"), name="contact"),
|
|
59
|
+
),
|
|
60
|
+
ui.notebook(
|
|
61
|
+
ui.page("Branches",
|
|
62
|
+
ui.field("child_ids", ui.list(ui.field("name"), ui.field("email")),
|
|
63
|
+
nolabel=True),
|
|
64
|
+
name="branches"),
|
|
65
|
+
),
|
|
66
|
+
)
|
|
67
|
+
) # fmt: skip
|
|
68
|
+
|
|
69
|
+
@view(id="company_search", type="search")
|
|
70
|
+
def _view_search(self) -> Any:
|
|
71
|
+
return ui.search(ui.field("name"), ui.field("email"), ui.group_by("parent_id"))
|