Added validation for user groups on front-end.
This commit is contained in:
parent
b92529d8c0
commit
2d40d6fe09
@ -44,6 +44,10 @@ const GroupPermissionPage = () => {
|
|||||||
setGroups(groups.filter(g => g.id != group.id))
|
setGroups(groups.filter(g => g.id != group.id))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function containsGroup(groupName: string) {
|
||||||
|
return groups.some(g => g.name == groupName)
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (status !== "authenticated" || previousUsername == session.user?.name)
|
if (status !== "authenticated" || previousUsername == session.user?.name)
|
||||||
return
|
return
|
||||||
@ -56,7 +60,6 @@ const GroupPermissionPage = () => {
|
|||||||
if (!d.data.some((g: { id: string, name: string, priority: number }) => g.name == groupName))
|
if (!d.data.some((g: { id: string, name: string, priority: number }) => g.name == groupName))
|
||||||
d.data.push({ id: "$" + groupName, name: groupName, priority: 0 });
|
d.data.push({ id: "$" + groupName, name: groupName, priority: 0 });
|
||||||
|
|
||||||
|
|
||||||
axios.get('/api/settings/groups/permissions')
|
axios.get('/api/settings/groups/permissions')
|
||||||
.then(d2 => {
|
.then(d2 => {
|
||||||
setPermissions(d2.data)
|
setPermissions(d2.data)
|
||||||
@ -89,7 +92,8 @@ const GroupPermissionPage = () => {
|
|||||||
permissionPaths={permissionPaths}
|
permissionPaths={permissionPaths}
|
||||||
specialGroups={specialGroups}
|
specialGroups={specialGroups}
|
||||||
adder={addGroup}
|
adder={addGroup}
|
||||||
remover={removeGroup} />
|
remover={removeGroup}
|
||||||
|
contains={containsGroup} />
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<div
|
<div
|
||||||
@ -105,7 +109,8 @@ const GroupPermissionPage = () => {
|
|||||||
permissionPaths={permissionPaths}
|
permissionPaths={permissionPaths}
|
||||||
specialGroups={specialGroups}
|
specialGroups={specialGroups}
|
||||||
adder={addGroup}
|
adder={addGroup}
|
||||||
remover={removeGroup} />
|
remover={removeGroup}
|
||||||
|
contains={containsGroup} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -19,7 +19,8 @@ interface Group {
|
|||||||
permissionPaths: { path: string, description: string }[]
|
permissionPaths: { path: string, description: string }[]
|
||||||
specialGroups: string[]
|
specialGroups: string[]
|
||||||
adder: (id: string, name: string, priority: number) => void
|
adder: (id: string, name: string, priority: number) => void
|
||||||
remover: (group: { id: string, name: string, priority: number }) => void
|
remover: (group: { id: string, name: string, priority: number }) => void,
|
||||||
|
contains: (groupName: string) => boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const GroupElement = ({
|
const GroupElement = ({
|
||||||
@ -33,12 +34,13 @@ const GroupElement = ({
|
|||||||
permissionPaths,
|
permissionPaths,
|
||||||
specialGroups,
|
specialGroups,
|
||||||
adder,
|
adder,
|
||||||
remover
|
remover,
|
||||||
|
contains
|
||||||
}: Group) => {
|
}: Group) => {
|
||||||
const [isEditable, setIsEditable] = useState(edit)
|
const [isEditable, setIsEditable] = useState(edit)
|
||||||
const [isNew, setIsNew] = useState(isNewGroup)
|
const [isNew, setIsNew] = useState(isNewGroup)
|
||||||
const [isMinimized, setIsMinimized] = useState(true)
|
const [isMinimized, setIsMinimized] = useState(true)
|
||||||
const [oldData, setOldData] = useState<{ name: string, priority: number } | undefined>(undefined)
|
const [oldData, setOldData] = useState<{ id: string|undefined, name: string, priority: number } | undefined>(undefined)
|
||||||
const [group, setGroup] = useState<{ id: string|undefined, name: string, priority: number }>({ id, name, priority })
|
const [group, setGroup] = useState<{ id: string|undefined, name: string, priority: number }>({ id, name, priority })
|
||||||
const [permissions, setPermissions] = useState<{ id: string, path: string, allow: boolean | null }[]>(permissionsLoaded);
|
const [permissions, setPermissions] = useState<{ id: string, path: string, allow: boolean | null }[]>(permissionsLoaded);
|
||||||
const isSpecial = (isEditable || oldData === undefined) && !!group && specialGroups.includes(group?.name)
|
const isSpecial = (isEditable || oldData === undefined) && !!group && specialGroups.includes(group?.name)
|
||||||
@ -64,6 +66,11 @@ const GroupElement = ({
|
|||||||
if (!isNew && !id)
|
if (!isNew && !id)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if (!group.name) {
|
||||||
|
setError("Set a value for the group name")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
const nameValidation = nameSchema.safeParse(group.name)
|
const nameValidation = nameSchema.safeParse(group.name)
|
||||||
if (!nameValidation.success) {
|
if (!nameValidation.success) {
|
||||||
setError(JSON.parse(nameValidation.error['message'])[0].message)
|
setError(JSON.parse(nameValidation.error['message'])[0].message)
|
||||||
@ -76,6 +83,11 @@ const GroupElement = ({
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (contains(group.name)) {
|
||||||
|
setError("Group already exist. Use another name.")
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isNew || group.id?.startsWith('$')) {
|
if (isNew || group.id?.startsWith('$')) {
|
||||||
axios.post("/api/settings/groups", {
|
axios.post("/api/settings/groups", {
|
||||||
name: group.name,
|
name: group.name,
|
||||||
@ -96,7 +108,7 @@ const GroupElement = ({
|
|||||||
setGroup({ id: undefined, name: "", priority: 0 })
|
setGroup({ id: undefined, name: "", priority: 0 })
|
||||||
}
|
}
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
setError("Potential group name duplicate.")
|
setError("Something went wrong.")
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
axios.put("/api/settings/groups", {
|
axios.put("/api/settings/groups", {
|
||||||
@ -104,10 +116,9 @@ const GroupElement = ({
|
|||||||
name: group.name,
|
name: group.name,
|
||||||
priority: group.priority
|
priority: group.priority
|
||||||
}).then(d => {
|
}).then(d => {
|
||||||
console.log("DATA", d.data)
|
|
||||||
setIsEditable(false)
|
setIsEditable(false)
|
||||||
}).catch(() => {
|
}).catch(() => {
|
||||||
setError("Potential group name duplicate.")
|
setError("Something went wrong.")
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,6 +134,18 @@ const GroupElement = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
function Delete() {
|
function Delete() {
|
||||||
|
if (!group.id) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!contains(oldData?.name ?? group.name)) {
|
||||||
|
if (oldData?.id)
|
||||||
|
remover({ ...oldData, id: oldData.id })
|
||||||
|
else
|
||||||
|
remover({ ...group, id: group.id })
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
axios.delete("/api/settings/groups?id=" + group.id)
|
axios.delete("/api/settings/groups?id=" + group.id)
|
||||||
.then(d => {
|
.then(d => {
|
||||||
if (specialGroups.includes(group.name)) {
|
if (specialGroups.includes(group.name)) {
|
||||||
@ -150,7 +173,7 @@ const GroupElement = ({
|
|||||||
Group Name
|
Group Name
|
||||||
</Label>
|
</Label>
|
||||||
{isSpecial &&
|
{isSpecial &&
|
||||||
<div className="bg-white text-muted text-xs p-1 rounded m-1 inline-block">
|
<div className="bg-white text-muted text-xs p-1 rounded m-0 inline-block">
|
||||||
auto-generated
|
auto-generated
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user