Search in sources :

Example 16 with UserRole

use of org.onebusaway.users.model.UserRole in project onebusaway-application-modules by camsys.

the class UserManagementServiceImplTest method testUpdateUser.

@Test
public void testUpdateUser() {
    String credentials = "encryptedPassword";
    Integer userId = 1;
    UserDetail userDetail = mock(UserDetail.class);
    UserIndex userIndex = mock(UserIndex.class);
    UserRole userRole = mock(UserRole.class);
    UserRole adminRole = mock(UserRole.class);
    Set<UserIndex> userIndices = new HashSet<UserIndex>();
    userIndices.add(userIndex);
    Set<UserRole> userRoles = new HashSet<UserRole>();
    userRoles.add(userRole);
    when(userRole.getName()).thenReturn("ROLE_USER");
    buildUserDetail(userId, userDetail, "password");
    when(userService.getUserForId(userId)).thenReturn(user);
    when(passwordEncoder.encodePassword("password", "admin")).thenReturn(credentials);
    when(user.getUserIndices()).thenReturn(userIndices);
    when(user.getRoles()).thenReturn(userRoles);
    when(authoritiesService.getUserRoleForName("ROLE_ADMINISTRATOR")).thenReturn(adminRole);
    boolean success = service.updateUser(userDetail);
    assertTrue("User updated successfully", success);
    verify(passwordEncoder).encodePassword("password", "admin");
    verify(authoritiesService).getUserRoleForName("ROLE_ADMINISTRATOR");
    verify(userDao).saveOrUpdateUser(user);
}
Also used : UserDetail(org.onebusaway.admin.model.ui.UserDetail) UserIndex(org.onebusaway.users.model.UserIndex) UserRole(org.onebusaway.users.model.UserRole) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 17 with UserRole

use of org.onebusaway.users.model.UserRole in project onebusaway-application-modules by camsys.

the class UserManagementServiceImpl method buildUserDetail.

private UserDetail buildUserDetail(User user) {
    UserDetail userDetail = new UserDetail();
    userDetail.setId(user.getId());
    for (UserIndex userIndex : user.getUserIndices()) {
        userDetail.setUsername(userIndex.getId().getValue());
    }
    for (UserRole role : user.getRoles()) {
        // There should be only one role
        userDetail.setRole(role.getName());
    }
    UserBean bean = userService.getUserAsBean(user);
    userDetail.setDisabled(bean.isDisabled());
    return userDetail;
}
Also used : UserDetail(org.onebusaway.admin.model.ui.UserDetail) UserIndex(org.onebusaway.users.model.UserIndex) UserBean(org.onebusaway.users.client.model.UserBean) UserRole(org.onebusaway.users.model.UserRole)

Example 18 with UserRole

use of org.onebusaway.users.model.UserRole in project onebusaway-application-modules by camsys.

the class AccessControlServiceImpl method userHasPrivilege.

@Override
public boolean userHasPrivilege(User user, Privilege privilege) {
    if (user == null) {
        // anonymous user
        Role role = roleByName.get(StandardAuthoritiesService.ANONYMOUS);
        return roleHasPrivilege(role, privilege);
    }
    Set<UserRole> roles = user.getRoles();
    for (UserRole userRole : roles) {
        Role role = roleByName.get(userRole.getName());
        if (role == null)
            _log.info("No privileges found for role " + userRole.getName());
        else if (role.hasAllPrivileges() || (privilege != null && roleHasPrivilege(role, privilege))) {
            _log.debug(userRole.getName() + " has privileges for " + privilege.getName());
            return true;
        }
    }
    _log.warn("Auth failed for " + user + ", " + privilege);
    return false;
}
Also used : Role(org.onebusaway.admin.model.role.Role) UserRole(org.onebusaway.users.model.UserRole) UserRole(org.onebusaway.users.model.UserRole)

Aggregations

UserRole (org.onebusaway.users.model.UserRole)18 UserIndex (org.onebusaway.users.model.UserIndex)8 HashSet (java.util.HashSet)6 Test (org.junit.Test)6 UserDetail (org.onebusaway.admin.model.ui.UserDetail)4 Date (java.util.Date)3 User (org.onebusaway.users.model.User)3 UserBean (org.onebusaway.users.client.model.UserBean)2 UserIndexKey (org.onebusaway.users.model.UserIndexKey)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 ArrayList (java.util.ArrayList)1 Role (org.onebusaway.admin.model.role.Role)1 UserIndexBean (org.onebusaway.users.client.model.UserIndexBean)1 UserPropertiesV1 (org.onebusaway.users.model.UserPropertiesV1)1 UserPropertiesV2 (org.onebusaway.users.model.properties.UserPropertiesV2)1 UserPropertiesV4 (org.onebusaway.users.model.properties.UserPropertiesV4)1 GrantedAuthorityImpl (org.springframework.security.core.authority.GrantedAuthorityImpl)1