Ejemplo n.º 1
0
 @PUT
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 @Path("/{userLogin}")
 @PermitAll
 public void activate(@NotNull @PathParam("userLogin") String userLogin, @NotNull String token) {
   User user = userFinder.findByLogin(userLogin);
   if (user != null
       && user.getActionToken() != null
       && user.getActionToken().equals(UUID.fromString(token))) {
     user.setActivated(true);
     user.setActionToken(null);
   } else {
     throw new WebApplicationException(Response.Status.NOT_FOUND);
   }
 }
Ejemplo n.º 2
0
  public static TestUser getInstance() {
    if (instance != null) return instance;

    EntityManager entityManager =
        Persistence.createEntityManagerFactory(UserPersistenceUnit.NAME).createEntityManager();

    entityManager.getTransaction().begin();

    Address address =
        new Address("21 Blue street", "Chicago", "78801", "John", "Doe", "M.", null, "FRA");
    user1 =
        new User(
            "*****@*****.**", "test", "John", "Doe", "+33616161616", null, yesterday, "fr_FR", null);
    user1.setGender("M.");

    entityManager.persist(address);
    entityManager.persist(user1);

    Role adminRole = new Role(RoleName.admin);
    Role userRole = new Role(RoleName.user);
    entityManager.persist(adminRole);
    entityManager.persist(userRole);

    entityManager.getTransaction().commit();

    instance = new TestUser();
    entityManager.close();
    return instance;
  }
Ejemplo n.º 3
0
  private User generateActionTokenAndSendMail(User user, Mails mailType) {

    user.setActionToken(UUID.randomUUID());

    sendMail(user, mailType);

    return user;
  }
Ejemplo n.º 4
0
  private void sendMail(User user, Mails mailType) {
    MailTemplate mailTemplate =
        mailTemplateFinder.findByNameAndLocale(mailType.name(), user.getPreferredLocale());

    if (mailTemplate == null) {
      LOG.debug("Mail template " + mailType + " is not configured.");
      return;
    }

    try {
      Template mailContentTpl =
          new Template(
              mailType.name(),
              mailTemplate.getContent(),
              new Configuration(Configuration.VERSION_2_3_21));
      final StringWriter mailBody = new StringWriter();
      mailContentTpl.process(user, mailBody);
      mailer.sendMail(mailTemplate.getSubject(), user.getLogin(), mailBody.toString());
    } catch (Exception e) {
      LOG.error("Unable to send mail " + mailType + " to user " + user.getLogin(), e);
    }

    return;
  }
Ejemplo n.º 5
0
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @PermitAll
  public User create(@NotNull User user) {

    if (user.getId() != null) {
      throw new WebApplicationException(Response.Status.BAD_REQUEST);
    }
    User userByLogin = userFinder.findByLogin(user.getLogin());

    if (userByLogin != null) {
      throw new WebApplicationException(Response.Status.CONFLICT);
    }

    final Address userAddress = user.getAddress();

    if (userAddress != null) {
      if (userAddress.getId() != null) {
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
      }

      if (!countryChecker.isAvailable(userAddress.getCountryIso3Code())) {
        LOG.error("Country {} is not available", userAddress.getCountryIso3Code());
        throw new WebApplicationException(Response.Status.BAD_REQUEST);
      }
    }

    entityManager.persist(user);
    Role userRole = roleFinder.findByName(RoleName.user);
    user.setRoles(Sets.newHashSet(userRole));

    user.setPassword(hashSha256Base64(user.getPassword()));

    if (!sessionContext.isCallerInRole(ADMIN)) {
      user.setActivated(false);
      generateActionTokenAndSendMail(user, Mails.userRegistration);
    }

    return user;
  }
Ejemplo n.º 6
0
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Path("/{userLogin}/password")
  @PermitAll
  public void resetPassword(
      @NotNull @PathParam("userLogin") String userLogin,
      @QueryParam("token") String token,
      @NotNull String newPassword) {

    User user;

    if (sessionContext.isCallerInRole(ADMIN)) {

      user = userFinder.findByLogin(userLogin);

    } else if (sessionContext.isCallerInRole(USER)) {

      user = userFinder.findByLogin(sessionContext.getCallerPrincipal().getName());

      if (!userLogin.equals(user.getLogin())) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
      }
    } else {
      user = userFinder.findByLogin(userLogin);

      if (user == null || !user.getActionToken().equals(UUID.fromString(token))) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
      }
      user.setActionToken(null);
    }

    user.setPassword(hashSha256Base64(newPassword));
    user.setActivated(true);
    sendMail(user, Mails.userChangePassword);
  }
Ejemplo n.º 7
0
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @RolesAllowed({ADMIN, USER})
  public User modify(@NotNull User user) {

    User existingUser = null;
    if (sessionContext.isCallerInRole(USER) && !sessionContext.isCallerInRole(ADMIN)) {
      existingUser = userFinder.findByLogin(sessionContext.getCallerPrincipal().getName());

      if (!existingUser.getId().equals(user.getId())
          || !existingUser.getLogin().equals(user.getLogin())) {
        throw new WebApplicationException(Response.Status.UNAUTHORIZED);
      }

      user.setActivated(existingUser.getActivated());
      user.setDisabled(existingUser.getDisabled());
      user.setActionToken(existingUser.getActionToken());
    }

    if (existingUser == null) {
      existingUser = entityManager.find(User.class, user.getId());
    }
    checkNotNull(existingUser);
    user.setPassword(existingUser.getPassword());
    user.setCreationDate(existingUser.getCreationDate());
    user.setRoles(existingUser.getRoles());
    return entityManager.merge(user);
  }