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:ubc.pavlab.aspiredb.server.security.UserGroupServiceTest.java

/**
 * Tests creating a UserGroup/*  w  ww .  j  a  va2 s  .  c o m*/
 */
@Test
public void testCreateUserGroup() {

    List<GrantedAuthority> authos = new ArrayList<GrantedAuthority>();
    authos.add(new GrantedAuthorityImpl("GROUP_TESTING"));
    this.userManager.createGroup(this.groupName, authos);

    List<GrantedAuthority> findGroupAuthorities = this.userManager.findGroupAuthorities(this.groupName);

    for (GrantedAuthority grantedAuthority : findGroupAuthorities) {
        assertEquals("GROUP_TESTING", grantedAuthority.getAuthority());
    }

}

From source file:ubc.pavlab.aspiredb.server.security.UserGroupServiceTest.java

/**
 * Tests updating the UserGroup//w  w w  . j a  v a  2s. c o m
 */
@Test
public void testUpdateUserGroup() {
    List<GrantedAuthority> authos = new ArrayList<GrantedAuthority>();
    authos.add(new GrantedAuthorityImpl("GROUP_TESTING"));
    this.userManager.createGroup(this.groupName, authos);

    List<GrantedAuthority> findGroupAuthorities = this.userManager.findGroupAuthorities(this.groupName);

    for (GrantedAuthority grantedAuthority : findGroupAuthorities) {
        assertEquals("GROUP_TESTING", grantedAuthority.getAuthority());
    }

    /*
     * Add a user to the group
     */

    this.userManager.addUserToGroup(this.userName1, this.groupName);

    List<String> users = this.userManager.findUsersInGroup(this.groupName);
    assertTrue(users.contains(this.userName1));

    /*
     * Make sure user can see group (from bug 2822)
     */
    gemma.gsec.model.UserGroup group = this.userService.findGroupByName(this.groupName);
    this.securityService.isViewableByUser(group, this.userName1);

    /*
     * Remove a user from the group.
     */
    this.userManager.removeUserFromGroup(this.userName1, this.groupName);
    users = this.userManager.findUsersInGroup(this.groupName);
    assertTrue(!users.contains(this.userName1));

    super.runAsUser(this.userName1);

    /*
     * Can the user remove themselves from the group?
     */
    try {
        this.userManager.removeUserFromGroup(this.userName1, this.groupName);
        fail("Should have gotten access denied when user tried to remove themselves from a group");
    } catch (AccessDeniedException ok) {
        // expected behaviour
    }

    /*
     * Can they elevate the group authority?
     */
    try {
        this.userManager.addGroupAuthority(this.groupName, new GrantedAuthorityImpl("GROUP_ADMIN"));
        fail("Should have gotten access denied when user tried to make group ADMIN");
    } catch (AccessDeniedException ok) {
        // expected behaviour
    }

}

From source file:ubic.gemma.core.security.authentication.UserManagerImpl.java

@Override
public void createGroup(String groupName, List<GrantedAuthority> authorities) {

    UserGroup g = ubic.gemma.model.common.auditAndSecurity.UserGroup.Factory.newInstance();
    g.setName(groupName);//w w w .  j  av  a2s  . c  o m
    for (GrantedAuthority ga : authorities) {
        g.getAuthorities().add(
                ubic.gemma.model.common.auditAndSecurity.GroupAuthority.Factory.newInstance(ga.getAuthority()));
    }

    userService.create(g);
}

From source file:ubic.gemma.core.security.authentication.UserManagerImpl.java

@Override
public void addGroupAuthority(String groupName, GrantedAuthority authority) {
    UserGroup g = this.loadGroup(groupName);

    for (gemma.gsec.model.GroupAuthority ga : g.getAuthorities()) {
        if (ga.getAuthority().equals(authority.getAuthority())) {
            logger.warn("Group already has authority" + authority.getAuthority());
            return;
        }/*from   www.  j a v  a  2 s . c  om*/
    }

    GroupAuthority auth = ubic.gemma.model.common.auditAndSecurity.GroupAuthority.Factory.newInstance();
    auth.setAuthority(authority.getAuthority());

    g.getAuthorities().add(auth);

    userService.update(g);
}

From source file:ubic.gemma.core.security.authentication.UserManagerImpl.java

