/** * Extracts user groups from {@link SecurityRealm}. * * @param userId * @return List of effective groups. Null if there's no info */ private static @CheckForNull List<String> getAuthoritiesFromRealm(@Nonnull String userId) { final Jenkins instance = Jenkins.getInstance(); if (instance == null) { return null; // Jenkins has not been started yet } @CheckForNull UserDetails userDetails = null; try { final SecurityRealm sr = instance.getSecurityRealm(); userDetails = sr.loadUserByUsername(userId); } catch (DataAccessException ex) { // fallback to null handler } catch (UsernameNotFoundException ex) { // fallback to null handler } if (userDetails == null) { return null; } GrantedAuthority[] authorities = userDetails.getAuthorities(); List<String> authorityList = new ArrayList<String>(authorities.length); for (GrantedAuthority auth : authorities) { authorityList.add(auth.getAuthority()); } return authorityList; }
@Override public UserQuery createUserQuery() { if (jenkins.isUseSecurity()) return new JenkowUserQueryImpl(jenkins.getSecurityRealm()); else return new AnonymousUserQueryImpl(); }