Add the backend logic to export all users to .csv

This commit is contained in:
George Kalampokis 2020-10-07 17:29:53 +03:00
parent 4479a0df80
commit 78903bf930
2 changed files with 61 additions and 0 deletions

View File

@ -103,6 +103,12 @@ public class Users extends BaseController {
userManager.deleteDOIToken(principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UserProfile>().status(ApiMessageCode.NO_MESSAGE));
}
@RequestMapping(method = RequestMethod.GET, value = {"/getCsv"})
public @ResponseBody
ResponseEntity exportCsv(@ClaimedAuthorities(claims = {ADMIN}) Principal principal) throws Exception {
return userManager.exportToCsv(principal);
}
}

View File

@ -29,20 +29,35 @@ import eu.eudat.models.data.security.Principal;
import eu.eudat.models.data.userinfo.UserListingModel;
import eu.eudat.models.data.userinfo.UserProfile;
import eu.eudat.queryable.QueryableList;
import eu.eudat.types.Authorities;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class UserManager {
private static final Logger logger = LoggerFactory.getLogger(UserManager.class);
private ApiContext apiContext;
private ZenodoCustomProvider zenodoCustomProvider;
@ -168,4 +183,44 @@ public class UserManager {
settings.put("expirationDate", 0);
this.updateSettings(settings, principal);
}
public ResponseEntity<byte[]> exportToCsv(Principal principal) throws IOException {
if (!principal.getAuthz().contains(Authorities.ADMIN))
throw new UnauthorisedException();
List<UserInfo> users = this.apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().asQueryable().toList();
StringBuilder resultBuilder = new StringBuilder();
resultBuilder.append("User Id").append(",").append("User Name").append(",").append("User Email").append("\n");
users.stream().forEach(user -> resultBuilder.append(user.getId().toString()).append(",")
.append(user.getName()).append(",")
.append(user.getEmail()).append("\n"));
String result = resultBuilder.toString();
String fileName = "Users_dump";//dmp.getLabel();
fileName = fileName.replaceAll("[^a-zA-Z0-9+ ]", "");
String uuid = UUID.randomUUID().toString();
File file = new File(this.environment.getProperty("temp.temp") + uuid + ".csv");
OutputStream output = new FileOutputStream(file);
try {
// mapper.writeValue(file, rdaExportModel);
output.write(result.getBytes());
output.flush();
output.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
InputStream resource = new FileInputStream(file);
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentLength(file.length());
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
responseHeaders.set("Content-Disposition", "attachment;filename=" + fileName + ".csv");
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
resource.close();
Files.deleteIfExists(file.toPath());
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
}
}