コード例 #1
0
  public UserProfile providerSignIn(String username, String providerId, String providerUserId) {
    UserProfile existingUser =
        context.getUserProfileDao().loadUserByProviderUserId(providerId, providerUserId);
    if (existingUser != null) {
      return existingUser;
    } else {
      String email = null;

      if (username.matches("^\\S+@\\S+\\.\\S+$")) {
        email = username;
        username = username.substring(0, username.indexOf('@'));
      }

      String initialUsername = username;

      for (int i = 1; !usernameIsAvailable(initialUsername); i++) {
        initialUsername = String.format("%s_%d", username, i);
      }

      UserProfile user =
          context.getUserProfileDao().createUser(initialUsername, null, email, email != null);
      newUser(user.getUsername());
      return user;
    }
  }
コード例 #2
0
 public LoggedInProfilesResponse getLoggedUsers(int max) {
   LoggedInProfilesResponse response = new LoggedInProfilesResponse();
   List<UserProfile> users = context.getLogManager().getRecentUsers(SESSION_TIMEOUT);
   response.setUsers(users.subList(0, Math.min(max, users.size())));
   response.setCount(users.size());
   return response;
 }
コード例 #3
0
  private boolean initSession(
      UserProfile user,
      String password,
      boolean requirePassword,
      HttpServletRequest request,
      HttpServletResponse response) {

    Authentication auth;

    try {
      if (requirePassword) {
        UsernamePasswordAuthenticationToken token =
            new UsernamePasswordAuthenticationToken(user.getUsername(), password);
        auth = authenticationManager.authenticate(token);
      } else {
        auth = new UsernamePasswordAuthenticationToken(user, null, user.getAuthorities());
      }

      context.getLogManager().loggedIn(user);
      SecurityContextHolder.getContext().setAuthentication(auth);
      securityContextRepository.saveContext(SecurityContextHolder.getContext(), request, response);
      rememberMeServices.loginSuccess(request, response, auth);
      request
          .getSession()
          .setAttribute("nquire-it-token", new BigInteger(260, random).toString(32));
    } catch (Exception ex) {
      auth = null;
    }

    return auth != null
        && auth.getPrincipal() != null
        && auth.getPrincipal() instanceof UserProfile;
  }
コード例 #4
0
  public PublicProfileResponse getPublicProfile(Long userId) {
    PublicProfileResponse response = new PublicProfileResponse();
    UserProfile profile = context.getUserProfileDao().loadUserById(userId);

    if (profile != null) {
      response.setId(profile.getId());
      response.setUsername(profile.getUsername());
      response.setImage(profile.getImage());

      if (profile.getVisibility().get("metadata") && profile.getMetadata() != null) {
        response.getMetadata().putAll(profile.getMetadata());
      }

      boolean joined = profile.getVisibility().get("projectsJoined");
      boolean created = profile.getVisibility().get("projectsCreated");
      response.setProjects(context.getProjectDao().getMyProjects(profile, joined, created));
    }

    return response;
  }
コード例 #5
0
  public boolean deleteConnection(StatusResponse currentStatus, String providerId) {
    if (((currentStatus.getProfile().getPassword() != null
                && currentStatus.getProfile().getPassword().length() > 0)
            || currentStatus.getConnections().size() > 1)
        && context.getUserProfileDao().deleteConnection(currentStatus.getProfile(), providerId)) {
      currentStatus.getConnections().remove(providerId);
      return true;
    }

    return false;
  }
コード例 #6
0
 public StatusResponse logout(
     UserProfile user,
     HashMap<String, Connection<?>> connections,
     HttpServletRequest request,
     HttpServletResponse response) {
   context.getLogManager().loggedOut(user);
   CookieClearingLogoutHandler cookieClearingLogoutHandler =
       new CookieClearingLogoutHandler(
           AbstractRememberMeServices.SPRING_SECURITY_REMEMBER_ME_COOKIE_KEY);
   SecurityContextLogoutHandler securityContextLogoutHandler = new SecurityContextLogoutHandler();
   cookieClearingLogoutHandler.logout(request, response, null);
   securityContextLogoutHandler.logout(request, response, null);
   return status(connections, request.getSession());
 }
