Search in sources :

Example 26 with User

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

the class BridgeForLinkedDataController method currentUserHasIdentity.

/**
 * Check if the current user has the claimed identity represented by web-id of
 * the need. I.e. if the identity is that of the need that belongs to the user -
 * return true, otherwise - false.
 *
 * @param requesterWebId
 * @return
 */
private boolean currentUserHasIdentity(final String requesterWebId) {
    String username = SecurityContextHolder.getContext().getAuthentication().getName();
    User user = (User) wonUserDetailService.loadUserByUsername(username);
    Set<URI> needUris = getUserNeedUris(user);
    if (needUris.contains(URI.create(requesterWebId))) {
        return true;
    }
    return false;
}
Also used : User(won.owner.model.User) URI(java.net.URI)

Example 27 with User

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

the class RestNeedController 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();
    Iterator<URI> draftURIIterator = draftURIs.iterator();
    while (draftURIIterator.hasNext()) {
        URI draftURI = draftURIIterator.next();
        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) ArrayList(java.util.ArrayList) CreateDraftPojo(won.owner.pojo.CreateDraftPojo) URI(java.net.URI) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 28 with User

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

the class RestNeedController method createDraft.

/**
 * saves draft of a draft
 * @param createDraftObject an object containing information of the need 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) throws ParseException {
    User user = getCurrentUser();
    URI draftURI = URI.create(createDraftObject.getDraftURI());
    user.getDraftURIs().add(draftURI);
    wonUserDetailService.save(user);
    Draft draft = null;
    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) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with User

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

the class RestUserController method registerUser.

/**
 * Registers the specified user with password and an opional role.
 * Assumes values have already been checked for syntactic validity.
 * @param email
 * @param password
 * @param role
 * @throws UserAlreadyExistsException
 */
private void registerUser(String email, String password, String role) throws UserAlreadyExistsException {
    User user = userRepository.findByUsername(email);
    if (user != null) {
        throw new UserAlreadyExistsException();
    }
    try {
        PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        user = new User(email, passwordEncoder.encode(password), role);
        user.setEmail(email);
        KeystorePasswordHolder keystorePassword = new KeystorePasswordHolder();
        // generate a password for the keystore and save it in the database, encrypted with a symmetric key
        // derived from the user's password
        keystorePassword.setPassword(KeystorePasswordUtils.generatePassword(KeystorePasswordUtils.KEYSTORE_PASSWORD_BYTES), password);
        // keystorePassword = keystorePasswordRepository.save(keystorePassword);
        // generate the keystore for the user
        KeystoreHolder keystoreHolder = new KeystoreHolder();
        try {
            // create the keystore if it doesnt exist yet
            keystoreHolder.getKeystore(keystorePassword.getPassword(password));
        } catch (Exception e) {
            throw new IllegalStateException("could not create keystore for user " + email);
        }
        // keystoreHolder = keystoreHolderRepository.save(keystoreHolder);
        user.setKeystorePasswordHolder(keystorePassword);
        user.setKeystoreHolder(keystoreHolder);
        userRepository.save(user);
    } catch (DataIntegrityViolationException e) {
        // username is already in database
        throw new UserAlreadyExistsException();
    }
}
Also used : User(won.owner.model.User) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) PasswordEncoder(org.springframework.security.crypto.password.PasswordEncoder) KeystoreHolder(won.owner.model.KeystoreHolder) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) KeystorePasswordHolder(won.owner.model.KeystorePasswordHolder) BCryptPasswordEncoder(org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder) URISyntaxException(java.net.URISyntaxException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UserAlreadyExistsException(won.owner.service.impl.UserAlreadyExistsException) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Example 30 with User

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

the class WonWebSocketHandler method afterConnectionEstablished.

@Override
public void afterConnectionEstablished(final WebSocketSession session) throws Exception {
    super.afterConnectionEstablished(session);
    // remember which user or (if not logged in) which atomUri the session is bound
    // to
    User user = getUserForSession(session);
    if (user != null) {
        logger.debug("connection established, binding session to user {}", user.getId());
        this.webSocketSessionService.addMapping(user, session);
    } else {
        logger.debug("connection established, but no user found in session to bind to");
    }
}
Also used : User(won.owner.model.User)

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