1
Fork 0
mirror of https://github.com/Steffo99/sophon.git synced 2024-12-22 23:04:21 +00:00

💥 Disallow group members from editing group name and description

This commit is contained in:
Steffo 2021-10-13 03:52:06 +02:00
parent 4c5b0bd118
commit 7363521b65
Signed by: steffo
GPG key ID: 6965406171929D01
2 changed files with 49 additions and 29 deletions

View file

@ -374,14 +374,13 @@ class ResearchGroup(SophonGroupModel):
@classmethod @classmethod
def get_editable_fields(cls) -> set[str]: def get_editable_fields(cls) -> set[str]:
return { return {}
"name",
"description",
}
@classmethod @classmethod
def get_administrable_fields(cls) -> set[str]: def get_administrable_fields(cls) -> set[str]:
return { return {
"name",
"description",
"members", "members",
"access", "access",
} }

View file

@ -6,8 +6,20 @@ import {ManagedResource, ManagedViewSet} from "../../hooks/useManagedViewSet"
import {SophonResearchGroup} from "../../types/SophonTypes" import {SophonResearchGroup} from "../../types/SophonTypes"
/**
* The props of {@link GroupCreateBox}.
*
* Only one between `viewSet` and `resource` should be defined.
*/
export interface GroupCreateBoxProps { export interface GroupCreateBoxProps {
/**
* The viewSet to use to create new resources.
*/
viewSet?: ManagedViewSet<SophonResearchGroup>, viewSet?: ManagedViewSet<SophonResearchGroup>,
/**
* The resource to be edited.
*/
resource?: ManagedResource<SophonResearchGroup>, resource?: ManagedResource<SophonResearchGroup>,
} }
@ -69,21 +81,6 @@ export function GroupCreateBox({viewSet, resource}: GroupCreateBoxProps): JSX.El
[viewSet, resource, name, slug, description, members, access], [viewSet, resource, name, slug, description, members, access],
) )
const trueMembers =
React.useMemo(
() => resource ? [...new Set([resource.value.owner, ...resource.value.members])] : undefined,
[resource],
)
const canEdit =
React.useMemo(
() => !resource ||
(
authorization && authorization.state.user && trueMembers?.includes(authorization.state.user.id)
),
[authorization, resource, trueMembers],
)
const canAdministrate = const canAdministrate =
React.useMemo( React.useMemo(
() => !resource || () => !resource ||
@ -107,10 +104,12 @@ export function GroupCreateBox({viewSet, resource}: GroupCreateBoxProps): JSX.El
)) { )) {
return null return null
} }
if(!canEdit) { if(!canAdministrate) {
return null return null
} }
const hasError = viewSet?.operationError || resource?.error
return ( return (
<Box> <Box>
<Details> <Details>
@ -119,14 +118,35 @@ export function GroupCreateBox({viewSet, resource}: GroupCreateBoxProps): JSX.El
</Details.Summary> </Details.Summary>
<Details.Content> <Details.Content>
<Form> <Form>
<Form.Field label={"Name"} required disabled={!canEdit} {...name}/> <Form.Field
<Form.Field label={"Slug"} required disabled={true} value={slug} validity={slug.length > 0 ? true : undefined}/> label={"Name"}
<Form.Area label={"Description"} disabled={!canEdit} {...description}/> required={true}
<Form.Multiselect label={"Members"} disabled={!canAdministrate} options={membersOptions ?? {}} {...members}/> {...name}
<Form.Field label={"Owner"} required disabled={true} value={authorization?.state.user?.username} validity={Boolean(authorization?.state.user?.username)}/> />
<Form.Field
label={"Slug"}
required={true}
disabled={true}
value={slug}
validity={slug.length > 0 ? true : undefined}
/>
<Form.Area
label={"Description"}
{...description}
/>
<Form.Multiselect
label={"Members"}
options={membersOptions ?? {}} {...members}
/>
<Form.Field
label={"Owner"}
required={true}
disabled={true}
value={authorization?.state.user?.username}
validity={Boolean(authorization?.state.user?.username)}
/>
<Form.Select <Form.Select
label={"Access"} label={"Access"}
disabled={!canAdministrate}
options={{ options={{
"": undefined, "": undefined,
"⛔️ Collaborators must be added manually": "MANUAL", "⛔️ Collaborators must be added manually": "MANUAL",
@ -136,9 +156,10 @@ export function GroupCreateBox({viewSet, resource}: GroupCreateBoxProps): JSX.El
/> />
<Form.Row> <Form.Row>
<Form.Button <Form.Button
type={"button"} onClick={onSubmit} disabled={!canSubmit} builtinColor={( type={"button"}
viewSet?.operationError || resource?.error onClick={onSubmit}
) ? "red" : undefined} disabled={!canSubmit}
builtinColor={hasError ? "red" : undefined}
> >
{resource ? "Edit" : "Create"} {resource ? "Edit" : "Create"}
</Form.Button> </Form.Button>