Skip to content
Open
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
29 changes: 26 additions & 3 deletions spp_starter_sp_mis/static/src/js/registry_restriction.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ fetchRestriction();
// Actions to strip from the action menu when restricted
const BLOCKED_ACTIONS = ["delete", "archive", "unarchive", "duplicate"];

function bypassRegistryRestriction(component) {
const context = component.props?.context || {};
return context.bypass_registry_admin_only_crud === true;
}

/**
* Patch FormController to enforce read-only on registry forms when restricted.
*
Expand All @@ -69,6 +74,10 @@ const BLOCKED_ACTIONS = ["delete", "archive", "unarchive", "duplicate"];
patch(FormController.prototype, {
setup() {
const modelName = this.props.resModel;
if (REGISTRY_MODELS.includes(modelName) && bypassRegistryRestriction(this)) {
super.setup(...arguments);
return;
}
// Check synchronous cache BEFORE super.setup() creates the model
this._registryRestricted =
REGISTRY_MODELS.includes(modelName) && _restrictionResult === true;
Expand Down Expand Up @@ -153,7 +162,10 @@ patch(FormController.prototype, {
get modelParams() {
const params = super.modelParams;
// Force readonly mode when registry is restricted
if (this._registryRestricted) {
if (
this._registryRestricted &&
!bypassRegistryRestriction(this)
) {
params.config.mode = "readonly";
}
Comment on lines +165 to 170

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this._registryRestricted is already set to false when the restriction is bypassed, we don't need to check !bypassRegistryRestriction(this) here. We can revert this to the simpler original check.

        if (this._registryRestricted) {
            params.config.mode = "readonly";
        }

return params;
Expand All @@ -162,7 +174,11 @@ patch(FormController.prototype, {
get actionMenuItems() {
const menuItems = super.actionMenuItems;

if (this._registryRestricted && REGISTRY_MODELS.includes(this.props.resModel)) {
if (
this._registryRestricted &&
REGISTRY_MODELS.includes(this.props.resModel) &&
!bypassRegistryRestriction(this)
) {
Comment on lines +177 to +181

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this._registryRestricted is already set to false when the restriction is bypassed, we don't need to check !bypassRegistryRestriction(this) here. We can revert this to the simpler original check.

        if (this._registryRestricted && REGISTRY_MODELS.includes(this.props.resModel)) {

if (menuItems.action) {
menuItems.action = menuItems.action.filter(
(item) => !BLOCKED_ACTIONS.includes(item.key)
Expand All @@ -185,6 +201,9 @@ patch(ListController.prototype, {
if (!REGISTRY_MODELS.includes(modelName)) {
return;
}
if (bypassRegistryRestriction(this)) {
return;
}
Comment on lines 201 to +206

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

We can combine these two if statements into a single, cleaner condition.

Suggested change
if (!REGISTRY_MODELS.includes(modelName)) {
return;
}
if (bypassRegistryRestriction(this)) {
return;
}
if (!REGISTRY_MODELS.includes(modelName) || bypassRegistryRestriction(this)) {
return;
}


this._registryRestricted = false;
this._restrictionObserver = null;
Expand Down Expand Up @@ -247,7 +266,11 @@ patch(ListController.prototype, {
get actionMenuItems() {
const menuItems = super.actionMenuItems;

if (this._registryRestricted && REGISTRY_MODELS.includes(this.props.resModel)) {
if (
this._registryRestricted &&
REGISTRY_MODELS.includes(this.props.resModel) &&
!bypassRegistryRestriction(this)
) {
Comment on lines +269 to +273

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since we return early in setup() when bypassRegistryRestriction(this) is true, this._registryRestricted will remain undefined (falsy). Therefore, we don't need to check !bypassRegistryRestriction(this) here. We can revert this to the simpler original check.

        if (this._registryRestricted && REGISTRY_MODELS.includes(this.props.resModel)) {

if (menuItems.action) {
menuItems.action = menuItems.action.filter(
(item) => !BLOCKED_ACTIONS.includes(item.key)
Expand Down