Search in sources :

Example 1 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class WonWebSocketHandler method saveNeedUriWithUser.

private void saveNeedUriWithUser(final WonMessage wonMessage, final WebSocketSession session) {
    User user = getUserForSession(session);
    URI needURI = wonMessage.getReceiverNeedURI();
    UserNeed userNeed = new UserNeed(needURI);
    // reload the user so we can save it
    // (the user object we get from getUserForSession is detached)
    user = userRepository.findOne(user.getId());
    userNeedRepository.save(userNeed);
    user.addNeedUri(userNeed);
    userRepository.save(user);
}
Also used : User(won.owner.model.User) UserNeed(won.owner.model.UserNeed) URI(java.net.URI)

Example 2 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestNeedController method deleteDraft.

@ResponseBody
@RequestMapping(value = "/drafts/draft", method = RequestMethod.DELETE)
public ResponseEntity<String> deleteDraft(@RequestParam("uri") String uri) {
    logger.debug("deleting draft: " + uri);
    URI draftURI = null;
    CreateDraftPojo draftPojo = null;
    User user = getCurrentUser();
    try {
        draftURI = new URI(uri);
        user.getDraftURIs().remove(draftURI);
        wonUserDetailService.save(user);
        Draft draft = draftRepository.findOneByDraftURI(draftURI);
        if (draft == null) {
            logger.warn("draft requested for delete was not found: " + uri);
        } else {
            draftRepository.delete(draft);
        }
        return ResponseEntity.ok().body("\"deleted draft: " + uri + "\"");
    } catch (URISyntaxException e) {
        logger.warn("draft uri problem.", e);
        return ResponseEntity.badRequest().body("draft uri problem");
    }
}
Also used : Draft(won.owner.model.Draft) User(won.owner.model.User) CreateDraftPojo(won.owner.pojo.CreateDraftPojo) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestNeedController method getAllNeedsOfUser.

/**
 * returns a List containing needs belonging to the user
 * @return JSON List of need objects
 */
@ResponseBody
@RequestMapping(value = "/", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public List<URI> getAllNeedsOfUser() {
    logger.info("Getting all needs of user: ");
    User user = getCurrentUser();
    List<UserNeed> userNeeds = user.getUserNeeds();
    List<URI> needUris = new ArrayList(userNeeds.size());
    for (UserNeed userNeed : userNeeds) {
        needUris.add(userNeed.getUri());
    }
    return needUris;
}
Also used : User(won.owner.model.User) UserNeed(won.owner.model.UserNeed) ArrayList(java.util.ArrayList) URI(java.net.URI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class OwnerPersistenceTest method test_delete_UserAtom.

@Test
public void test_delete_UserAtom() throws Exception {
    URI atomUri = URI.create("some:/atom.uri");
    String email = "user@example.com";
    createUserWithAtom(atomUri, email);
    Thread t1 = new Thread(() -> helper.doInSeparateTransaction(() -> createUserWithAtom(atomUri, email)));
    Thread t2 = new Thread(() -> helper.doInSeparateTransaction(() -> {
        User sameUser = userRepository.findByUsername(email);
        UserAtom sameAtom = userAtomRepository.findByAtomUri(atomUri);
        sameUser.removeUserAtom(sameAtom);
        userAtomRepository.delete(sameAtom);
    }));
    t1.start();
    t1.join();
    t2.start();
    t2.join();
}
Also used : User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) URI(java.net.URI) Test(org.junit.Test)

Example 5 with User

use of won.owner.model.User in project webofneeds by researchstudio-sat.

the class RestAtomController method getAllAtomsOfUser.

/**
 * returns a List containing atoms belonging to the user
 *
 * @return JSON List of atom objects
 */
@ResponseBody
@RequestMapping(value = { "/", "" }, produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
public Map<URI, AtomPojo> getAllAtomsOfUser(@RequestParam(value = "state", required = false) AtomState state) {
    User user = getCurrentUser();
    Set<UserAtom> userAtoms = user.getUserAtoms();
    final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    return userAtoms.parallelStream().map(userAtom -> {
        try {
            AuthenticationThreadLocal.setAuthentication(authentication);
            return new AtomPojo(linkedDataSourceOnBehalfOfAtom.getDataForResource(userAtom.getUri(), userAtom.getUri()));
        } catch (Exception e) {
            // we catch all exceptions here as we want to be more robust against
            // unforseen error conditions down the stack
            logger.debug("Could not retrieve atom<" + userAtom.getUri() + "> cause: " + e.getMessage());
            return null;
        } finally {
            // be sure to remove the principal from the threadlocal
            AuthenticationThreadLocal.remove();
        }
    }).filter(Objects::nonNull).collect(Collectors.toMap(AtomPojo::getUri, atomPojo -> atomPojo));
}
Also used : DraftRepository(won.owner.repository.DraftRepository) StringUtils(org.apache.commons.lang.StringUtils) LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) java.util(java.util) UserAtom(won.owner.model.UserAtom) Coordinate(won.protocol.model.Coordinate) AuthenticationThreadLocal(won.protocol.util.AuthenticationThreadLocal) URISyntaxException(java.net.URISyntaxException) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) UserRepository(won.owner.repository.UserRepository) Controller(org.springframework.stereotype.Controller) AtomPojo(won.owner.pojo.AtomPojo) Propagation(org.springframework.transaction.annotation.Propagation) CreateDraftPojo(won.owner.pojo.CreateDraftPojo) Qualifier(org.springframework.beans.factory.annotation.Qualifier) URI(java.net.URI) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Dataset(org.apache.jena.query.Dataset) WonNodeInformationService(won.protocol.service.WonNodeInformationService) Logger(org.slf4j.Logger) WonLinkedDataUtils(won.protocol.util.linkeddata.WonLinkedDataUtils) MediaType(org.springframework.http.MediaType) MethodHandles(java.lang.invoke.MethodHandles) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Collectors(java.util.stream.Collectors) AtomState(won.protocol.model.AtomState) LDPContainerPage(won.protocol.util.linkeddata.LDPContainerPage) HttpStatus(org.springframework.http.HttpStatus) User(won.owner.model.User) WONUserDetailService(won.owner.service.impl.WONUserDetailService) DateTimeFormatter(java.time.format.DateTimeFormatter) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) ResponseEntity(org.springframework.http.ResponseEntity) Draft(won.owner.model.Draft) Authentication(org.springframework.security.core.Authentication) Transactional(org.springframework.transaction.annotation.Transactional) User(won.owner.model.User) UserAtom(won.owner.model.UserAtom) Authentication(org.springframework.security.core.Authentication) AtomPojo(won.owner.pojo.AtomPojo) URISyntaxException(java.net.URISyntaxException) AccessDeniedException(org.springframework.security.access.AccessDeniedException)

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