-
Notifications
You must be signed in to change notification settings - Fork 10
fix(starter_sp_mis): make registry access control persist and enforce #411
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
655e321
fix(starter_sp_mis): make registry access control persist and enforce
emjay0921 915b119
Merge remote-tracking branch 'origin/19.0' into fix/1142-spmis-regist…
emjay0921 1bf76f6
chore(starter_sp_mis): justify the sudo() calls for semgrep
emjay0921 9a3a9a3
fix(starter_sp_mis): make the noupdate move real and close the promot…
emjay0921 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,2 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| from . import controllers | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
spp_starter_sp_mis/migrations/19.0.2.1.0/post-migration.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| """Stop re-locking the registry on every upgrade (OP#1142). | ||
|
|
||
| ``config_registry_admin_only_crud`` is now declared ``noupdate="1"`` so an | ||
| administrator's choice survives module upgrades. That declaration only governs | ||
| xml_ids created from now on: ``_build_update_xmlids_query`` upserts with | ||
| ``ON CONFLICT … DO UPDATE SET (model, res_id, write_date)`` and never touches | ||
| the stored ``noupdate`` flag of an existing row. So every database that already | ||
| carries this xml_id keeps ``noupdate = false``, and each upgrade re-applies | ||
| ``value = True`` — silently re-locking a registry that was deliberately opened, | ||
| which is the bug this module set out to fix. | ||
|
|
||
| The flag has to be flipped in the row itself, once. | ||
| """ | ||
|
|
||
| import logging | ||
|
|
||
| _logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def migrate(cr, version): | ||
| if not version: | ||
| return | ||
|
|
||
| cr.execute( | ||
| """ | ||
| UPDATE ir_model_data | ||
| SET noupdate = true | ||
| WHERE module = 'spp_starter_sp_mis' | ||
| AND name = 'config_registry_admin_only_crud' | ||
| AND noupdate IS NOT TRUE | ||
| """ | ||
| ) | ||
| if cr.rowcount: | ||
| _logger.info("Marked config_registry_admin_only_crud noupdate; the setting now survives upgrades") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
| from . import res_config_settings | ||
| from . import res_partner |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| # Part of OpenSPP. See LICENSE file for full copyright and licensing details. | ||
|
|
||
| from odoo import _, models | ||
| from odoo.exceptions import AccessError | ||
|
|
||
| from .res_config_settings import REGISTRY_ADMIN_ONLY_CRUD_PARAM | ||
|
|
||
| ADMIN_GROUP = "spp_security.group_spp_admin" | ||
|
|
||
| # Operations the setting withholds from non-admins. ``read`` is deliberately | ||
| # absent: the whole point is that everyone keeps visibility of the registry. | ||
| RESTRICTED_OPERATIONS = ("create", "write", "unlink") | ||
|
|
||
|
|
||
| class ResPartner(models.Model): | ||
| _inherit = "res.partner" | ||
|
|
||
| def _is_registry_crud_restricted(self): | ||
| """Whether registry changes are currently withheld from this user.""" | ||
| if self.env.user.has_group(ADMIN_GROUP): | ||
| return False | ||
| # Reads one system setting in order to decide whether to *withhold* | ||
| # access. Every user has to be able to read it, and it exposes nothing | ||
| # beyond the flag itself. | ||
| # nosemgrep: odoo-sudo-without-context | ||
| param = self.env["ir.config_parameter"].sudo().get_param(REGISTRY_ADMIN_ONLY_CRUD_PARAM, "False") | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| return param == "True" | ||
|
|
||
| def _make_registry_access_error(self): | ||
| return AccessError( | ||
| _( | ||
| "Registry records are restricted to administrators. Ask an administrator to make " | ||
| "this change, or turn off 'Restrict Registry Edits to Admin Only' in SP-MIS Settings." | ||
| ) | ||
| ) | ||
|
|
||
| def write(self, vals): | ||
| """Refuse promoting a plain contact into the registry (OP#1142 review). | ||
|
|
||
| ``_check_access('write')`` filters on the record's *current* values, and | ||
| there is no post-write pass, so a restricted user could create a plain | ||
| contact and then flip ``is_registrant`` on it — two allowed steps adding | ||
| up to a registrant they were never allowed to create. The same move | ||
| promotes any existing contact. Unflagging needs no guard: the record is | ||
| already a registrant when the check runs. | ||
| """ | ||
| if vals.get("is_registrant") and self._is_registry_crud_restricted(): | ||
| # Reading the current flag to find what is being promoted; filtering | ||
| # as the user would recurse back into the access check. | ||
| # nosemgrep: odoo-sudo-without-context | ||
| promoted = self.sudo().filtered(lambda partner: not partner.is_registrant) | ||
| if promoted: | ||
| raise self._make_registry_access_error() | ||
| return super().write(vals) | ||
|
|
||
| def _check_access(self, operation): | ||
| """Withhold registrant create/write/unlink from non-admins (OP#1142). | ||
|
|
||
| This is the single chokepoint behind ``check_access``, ``has_access`` | ||
| and the ORM's own create/write/unlink guards, so one override both | ||
| refuses the change — over RPC and import as much as through the UI — | ||
| and takes New/Edit/Delete off registry views for free, because | ||
| ``ir.ui.view._postprocess_access_rights`` stamps ``create="false"`` onto | ||
| an arch whenever ``has_access('create')`` comes back False. That is what | ||
| actually removes the button; the previous JavaScript patch assigned a | ||
| ``canCreate`` property Odoo 19's ListController never reads. | ||
|
|
||
| Enforcement is scoped to registrants so the setting cannot lock the | ||
| whole Contacts app. On a populated recordset that is a plain filter, and | ||
| it is the check the ORM applies to real writes — including the | ||
| post-create pass — so it holds regardless of how the call arrives. | ||
|
|
||
| The empty recordset is the model-level probe, used by views and by | ||
| ``create()`` before any record exists. There is nothing to filter there, | ||
| so registrant intent is read from the context both registry actions | ||
| carry. Note that ``get_view`` documents its result as depending only on | ||
| access rights and a few context keys, so should that ever stop flowing, | ||
| the button reappears but the refusal above still stands — the failure | ||
| mode is cosmetic, not a loss of enforcement. | ||
| """ | ||
| result = super()._check_access(operation) | ||
| if result is not None or operation not in RESTRICTED_OPERATIONS: | ||
| return result | ||
| if not self._is_registry_crud_restricted(): | ||
| return None | ||
|
|
||
| if self: | ||
| # Reads is_registrant to decide what to refuse. Filtering as the | ||
| # user would recurse straight back into this check, and nothing is | ||
| # returned but ids the caller already holds. | ||
| # nosemgrep: odoo-sudo-without-context | ||
| forbidden = self.browse(self.sudo().filtered("is_registrant").ids) | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| if not forbidden: | ||
| return None | ||
| elif self.env.context.get("default_is_registrant"): | ||
| forbidden = self | ||
| else: | ||
| return None | ||
|
|
||
| return forbidden, self._make_registry_access_error | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,7 @@ | ||
| ### 19.0.2.1.0 | ||
|
|
||
| - fix(starter_sp_mis): make the registry restriction hold and stop it re-locking itself. Enforcement moves from a JavaScript patch Odoo 19 no longer reads to the access check every create, write and delete passes through, so it applies over RPC and data import too; promoting a plain contact into the registry is refused as well. The setting is marked `noupdate`, with a migration for databases where an upgrade would otherwise keep switching it back on (#1142) | ||
|
|
||
| ### 19.0.2.0.0 | ||
|
|
||
| - Initial migration to OpenSPP2 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.