Search in sources :

Example 36 with User

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();
        }
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) Arrays(java.util.Arrays) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AuthenticationThreadLocal(won.protocol.util.AuthenticationThreadLocal) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) CachingLinkedDataSource(won.protocol.util.linkeddata.CachingLinkedDataSource) KeyStoreException(java.security.KeyStoreException) HashSet(java.util.HashSet) OnExportUserEvent(won.owner.web.events.OnExportUserEvent) Charset(java.nio.charset.Charset) URI(java.net.URI) ZipEntry(java.util.zip.ZipEntry) Dataset(org.apache.jena.query.Dataset) Logger(org.slf4j.Logger) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) MethodHandles(java.lang.invoke.MethodHandles) WonOwnerMailSender(won.owner.web.WonOwnerMailSender) FileOutputStream(java.io.FileOutputStream) Set(java.util.Set) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) ApplicationListener(org.springframework.context.ApplicationListener) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) File(java.io.File) Component(org.springframework.stereotype.Component) KeystoreEnabledUserDetails(won.owner.service.impl.KeystoreEnabledUserDetails) User(won.owner.model.User) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RDFFormat(org.apache.jena.riot.RDFFormat) Authentication(org.springframework.security.core.Authentication) UserService(won.owner.service.impl.UserService) User(won.owner.model.User) ZipEntry(java.util.zip.ZipEntry) KeystoreEnabledUserDetails(won.owner.service.impl.KeystoreEnabledUserDetails) CertificateException(java.security.cert.CertificateException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Authentication(org.springframework.security.core.Authentication) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) File(java.io.File)

Example 37 with User

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;
}
Also used : Draft(won.owner.model.Draft) User(won.owner.model.User) CreateDraftPojo(won.owner.pojo.CreateDraftPojo) URI(java.net.URI)

Example 38 with User

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;
}
Also used : Draft(won.owner.model.Draft) User(won.owner.model.User) URI(java.net.URI) Transactional(org.springframework.transaction.annotation.Transactional)

Example 39 with User

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);
}
Also used : User(won.owner.model.User) EmailVerificationToken(won.owner.model.EmailVerificationToken) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 40 with User

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);
}
Also used : User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (won.owner.model.User)47 Transactional (org.springframework.transaction.annotation.Transactional)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 URI (java.net.URI)17 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)15 KeystorePasswordHolder (won.owner.model.KeystorePasswordHolder)9 UserAtom (won.owner.model.UserAtom)8 BCryptPasswordEncoder (org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder)7 PasswordEncoder (org.springframework.security.crypto.password.PasswordEncoder)7 Draft (won.owner.model.Draft)7 KeystoreHolder (won.owner.model.KeystoreHolder)6 URISyntaxException (java.net.URISyntaxException)5 Authentication (org.springframework.security.core.Authentication)5 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)4 Autowired (org.springframework.beans.factory.annotation.Autowired)4 CreateDraftPojo (won.owner.pojo.CreateDraftPojo)4 IncorrectPasswordException (won.owner.service.impl.IncorrectPasswordException)4 UserNotFoundException (won.owner.service.impl.UserNotFoundException)4 OnRecoveryKeyGeneratedEvent (won.owner.web.events.OnRecoveryKeyGeneratedEvent)4 ExpensiveSecureRandomString (won.protocol.util.ExpensiveSecureRandomString)4