Skip to content

Commit

Permalink
Merge pull request #550 from openkfw/get-request-bug
Browse files Browse the repository at this point in the history
ui: Add url validation to filter empty parameters
  • Loading branch information
Stezido authored Oct 6, 2020
2 parents 2253277 + 26cdbaa commit 1e55888
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed

- Fixed a bug where the email field dissappears in the user profile [#551](https://github.com/openkfw/TruBudget/issues/551)
- Fixed a bug where opening a history sometimes resulted in an error [#549](https://github.com/openkfw/TruBudget/issues/549)

<!-- ### Security -->

Expand All @@ -34,6 +35,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- Fixed the global permission list to set permissions to disable or enable users [#537](https://github.com/openkfw/TruBudget/pull/537)


# [1.12.0] - 2020-08-10

### Added
Expand Down
49 changes: 32 additions & 17 deletions frontend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Api {
newPassword
});

listUserAssignments = userId => instance.get(`/global.listAssignments?userId=${userId}`);
listUserAssignments = userId => instance.get(removeEmptyQueryParams(`/global.listAssignments?userId=${userId}`));

createGroup = (groupId, displayName, users) =>
instance.post(`/global.createGroup`, {
Expand Down Expand Up @@ -121,7 +121,7 @@ class Api {
address
});
listProjects = () => instance.get(`/project.list`);
listSubprojects = projectId => instance.get(`/subproject.list?projectId=${projectId}`);
listSubprojects = projectId => instance.get(removeEmptyQueryParams(`/subproject.list?projectId=${projectId}`));

createProject = (displayName, description, thumbnail, projectedBudgets, tags) =>
instance.post(`/global.createProject`, {
Expand All @@ -140,9 +140,10 @@ class Api {
...changes
});

viewProjectDetails = projectId => instance.get(`/project.viewDetails?projectId=${projectId}`);
viewProjectDetails = projectId => instance.get(removeEmptyQueryParams(`/project.viewDetails?projectId=${projectId}`));
viewProjectHistory = (projectId, offset, limit, filter) => {
let url = `/project.viewHistory.v2?projectId=${projectId}&offset=${offset}&limit=${limit}`;
let url = removeEmptyQueryParams(`/project.viewHistory.v2?projectId=${projectId}&offset=${offset}&limit=${limit}`);

// filter: startAt|endAt|publisher|eventType
for (const key in filter) {
if (!_isEmpty(filter[key])) {
Expand All @@ -152,7 +153,7 @@ class Api {
return instance.get(url);
};

listProjectIntents = projectId => instance.get(`/project.intent.listPermissions?projectId=${projectId}`);
listProjectIntents = projectId => instance.get(removeEmptyQueryParams(`/project.intent.listPermissions?projectId=${projectId}`));

grantProjectPermissions = (projectId, intent, identity) =>
instance.post(`/project.intent.grantPermission`, {
Expand Down Expand Up @@ -187,10 +188,12 @@ class Api {
});

viewSubProjectDetails = (projectId, subprojectId) =>
instance.get(`/subproject.viewDetails?projectId=${projectId}&subprojectId=${subprojectId}`);
instance.get(removeEmptyQueryParams(`/subproject.viewDetails?projectId=${projectId}&subprojectId=${subprojectId}`));

viewSubProjectHistory = (projectId, subprojectId, offset, limit, filter) => {
let url = `/subproject.viewHistory.v2?projectId=${projectId}&subprojectId=${subprojectId}&offset=${offset}&limit=${limit}`;
let url = removeEmptyQueryParams(
`/subproject.viewHistory.v2?projectId=${projectId}&subprojectId=${subprojectId}&offset=${offset}&limit=${limit}`
);
// filter: startAt|endAt|publisher|eventType
for (const key in filter) {
if (!_isEmpty(filter[key])) {
Expand All @@ -201,7 +204,9 @@ class Api {
};

viewWorkflowitemHistory = (projectId, subprojectId, workflowitemId, offset, limit, filter) => {
let url = `/workflowitem.viewHistory?projectId=${projectId}&subprojectId=${subprojectId}&workflowitemId=${workflowitemId}&offset=${offset}&limit=${limit}`;
let url = removeEmptyQueryParams(
`/workflowitem.viewHistory?projectId=${projectId}&subprojectId=${subprojectId}&workflowitemId=${workflowitemId}&offset=${offset}&limit=${limit}`
);
// filter: startAt|endAt|publisher|eventType
for (const key in filter) {
if (!_isEmpty(filter[key])) {
Expand Down Expand Up @@ -260,7 +265,7 @@ class Api {
};

listSubProjectPermissions = (projectId, subprojectId) =>
instance.get(`/subproject.intent.listPermissions?projectId=${projectId}&subprojectId=${subprojectId}`);
instance.get(removeEmptyQueryParams(`/subproject.intent.listPermissions?projectId=${projectId}&subprojectId=${subprojectId}`));

grantSubProjectPermissions = (projectId, subprojectId, intent, identity) =>
instance.post(`/subproject.intent.grantPermission`, {
Expand Down Expand Up @@ -304,9 +309,9 @@ class Api {
validateDocument = (base64String, hash) => instance.post(`/workflowitem.validateDocument`, { base64String, hash });

listWorkflowItemPermissions = (projectId, subprojectId, workflowitemId) =>
instance.get(
instance.get(removeEmptyQueryParams(
`/workflowitem.intent.listPermissions?projectId=${projectId}&subprojectId=${subprojectId}&workflowitemId=${workflowitemId}`
);
));

grantWorkflowItemPermissions = (projectId, subprojectId, workflowitemId, intent, identity) =>
instance.post(`/workflowitem.intent.grantPermission`, {
Expand Down Expand Up @@ -366,10 +371,7 @@ class Api {
});

fetchNotifications = (offset, limit) => {
let url = `/notification.list?offset=${offset}`;
if (!_isEmpty(limit)) {
url = url + `limit=${limit}`;
}
let url = removeEmptyQueryParams(`/notification.list?offset=${offset}&limit=${limit}`);
return instance.get(url);
};

Expand Down Expand Up @@ -429,8 +431,8 @@ class Api {

downloadDocument = (projectId, subprojectId, workflowitemId, documentId) =>
instance
.get(
`/workflowitem.downloadDocument?projectId=${projectId}&subprojectId=${subprojectId}&workflowitemId=${workflowitemId}&documentId=${documentId}`,
.get(removeEmptyQueryParams(
`/workflowitem.downloadDocument?projectId=${projectId}&subprojectId=${subprojectId}&workflowitemId=${workflowitemId}&documentId=${documentId}`),
{ responseType: "blob" }
)
.then(response => {
Expand Down Expand Up @@ -465,4 +467,17 @@ const hasAttachment = response => {
return dispositionHeader && dispositionHeader.indexOf("attachment") !== -1;
};

/**
*
* @param url url that needs to be checked for empty parameters
* @returns the url without empty or undefined parameters
*/
const removeEmptyQueryParams = url => {
return url
.replace(/[^=&]+=(&|$)/g, "") // removes a parameter if the '=' is followed by a '&' or if it's the end of the line
.replace(/[^=&]+=(undefined|$)/g, "") // removes a parameter if the '=' is followed by 'undefined'
.replace(/&$/, ""); // removes any leftover '$'
};

export default Api;

0 comments on commit 1e55888

Please sign in to comment.