use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class ExportListener method onApplicationEvent.
@Override
public void onApplicationEvent(OnExportUserEvent onExportUserEvent) {
Authentication authentication = onExportUserEvent.getAuthentication();
KeystoreEnabledUserDetails userDetails = ((KeystoreEnabledUserDetails) authentication.getPrincipal());
String password = onExportUserEvent.getKeyStorePassword();
User user = userService.getByUsername(userDetails.getUsername());
String responseMail = onExportUserEvent.getResponseEmail();
File tmpFile = null;
try {
tmpFile = File.createTempFile("won", null);
tmpFile.deleteOnExit();
ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(tmpFile), Charset.forName("UTF-8"));
ZipEntry atomsEntry = new ZipEntry("export.nq");
zip.putNextEntry(atomsEntry);
user.getUserAtoms().stream().parallel().map(userAtom -> fetchAtomData(authentication, userAtom.getUri())).forEach(dataset -> {
RDFDataMgr.write(zip, dataset, RDFFormat.NQUADS_UTF8);
});
zip.closeEntry();
ZipEntry keystoreEntry = new ZipEntry("keystore.jks");
zip.putNextEntry(keystoreEntry);
if (password != null && !password.isEmpty()) {
ByteArrayOutputStream tmpStream = new ByteArrayOutputStream();
userDetails.getKeyStore().store(tmpStream, password.toCharArray());
tmpStream.writeTo(zip);
} else {
zip.write("You need to supply a keyStorePassword to get your keystore for security reasons".getBytes());
}
zip.closeEntry();
zip.close();
emailSender.sendExportMessage(onExportUserEvent.getResponseEmail(), tmpFile);
} catch (LinkedDataFetchingException e) {
logger.warn(e.getMessage());
emailSender.sendExportFailedMessage(responseMail);
} catch (KeyStoreException | NoSuchAlgorithmException | CertificateException | IOException e) {
emailSender.sendExportFailedMessage(responseMail);
throw new RuntimeException(e);
} catch (Exception e) {
emailSender.sendExportFailedMessage(responseMail);
throw e;
} finally {
if (tmpFile != null) {
tmpFile.delete();
}
}
}
use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class RestAtomController method getAllDrafts.
@ResponseBody
@RequestMapping(value = "/drafts", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public // TODO: move transactionality annotation into the service layer
List<CreateDraftPojo> getAllDrafts() {
User user = getCurrentUser();
List<CreateDraftPojo> createDraftPojos = new ArrayList<>();
Set<URI> draftURIs = user.getDraftURIs();
for (URI draftURI : draftURIs) {
Draft draft = draftRepository.findByDraftURI(draftURI).get(0);
CreateDraftPojo createDraftPojo = new CreateDraftPojo(draftURI.toString(), draft.getContent());
createDraftPojos.add(createDraftPojo);
}
return createDraftPojos;
}
use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class RestAtomController method createDraft.
/**
* saves draft of a draft
*
* @param createDraftObject an object containing information of the atom draft
* @return a JSON object of the draft with its temprory id.
*/
@ResponseBody
@RequestMapping(value = "/drafts", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
// TODO: move transactionality annotation into the service layer
@Transactional(propagation = Propagation.SUPPORTS)
public CreateDraftPojo createDraft(@RequestBody CreateDraftPojo createDraftObject) {
User user = getCurrentUser();
URI draftURI = URI.create(createDraftObject.getDraftURI());
user.getDraftURIs().add(draftURI);
wonUserDetailService.save(user);
Draft draft = draftRepository.findOneByDraftURI(draftURI);
if (draft == null) {
draft = new Draft(draftURI, createDraftObject.getDraft());
}
draft.setContent(createDraftObject.getDraft());
draftRepository.save(draft);
return createDraftObject;
}
use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class RestUserController method confirmRegistration.
@ResponseBody
@RequestMapping(value = "/confirmRegistration", method = RequestMethod.POST)
@Transactional(propagation = Propagation.SUPPORTS)
public ResponseEntity confirmRegistration(@RequestBody VerificationTokenPojo token) {
logger.debug("processing request to /confirmRegistration");
EmailVerificationToken verificationToken = userService.getEmailVerificationToken(token.getToken());
if (verificationToken == null) {
return generateStatusResponse(RestStatusResponse.TOKEN_NOT_FOUND);
}
if (verificationToken.getPurpose() != TokenPurpose.INITIAL_EMAIL_VERIFICATION) {
return generateStatusResponse(RestStatusResponse.TOKEN_PURPOSE_MISMATCH);
}
User user = verificationToken.getUser();
Calendar cal = Calendar.getInstance();
if ((verificationToken.getExpiryDate().getTime() - cal.getTime().getTime()) <= 0) {
return generateStatusResponse(RestStatusResponse.TOKEN_EXPIRED);
}
user.setEmailVerified(true);
userService.save(user);
return generateStatusResponse(RestStatusResponse.TOKEN_VERIFICATION_SUCCESS);
}
use of won.owner.model.User in project webofneeds by researchstudio-sat.
the class RestUserController method setUserSettings.
@ResponseBody
@RequestMapping(value = "/settings", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
// TODO: move transactionality annotation into the service layer
@Transactional(propagation = Propagation.SUPPORTS)
public ResponseEntity setUserSettings(@RequestBody UserSettingsPojo userSettingsPojo) {
logger.debug("processing POST request to /settings");
String username = SecurityContextHolder.getContext().getAuthentication().getName();
// cannot use user object from context since hw doesn't know about created in
// this session atom,
// therefore, we have to retrieve the user object from the user repository
User user = userService.getByUsername(username);
if (!userSettingsPojo.getUsername().isEmpty() && !user.getUsername().equals(userSettingsPojo.getUsername())) {
return generateStatusResponse(RestStatusResponse.USERNAME_MISMATCH);
}
if (!userSettingsPojo.getEmail().isEmpty() && user.getEmail() == null) {
// TODO validate email server-side?
// set email:
user.setEmail(userSettingsPojo.getEmail());
userService.save(user);
} else if (!userSettingsPojo.getEmail().isEmpty() && !user.getEmail().equals(userSettingsPojo.getEmail())) {
// TODO validate email server-side?
// change email:
user.setEmail(userSettingsPojo.getEmail());
userService.save(user);
logger.info("change email requested - email changed");
}
// retrieve UserAtom
for (UserAtom userAtom : user.getUserAtoms()) {
if (userAtom.getUri().equals(userSettingsPojo.getAtomUri())) {
userAtom.setMatches(userSettingsPojo.isNotifyMatches());
userAtom.setRequests(userSettingsPojo.isNotifyRequests());
userAtom.setConversations(userSettingsPojo.isNotifyConversations());
userAtomRepository.save(userAtom);
break;
}
}
return generateStatusResponse(RestStatusResponse.SETTINGS_CREATED);
}
Aggregations