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