-
Notifications
You must be signed in to change notification settings - Fork 261
/
SetPolicy.tsx
211 lines (195 loc) · 6.02 KB
/
SetPolicy.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// This file is part of MinIO Console Server
// Copyright (c) 2021 MinIO, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
import React, { useEffect, useState, Fragment } from "react";
import get from "lodash/get";
import { Theme } from "@mui/material/styles";
import createStyles from "@mui/styles/createStyles";
import withStyles from "@mui/styles/withStyles";
import { Button, LinearProgress } from "@mui/material";
import Grid from "@mui/material/Grid";
import {
modalBasic,
spacingUtils,
tableStyles,
} from "../Common/FormComponents/common/styleLibrary";
import { User } from "../Users/types";
import { ErrorResponseHandler } from "../../../common/types";
import ModalWrapper from "../Common/ModalWrapper/ModalWrapper";
import api from "../../../common/api";
import PolicySelectors from "./PolicySelectors";
import PredefinedList from "../Common/FormComponents/PredefinedList/PredefinedList";
import { encodeURLString } from "../../../common/utils";
import { setModalErrorSnackMessage } from "../../../systemSlice";
import { AppState, useAppDispatch } from "../../../store";
import { useSelector } from "react-redux";
interface ISetPolicyProps {
classes: any;
closeModalAndRefresh: () => void;
selectedUser: User | null;
selectedGroups: string[] | null;
open: boolean;
}
const styles = (theme: Theme) =>
createStyles({
...modalBasic,
...spacingUtils,
tableBlock: {
...tableStyles.tableBlock,
marginTop: 15,
},
buttonContainer: {
textAlign: "right",
marginTop: ".9rem",
},
});
const SetPolicy = ({
classes,
closeModalAndRefresh,
selectedUser,
selectedGroups,
open,
}: ISetPolicyProps) => {
const dispatch = useAppDispatch();
//Local States
const [loading, setLoading] = useState<boolean>(false);
const [actualPolicy, setActualPolicy] = useState<string[]>([]);
const [selectedPolicy, setSelectedPolicy] = useState<string[]>([]);
const currentPolicies = useSelector(
(state: AppState) => state.createUser.selectedPolicies
);
const setPolicyAction = () => {
let users = null;
let groups = null;
if (selectedGroups !== null) {
groups = selectedGroups;
} else {
if (selectedUser !== null) {
users = [selectedUser.accessKey] || [" "];
}
}
setLoading(true);
api
.invoke("PUT", `/api/v1/set-policy-multi`, {
name: currentPolicies,
groups: groups,
users: users,
})
.then(() => {
setLoading(false);
closeModalAndRefresh();
})
.catch((err: ErrorResponseHandler) => {
setLoading(false);
dispatch(setModalErrorSnackMessage(err));
});
};
const fetchGroupInformation = () => {
if (selectedGroups?.length === 1) {
api
.invoke("GET", `/api/v1/group/${encodeURLString(selectedGroups[0])}`)
.then((res: any) => {
const groupPolicy: String = get(res, "policy", "");
setActualPolicy(groupPolicy.split(","));
setSelectedPolicy(groupPolicy.split(","));
})
.catch((err: ErrorResponseHandler) => {
dispatch(setModalErrorSnackMessage(err));
setLoading(false);
});
}
};
const resetSelection = () => {
setSelectedPolicy(actualPolicy);
};
useEffect(() => {
if (open) {
if (selectedGroups?.length === 1) {
fetchGroupInformation();
return;
}
const userPolicy: string[] = get(selectedUser, "policy", []);
setActualPolicy(userPolicy);
setSelectedPolicy(userPolicy);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [open, selectedGroups?.length, selectedUser]);
const userName = get(selectedUser, "accessKey", "");
return (
<ModalWrapper
onClose={() => {
closeModalAndRefresh();
}}
modalOpen={open}
title="Set Policies"
>
<Grid container>
{(selectedGroups?.length === 1 || selectedUser != null) && (
<Fragment>
<Grid item xs={12}>
<PredefinedList
label={`Selected ${selectedGroups !== null ? "Group" : "User"}`}
content={selectedGroups !== null ? selectedGroups[0] : userName}
/>
</Grid>
<Grid item xs={12}>
<PredefinedList
label={"Current Policy"}
content={actualPolicy.join(", ")}
/>
</Grid>
</Fragment>
)}
{selectedGroups && selectedGroups?.length > 1 && (
<PredefinedList
label={"Selected Groups"}
content={selectedGroups.join(", ")}
/>
)}
<Grid item xs={12}>
<div className={classes.tableBlock}>
<PolicySelectors selectedPolicy={selectedPolicy} />
</div>
</Grid>
</Grid>
<Grid item xs={12} className={classes.buttonContainer}>
<Button
type="button"
variant="outlined"
color="primary"
className={classes.spacerRight}
onClick={resetSelection}
>
Reset
</Button>
<Button
type="button"
variant="contained"
color="primary"
disabled={loading}
onClick={setPolicyAction}
>
Save
</Button>
</Grid>
{loading && (
<Grid item xs={12}>
<LinearProgress />
</Grid>
)}
</ModalWrapper>
);
};
export default withStyles(styles)(SetPolicy);