@@ -12,20 +12,37 @@ import miqRedirectBack from '../../helpers/miq-redirect-back';
12
12
let idCounter = 0 ;
13
13
14
14
let modified = false ;
15
- const features = [ ] ;
15
+ let features = new Set ( ) ;
16
16
const RbacRoleForm = ( props ) => {
17
17
const {
18
- selectOptions, url, getURL, customProps, role,
18
+ selectOptions, url, getURL, customProps, role, existingProductFeatures ,
19
19
} = props ;
20
20
21
21
const generateId = ( ) => idCounter ++ ;
22
22
23
+ // necessary for older roles that do only have top level nodes as features
24
+ const checkChildren = ( productFeature , child ) => {
25
+ if ( ! child . children ) {
26
+ features . add ( child . value ) ;
27
+ } else {
28
+ for ( let nextChild of child . children ) {
29
+ checkChildren ( productFeature , nextChild ) ;
30
+ }
31
+ }
32
+ } ;
33
+
34
+ // find checked boxes for all role features
23
35
const findCheck = ( productFeature , node ) => {
24
36
const result = node . value . split ( '__' ) [ 1 ] . split ( '#' ) [ 0 ] ;
37
+
25
38
if ( result === productFeature ) {
26
- features . push ( node . value ) ;
39
+ features . add ( node . value ) ;
40
+ if ( node . children ) {
41
+ for ( let child of node . children ) {
42
+ checkChildren ( productFeature , child ) ;
43
+ }
44
+ }
27
45
}
28
-
29
46
if ( node . children ) {
30
47
for ( let child of node . children ) {
31
48
findCheck ( productFeature , child ) ;
@@ -41,9 +58,6 @@ const RbacRoleForm = (props) => {
41
58
label : node . text ,
42
59
} ;
43
60
44
- if ( node . key === 'infra_topology' ) {
45
- console . log ( 'infra_topology' ) ;
46
- }
47
61
let icon ;
48
62
switch ( node . icon ) {
49
63
case 'fa fa-search' :
@@ -87,13 +101,13 @@ const RbacRoleForm = (props) => {
87
101
const customValidation = ( values ) => {
88
102
const errors = { } ;
89
103
if ( values . tree_dropdown === undefined ) {
90
- values . tree_dropdown = formData . initialValues . featuresWithId || [ ] ;
104
+ values . tree_dropdown = formData . initialValues . miqProductFeatures || [ ] ;
91
105
}
92
106
if ( values ) {
93
107
if ( values . name === formData . initialValues . name
94
108
&& values . vm_restriction === formData . initialValues . vm_restriction
95
109
&& values . service_template_restriction === formData . initialValues . service_template_restriction
96
- && JSON . stringify ( values . tree_dropdown ) === JSON . stringify ( formData . initialValues . featuresWithId ) ) {
110
+ && JSON . stringify ( values . tree_dropdown ) === JSON . stringify ( formData . initialValues . miqProductFeatures ) ) {
97
111
modified = false ;
98
112
} else {
99
113
modified = true ;
@@ -121,42 +135,35 @@ const RbacRoleForm = (props) => {
121
135
if ( roleValues ) {
122
136
const bsTree = JSON . parse ( customProps . bs_tree ) ;
123
137
const nodes = bsTree . map ( transformTree ) ;
124
- const uniqueBoxes = [ ] ;
125
- const identifiers = [ ] ;
126
- roleValues . featuresWithId . forEach ( ( feature ) => {
127
- identifiers . push ( feature . identifier ) ;
128
- } ) ;
129
- console . log ( "roleValues: " , identifiers ) ;
130
- roleValues . featuresWithId . forEach ( ( productFeature ) => {
131
- uniqueBoxes . push ( findCheck ( productFeature . identifier , nodes [ 0 ] ) ) ;
138
+ roleValues . miqProductFeatures . forEach ( ( productFeature ) => {
139
+ findCheck ( productFeature . identifier , nodes [ 0 ] ) ;
132
140
} ) ;
133
- console . log ( "features: " , features ) ;
134
141
setFormData ( {
135
142
...formData , isLoading : false , initialValues : roleValues , nodes, checked : features ,
136
143
} ) ;
137
144
}
138
145
} ) ;
139
146
} else {
140
- let uniqueBoxes = [ ] ;
141
- if ( role . features_with_id ) {
142
- uniqueBoxes = role . features_with_id . map ( ( str ) => {
143
- const parts = str . split ( '__' ) ;
144
- parts [ 0 ] = 'new' ;
145
- return parts . join ( '__' ) ;
146
- } ) ;
147
- }
148
147
const initialValues = {
149
148
name : role . name !== null ? `Copy of ${ role . name } ` : '' ,
150
149
vm_restriction : role && role . settings && role . settings . restrictions && role . settings . restrictions . vms ,
151
150
service_template_restriction : role && role . settings && role . settings . restrictions && role . settings . restrictions . service_templates ,
152
- tree_dropdown : role . name !== null ? role && role . features_with_id : [ ] ,
153
151
} ;
154
152
const bsTree = JSON . parse ( customProps . bs_tree ) ;
155
153
const nodes = bsTree . map ( transformTree ) ;
156
- console . log ( nodes ) ;
154
+ if ( role . name ) {
155
+ idCounter = 0 ;
156
+ const bsTree = JSON . parse ( customProps . bs_tree ) ;
157
+ const nodes = bsTree . map ( transformTree ) ;
158
+ existingProductFeatures . forEach ( ( productFeature ) => {
159
+ findCheck ( productFeature . identifier , nodes [ 0 ] ) ;
160
+ } ) ;
161
+ }
162
+ initialValues . tree_dropdown = features ;
163
+ console . log ( "initial Values: " , initialValues ) ;
157
164
if ( initialValues ) {
158
165
setFormData ( {
159
- ...formData , isLoading : false , initialValues, nodes, checked : uniqueBoxes ,
166
+ ...formData , isLoading : false , initialValues, nodes, checked : features ,
160
167
} ) ;
161
168
}
162
169
}
@@ -167,10 +174,10 @@ const RbacRoleForm = (props) => {
167
174
if ( ! values . tree_dropdown ) {
168
175
values . tree_dropdown = formData . checked ;
169
176
}
170
-
171
- const treeDropdown = values . tree_dropdown ;
172
- const treeValues = treeDropdown . map ( ( string ) => string . split ( '#' ) [ 0 ] ) ;
177
+ const checkedBoxes = Array . from ( values . tree_dropdown ) ;
178
+ const treeValues = checkedBoxes . map ( ( feature ) => feature . split ( '#' ) [ 0 ] ) ;
173
179
const splitValues = treeValues . map ( ( string ) => string . split ( '__' ) [ 1 ] ) ;
180
+ const productFeatures = splitValues ;
174
181
values . tree_dropdown = splitValues ;
175
182
176
183
if ( values . vm_restriction === '-1' ) {
@@ -186,8 +193,7 @@ const RbacRoleForm = (props) => {
186
193
name : values . name ,
187
194
vm_restriction : values . vm_restriction ,
188
195
service_template_restriction : values . service_template_restriction ,
189
- features : values . tree_dropdown ,
190
- featuresWithId : treeDropdown ,
196
+ features : productFeatures ,
191
197
} ;
192
198
193
199
setFormData ( {
@@ -207,18 +213,19 @@ const RbacRoleForm = (props) => {
207
213
} ;
208
214
209
215
const onReset = ( ) => {
216
+ features = new Set ( ) ;
217
+ idCounter = 0 ;
210
218
http . get ( `${ getURL } /${ role . id } ` ) . then ( ( roleValues ) => {
211
219
if ( roleValues ) {
212
- let uniqueBoxes = [ ] ;
213
- if ( roleValues . featuresWithId ) {
214
- uniqueBoxes = roleValues . featuresWithId . map ( ( str ) => {
215
- const parts = str . split ( '__' ) ;
216
- parts [ 0 ] = role . id ;
217
- return parts . join ( '__' ) ;
220
+ if ( roleValues . miqProductFeatures ) {
221
+ const bsTree = JSON . parse ( customProps . bs_tree ) ;
222
+ const nodes = bsTree . map ( transformTree ) ;
223
+ roleValues . miqProductFeatures . forEach ( ( productFeature ) => {
224
+ findCheck ( productFeature . identifier , nodes [ 0 ] ) ;
218
225
} ) ;
219
226
}
220
227
setFormData ( {
221
- ...formData , isLoading : false , initialValues : roleValues , checked : uniqueBoxes ,
228
+ ...formData , isLoading : false , initialValues : roleValues , checked : features ,
222
229
} ) ;
223
230
}
224
231
} ) ;
@@ -321,14 +328,15 @@ RbacRoleForm.propTypes = {
321
328
vms : PropTypes . string . isRequired ,
322
329
} ) ,
323
330
} ) ,
324
- features_with_id : PropTypes . arrayOf ( PropTypes . string ) ,
325
331
} ) ,
332
+ existingProductFeatures : PropTypes . oneOfType ( [ PropTypes . object , PropTypes . array ] ) ,
326
333
} ;
327
334
328
335
RbacRoleForm . defaultProps = {
329
336
url : '' ,
330
337
getURL : '' ,
331
338
role : undefined ,
339
+ existingProductFeatures : undefined ,
332
340
} ;
333
341
334
342
FormTemplate . propTypes = {
0 commit comments