Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure user input UUIDs through REST API are unique #326

Merged
merged 2 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions API/Backend/Config/routes/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,14 @@ function addLayer(req, res, next, cb, forceConfig, caller = "addLayer") {
);
}

// This adds the proposed_uuid key to all of the new layers/sublayers to be added that have
// user defined UUIDs. We remove the proposed_uuid key after using it to check for unique UUIDs.
Utils.traverseLayers([req.body.layer], (layer) => {
if (layer.uuid != null) {
layer.proposed_uuid = layer.uuid;
}
});

const didSet = Utils.setIn(
config.layers,
`${placementPath}${placementIndex}`,
Expand Down
17 changes: 16 additions & 1 deletion API/Backend/Config/uuids.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const { v4: uuidv4, validate: uuidValidate } = require("uuid");

const populateUUIDs = (config) => {
const newlyAddedUUIDs = [];
const definedUUIDs = [];

// Track of all of the previously defined UUIDs (i.e. ignore the UUIDs of the newly added layers)
Utils.traverseLayers(config.layers, (layer) => {
if (layer.uuid != null && !layer.proposed_uuid) {
definedUUIDs.push(layer.uuid)
}
});

Utils.traverseLayers(config.layers, (layer) => {
if (layer.uuid == null) {
Expand All @@ -12,16 +20,23 @@ const populateUUIDs = (config) => {
name: layer.name,
uuid: layer.uuid,
});
} else if (!uuidValidate(layer.uuid)) {
} else if (!uuidValidate(layer.uuid) || definedUUIDs.includes(layer.proposed_uuid)) {
const badUUID = layer.uuid;
layer.uuid = uuidv4();
newlyAddedUUIDs.push({
name: layer.name,
uuid: layer.uuid,
replacesBadUUID: badUUID,
});
} else {
definedUUIDs.push(layer.uuid);
}

if (layer.proposed_uuid) {
delete layer.proposed_uuid;
}
});

return newlyAddedUUIDs;
};

Expand Down