コード例 #7
0
  public boolean updateProfile(StatusResponse currentStatus, ProfileRequest data) {
    if (data.getUsername() != null) {
      if (!data.getUsername().equals(currentStatus.getProfile().getUsername())) {

        if (data.getUsername().length() == 0) {
          currentStatus.getResponses().put("username", "username_empty");
        } else if (!usernameIsAvailable(data.getUsername())) {
          currentStatus.getResponses().put("username", "username_not_available");
        } else {
          context
              .getUserProfileDao()
              .updateUsername(currentStatus.getProfile(), data.getUsername());
        }
      }
    }

    if (!data.getEmail().isEmpty()
        && !data.getEmail().equals(currentStatus.getProfile().getEmail())) {
      context.getUserProfileDao().updateEmail(currentStatus.getProfile(), data.getEmail());
    }

    if (!data.getNotify1().equals(currentStatus.getProfile().getNotify1())) {
      context.getUserProfileDao().updateNotify1(currentStatus.getProfile(), data.getNotify1());
    }
    if (!data.getNotify2().equals(currentStatus.getProfile().getNotify2())) {
      context.getUserProfileDao().updateNotify2(currentStatus.getProfile(), data.getNotify2());
    }
    if (!data.getNotify3().equals(currentStatus.getProfile().getNotify3())) {
      context.getUserProfileDao().updateNotify3(currentStatus.getProfile(), data.getNotify3());
    }
    if (!data.getNotify4().equals(currentStatus.getProfile().getNotify4())) {
      context.getUserProfileDao().updateNotify4(currentStatus.getProfile(), data.getNotify4());
    }
    if (!data.getNotify5().equals(currentStatus.getProfile().getNotify5())) {
      context.getUserProfileDao().updateNotify5(currentStatus.getProfile(), data.getNotify5());
    }

    context
        .getUserProfileDao()
        .updateUserInformation(
            currentStatus.getProfile(), data.getMetadata(), data.getVisibility());

    return true;
  }
コード例 #8
0
 public boolean mergeAccount(UserProfile user, UserProfile mergedUser, String provider) {
   if (context.getUserProfileDao().deleteConnection(mergedUser, provider)) {
     context.getLogManager().usersMerged(user, mergedUser);
     context.getVotableDao().transferContent(mergedUser, user);
     context.getVoteDao().transferVotes(mergedUser, user);
     context.getRoleDao().transferRoles(mergedUser, user);
     context.getUserProfileDao().deleteUser(mergedUser);
     return true;
   } else {
     return false;
   }
 }
コード例 #9
0
 public List<UserProfile> getLoggedUsers() {
   return context.getLogManager().getRecentUsers(SESSION_TIMEOUT);
 }
コード例 #10
0
 public Boolean login(
     String username, String password, HttpServletRequest request, HttpServletResponse response) {
   UserProfile user = context.getUserProfileDao().loadUserByUsername(username);
   return user != null && initSession(user, password, true, request, response);
 }
コード例 #11
0
 public boolean isLoggedIn(UserProfile user) {
   return context.getLogManager().userRecentAction(user, SESSION_TIMEOUT);
 }
コード例 #12
0
 public boolean matchPassword(UserProfile user, String password) {
   return context.getUserProfileDao().matchPassword(user, password);
 }
コード例 #13
0
  public StatusResponse remindUser(
      RegisterRequest data,
      HashMap<String, Connection<?>> connections,
      HttpServletRequest request,
      HttpServletResponse response) {
    StatusResponse result = new StatusResponse();
    String string = new String();

    try {
      UserProfile userProfile = loadUserByUsernameOrEmail(data.getEmail());

      System.out.println("ProxyHost=" + this.proxyHost);
      System.out.println("ProxyPort=" + this.proxyPort);
      System.out.println("recaptchaSecretKey=" + this.recaptchaSecretKey);
      // Newer versions of Java need a "http." prefix on the system properties
      System.setProperty("proxyHost", this.proxyHost);
      System.setProperty("proxyPort", this.proxyPort);
      System.setProperty("http.proxyHost", this.proxyHost);
      System.setProperty("http.proxyPort", this.proxyPort);
      URL url =
          new URL(
              "https://www.google.com/recaptcha/api/siteverify?secret="
                  + this.recaptchaSecretKey
                  + "&response="
                  + data.getRecaptcha());
      System.out.println(url.toString());
      Scanner scanner = new Scanner(url.openStream());
      while (scanner.hasNext()) {
        string += scanner.nextLine();
      }
      scanner.close();

      result.setLogged(false);
      result.setProfile(null);
      result.getResponses().put("reminder", "reminder_sent");

      if (string.indexOf("true") == -1) {
        result.setLogged(false);
        result.setProfile(null);
        result.getResponses().put("reminder", "bad_recaptcha");
        return result;
      }

      // Simple random password with 16 hex digits
      String newPassword = Long.toHexString(Double.doubleToLongBits(Math.random()));

      context.getUserProfileDao().setPassword(userProfile, newPassword);

      List<UserProfile> recipients = new ArrayList<UserProfile>();
      recipients.add(userProfile);

      Mailer mailer = new Mailer();
      mailer.sendMail(
          "Account information",
          "Hello nQuire-it user,\n\n"
              + "You (or someone claiming to be you) has requested a new password for your account.\n\n"
              + "Your username is "
              + userProfile.getUsername()
              + "\n"
              + "Your new password is "
              + newPassword
              + "\n\n"
              + "You should login and change this to something more memorable as soon as possible.\n\n"
              + "Warm regards,\nnQuire-it team",
          recipients,
          false);

      return result;
    } catch (UsernameNotFoundException e) {
      result.setLogged(false);
      result.setProfile(null);
      result.getResponses().put("reminder", "email_not_exists");
      return result;
    } catch (java.io.IOException e3) {
      System.out.println("!!!!!" + e3.toString() + "!!!!!");
      result.setLogged(false);
      result.setProfile(null);
      result.getResponses().put("reminder", "bad_recaptcha");
      return result;
    }
  }
