14
14
// You should have received a copy of the GNU Affero General Public License
15
15
// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
16
17
- import React , { useEffect , useState } from "react" ;
17
+ import React , { useEffect , useState , Fragment } from "react" ;
18
18
import { useDispatch } from "react-redux" ;
19
19
import get from "lodash/get" ;
20
20
import { Theme } from "@mui/material/styles" ;
@@ -41,7 +41,7 @@ interface ISetPolicyProps {
41
41
classes : any ;
42
42
closeModalAndRefresh : ( ) => void ;
43
43
selectedUser : User | null ;
44
- selectedGroup : string | null ;
44
+ selectedGroups : string [ ] | null ;
45
45
open : boolean ;
46
46
}
47
47
@@ -63,7 +63,7 @@ const SetPolicy = ({
63
63
classes,
64
64
closeModalAndRefresh,
65
65
selectedUser,
66
- selectedGroup ,
66
+ selectedGroups ,
67
67
open,
68
68
} : ISetPolicyProps ) => {
69
69
const dispatch = useDispatch ( ) ;
@@ -73,24 +73,23 @@ const SetPolicy = ({
73
73
const [ selectedPolicy , setSelectedPolicy ] = useState < string [ ] > ( [ ] ) ;
74
74
75
75
const setPolicyAction = ( ) => {
76
- let entity = "user" ;
77
- let value = null ;
78
- if ( selectedGroup !== null ) {
79
- entity = "group" ;
80
- value = selectedGroup ;
76
+ let users = null ;
77
+ let groups = null ;
78
+ if ( selectedGroups !== null ) {
79
+ groups = selectedGroups ;
81
80
} else {
82
81
if ( selectedUser !== null ) {
83
- value = selectedUser . accessKey ;
82
+ users = [ selectedUser . accessKey ] || [ " " ] ;
84
83
}
85
84
}
86
85
87
86
setLoading ( true ) ;
88
87
89
88
api
90
- . invoke ( "PUT" , `/api/v1/set-policy` , {
89
+ . invoke ( "PUT" , `/api/v1/set-policy-multi ` , {
91
90
name : selectedPolicy ,
92
- entityName : value ,
93
- entityType : entity ,
91
+ groups : groups ,
92
+ users : users ,
94
93
} )
95
94
. then ( ( ) => {
96
95
setLoading ( false ) ;
@@ -103,9 +102,9 @@ const SetPolicy = ({
103
102
} ;
104
103
105
104
const fetchGroupInformation = ( ) => {
106
- if ( selectedGroup ) {
105
+ if ( selectedGroups ?. length === 1 ) {
107
106
api
108
- . invoke ( "GET" , `/api/v1/group/${ encodeURLString ( selectedGroup ) } ` )
107
+ . invoke ( "GET" , `/api/v1/group/${ encodeURLString ( selectedGroups [ 0 ] ) } ` )
109
108
. then ( ( res : any ) => {
110
109
const groupPolicy : String = get ( res , "policy" , "" ) ;
111
110
setActualPolicy ( groupPolicy . split ( "," ) ) ;
@@ -121,10 +120,10 @@ const SetPolicy = ({
121
120
const resetSelection = ( ) => {
122
121
setSelectedPolicy ( actualPolicy ) ;
123
122
} ;
124
-
123
+
125
124
useEffect ( ( ) => {
126
125
if ( open ) {
127
- if ( selectedGroup !== null ) {
126
+ if ( selectedGroups ?. length === 1 ) {
128
127
fetchGroupInformation ( ) ;
129
128
return ;
130
129
}
@@ -134,7 +133,7 @@ const SetPolicy = ({
134
133
setSelectedPolicy ( userPolicy ) ;
135
134
}
136
135
// eslint-disable-next-line react-hooks/exhaustive-deps
137
- } , [ open , selectedGroup , selectedUser ] ) ;
136
+ } , [ open , selectedGroups ?. length , selectedUser ] ) ;
138
137
139
138
const userName = get ( selectedUser , "accessKey" , "" ) ;
140
139
@@ -146,11 +145,14 @@ const SetPolicy = ({
146
145
modalOpen = { open }
147
146
title = "Set Policies"
148
147
>
148
+
149
149
< Grid container >
150
- < Grid item xs = { 12 } >
150
+ { ( selectedGroups ?. length === 1 || selectedUser != null ) &&
151
+ < Fragment >
152
+ < Grid item xs = { 12 } >
151
153
< PredefinedList
152
- label = { `Selected ${ selectedGroup !== null ? "Group" : "User" } ` }
153
- content = { selectedGroup !== null ? selectedGroup : userName }
154
+ label = { `Selected ${ selectedGroups !== null ? "Group" : "User" } ` }
155
+ content = { selectedGroups !== null ? selectedGroups [ 0 ] : userName }
154
156
/>
155
157
</ Grid >
156
158
< Grid item xs = { 12 } >
@@ -159,6 +161,14 @@ const SetPolicy = ({
159
161
content = { actualPolicy . join ( ", " ) }
160
162
/>
161
163
</ Grid >
164
+ </ Fragment >
165
+ }
166
+ { selectedGroups && selectedGroups ?. length > 1 &&
167
+ < PredefinedList
168
+ label = { "Selected Groups" }
169
+ content = { selectedGroups . join ( ", " ) }
170
+ />
171
+ }
162
172
< Grid item xs = { 12 } >
163
173
< div className = { classes . tableBlock } >
164
174
< PolicySelectors
0 commit comments