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:com.healthcit.cacure.businessdelegates.LdapUserManager.java

public EnumSet<RoleCode> getCurrentUserRoleCodes() {
    ArrayList<RoleCode> userRoleCodes = new ArrayList<Role.RoleCode>();

    Collection<GrantedAuthority> currentUserRoles1 = SecurityContextHolder.getContext().getAuthentication()
            .getAuthorities();/*from   w w w. j a va 2  s  .  c  o  m*/
    for (GrantedAuthority currentUserRoles : currentUserRoles1) {
        userRoleCodes.add(RoleCode.valueOf(currentUserRoles.getAuthority()));
    }

    return EnumSet.copyOf(userRoleCodes);
}

From source file:cz.zcu.kiv.eegdatabase.wui.app.session.EEGDataBaseSession.java

public boolean authenticatedSocial() {

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

    for (GrantedAuthority auth : authentication.getAuthorities()) {
        if (auth.getAuthority().equals("ROLE_ANONYMOUS"))
            return false;
    }//from ww w .j a  va2 s  .c  om

    if (authentication.isAuthenticated()) {

        String username = "";

        if (authentication.getPrincipal() instanceof User)
            username = ((User) authentication.getPrincipal()).getUsername();
        else if (authentication.getPrincipal() instanceof Person)
            username = ((Person) authentication.getPrincipal()).getUsername();

        return signIn(username, SOCIAL_PASSWD);
    }

    return false;
}

From source file:cz.zcu.kiv.eegdatabase.wui.app.session.EEGDataBaseSession.java

@Override
public Roles getRoles() {
    Roles roles = new Roles();
    if (isSignedIn()) {

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

        for (GrantedAuthority auth : authentication.getAuthorities()) {
            roles.add(auth.getAuthority());
        }/*from   w w w  .  j av  a 2  s .  c o  m*/
    }

    return roles;
}

From source file:com.blackducksoftware.tools.appedit.web.controller.appdetails.EditAppDetailsController.java

private void verifyUserIsNotAnAuditor(String paramString) throws AppEditControllerException {

    Collection<GrantedAuthority> authorities = getUserRoles();

    // If this user is an auditor, display the Edit NAI Audit Details
    // form/*from   w  w w .  ja v a  2 s .co  m*/
    for (GrantedAuthority auth : authorities) {
        String roleString = auth.getAuthority();
        logger.info("Role: " + roleString);
        Role role = Role.valueOf(roleString);
        logger.info("Role enum value: " + role);
        if (role == Role.ROLE_AUDITOR) {

            String redirectString = "redirect:editnaiauditdetails?" + paramString;

            logger.info("User is an auditor. Redirecting to: " + redirectString);

            throw new AppEditControllerException(redirectString, null);
        }
    }
}

From source file:ch.astina.hesperid.web.components.security.IfRole.java

@SuppressWarnings("unchecked")
private Set authoritiesToRoles(Collection c) {
    Set target = new HashSet();

    for (Iterator iterator = c.iterator(); iterator.hasNext();) {
        GrantedAuthority authority = (GrantedAuthority) iterator.next();

        if (null == authority.getAuthority()) {
            throw new IllegalArgumentException(
                    "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process "
                            + authority.toString());
        }//from w w w . j a  v  a 2  s.  com

        target.add(authority.getAuthority());
    }

    return target;
}

From source file:org.web4thejob.security.SpringSecurityContext.java

@Override
public boolean hasRole(String role) {
    role = "ROLE_" + role;
    if (SecurityContextHolder.getContext().getAuthentication() != null) {
        for (GrantedAuthority grantedAuthority : SecurityContextHolder.getContext().getAuthentication()
                .getAuthorities()) {//  w  w  w .j av a  2  s  . c  o m
            if (grantedAuthority.getAuthority().equals(role)) {
                return true;
            }
        }
    }
    return false;
}

From source file:ch.entwine.weblounge.kernel.security.UserContextFilter.java

/**
 * Loads the user from the given site./*from w w w.j  a  v a2s  .c  o m*/
 * 
 * @param site
 *          the site
 * @return the user
 */
