Skip to content

Commit

Permalink
feat(admin-ui): design the layout for role-permission mapping #336
Browse files Browse the repository at this point in the history
  • Loading branch information
syntrydy committed Dec 22, 2021
1 parent f8ccaf5 commit 256a24d
Show file tree
Hide file tree
Showing 7 changed files with 578 additions and 38 deletions.
1 change: 0 additions & 1 deletion plugins/admin/components/Mapping/ItemRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ function ItemRow({ key, candidate, permission }) {
</Button>
</Col>
</Row>
<FormGroup row />
</div>
)
}
Expand Down
196 changes: 196 additions & 0 deletions plugins/admin/components/Mapping/JsonPropertyBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import React, { useState } from 'react'
import { Accordion, FormGroup, Col, Button } from '../../../../app/components'
import GluuInlineInput from '../../../../app/routes/Apps/Gluu/GluuInlineInput'
import { useTranslation } from 'react-i18next'

function JsonPropertyBuilder({
propKey,
propValue,
lSize,
path,
handler,
parentIsArray,
}) {
const { t } = useTranslation()
const [show, setShow] = useState(true)
if (!path) {
path = '/' + propKey
} else {
path = path + '/' + propKey
}
function isBoolean(item) {
return typeof item === 'boolean'
}

function isString(item) {
return typeof item === 'string'
}

function isNumber(item) {
return typeof item === 'number' || typeof item === 'bigint'
}
const removeHandler = () => {
const patch = {}
patch['path'] = path
patch['value'] = propValue
patch['op'] = 'remove'
handler(patch)
setShow(false)
}

function isStringArray(item) {
return (
Array.isArray(item) && item.length >= 1 && typeof item[0] === 'string'
)
}

function isObjectArray(item) {
return (
Array.isArray(item) && item.length >= 1 && typeof item[0] === 'object'
)
}
function isObject(item) {
return typeof item === 'object'
}

function generateLabel(name) {
const result = name.replace(/([A-Z])/g, ' $1')
return result.charAt(0).toUpperCase() + result.slice(1)
}

if (isBoolean(propValue)) {
return (
<GluuInlineInput
id={propKey}
name={propKey}
lsize={lSize}
rsize={lSize}
label={generateLabel(propKey)}
isBoolean={true}
handler={handler}
value={propValue}
parentIsArray={parentIsArray}
path={path}
/>
)
}
if (isString(propValue)) {
return (
<GluuInlineInput
id={propKey}
name={propKey}
lsize={lSize}
rsize={lSize}
label={generateLabel(propKey)}
handler={handler}
value={propValue}
parentIsArray={parentIsArray}
path={path}
/>
)
}
if (isNumber(propValue)) {
return (
<GluuInlineInput
id={propKey}
name={propKey}
lsize={lSize}
type="number"
rsize={lSize}
label={generateLabel(propKey)}
handler={handler}
value={propValue}
parentIsArray={parentIsArray}
path={path}
/>
)
}
if (isStringArray(propValue)) {
return (
<GluuInlineInput
id={propKey}
name={propKey}
label={generateLabel(propKey)}
value={propValue}
lsize={lSize}
rsize={lSize}
isArray={true}
handler={handler}
options={propValue}
parentIsArray={parentIsArray}
path={path}
/>
)
}

if (isObjectArray(propValue)) {
return (
<Accordion className="mb-2 b-primary" initialOpen>
<Accordion.Header className="text-primary">
{propKey.toUpperCase()}{' '}
</Accordion.Header>
<Accordion.Body>
{Object.keys(propValue).map((item, idx) => (
<JsonPropertyBuilder
key={idx}
propKey={item}
propValue={propValue[item]}
handler={handler}
lSize={lSize}
parentIsArray={true}
path={path}
/>
))}
</Accordion.Body>
</Accordion>
)
}
if (isObject(propValue)) {
return (
<div>
{show && (
<Accordion className="mb-2 b-primary" initialOpen>
<Accordion.Header className="text-primary">
{propKey.toUpperCase().length > 10 ? propKey.toUpperCase() : ''}
</Accordion.Header>
<Accordion.Body>
{parentIsArray && (
<FormGroup row>
<Col sm={11} md={11}></Col>
<Col sm={1} md={1}>
<Button
color="danger"
size="sm"
style={{ float: 'right' }}
onClick={removeHandler}
>
<i className="fa fa-remove mr-2"></i>
{' '}
{t('actions.remove')}
{' '}
</Button>
</Col>
</FormGroup>
)}
{Object.keys(propValue).map((objKey, idx) => (
<JsonPropertyBuilder
key={idx}
propKey={objKey}
propValue={propValue[objKey]}
handler={handler}
lSize={lSize}
parentIsArray={parentIsArray}
enableRemove={parentIsArray}
path={path}
/>
))}
</Accordion.Body>
</Accordion>
)}
</div>
)
}
return <div></div>
}

export default JsonPropertyBuilder
25 changes: 21 additions & 4 deletions plugins/admin/components/Mapping/MappingPage.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, { useEffect } from 'react'
import React, { useEffect, useState } from 'react'
import { connect } from 'react-redux'
import { useTranslation } from 'react-i18next'
import { Card, CardBody, FormGroup } from '../../../../app/components'
import GluuViewWrapper from '../../../../app/routes/Apps/Gluu/GluuViewWrapper'
import GluuRibbon from '../../../../app/routes/Apps/Gluu/GluuRibbon'
import MappingItem from './MappingItem'
import PropertyBuilder from './JsonPropertyBuilder'
import { getMapping } from '../../redux/actions/MappingActions'
import config from './apiconfig'
import {
hasPermission,
buildPayload,
Expand All @@ -14,12 +15,22 @@ import {

function MappingPage({ mapping, permissions, dispatch }) {
const { t } = useTranslation()
const lSize = 6
const options = []
const [patches, setPatches] = useState([])
const userAction = {}
useEffect(() => {
buildPayload(userAction, 'ROLES_MAPPING', options)
dispatch(getMapping(userAction))
}, [])

const patchHandler = (patch) => {
setPatches((existingPatches) => [...existingPatches, patch])
const newPatches = patches
newPatches.push(patch)
setPatches(newPatches)
}

return (
<Card>
<GluuRibbon title={t('titles.mapping')} fromLeft />
Expand All @@ -28,8 +39,14 @@ function MappingPage({ mapping, permissions, dispatch }) {
<FormGroup row />
<FormGroup row />
<GluuViewWrapper canShow={hasPermission(permissions, ROLE_READ)}>
{mapping.map((candidate, key) => (
<MappingItem key={key} candidate={candidate} />
{Object.keys(config.rolePermissionMapping).map((propKey, idx) => (
<PropertyBuilder
key={idx}
propKey={propKey}
propValue={config.rolePermissionMapping[propKey]}
lSize={lSize}
handler={patchHandler}
/>
))}
</GluuViewWrapper>
</CardBody>
Expand Down
Loading

0 comments on commit 256a24d

Please sign in to comment.