1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-24 01:18:01 +02:00

fix: improve SSO default role resolution (#5916)

This improves the role resolution in the value of the default root role,
preventing a bug where settings saved
pre-https://github.com/Unleash/unleash/pull/5887 would show an empty
default root role in the dropdown.

Also makes the role update more robust.
This commit is contained in:
Nuno Góis 2024-01-16 16:08:46 +00:00 committed by GitHub
parent 3bebc11fb2
commit d2366a8aa1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 12 deletions

View File

@ -22,11 +22,13 @@ interface IAutoCreateFormProps {
name: string, name: string,
value: string | boolean | number | undefined, value: string | boolean | number | undefined,
) => void; ) => void;
onUpdateRole: (role: IRole | null) => void;
} }
export const AutoCreateForm = ({ export const AutoCreateForm = ({
data = { enabled: false, autoCreate: false }, data = { enabled: false, autoCreate: false },
setValue, setValue,
onUpdateRole,
}: IAutoCreateFormProps) => { }: IAutoCreateFormProps) => {
const { roles } = useRoles(); const { roles } = useRoles();
@ -34,16 +36,21 @@ export const AutoCreateForm = ({
setValue('autoCreate', !data.autoCreate); setValue('autoCreate', !data.autoCreate);
}; };
const updateDefaultRootRoleId = (role: IRole | null) => {
setValue('defaultRootRoleId', role?.id);
};
const updateField = (e: ChangeEvent<HTMLInputElement>) => { const updateField = (e: ChangeEvent<HTMLInputElement>) => {
setValue(e.target.name, e.target.value); setValue(e.target.name, e.target.value);
}; };
const roleIdToRole = (rootRoleId: number | undefined): IRole | null => { const resolveRole = ({
return roles.find((role: IRole) => role.id === rootRoleId) || null; defaultRootRole,
defaultRootRoleId,
}: {
defaultRootRole?: string;
defaultRootRoleId?: number;
}): IRole | null => {
if (defaultRootRoleId) {
return roles.find(({ id }) => id === defaultRootRoleId) || null;
}
return roles.find(({ name }) => name === defaultRootRole) || null;
}; };
return ( return (
@ -81,8 +88,8 @@ export const AutoCreateForm = ({
<FormControl style={{ width: '400px' }}> <FormControl style={{ width: '400px' }}>
<RoleSelect <RoleSelect
roles={roles} roles={roles}
value={roleIdToRole(data.defaultRootRoleId)} value={resolveRole(data)}
setValue={updateDefaultRootRoleId} setValue={onUpdateRole}
disabled={!data.autoCreate || !data.enabled} disabled={!data.autoCreate || !data.enabled}
required required
hideDescription hideDescription

View File

@ -19,6 +19,7 @@ import useToast from 'hooks/useToast';
import { formatUnknownError } from 'utils/formatUnknownError'; import { formatUnknownError } from 'utils/formatUnknownError';
import { removeEmptyStringFields } from 'utils/removeEmptyStringFields'; import { removeEmptyStringFields } from 'utils/removeEmptyStringFields';
import { SsoGroupSettings } from '../SsoGroupSettings'; import { SsoGroupSettings } from '../SsoGroupSettings';
import { IRole } from 'interfaces/role';
const initialState = { const initialState = {
enabled: false, enabled: false,
@ -35,10 +36,15 @@ const initialState = {
idTokenSigningAlgorithm: 'RS256', idTokenSigningAlgorithm: 'RS256',
}; };
type State = typeof initialState & {
defaultRootRole?: string;
defaultRootRoleId?: number;
};
export const OidcAuth = () => { export const OidcAuth = () => {
const { setToastData, setToastApiError } = useToast(); const { setToastData, setToastApiError } = useToast();
const { uiConfig } = useUiConfig(); const { uiConfig } = useUiConfig();
const [data, setData] = useState(initialState); const [data, setData] = useState<State>(initialState);
const { config } = useAuthSettings('oidc'); const { config } = useAuthSettings('oidc');
const { updateSettings, errors, loading } = useAuthSettingsApi('oidc'); const { updateSettings, errors, loading } = useAuthSettingsApi('oidc');
@ -70,6 +76,14 @@ export const OidcAuth = () => {
}); });
}; };
const onUpdateRole = (role: IRole | null) => {
setData({
...data,
defaultRootRole: undefined,
defaultRootRoleId: role?.id,
});
};
const onSubmit = async (event: React.SyntheticEvent) => { const onSubmit = async (event: React.SyntheticEvent) => {
event.preventDefault(); event.preventDefault();
@ -240,7 +254,11 @@ export const OidcAuth = () => {
data={data} data={data}
setValue={setValue} setValue={setValue}
/> />
<AutoCreateForm data={data} setValue={setValue} /> <AutoCreateForm
data={data}
setValue={setValue}
onUpdateRole={onUpdateRole}
/>
<Grid container spacing={3} mb={2}> <Grid container spacing={3} mb={2}>
<Grid item md={5}> <Grid item md={5}>
<strong>ID Signing algorithm</strong> <strong>ID Signing algorithm</strong>

View File

@ -15,6 +15,7 @@ import useAuthSettingsApi from 'hooks/api/actions/useAuthSettingsApi/useAuthSett
import { formatUnknownError } from 'utils/formatUnknownError'; import { formatUnknownError } from 'utils/formatUnknownError';
import { removeEmptyStringFields } from 'utils/removeEmptyStringFields'; import { removeEmptyStringFields } from 'utils/removeEmptyStringFields';
import { SsoGroupSettings } from '../SsoGroupSettings'; import { SsoGroupSettings } from '../SsoGroupSettings';
import { IRole } from 'interfaces/role';
const initialState = { const initialState = {
enabled: false, enabled: false,
@ -30,10 +31,15 @@ const initialState = {
groupJsonPath: '', groupJsonPath: '',
}; };
type State = typeof initialState & {
defaultRootRole?: string;
defaultRootRoleId?: number;
};
export const SamlAuth = () => { export const SamlAuth = () => {
const { setToastData, setToastApiError } = useToast(); const { setToastData, setToastApiError } = useToast();
const { uiConfig } = useUiConfig(); const { uiConfig } = useUiConfig();
const [data, setData] = useState(initialState); const [data, setData] = useState<State>(initialState);
const { config } = useAuthSettings('saml'); const { config } = useAuthSettings('saml');
const { updateSettings, errors, loading } = useAuthSettingsApi('saml'); const { updateSettings, errors, loading } = useAuthSettingsApi('saml');
@ -61,6 +67,14 @@ export const SamlAuth = () => {
}); });
}; };
const onUpdateRole = (role: IRole | null) => {
setData({
...data,
defaultRootRole: undefined,
defaultRootRoleId: role?.id,
});
};
const onSubmit = async (event: React.SyntheticEvent) => { const onSubmit = async (event: React.SyntheticEvent) => {
event.preventDefault(); event.preventDefault();
@ -248,7 +262,11 @@ export const SamlAuth = () => {
setValue={setValue} setValue={setValue}
/> />
<AutoCreateForm data={data} setValue={setValue} /> <AutoCreateForm
data={data}
setValue={setValue}
onUpdateRole={onUpdateRole}
/>
<Grid container spacing={3}> <Grid container spacing={3}>
<Grid item md={5}> <Grid item md={5}>
<Button <Button