github.com/minio/console@v1.4.1/web-app/src/screens/Console/Users/SetUserPolicies.tsx (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 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, { useEffect, useState } from "react"; 18 import { 19 Box, 20 Button, 21 FormLayout, 22 IAMPoliciesIcon, 23 ProgressBar, 24 Grid, 25 } from "mds"; 26 import { useSelector } from "react-redux"; 27 import { modalStyleUtils } from "../Common/FormComponents/common/styleLibrary"; 28 import { IPolicyItem } from "../Users/types"; 29 import { ErrorResponseHandler } from "../../../common/types"; 30 import { setModalErrorSnackMessage } from "../../../systemSlice"; 31 import { AppState, useAppDispatch } from "../../../store"; 32 import { setSelectedPolicies } from "./AddUsersSlice"; 33 import ModalWrapper from "../Common/ModalWrapper/ModalWrapper"; 34 import api from "../../../common/api"; 35 import PolicySelectors from "../Policies/PolicySelectors"; 36 37 interface ISetUserPoliciesProps { 38 closeModalAndRefresh: () => void; 39 selectedUser: string; 40 currentPolicies: IPolicyItem[]; 41 open: boolean; 42 } 43 44 const SetUserPolicies = ({ 45 closeModalAndRefresh, 46 selectedUser, 47 currentPolicies, 48 open, 49 }: ISetUserPoliciesProps) => { 50 const dispatch = useAppDispatch(); 51 //Local States 52 const [loading, setLoading] = useState<boolean>(false); 53 const [actualPolicy, setActualPolicy] = useState<string[]>([]); 54 55 const statePolicies = useSelector( 56 (state: AppState) => state.createUser.selectedPolicies, 57 ); 58 59 const SetUserPoliciesAction = () => { 60 let entity = "user"; 61 let value = selectedUser; 62 63 setLoading(true); 64 65 api 66 .invoke("PUT", `/api/v1/set-policy`, { 67 name: statePolicies, 68 entityName: value, 69 entityType: entity, 70 }) 71 .then(() => { 72 setLoading(false); 73 dispatch(setSelectedPolicies([])); 74 closeModalAndRefresh(); 75 }) 76 .catch((err: ErrorResponseHandler) => { 77 setLoading(false); 78 dispatch(setModalErrorSnackMessage(err)); 79 }); 80 }; 81 82 const resetSelection = () => { 83 dispatch(setSelectedPolicies(actualPolicy)); 84 }; 85 86 useEffect(() => { 87 if (open) { 88 const userPolicy: string[] = currentPolicies.map((pol) => { 89 return pol.policy; 90 }); 91 setActualPolicy(userPolicy); 92 dispatch(setSelectedPolicies(userPolicy)); 93 } 94 // eslint-disable-next-line react-hooks/exhaustive-deps 95 }, [open, selectedUser]); 96 97 return ( 98 <ModalWrapper 99 onClose={() => { 100 closeModalAndRefresh(); 101 }} 102 modalOpen={open} 103 title="Set Policies" 104 titleIcon={<IAMPoliciesIcon />} 105 > 106 <FormLayout withBorders={false} containerPadding={false}> 107 <PolicySelectors selectedPolicy={statePolicies} /> 108 </FormLayout> 109 <Box sx={modalStyleUtils.modalButtonBar}> 110 <Button 111 id={"reset-user-policies"} 112 type="button" 113 variant="regular" 114 color="primary" 115 onClick={resetSelection} 116 label={"Reset"} 117 /> 118 <Button 119 id={"save-user-policy"} 120 type="button" 121 variant="callAction" 122 color="primary" 123 disabled={loading} 124 onClick={SetUserPoliciesAction} 125 label={"Save"} 126 /> 127 </Box> 128 {loading && ( 129 <Grid item xs={12}> 130 <ProgressBar /> 131 </Grid> 132 )} 133 </ModalWrapper> 134 ); 135 }; 136 137 export default SetUserPolicies;