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

Improve validation on requestor #156

Merged
merged 1 commit into from
Apr 16, 2024
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
25 changes: 25 additions & 0 deletions LabPurchasing/resources/queries/labpurchasing/purchases.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,31 @@ var console = require("console");
var triggerHelper = new org.labkey.labpurchasing.LabPurchasingTriggerHelper(LABKEY.Security.currentUser.id, LABKEY.Security.currentContainer.id);

function beforeInsert(row, errors){
beforeUpsert(row, errors)
}

function beforeUpdate(row, oldRow, errors){
beforeUpsert(row, errors)
}

function beforeUpsert(row, errors){
// Validate requestor:
if (row.requestor) {
if (!isNaN(row.requestor) && !triggerHelper.isValidUserId(row.requestor)) {
errors.requestor = 'Unknown userId for requestor: ' + row.requestor;
}
// Try to resolve strings:
else if (isNaN(row.requestor)) {
var id = triggerHelper.resolveUserId(String(row.requestor));
if (!id) {
errors.requestor = 'Unknown userId for requestor: ' + row.requestor;
}
else {
row.requestor = id;
}
}
}

// The purpose of this is to allow the user to provide a string value for
// vendorId or vendorName, and attempt to resolve this against known vendors:
if (!row.vendorId || isNaN(row.vendorId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,16 @@ public void sendNotifications(List<Integer> rowIds) {
_log.error("Unable to send purchasing email", e);
}
}

public boolean isValidUserId(int userId)
{
return UserManager.getUser(userId) != null;
}

public Integer resolveUserId(String userNameOrEmail)
{
User u = UserManager.getUserByDisplayName(userNameOrEmail);

return u == null ? null : u.getUserId();
}
}