コード例 #1
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @GET
  @Path("logout")
  @Consumes(MediaType.TEXT_PLAIN)
  @Override
  public Response logout(
      @QueryParam("serviceKey") String serviceKey,
      @QueryParam("email") String email,
      @QueryParam("sessionKey") String sessionKey) {
    RegisteredServiceRequest request = new RegisteredServiceRequest() {};
    request.setServiceKey(serviceKey);

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException e) {
      return Response.status(405).build();
    }

    if (StringUtils.isBlank(email) || StringUtils.isBlank(sessionKey)) {
      return Response.status(400).build();
    }

    userService.logout(email, sessionKey);

    return Response.status(200).build();
  }
コード例 #2
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @POST
  @Path("register")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public AuthenticationResponse register(UserRegistrationRequest request) {
    AuthenticationResponse response = new AuthenticationResponse();

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException ee) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/register", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    }

    if (StringUtils.isBlank(request.getEmail())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "email"));
      return response;
    } else if (StringUtils.isBlank(request.getPassword())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "password"));
      return response;
    } else if (StringUtils.isBlank(request.getFirstName())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "firstName"));
      return response;
    } else if (StringUtils.isBlank(request.getLastName())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/register", request, "lastName"));
      return response;
    }

    if (!EmailValidator.getInstance().isValid(request.getEmail())) {
      response.setInvalidEmailJSONException(
          new InvalidEmailJSONException("user/register", request.getEmail()));
      return response;
    }

    User user = new User();
    user.setEmail(request.getEmail().toLowerCase());
    user.setFirstName(request.getFirstName());
    user.setLastName(request.getLastName());

    try {
      String sessionKey = userService.register(user, request.getPassword(), Platform.OTHER);
      response.setSessionKey(sessionKey);
      return response;
    } catch (EmailAlreadyInUseException e) {
      response.setRegisterEmailAlreadyInUseJSONException(
          new RegisterEmailAlreadyInUseJSONException("user/register", request.getEmail()));
      return response;
    } catch (PasswordLenghtInvalidException e) {
      response.setPasswordLengthInvalidJSONException(
          new PasswordLengthInvalidJSONException("user/register"));
      return response;
    }
  }
コード例 #3
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @POST
  @Path("login")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public AuthenticationResponse login(UserLoginRequest request) {
    AuthenticationResponse response = new AuthenticationResponse();

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException e1) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/login", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    }

    try {
      String sessionKey =
          userService.login(request.getEmail(), request.getPassword(), Platform.OTHER);
      response.setSessionKey(sessionKey);
      return response;
    } catch (UserNotFoundException e) {
      response.setEmailOrPasswordIncorrectJSONException(
          new EmailOrPasswordIncorrectJSONException("user/login"));
      return response;
    } catch (PasswordIncorrectException e) {
      response.setEmailOrPasswordIncorrectJSONException(
          new EmailOrPasswordIncorrectJSONException("user/login"));
      return response;
    }
  }
コード例 #4
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @GET
  @Path("profile")
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public UserProfileResponse profile(
      @QueryParam("serviceKey") String serviceKey,
      @QueryParam("email") String email,
      @QueryParam("sessionKey") String sessionKey) {
    UserProfileResponse response = new UserProfileResponse();

    AuthenticatedUserRequest request = new AuthenticatedUserRequest() {};
    request.setServiceKey(serviceKey);
    request.setEmail(email);
    request.setSessionKey(sessionKey);

    try {
      securityChecker.checkUserLoggedIn(request);
    } catch (ServiceNotAllowedException e) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/profile", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    } catch (UserNotLoggedInException e) {
      UserNotLoggedInJSONException exception = new UserNotLoggedInJSONException("user/profile");
      response.setUserNotLoggedInException(exception);
      return response;
    }

    User user = userService.findUser(email);

    response.setFirstName(user.getFirstName());
    response.setLastName(user.getLastName());
    response.setEmail(user.getEmail());
    response.setRegisteredSince(user.getRegistrationDate());
    response.setRole(user.getRole());
    response.setLoggedInSince(userService.getLogInTime(user, sessionKey));

    return response;
  }
コード例 #5
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @GET
  @Path("resetPasswordRequest")
  @Consumes(MediaType.TEXT_PLAIN)
  @Override
  public Response resetPasswordRequest(
      @QueryParam("serviceKey") String serviceKey, @QueryParam("email") String email) {
    RegisteredServiceRequest request = new RegisteredServiceRequest() {};
    request.setServiceKey(serviceKey);

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException e) {
      return Response.status(405).build();
    }

    userService.resetPasswordRequest(email);

    return Response.status(200).build();
  }
コード例 #6
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @POST
  @Path("resetPassword")
  @Consumes(MediaType.APPLICATION_JSON)
  @Override
  public ResetPasswordResponse resetPassword(ResetPasswordRequest request) {
    ResetPasswordResponse response = null;

    if (StringUtils.isBlank(request.getNewPassword())) {
      response.setFieldRequiredJSONException(
          new FieldRequiredJSONException("user/resetPassword", request, "newPassword"));
    }

    try {
      securityChecker.checkService(request);
    } catch (ServiceNotAllowedException e) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/resetPassword", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    }

    try {
      userService.resetPassword(request.getPasswordResetKey(), request.getNewPassword());
    } catch (PasswordLenghtInvalidException e) {
      response.setPasswordLengthInvalidJSONException(
          new PasswordLengthInvalidJSONException("user/resetPassword"));
    } catch (InvalidPasswordResetKeyException e) {
      response.setInvalidPasswordResetKeyJSONException(new InvalidPasswordResetKeyJSONException());
    } catch (PasswordResetKeyAlreadyUsedException e) {
      response.setPasswordResetKeyAlreadyUsedJSONException(
          new PasswordResetKeyAlreadyUsedJSONException());
    } catch (PasswordResetKeyExpiredException e) {
      response.setPasswordResetKeyExpiredJSONException(new PasswordResetKeyExpiredJSONException());
    }

    return response;
  }
コード例 #7
0
ファイル: UserEndpoint.java プロジェクト: janls/worktime
  @POST
  @Path("changePermissions")
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.APPLICATION_JSON)
  @Override
  public ChangePermissionsResponse changePermissions(UserChangePermissionsRequest request) {
    ChangePermissionsResponse response = new ChangePermissionsResponse();

    try {
      securityChecker.checkUserIsAdmin(request);
    } catch (ServiceNotAllowedException e) {
      ServiceNotAllowedJSONException exception =
          new ServiceNotAllowedJSONException("user/changePermissions", request.getServiceKey());
      response.setServiceNotAllowedException(exception);
      return response;
    } catch (UserNotLoggedInException e) {
      UserNotLoggedInJSONException exception =
          new UserNotLoggedInJSONException("user/changePermissions");
      response.setUserNotLoggedInException(exception);
      return response;
    } catch (UserNotAdminException e) {
      UserIncorrectRoleException exception =
          new UserIncorrectRoleException("user/changePermissions", Role.ADMIN);
      response.setUserIncorrectRoleException(exception);
      return response;
    }

    try {
      userService.changePermissions(request.getUserToChange(), request.getNewRole());
      return response;
    } catch (UserNotFoundException e) {
      response.setUserNotFoundJSONException(
          new UserNotFoundJSONException("user/changePermissions", request.getUserToChange()));
      return response;
    }
  }