Example #1
0
  @Override
  public UserProfile getUser(Authentication authentication) {
    Object userName = authentication.getPrincipal();
    String login;
    User auth = null;
    if (userName instanceof String) login = (String) userName;
    else {
      login = ((User) authentication.getPrincipal()).getUsername();
      auth = (User) authentication.getPrincipal();
    }

    UserProfile userProfile = new UserProfile();
    userProfile.setUserId(login);
    userProfile.setStatus("ENABLED");

    if (auth != null && !auth.getAuthorities().isEmpty()) {
      for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
        userProfile.addUserRole(grantedAuthority.getAuthority());
      }
    }
    if (auth != null) {
      SystemUser sysUser = systemUserService.findByLogin(login);
      if (sysUser != null) {
        userProfile.setApiKey(sysUser.getApiKey());
        userProfile.setCompany(sysUser.getCompany().getName());
      }
    }

    return userProfile;
  }
Example #2
0
  @Override
  public void enter(ViewChangeListener.ViewChangeEvent event) {
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    List<String> roles = new ArrayList<String>();
    for (GrantedAuthority grantedAuthority : user.getAuthorities()) {
      roles.add(grantedAuthority.getAuthority());
    }

    usernameLabel.setValue(user.getUsername());
    rolesLabel.setValue(StringUtils.join(roles, ","));
  }
 /**
  * Turns the users password, granted authorities and enabled state into a property file value
  *
  * @param user
  * @return
  */
 String serializeUser(User user) {
   StringBuffer sb = new StringBuffer();
   sb.append(user.getPassword());
   sb.append(",");
   for (GrantedAuthority ga : user.getAuthorities()) {
     sb.append(ga.getAuthority());
     sb.append(",");
   }
   sb.append(user.isEnabled() ? "enabled" : "disabled");
   return sb.toString();
 }
  /**
   * Get the list of roles currently known by users (there's guarantee the well known
   * ROLE_ADMINISTRATOR will be part of the lot)
   */
  public List<String> getRoles() {
    checkUserMap();

    Set<String> roles = new TreeSet<String>();
    roles.add("ROLE_ADMINISTRATOR");
    for (User user : getUsers()) {
      for (GrantedAuthority ga : user.getAuthorities()) {
        roles.add(ga.getAuthority());
      }
    }
    return new ArrayList<String>(roles);
  }
 /**
  * Login a user manually/programmatically.
  *
  * @param user the user' object
  */
 @Transactional
 public void login(User user) {
   org.springframework.security.core.userdetails.User authUser =
       new org.springframework.security.core.userdetails.User(
           user.getUsername(),
           user.getPassword(),
           AuthorityUtils.createAuthorityList("ROLE_USER"));
   Authentication auth =
       new UsernamePasswordAuthenticationToken(
           authUser, authUser.getPassword(), authUser.getAuthorities());
   SecurityContextHolder.getContext().setAuthentication(auth);
   LOGGER.info("Programmatically logged in user={}", user);
 }
 public AuthenticationUserDetails(User user) {
   this.login = user.getUsername();
   this.passwordHash = user.getPassword();
   this.enabled = user.isEnabled();
   this.grantedAuthorities.addAll(user.getAuthorities());
 }
 public static boolean hasRole(String role) {
   User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
   return user.getAuthorities().contains(new SimpleGrantedAuthority(role));
 }