-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathMappingItem.js
More file actions
331 lines (311 loc) · 11.5 KB
/
MappingItem.js
File metadata and controls
331 lines (311 loc) · 11.5 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import React, { useEffect, useState, useRef, useContext } from 'react'
import { Row, Badge, Col, Button, FormGroup, Accordion, Form } from 'Components'
import { useDispatch, useSelector } from 'react-redux'
import { DeleteOutlined, HelpOutline } from '@mui/icons-material'
import { useCedarling } from '@/cedarling'
import { MAPPING_WRITE, MAPPING_DELETE } from 'Utils/PermChecker'
import {
updateMapping,
addPermissionsToRole,
updatePermissionsToServer,
updatePermissionsServerResponse,
deleteMapping,
} from 'Plugins/admin/redux/features/mappingSlice'
import GluuTypeAhead from 'Routes/Apps/Gluu/GluuTypeAhead'
import applicationStyle from 'Routes/Apps/Gluu/styles/applicationstyle'
import mappingItemStyles from './styles/MappingItem.style'
import GluuTooltip from 'Routes/Apps/Gluu/GluuTooltip'
import { Formik } from 'formik'
import { ThemeContext } from 'Context/theme/themeContext'
import { useTranslation } from 'react-i18next'
import customColors from '@/customColors'
import getThemeColor from '@/context/theme/config'
function MappingItem({ candidate, roles }) {
const dispatch = useDispatch()
const { t } = useTranslation()
const { hasCedarPermission, authorize } = useCedarling()
const autocompleteRef = useRef(null)
const permissions = useSelector((state) => state.apiPermissionReducer.items)
const { permissions: cedarPermissions } = useSelector((state) => state.cedarPermissions)
const [searchablePermissions, setSearchAblePermissions] = useState([])
const [essentialPermissions, setEssentialPermissions] = useState([])
const [serverPermissions, setServerPermissions] = useState(null)
const [isDeleteable, setIsDeleteable] = useState(false)
const theme = useContext(ThemeContext)
const selectedTheme = theme.state.theme
const themeColors = getThemeColor(selectedTheme)
const authorizePermissions = async () => {
const permissions = [MAPPING_WRITE, MAPPING_DELETE]
try {
for (const permission of permissions) {
await authorize([permission])
}
} catch (error) {
console.error('Error authorizing mapping permissions:', error)
}
}
useEffect(() => {
authorizePermissions()
setServerPermissionsToLocalState()
}, [])
useEffect(() => {
if (roles) {
for (const i in roles) {
if (roles[i].role == candidate.role) {
if (roles[i].deletable) {
setIsDeleteable(true)
} else {
setIsDeleteable(false)
}
}
}
}
}, [roles])
const getPermissionsForSearch = () => {
const selectedPermissions = candidate.permissions
const filteredArr = []
const essentialArr = []
for (const i in permissions) {
if (!selectedPermissions.includes(permissions[i].permission)) {
if (permissions[i].permission) {
// Check if it's an essential permission
if (permissions[i].essentialPermissionInAdminUI === true) {
essentialArr.push(permissions[i].permission)
} else {
filteredArr.push(permissions[i].permission)
}
}
}
}
setSearchAblePermissions(filteredArr)
setEssentialPermissions(essentialArr)
}
const revertLocalChanges = () => {
const data = Object.assign(JSON.parse(serverPermissions))
dispatch(updatePermissionsServerResponse({ data: data }))
}
const setServerPermissionsToLocalState = () => {
setServerPermissions(JSON.stringify(candidate))
}
useEffect(() => {
getPermissionsForSearch()
}, [permissions, candidate?.permissions?.length, cedarPermissions])
const initialValues = { mappingAddPermissions: [] }
const handleAddPermission = (values, { resetForm }) => {
if (values?.mappingAddPermissions?.length) {
dispatch(
addPermissionsToRole({
data: {
data: values?.mappingAddPermissions,
userRole: candidate.role,
},
}),
)
}
resetForm()
autocompleteRef.current.clear()
}
const handleAddEssentialPermission = (permission) => {
dispatch(
addPermissionsToRole({
data: {
data: [permission],
userRole: candidate.role,
},
}),
)
}
const handleRemovePermission = (permissionIndex, role) => {
dispatch(
updateMapping({
data: {
id: permissionIndex,
role,
},
}),
)
}
const handleDeleteRole = () => {
dispatch(
deleteMapping({
data: {
role: candidate.role,
permissions: candidate.permissions,
},
}),
)
}
return (
<div>
<FormGroup row />
<Row>
<Col sm={12}>
<Accordion className="mb-12">
<Accordion.Header className="text-info" style={{ color: themeColors?.background }}>
<span
style={{
color: themeColors?.background,
display: 'inline-flex',
alignItems: 'center',
}}
>
<Accordion.Indicator className="me-2" />
{candidate.role}
</span>
{isDeleteable && (
<DeleteOutlined
onClick={() => handleDeleteRole()}
style={{
float: 'right',
color: customColors.black,
}}
/>
)}
<Badge
color={`primary-${selectedTheme}`}
style={{
float: 'right',
}}
>
{candidate.permissions.length}
</Badge>
</Accordion.Header>
<Accordion.Body>
<div style={{ marginTop: 10 }}></div>
{hasCedarPermission(MAPPING_WRITE) && (
<Formik initialValues={initialValues} onSubmit={handleAddPermission}>
{(formik) => (
<>
<Form onSubmit={formik.handleSubmit}>
<Row>
<Col sm={10}>
<GluuTypeAhead
name="mappingAddPermissions"
label={t('actions.search')}
formik={formik}
options={searchablePermissions}
required={false}
forwardRef={autocompleteRef}
doc_category={'Mapping'}
allowNew={false}
></GluuTypeAhead>
</Col>
<Col>
<Button
type="submit"
color={`primary-${selectedTheme}`}
style={applicationStyle.buttonStyle}
>
<i className="fa fa-plus me-2"></i>
{t('actions.add')}
</Button>
</Col>
</Row>
</Form>
</>
)}
</Formik>
)}
{candidate.permissions.map((permission, id) => (
<Row key={id} style={mappingItemStyles.permissionRow}>
<Col sm={10} style={mappingItemStyles.permissionColumn}>
<span style={mappingItemStyles.permissionText}>{permission}</span>
</Col>
{hasCedarPermission(MAPPING_DELETE) ? (
<Col sm={2} style={mappingItemStyles.buttonContainer}>
<Button
type="button"
color="danger"
size="sm"
onClick={() => handleRemovePermission(id, candidate.role)}
style={{
...applicationStyle.buttonStyle,
...mappingItemStyles.removeButton,
}}
>
<i className="fa fa-trash"></i>
{t('actions.remove')}
</Button>
</Col>
) : null}
</Row>
))}
{hasCedarPermission(MAPPING_WRITE) && essentialPermissions.length > 0 && (
<div style={mappingItemStyles.essentialSection}>
<div style={mappingItemStyles.essentialSectionHeader}>
<h6 style={mappingItemStyles.essentialTitle}>
{t('titles.followingPermissionRequiredToBeAdded')}
<GluuTooltip
doc_category={t('tooltips.followingPermissionRequiredToBeAdded')}
doc_entry="essential-permissions-help"
isDirect={true}
>
<HelpOutline style={mappingItemStyles.tooltipIcon} />
</GluuTooltip>
</h6>
</div>
{essentialPermissions.map((permission, id) => (
<Row key={`essential-${id}`} style={mappingItemStyles.essentialPermissionRow}>
<Col sm={10} style={mappingItemStyles.permissionColumn}>
<span style={mappingItemStyles.essentialPermissionText}>{permission}</span>
</Col>
<Col sm={2} style={mappingItemStyles.buttonContainer}>
<Button
type="button"
color="success"
size="sm"
onClick={() => handleAddEssentialPermission(permission)}
style={{
...applicationStyle.buttonStyle,
...mappingItemStyles.addButton,
}}
>
<i className="fa fa-plus"></i>
{t('actions.add')}
</Button>
</Col>
</Row>
))}
</div>
)}
{/* Bottom Buttons */}
<FormGroup row />
{hasCedarPermission(MAPPING_WRITE) ? (
<Row>
<Col sm={6}>
<Button
type="button"
color={`primary-${selectedTheme}`}
style={applicationStyle.buttonStyle}
onClick={() => revertLocalChanges()}
>
<i className="fa fa-undo me-2"></i>
{t('actions.revert')}
</Button>
</Col>
<Col sm={6} className="text-end">
<Button
type="button"
color={`primary-${selectedTheme}`}
style={applicationStyle.buttonStyle}
onClick={() => {
dispatch(updatePermissionsToServer({ data: candidate }))
setServerPermissionsToLocalState()
}}
>
<i className="fa fa-plus me-2"></i>
{t('actions.save')}
</Button>
</Col>
</Row>
) : null}
{/* Bottom Buttons */}
</Accordion.Body>
</Accordion>
</Col>
<FormGroup row />
</Row>
</div>
)
}
export default MappingItem