@Override
public void removeGroupAuthority(String groupName, GrantedAuthority authority) {

    UserGroup group = this.loadGroup(groupName);

    userService.removeGroupAuthority(group, authority.getAuthority());
}

From source file:ubic.gemma.core.security.UserGroupServiceTest.java

/**
 * Tests creating a UserGroup/*w w w. j  a  va 2 s  . co  m*/
 */
@Test
public void testCreateUserGroup() {

    List<GrantedAuthority> authos = new ArrayList<>();
    authos.add(new SimpleGrantedAuthority("GROUP_TESTING"));
    this.userManager.createGroup(this.groupName, authos);

    List<GrantedAuthority> findGroupAuthorities = this.userManager.findGroupAuthorities(this.groupName);

    for (GrantedAuthority grantedAuthority : findGroupAuthorities) {
        assertEquals("GROUP_TESTING", grantedAuthority.getAuthority());
    }

}

From source file:ubic.gemma.core.security.UserGroupServiceTest.java

/**
 * Tests updating the UserGroup/*from w w  w. j a v a 2 s.com*/
 */
@Test
public void testUpdateUserGroup() {
    List<GrantedAuthority> authos = new ArrayList<>();
    authos.add(new SimpleGrantedAuthority("GROUP_TESTING"));
    this.userManager.createGroup(this.groupName, authos);

    List<GrantedAuthority> findGroupAuthorities = this.userManager.findGroupAuthorities(this.groupName);

    for (GrantedAuthority grantedAuthority : findGroupAuthorities) {
        assertEquals("GROUP_TESTING", grantedAuthority.getAuthority());
    }

    /*
     * Add a user to the group
     */

    this.userManager.addUserToGroup(this.userName1, this.groupName);

    List<String> users = this.userManager.findUsersInGroup(this.groupName);
    assertTrue(users.contains(this.userName1));

    /*
     * Make sure user can see group (from bug 2822)
     */
    UserGroup group = this.userService.findGroupByName(this.groupName);
    this.securityService.isViewableByUser(group, this.userName1);

    /*
     * Remove a user from the group.
     */
    this.userManager.removeUserFromGroup(this.userName1, this.groupName);
    users = this.userManager.findUsersInGroup(this.groupName);
    assertTrue(!users.contains(this.userName1));

    super.runAsUser(this.userName1);

    /*
     * Can the user remove themselves from the group?
     */
    try {
        this.userManager.removeUserFromGroup(this.userName1, this.groupName);
        fail("Should have gotten access denied when user tried to remove themselves from a group");
    } catch (AccessDeniedException ok) {
        // expected behaviour
    }

    /*
     * Can they elevate the group authority?
     */
    try {
        this.userManager.addGroupAuthority(this.groupName,
                new SimpleGrantedAuthority(AuthorityConstants.ADMIN_GROUP_AUTHORITY));
        fail("Should have gotten access denied when user tried to make group ADMIN");
    } catch (AccessDeniedException ok) {
        // expected behaviour
    }

}

From source file:ubic.gemma.security.authentication.UserManagerImpl.java

@Override
public void addGroupAuthority(String groupName, GrantedAuthority authority) {
    UserGroup g = loadGroup(groupName);//from ww  w  .ja v  a  2  s  .c  o  m

    for (GroupAuthority ga : g.getAuthorities()) {
        if (ga.getAuthority().equals(authority.getAuthority())) {
            logger.warn("Group already has authority" + authority.getAuthority());
            return;
        }
    }

    GroupAuthority auth = GroupAuthority.Factory.newInstance();
    auth.setAuthority(authority.getAuthority());

    g.getAuthorities().add(auth);

    userService.update(g);
}

From source file:ubic.gemma.security.authentication.UserManagerImpl.java

@Override
public void createGroup(String groupName, List<GrantedAuthority> authorities) {

    UserGroup g = UserGroup.Factory.newInstance();
    g.setName(groupName);//from w w  w . jav a2  s  .c  o  m
    for (GrantedAuthority ga : authorities) {
        g.getAuthorities().add(GroupAuthority.Factory.newInstance(ga.getAuthority()));
    }

    userService.create(g);
}

From source file:ubic.gemma.security.authentication.UserManagerImpl.java

@Override
public void removeGroupAuthority(String groupName, GrantedAuthority authority) {

    UserGroup group = loadGroup(groupName);

    userService.removeGroupAuthority(group, authority.getAuthority());
}