Skip to content

Commit

Permalink
#3331 get user info and ds gb
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Feb 13, 2018
1 parent d54117b commit d30aeca
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,32 @@ private void initCustomQuestions(GuestbookResponse guestbookResponse, Dataset da
}
}

private void setUserDefaultResponses(GuestbookResponse guestbookResponse, DataverseSession session, User userIn) {
User user;
User sessionUser = session.getUser();

if (userIn != null){
user = userIn;
} else{
user = sessionUser;
}

if (user != null) {
guestbookResponse.setEmail(getUserEMail(user));
guestbookResponse.setName(getUserName(user));
guestbookResponse.setInstitution(getUserInstitution(user));
guestbookResponse.setPosition(getUserPosition(user));
guestbookResponse.setAuthenticatedUser(getAuthenticatedUser(user));
} else {
guestbookResponse.setEmail("");
guestbookResponse.setName("");
guestbookResponse.setInstitution("");
guestbookResponse.setPosition("");
guestbookResponse.setAuthenticatedUser(null);
}
guestbookResponse.setSessionId(session.toString());
}

private void setUserDefaultResponses(GuestbookResponse guestbookResponse, DataverseSession session) {
User user = session.getUser();
if (user != null) {
Expand Down Expand Up @@ -757,6 +783,30 @@ public GuestbookResponse initDefaultGuestbookResponse(Dataset dataset, DataFile
return guestbookResponse;
}

public GuestbookResponse initAPIGuestbookResponse(Dataset dataset, DataFile dataFile, DataverseSession session, User user) {
GuestbookResponse guestbookResponse = new GuestbookResponse();
Guestbook datasetGuestbook = dataset.getGuestbook();

if(datasetGuestbook == null){
guestbookResponse.setGuestbook(findDefaultGuestbook());
} else {
guestbookResponse.setGuestbook(datasetGuestbook);
}

if(dataset.getLatestVersion() != null && dataset.getLatestVersion().isDraft()){
guestbookResponse.setWriteResponse(false);
}
if (dataFile != null){
guestbookResponse.setDataFile(dataFile);
}
guestbookResponse.setDataset(dataset);
guestbookResponse.setResponseTime(new Date());
guestbookResponse.setSessionId(session.toString());
guestbookResponse.setDownloadtype("Download");
setUserDefaultResponses(guestbookResponse, session, user);
return guestbookResponse;
}

public boolean guestbookResponseValidator( UIInput toValidate, String value) {
if (value != null && value.length() > 255) {
(toValidate).setValid(false);
Expand Down
39 changes: 34 additions & 5 deletions src/main/java/edu/harvard/iq/dataverse/api/Access.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,6 @@ public DownloadInstance datafile(@PathParam("fileId") Long fileId, @QueryParam("
GuestbookResponse gbr = null;


if (gbrecs == null && df.isReleased()){
// Write Guestbook record if not done previously and file is released
gbr = guestbookResponseService.initDefaultGuestbookResponse(df.getOwner(), df, session);
}

if (df == null) {
logger.warning("Access: datafile service could not locate a DataFile object for id "+fileId+"!");
throw new WebApplicationException(Response.Status.NOT_FOUND);
Expand All @@ -198,6 +193,13 @@ public DownloadInstance datafile(@PathParam("fileId") Long fileId, @QueryParam("
apiToken = headers.getHeaderString(API_KEY_HEADER);
}


if (gbrecs == null && df.isReleased()){
// Write Guestbook record if not done previously and file is released
User apiTokenUser = findAPITokenUser(apiToken);
gbr = guestbookResponseService.initAPIGuestbookResponse(df.getOwner(), df, session, apiTokenUser);
}

// This will throw a WebApplicationException, with the correct
// exit code, if access isn't authorized:
checkAuthorization(df, apiToken);
Expand Down Expand Up @@ -1135,5 +1137,32 @@ private boolean isAccessAuthorized(DataFile df, String apiToken) {

return false;
}



private User findAPITokenUser(String apiToken) {
User apiTokenUser = null;

if ((apiToken != null) && (apiToken.length() != 64)) {
// We'll also try to obtain the user information from the API token,
// if supplied:

try {
logger.fine("calling apiTokenUser = findUserOrDie()...");
apiTokenUser = findUserOrDie();
return apiTokenUser;
} catch (WrappedResponse wr) {
logger.log(Level.FINE, "Message from findUserOrDie(): {0}", wr.getMessage());
return null;
}

}
return apiTokenUser;
}






}

0 comments on commit d30aeca

Please sign in to comment.