protected User getUser(Site site) {
    logger.trace("Looking up user from spring security context");
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    User user = null;
    Set<Role> roles = new HashSet<Role>();

    if (!securityService.isEnabled()) {
        user = new UserImpl(Security.ADMIN_USER, Security.SYSTEM_CONTEXT, Security.ADMIN_NAME);
        roles.add(SystemRole.SYSTEMADMIN);
        roles.add(getLocalRole(site, SystemRole.SYSTEMADMIN));
    } else if (auth == null) {
        logger.debug("No spring security context available, setting current user to anonymous");
        String realm = site != null ? site.getIdentifier() : Security.SYSTEM_CONTEXT;
        user = new UserImpl(Security.ANONYMOUS_USER, realm, Security.ANONYMOUS_NAME);
        roles.add(SystemRole.GUEST);
        roles.add(getLocalRole(site, SystemRole.GUEST));
    } else {
        Object principal = auth.getPrincipal();
        if (principal == null) {
            logger.warn("No principal found in spring security context, setting current user to anonymous");
            user = new Guest(site.getIdentifier());
            roles.add(getLocalRole(site, SystemRole.GUEST));
        } else if (principal instanceof SpringSecurityUser) {
            user = ((SpringSecurityUser) principal).getUser();
            logger.debug("Principal was identified as '{}'", user.getLogin());
        } else if (principal instanceof UserDetails) {
            UserDetails userDetails = (UserDetails) principal;
            user = new UserImpl(userDetails.getUsername());
            logger.debug("Principal was identified as '{}'", user.getLogin());

            Collection<? extends GrantedAuthority> authorities = auth.getAuthorities();
            if (authorities != null && authorities.size() > 0) {
                for (GrantedAuthority ga : authorities) {
                    logger.debug("Principal '{}' gained role '{}'", user.getLogin(), ga.getAuthority());
                    roles.add(new RoleImpl(ga.getAuthority()));
                }
            }

        } else if (Security.ANONYMOUS_USER.equals(principal)) {
            user = new Guest(site.getIdentifier());
            roles.add(getLocalRole(site, SystemRole.GUEST));
        } else {
            logger.warn("Principal was not compatible with spring security, setting current user to anonymous");
            user = new Guest(site.getIdentifier());
            roles.add(getLocalRole(site, SystemRole.GUEST));
        }
    }

    for (Role role : roles) {
        user.addPublicCredentials(role);
    }

    return user;
}

From source file:ch.astina.hesperid.web.components.security.IfRole.java

/**
 * @param grantedRoles//from  w w w  . j  ava 2  s.  co m
 * @param granted
 * @return a Set of Authorities corresponding to the roles in the grantedRoles
 * that are also in the granted Set of Authorities
 */
@SuppressWarnings("unchecked")
private Set rolesToAuthorities(Set grantedRoles, Collection granted) {
    Set target = new HashSet();

    for (Iterator iterator = grantedRoles.iterator(); iterator.hasNext();) {
        String role = (String) iterator.next();

        for (Iterator grantedIterator = granted.iterator(); grantedIterator.hasNext();) {
            GrantedAuthority authority = (GrantedAuthority) grantedIterator.next();

            if (authority.getAuthority().equals(role)) {
                target.add(authority);
                break;
            }
        }
    }

    return target;
}

From source file:com.kylinolap.rest.service.UserService.java

@Override
public List<String> getUserAuthorities() {
    Scan s = new Scan();
    s.addColumn(Bytes.toBytes(USER_AUTHORITY_FAMILY), Bytes.toBytes(USER_AUTHORITY_COLUMN));

    List<String> authorities = new ArrayList<String>();
    HTableInterface htable = null;/* w w w .ja  v a2  s  . c  o m*/
    ResultScanner scanner = null;
    try {
        htable = HBaseConnection.get(hbaseUrl).getTable(userTableName);
        scanner = htable.getScanner(s);

        for (Result result = scanner.next(); result != null; result = scanner.next()) {
            byte[] uaBytes = result.getValue(Bytes.toBytes(USER_AUTHORITY_FAMILY),
                    Bytes.toBytes(USER_AUTHORITY_COLUMN));
            Collection<? extends GrantedAuthority> authCollection = Arrays
                    .asList(ugaSerializer.deserialize(uaBytes));

            for (GrantedAuthority auth : authCollection) {
                if (!authorities.contains(auth.getAuthority())) {
                    authorities.add(auth.getAuthority());
                }
            }
        }
    } catch (IOException e) {
        logger.error(e.getLocalizedMessage(), e);
    } finally {
        IOUtils.closeQuietly(scanner);
        IOUtils.closeQuietly(htable);
    }

    return authorities;
}