예제 #1
0
  @Path("/user/save")
  @POST
  @ApiOperation(
      value = "Save or update a user",
      notes = "Save or update a user. If the id is not specified, a new user will be created")
  public Response save(@ApiParam(required = true) UserModel userModel) {
    Preconditions.checkNotNull(userModel);
    Preconditions.checkNotNull(userModel.getName());

    Long id = userModel.getId();
    if (id == null) {
      Preconditions.checkNotNull(userModel.getPassword());

      Set<Role> roles = Sets.newHashSet(Role.USER);
      if (userModel.isAdmin()) {
        roles.add(Role.ADMIN);
      }
      try {
        userService.register(
            userModel.getName(), userModel.getPassword(), userModel.getEmail(), roles, true);
      } catch (Exception e) {
        return Response.status(Status.CONFLICT).entity(e.getMessage()).build();
      }
    } else {
      User user = userDAO.findById(id);
      if (StartupBean.USERNAME_ADMIN.equals(user.getName()) && !userModel.isEnabled()) {
        return Response.status(Status.FORBIDDEN)
            .entity("You cannot disable the admin user.")
            .build();
      }
      user.setName(userModel.getName());
      if (StringUtils.isNotBlank(userModel.getPassword())) {
        user.setPassword(
            encryptionService.getEncryptedPassword(userModel.getPassword(), user.getSalt()));
      }
      user.setEmail(userModel.getEmail());
      user.setDisabled(!userModel.isEnabled());
      userDAO.saveOrUpdate(user);

      Set<Role> roles = userRoleDAO.findRoles(user);
      if (userModel.isAdmin() && !roles.contains(Role.ADMIN)) {
        userRoleDAO.saveOrUpdate(new UserRole(user, Role.ADMIN));
      } else if (!userModel.isAdmin() && roles.contains(Role.ADMIN)) {
        if (StartupBean.USERNAME_ADMIN.equals(user.getName())) {
          return Response.status(Status.FORBIDDEN)
              .entity("You cannot remove the admin role from the admin user.")
              .build();
        }
        for (UserRole userRole : userRoleDAO.findAll(user)) {
          if (userRole.getRole() == Role.ADMIN) {
            userRoleDAO.delete(userRole);
          }
        }
      }
    }
    return Response.ok(Status.OK).entity("OK").build();
  }
예제 #2
0
  public User login(String name, String password) {
    if (name == null || password == null) {
      return null;
    }

    User user = userDAO.findByName(name);
    if (user != null && !user.isDisabled()) {
      boolean authenticated =
          encryptionService.authenticate(password, user.getPassword(), user.getSalt());
      if (authenticated) {
        user.setLastLogin(Calendar.getInstance().getTime());
        userDAO.saveOrUpdate(user);
        return user;
      }
    }

    return null;
  }
예제 #3
0
  public User register(String name, String password, String email, Collection<Role> roles) {
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(password);

    if (userDAO.findByName(name) != null) {
      return null;
    }
    User user = new User();
    byte[] salt = encryptionService.generateSalt();
    user.setName(name);
    user.setEmail(email);
    user.setSalt(salt);
    user.setPassword(encryptionService.getEncryptedPassword(password, salt));
    for (Role role : roles) {
      user.getRoles().add(new UserRole(user, role));
    }
    userDAO.saveOrUpdate(user);
    return user;
  }