Skip to content

Commit

Permalink
Added validation for algorithm filter
Browse files Browse the repository at this point in the history
Former-commit-id: 364a9e83f7f3bb37d98cb63991641f031f094834
  • Loading branch information
rmathis committed Aug 11, 2022
1 parent 163c781 commit f1da227
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/src/main/resources/i18n/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,4 @@ value.algorithm-cbc-256=CBC (256) - http://www.w3.org/2001/04/xmlenc#aes256-cbc
value.algorithm-cbc-192=CBC (192) - http://www.w3.org/2001/04/xmlenc#aes192-cbc
value.algorithm-cbc-128=CBC (128) - http://www.w3.org/2001/04/xmlenc#aes128-cbc
value.algorithm-cbc-tripledes=CBC (TRIPLEDES) - http://www.w3.org/2001/04/xmlenc#tripledes-cbc
message.algorithms-unique=Each algorithm may only be used once.
2 changes: 1 addition & 1 deletion ui/src/app/core/components/ProtectRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useCurrentUser, useIsAdmin } from '../user/UserContext';
export function ProtectRoute({ children, redirectTo, ...rest }) {
const user = useCurrentUser();
const isAdmin = useIsAdmin();
if (isUndefined(user.role)) {
if (isUndefined(user?.role)) {
return <></>
}
return isAdmin ? children : <Redirect to={redirectTo} />;
Expand Down
3 changes: 2 additions & 1 deletion ui/src/app/core/components/ProtectRoute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ProtectRoute } from './ProtectRoute';
const mockIsAdmin = jest.fn();

jest.mock('../user/UserContext', () => ({
useIsAdmin: () => mockIsAdmin()
useIsAdmin: () => mockIsAdmin(),
useCurrentUser: () => ({role: 'ROLE_ADMIN'}),
}));

const renderWithRouter = (ui, { route = '/' } = {}) => {
Expand Down
4 changes: 4 additions & 0 deletions ui/src/app/form/component/widgets/SelectWidget.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const SelectWidget = ({
onFocus,
placeholder,
rawErrors = [],
uiSchema,
}) => {
const { enumOptions, enumDisabled } = options;

Expand Down Expand Up @@ -120,6 +121,9 @@ const SelectWidget = ({
onChange={(event) => {
const newValue = getValue(event, multiple);
onChange(processValue(schema, newValue));
if (uiSchema.checkOnChange) {
setTouched(true);
}
}}>
{!multiple && schema.default === undefined && (
<option value="">{translator(placeholder)}</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ export const AlgorithmFilterWizard = {
},
algorithms: {
"ui:options": {
orderable: false
orderable: false,
},
items: {
checkOnChange: true
}
}
}, BaseFilterDefinition.uiSchema),
Expand All @@ -21,7 +24,24 @@ export const AlgorithmFilterWizard = {
// schema: `${BASE_PATH}assets/schema/filter/algorithm.schema.json`,
steps: [],
validator: (data = [], current = { resourceId: null }, group) => {
return BaseFilterDefinition.validator(data, current, group, 'algorithmFilterTarget', 'algorithmFilterTargetType')
const base = BaseFilterDefinition.validator(data, current, group, 'algorithmFilterTarget', 'algorithmFilterTargetType');

return (formData, errors) => {
const errorList = base(formData, errors);
const { algorithms } = formData;

const dupes = algorithms.filter((item, index) => index !== algorithms.indexOf(item));

if (dupes.length) {
algorithms.forEach((value, index) => {
if (dupes.indexOf(value) > -1) {
errors.algorithms[index].addError('message.algorithms-unique');
}
});
}

return errorList;
}
},
formatter: (changes) => ({
...changes,
Expand Down

0 comments on commit f1da227

Please sign in to comment.