Skip to content

Commit 0ea2193

Browse files
authored
Adding SecureComponent to policy details page (#1251)
- Make Users & Policies clickeable only if user has permissions to view in bucket page - Add SecureComponent to policy detail page: api calls, menu options, raw policy editor, etc. - Add missing click action to groups in policy detail page - Fix NPE in list groups for policy endpoint - Added SecureComponent to ListPolicies page Signed-off-by: Lenin Alevski <alevsk.8772@gmail.com>
1 parent 06b0859 commit 0ea2193

File tree

5 files changed

+268
-124
lines changed

5 files changed

+268
-124
lines changed

portal-ui/src/common/SecureComponent/permissions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ export const IAM_SCOPES = {
117117
ADMIN_GET_GROUP: "admin:GetGroup",
118118
ADMIN_ENABLE_GROUP: "admin:EnableGroup",
119119
ADMIN_DISABLE_GROUP: "admin:DisableGroup",
120+
ADMIN_GET_USER: "admin:GetUser",
121+
ADMIN_CREATE_POLICY: "admin:CreatePolicy",
122+
ADMIN_DELETE_POLICY: "admin:DeletePolicy",
123+
ADMIN_ATTACH_USER_OR_GROUP_POLICY: "admin:AttachUserOrGroupPolicy",
120124
S3_ALL_ACTIONS: "s3:*",
121125
ADMIN_ALL_ACTIONS: "admin:*",
122126
};

portal-ui/src/screens/Console/Buckets/BucketDetails/AccessDetailsPanel.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import TableWrapper from "../../Common/TableWrapper/TableWrapper";
3030
import api from "../../../../common/api";
3131
import history from "../../../../history";
3232
import { BucketInfo } from "../types";
33-
import { IAM_SCOPES } from "../../../../common/SecureComponent/permissions";
33+
import {
34+
CONSOLE_UI_RESOURCE,
35+
IAM_SCOPES,
36+
} from "../../../../common/SecureComponent/permissions";
3437
import PanelTitle from "../../Common/PanelTitle/PanelTitle";
3538
import SecureComponent, {
3639
hasPermission,
@@ -90,6 +93,15 @@ const AccessDetails = ({
9093
true
9194
);
9295

96+
const viewUser = hasPermission(CONSOLE_UI_RESOURCE, [
97+
IAM_SCOPES.ADMIN_GET_USER,
98+
]);
99+
const viewPolicy = hasPermission(CONSOLE_UI_RESOURCE, [
100+
IAM_SCOPES.ADMIN_GET_POLICY,
101+
IAM_SCOPES.ADMIN_LIST_USERS,
102+
IAM_SCOPES.ADMIN_LIST_GROUPS,
103+
]);
104+
93105
useEffect(() => {
94106
if (loadingBucket) {
95107
setLoadingUsers(true);
@@ -100,6 +112,7 @@ const AccessDetails = ({
100112
const PolicyActions = [
101113
{
102114
type: "view",
115+
disableButtonFunction: () => !viewPolicy,
103116
onClick: (policy: any) => {
104117
history.push(`/policies/${policy.name}`);
105118
},
@@ -109,6 +122,7 @@ const AccessDetails = ({
109122
const userTableActions = [
110123
{
111124
type: "view",
125+
disableButtonFunction: () => !viewUser,
112126
onClick: (user: any) => {
113127
history.push(`/users/${user}`);
114128
},

portal-ui/src/screens/Console/Policies/ListPolicies.tsx

Lines changed: 84 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ import history from "../../../history";
4242
import SearchIcon from "../../../icons/SearchIcon";
4343
import HelpBox from "../../../common/HelpBox";
4444
import PageLayout from "../Common/Layout/PageLayout";
45+
import {
46+
CONSOLE_UI_RESOURCE,
47+
IAM_SCOPES,
48+
} from "../../../common/SecureComponent/permissions";
49+
import SecureComponent, {
50+
hasPermission,
51+
} from "../../../common/SecureComponent/SecureComponent";
4552

4653
const styles = (theme: Theme) =>
4754
createStyles({
@@ -91,38 +98,54 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
9198
const [filterPolicies, setFilterPolicies] = useState<string>("");
9299
const [policyEdit, setPolicyEdit] = useState<any>(null);
93100

101+
const viewPolicy = hasPermission(CONSOLE_UI_RESOURCE, [
102+
IAM_SCOPES.ADMIN_GET_POLICY,
103+
]);
104+
105+
const deletePolicy = hasPermission(CONSOLE_UI_RESOURCE, [
106+
IAM_SCOPES.ADMIN_DELETE_POLICY,
107+
]);
108+
109+
const displayPolicies = hasPermission(CONSOLE_UI_RESOURCE, [
110+
IAM_SCOPES.ADMIN_LIST_USER_POLICIES,
111+
]);
112+
94113
useEffect(() => {
95114
fetchRecords();
96115
}, []);
97116

98117
useEffect(() => {
99118
if (loading) {
100-
api
101-
.invoke("GET", `/api/v1/policies`)
102-
.then((res: PolicyList) => {
103-
const policies = get(res, "policies", []);
119+
if (displayPolicies) {
120+
api
121+
.invoke("GET", `/api/v1/policies`)
122+
.then((res: PolicyList) => {
123+
const policies = get(res, "policies", []);
104124

105-
policies.sort((pa, pb) => {
106-
if (pa.name > pb.name) {
107-
return 1;
108-
}
125+
policies.sort((pa, pb) => {
126+
if (pa.name > pb.name) {
127+
return 1;
128+
}
109129

110-
if (pa.name < pb.name) {
111-
return -1;
112-
}
130+
if (pa.name < pb.name) {
131+
return -1;
132+
}
113133

114-
return 0;
115-
});
134+
return 0;
135+
});
116136

117-
setLoading(false);
118-
setRecords(policies);
119-
})
120-
.catch((err: ErrorResponseHandler) => {
121-
setLoading(false);
122-
setErrorSnackMessage(err);
123-
});
137+
setLoading(false);
138+
setRecords(policies);
139+
})
140+
.catch((err: ErrorResponseHandler) => {
141+
setLoading(false);
142+
setErrorSnackMessage(err);
143+
});
144+
} else {
145+
setLoading(false);
146+
}
124147
}
125-
}, [loading, setLoading, setRecords, setErrorSnackMessage]);
148+
}, [loading, setLoading, setRecords, setErrorSnackMessage, displayPolicies]);
126149

127150
const fetchRecords = () => {
128151
setLoading(true);
@@ -154,8 +177,17 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
154177
};
155178

156179
const tableActions = [
157-
{ type: "view", onClick: viewAction },
158-
{ type: "delete", onClick: confirmDeletePolicy, sendOnlyId: true },
180+
{
181+
type: "view",
182+
onClick: viewAction,
183+
disableButtonFunction: () => !viewPolicy,
184+
},
185+
{
186+
type: "delete",
187+
onClick: confirmDeletePolicy,
188+
sendOnlyId: true,
189+
disableButtonFunction: () => !deletePolicy,
190+
},
159191
];
160192

161193
const filteredRecords = records.filter((elementItem) =>
@@ -199,30 +231,41 @@ const ListPolicies = ({ classes, setErrorSnackMessage }: IPoliciesProps) => {
199231
}}
200232
variant="standard"
201233
/>
202-
<Button
203-
variant="contained"
204-
color="primary"
205-
endIcon={<AddIcon />}
206-
onClick={() => {
207-
setAddScreenOpen(true);
208-
setPolicyEdit(null);
209-
}}
234+
<SecureComponent
235+
scopes={[IAM_SCOPES.ADMIN_CREATE_POLICY]}
236+
resource={CONSOLE_UI_RESOURCE}
210237
>
211-
Create Policy
212-
</Button>
238+
<Button
239+
variant="contained"
240+
color="primary"
241+
endIcon={<AddIcon />}
242+
onClick={() => {
243+
setAddScreenOpen(true);
244+
setPolicyEdit(null);
245+
}}
246+
>
247+
Create Policy
248+
</Button>
249+
</SecureComponent>
213250
</Grid>
214251
<Grid item xs={12}>
215252
<br />
216253
</Grid>
217254
<Grid item xs={12}>
218-
<TableWrapper
219-
itemActions={tableActions}
220-
columns={[{ label: "Name", elementKey: "name" }]}
221-
isLoading={loading}
222-
records={filteredRecords}
223-
entityName="Policies"
224-
idField="name"
225-
/>
255+
<SecureComponent
256+
scopes={[IAM_SCOPES.ADMIN_LIST_USER_POLICIES]}
257+
resource={CONSOLE_UI_RESOURCE}
258+
errorProps={{ disabled: true }}
259+
>
260+
<TableWrapper
261+
itemActions={tableActions}
262+
columns={[{ label: "Name", elementKey: "name" }]}
263+
isLoading={loading}
264+
records={filteredRecords}
265+
entityName="Policies"
266+
idField="name"
267+
/>
268+
</SecureComponent>
226269
</Grid>
227270
<Grid item xs={12}>
228271
<HelpBox

0 commit comments

Comments
 (0)