Search in sources :

Example 41 with UserIndex

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

the class UserManagementServiceImplTest method testCreateUser.

@Test
public void testCreateUser() {
    UserIndex userIndex = mock(UserIndex.class);
    when(userService.getOrCreateUserForUsernameAndPassword("operator", "password")).thenReturn(userIndex);
    boolean success = service.createUser("operator", "password", false);
    assertTrue("Expecting user to be created successfully", success);
    verify(userService).getOrCreateUserForUsernameAndPassword("operator", "password");
    verify(userService, times(0)).enableAdminRoleForUser(isA(User.class), isA(Boolean.class));
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) User(org.onebusaway.users.model.User) Test(org.junit.Test)

Example 42 with UserIndex

use of org.onebusaway.users.model.UserIndex 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 43 with UserIndex

use of org.onebusaway.users.model.UserIndex 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 44 with UserIndex

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

the class UserManagementServiceImpl method updateUser.

@Override
public boolean updateUser(UserDetail userDetail) {
    User user = userService.getUserForId(userDetail.getId());
    if (user == null) {
        log.info("User '{}' does not exist in the system", userDetail.getUsername());
        return false;
    }
    // Update user password
    if (StringUtils.isNotBlank(userDetail.getPassword())) {
        String credentials = passwordEncoder.encodePassword(userDetail.getPassword(), userDetail.getUsername());
        for (UserIndex userIndex : user.getUserIndices()) {
            userIndex.setCredentials(credentials);
        }
    }
    // Update user role
    updateRole(userDetail.getRole(), user);
    userDao.saveOrUpdateUser(user);
    log.info("User '{}' updated successfully", userDetail.getUsername());
    return true;
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) User(org.onebusaway.users.model.User)

Aggregations

UserIndex (org.onebusaway.users.model.UserIndex)44 User (org.onebusaway.users.model.User)23 UserIndexKey (org.onebusaway.users.model.UserIndexKey)22 Test (org.junit.Test)9 UserBean (org.onebusaway.users.client.model.UserBean)8 UserRole (org.onebusaway.users.model.UserRole)8 HashSet (java.util.HashSet)6 IOException (java.io.IOException)5 WebApplicationException (javax.ws.rs.WebApplicationException)5 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)5 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)5 UserDetail (org.onebusaway.admin.model.ui.UserDetail)5 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 PostConstruct (javax.annotation.PostConstruct)3 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 Response (javax.ws.rs.core.Response)2 UserPropertiesV2 (org.onebusaway.users.model.properties.UserPropertiesV2)2