Example usage for org.springframework.security.core GrantedAuthority getAuthority

List of usage examples for org.springframework.security.core GrantedAuthority getAuthority

Introduction

In this page you can find the example usage for org.springframework.security.core GrantedAuthority getAuthority.

Prototype

String getAuthority();

Source Link

Document

If the GrantedAuthority can be represented as a String and that String is sufficient in precision to be relied upon for an access control decision by an AccessDecisionManager (or delegate), this method should return such a String.

Usage

From source file:org.ihtsdo.otf.refset.security.RefsetIdentityService.java

private boolean isUserHasRole(User auth) {

    boolean isUserHasRole = false;

    if (auth != null && auth.isAuthenticated()) {

        Collection<? extends GrantedAuthority> roles = auth.getAuthorities();
        for (GrantedAuthority role : roles) {

            isUserHasRole = "ROLE_USER".equals(role.getAuthority()) ? true : false;

            if (isUserHasRole) {

                break;
            }/* w  w w . j  ava2  s.  com*/
        }

    }

    return isUserHasRole;
}

From source file:com.devnexus.ting.model.User.java

@Override
public Collection<GrantedAuthority> getAuthorities() {
    final Collection<GrantedAuthority> authorities = new java.util.ArrayList<GrantedAuthority>();
    for (GrantedAuthority authority : this.getUserAuthorities()) {
        authorities.add(new SimpleGrantedAuthority("ROLE_" + authority.getAuthority()));
    }/*from ww  w.  j a  v  a 2  s .c  o  m*/
    // authorities.addAll(this.getUserAuthorities());
    return authorities;
}

From source file:org.opendatakit.persistence.table.UserGrantedAuthority.java

private final void setGrantedAuthority(GrantedAuthority value) {
    if (!this.setStringField(GRANTED_AUTHORITY, value.getAuthority())) {
        throw new IllegalStateException("overflow grantedAuthority");
    }//from  w  ww. j a va  2 s  . co m
}

From source file:com.qpark.eip.core.spring.security.EipRoleVoter.java

public String getGrantedRoles(final Authentication authentication) {
    TreeSet<String> ts = new TreeSet<String>();
    StringBuffer sb = new StringBuffer(1024);
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority authority : authorities) {
        ts.add(authority.getAuthority());
    }//from  w  w  w.j ava2s  .  co m
    for (String string : ts) {
        if (sb.length() > 0) {
            sb.append(", ");
        }
        sb.append(string);
    }

    return sb.toString();
}

From source file:com.browsexml.ldap.LdapUserDetails.java

/**
 * Copy to a type that can be displayed in the gwt client
 * @param s/*from   w w w.j  a v a  2 s.c o m*/
 */
public SimpleLoginType copy(SimpleLoginType s) {
    s.setEmail(mail);
    s.setName(givenName + " " + surname);
    //s.setName(givenName);
    s.setPeopleId(employeeId);
    HashSet<String> roles = new HashSet<String>();
    for (GrantedAuthority x : authorities) {
        roles.add(x.getAuthority());
    }
    s.setRoles(roles);
    return s;
}

From source file:com.healthcit.cacure.businessdelegates.LdapUserManager.java

public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
        Collection<GrantedAuthority> authority) {

    userService.setAuthType(Constants.LDAP_AUTH_VALUE);

    // local inner class
    class GrantedAuthorityImpl implements GrantedAuthority {
        private static final long serialVersionUID = -4708051153956036063L;
        private final String role;

        public GrantedAuthorityImpl(String role) {
            this.role = role;
        }/*from w ww.  jav  a2 s  .  com*/

        @Override
        public String getAuthority() {
            return role;
        }

        @SuppressWarnings("unused")
        public int compareTo(Object o) {
            if (o instanceof GrantedAuthority) {
                return role.compareTo(((GrantedAuthority) o).getAuthority());
            }
            return -1;
        }
    }

    UserDetails originalUser = super.mapUserFromContext(ctx, username, authority);

    // Current authorities come from LDAP groups
    Collection<GrantedAuthority> newAuthorities = originalUser.getAuthorities();

    List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
    if (newAuthorities != null && !newAuthorities.isEmpty()) {

        for (GrantedAuthority currentUserRoles : newAuthorities) {
            grantedAuthorityList.add(new GrantedAuthorityImpl(currentUserRoles.getAuthority()));
        }
    }

    UserDetails res = new User(originalUser.getUsername(), originalUser.getPassword(), true, true, true, true,
            grantedAuthorityList);

    this.userDetails = res;
    return res;
}

From source file:com.cloudseal.spring.client.userdetails.CloudSealUserAttributesTest.java

@Test
public void rolesFromManyAttributeValues() {
    final GrantedAuthority role1 = new GrantedAuthorityImpl("USER");
    final GrantedAuthority role2 = new GrantedAuthorityImpl("ADMIN");

    setStringAttributes(ROLES, role1.getAuthority(), role2.getAuthority());
    assertThat(new CloudsealUserAttributes(credential).getRoles(), hasSameOrder(asList(role1, role2)));
}

From source file:org.exoplatform.acceptance.ui.model.CurrentUser.java

/**
 * Simple searches for an exactly matching {@link org.springframework.security.core.GrantedAuthority#getAuthority()}.
 * Will always return false if the SecurityContextHolder contains an Authentication with nullprincipal and/or GrantedAuthority[] objects.
 *
 * @param role the GrantedAuthorityString representation to check for
 * @return true if an exact (case sensitive) matching granted authority is located, false otherwise
 *///from  www .j  a v a2s  .  c  o m
public boolean hasRole(String role) {
    if (isAuthenticated()) {
        for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication()
                .getAuthorities()) {
            if (authority.getAuthority().equals(role)) {
                return true;
            }
        }
    }
    return false;
}

From source file:com.formkiq.core.service.SpringSecurityService.java

/**
 * Whether User is admin.//from ww  w  .  j av  a2 s .  c  o  m
 * @return boolean
 */
public boolean isAdmin() {

    Authentication auth = SecurityContextHolder.getContext().getAuthentication();

    if (auth != null) {

        for (GrantedAuthority grantedAuthority : auth.getAuthorities()) {
            if (UserRole.ROLE_ADMIN.name().equals(grantedAuthority.getAuthority())) {
                return true;
            }
        }
    }

    return false;
}

From source file:com.cloudseal.spring.client.userdetails.CloudSealUserAttributesTest.java

@Test
public void rolesFromManyAttributes() {
    final GrantedAuthority role1 = new GrantedAuthorityImpl("USER");
    final GrantedAuthority role2 = new GrantedAuthorityImpl("ADMIN");

    setAttributes(createStringAttribute(ROLES, role1.getAuthority()),
            createStringAttribute(ROLES, role2.getAuthority()));
    assertThat(new CloudsealUserAttributes(credential).getRoles(), hasSameOrder(asList(role1, role2)));
}