コード例 #14
0
  public StatusResponse registerUser(
      RegisterRequest data,
      HashMap<String, Connection<?>> connections,
      HttpServletRequest request,
      HttpServletResponse response) {
    try {
      loadUserByUsername(data.getUsername());
      StatusResponse result = new StatusResponse();
      result.setLogged(false);
      result.setProfile(null);
      result.getResponses().put("registration", "username_exists");
      return result;
    } catch (UsernameNotFoundException e) {

      try {
        context.getUserProfileDao().loadUserByUsername(data.getEmail());
        StatusResponse result = new StatusResponse();
        result.setLogged(false);
        result.setProfile(null);
        result.getResponses().put("registration", "email_exists");
        return result;
      } catch (UsernameNotFoundException e2) {
        String string = new String();

        try {
          System.out.println("ProxyHost=" + this.proxyHost);
          System.out.println("ProxyPort=" + this.proxyPort);
          System.out.println("recaptchaSecretKey=" + this.recaptchaSecretKey);
          // Newer versions of Java need a "http." prefix on the system properties
          System.setProperty("proxyHost", this.proxyHost);
          System.setProperty("proxyPort", this.proxyPort);
          System.setProperty("http.proxyHost", this.proxyHost);
          System.setProperty("http.proxyPort", this.proxyPort);
          URL url =
              new URL(
                  "https://www.google.com/recaptcha/api/siteverify?secret="
                      + this.recaptchaSecretKey
                      + "&response="
                      + data.getRecaptcha());
          System.out.println(url.toString());
          Scanner scanner = new Scanner(url.openStream());
          while (scanner.hasNext()) {
            string += scanner.nextLine();
          }
          scanner.close();
        } catch (java.io.IOException e3) {
          System.out.println("!!!!!" + e3.toString() + "!!!!!");
        }

        if (string.indexOf("true") == -1) {
          StatusResponse result = new StatusResponse();
          result.setLogged(false);
          result.setProfile(null);
          result.getResponses().put("registration", "bad_recaptcha");
          return result;
        }

        UserProfile user =
            context
                .getUserProfileDao()
                .createUser(data.getUsername(), data.getPassword(), data.getEmail(), false);
        login(user, request, response);
        return status(connections, request.getSession());
      }
    }
  }
コード例 #15
0
 public void setPassword(UserProfile user, String password) {
   context.getUserProfileDao().setPassword(user, password);
 }
コード例 #16
0
 @Override
 public UserProfile loadUserByUsername(String s) throws UsernameNotFoundException {
   return context.getUserProfileDao().loadUserByUsername(s);
 }
コード例 #17
0
 public boolean updateProfileImage(StatusResponse currentStatus, FileMapUpload files) {
   return context.getUserProfileDao().updateProfileImage(currentStatus.getProfile(), files);
 }
コード例 #18
0
 public UserProfile loadUserByUsernameOrEmail(String s) throws UsernameNotFoundException {
   return context.getUserProfileDao().loadUserByUsernameOrEmail(s, s);
 }
コード例 #19
0
 public UserProfile currentUser() {
   Authentication auth = SecurityContextHolder.getContext().getAuthentication();
   return auth != null && auth.getPrincipal() != null && auth.getPrincipal() instanceof UserProfile
       ? context.getUserProfileDao().user(((UserProfile) auth.getPrincipal()))
       : null;
 }