-
Notifications
You must be signed in to change notification settings - Fork 199
Feat(rest): Api for importing users via a .csv file #3280
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
base: main
Are you sure you want to change the base?
Feat(rest): Api for importing users via a .csv file #3280
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would request you to look into the old code how this request was handled by the Liferay portlet. We can copy most of the code in the service class here.
References:
sw360/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/admin/UserPortlet.java
Lines 898 to 911 in 20c818a
public void updateUsers(ActionRequest request, ActionResponse response) throws IOException { List<UserCSV> users = getUsersFromRequest(request, "file"); try { createOrganizations(request, users); } catch (SystemException | PortalException e) { log.error("Error creating organizations", e); } for (UserCSV user : users) { dealWithUser(request, user); } } sw360/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/portlets/admin/UserPortlet.java
Lines 1014 to 1026 in 20c818a
private User dealWithUser(PortletRequest request, UserCSV userRec) { User user = null; try { user = userRec.addLifeRayUser(request); if (user != null) { UserUtils.synchronizeUserWithDatabase(userRec, thriftClients, userRec::getEmail, userRec::getGid, UserUtils::fillThriftUserFromUserCSV); } } catch (SystemException | PortalException e) { log.error("Error creating a new user", e); } return user; } sw360/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/users/UserUtils.java
Lines 67 to 101 in 20c818a
public static <T> org.eclipse.sw360.datahandler.thrift.users.User synchronizeUserWithDatabase( T source, ThriftClients thriftClients, Supplier<String> emailSupplier, Supplier<String> extIdSupplier, BiConsumer<org.eclipse.sw360.datahandler.thrift.users.User, T> synchronizer) { UserService.Iface client = thriftClients.makeUserClient(); org.eclipse.sw360.datahandler.thrift.users.User existingThriftUser = null; String email = emailSupplier.get(); try { existingThriftUser = client.getByEmailOrExternalId(email, extIdSupplier.get()); } catch (TException e) { //This occurs for every new user, so there is not necessarily something wrong log.trace("User not found by email or external ID"); } org.eclipse.sw360.datahandler.thrift.users.User resultUser = null; try { if (existingThriftUser == null) { log.info("Creating new user."); resultUser = new org.eclipse.sw360.datahandler.thrift.users.User(); synchronizer.accept(resultUser, source); client.addUser(resultUser); } else { resultUser = existingThriftUser; if (!existingThriftUser.getEmail().equals(email)) { // email has changed resultUser.setFormerEmailAddresses(prepareFormerEmailAddresses(existingThriftUser, email)); } synchronizer.accept(resultUser, source); client.updateUser(resultUser); } } catch (TException e) { log.error("Thrift exception when saving the user", e); } return resultUser; } sw360/frontend/sw360-portlet/src/main/java/org/eclipse/sw360/portal/users/UserCSV.java
Line 28 in 20c818a
public class UserCSV {
} | ||
|
||
RequestSummary requestSummary = new RequestSummary(); | ||
requestSummary.setRequestStatus(org.eclipse.sw360.datahandler.thrift.RequestStatus.SUCCESS); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not import the name and simplify the usage?
return requestSummary; | ||
} | ||
|
||
if (file.getSize() > 10 * 1024 * 1024) { // 10MB limit |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why an arbitrary value which is not defined anywhere else?
if (givenName.length() > 100) { | ||
throw new IllegalArgumentException("GivenName too long (max 100 characters): " + givenName); | ||
} | ||
if (lastName.length() > 100) { | ||
throw new IllegalArgumentException("Lastname too long (max 100 characters): " + lastName); | ||
} | ||
if (email.length() > 255) { | ||
throw new IllegalArgumentException("Email too long (max 255 characters): " + email); | ||
} | ||
if (department.length() > 255) { | ||
throw new IllegalArgumentException("Department too long (max 255 characters): " + department); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here, the restrictions are not defined anywhere else but here.
Hello @ritankarsaha , do you have any update on the same? |
yes yes, doing this one after testing and pushing the 4th pr for fossology integration and deleting stuff from the 2nd pr. |
Summary
Added the API Endpoint for importing users via a .csv file.
Fixes #3190
Suggest Reviewer
@GMishx
@amritkv
@rudra-superrr
How To Test?
Added the response from the curl command above.
New Users imported via the csv properly.
Example of one.
Fixes #3190
Checklist
Must:
P.S. - I didn't add the test file for this, should I do that as well ?