Skip to content

Commit 9610edb

Browse files
authored
Repository edit: respect object permissions (#3628)
previously the permission check for rendering the edit form would happen before loading the repository, so we only used the global permissions .. postponing check until item has been loaded Issue: AAH-2305
1 parent b605619 commit 9610edb

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

CHANGES/2305.bug

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Fix Add/Remove collection ignoring repository object permissions
1+
Fix Add/Remove/Edit collection ignoring repository object permissions

src/components/page/page.tsx

+45-35
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,27 @@ export const Page = function <
102102
}
103103

104104
componentDidMount() {
105-
if (!condition(this.context)) {
106-
this.setState({ loading: false, unauthorised: true });
107-
} else {
108-
this.query();
109-
}
110-
111-
this.setState({ alerts: this.context.alerts || [] });
112-
this.context.setAlerts([]);
113-
114-
if (didMount) {
115-
didMount({
116-
context: this.context,
117-
addAlert: (alert) => this.addAlert(alert),
118-
});
119-
}
105+
// condition check after query, for object permissions
106+
this.query().then((item) => {
107+
const actionContext = {
108+
...this.context,
109+
hasObjectPermission: (permission) =>
110+
item?.my_permissions?.includes?.(permission),
111+
};
112+
if (!condition(actionContext)) {
113+
this.setState({ loading: false, unauthorised: true });
114+
}
115+
116+
this.setState({ alerts: this.context.alerts || [] });
117+
this.context.setAlerts([]);
118+
119+
if (didMount) {
120+
didMount({
121+
context: this.context,
122+
addAlert: (alert) => this.addAlert(alert),
123+
});
124+
}
125+
});
120126
}
121127

122128
render() {
@@ -193,29 +199,33 @@ export const Page = function <
193199

194200
if (!name) {
195201
this.setState({ loading: false });
196-
return;
202+
return Promise.resolve(null);
197203
}
198204

199-
this.setState({ loading: true }, () => {
200-
query({ name })
201-
.then((item) => {
202-
this.setState({
203-
item,
204-
loading: false,
205-
});
206-
})
207-
.catch((e) => {
208-
const { status, statusText } = e.response;
209-
this.setState({
210-
loading: false,
211-
item: null,
212-
});
213-
this.addAlert({
214-
title: errorTitle,
215-
variant: 'danger',
216-
description: errorMessage(status, statusText),
205+
return new Promise((resolve, reject) => {
206+
this.setState({ loading: true }, () => {
207+
query({ name })
208+
.then((item) => {
209+
this.setState({
210+
item,
211+
loading: false,
212+
});
213+
resolve(item);
214+
})
215+
.catch((e) => {
216+
const { status, statusText } = e.response;
217+
this.setState({
218+
loading: false,
219+
item: null,
220+
});
221+
this.addAlert({
222+
title: errorTitle,
223+
variant: 'danger',
224+
description: errorMessage(status, statusText),
225+
});
226+
reject();
217227
});
218-
});
228+
});
219229
});
220230
}
221231

0 commit comments

Comments
 (0)