Search in sources :

Example 11 with UserRole

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

the class UserManagementServiceImplTest method testUpdatePassword.

@Test
public void testUpdatePassword() {
    String credentials = "encryptedPassword";
    Integer userId = 1;
    UserDetail userDetail = mock(UserDetail.class);
    UserIndex userIndex = mock(UserIndex.class);
    UserRole userRole = 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_ADMINISTRATOR");
    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);
    boolean success = service.updateUser(userDetail);
    assertTrue("User's password updated successfully", success);
    verify(passwordEncoder).encodePassword("password", "admin");
    verify(authoritiesService, times(0)).getAdministratorRole();
    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 12 with UserRole

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

the class UserServiceImpl method getUserAsBean.

@Override
public UserBean getUserAsBean(User user) {
    UserBean bean = new UserBean();
    bean.setUserId(Integer.toString(user.getId()));
    UserRole anonymous = _authoritiesService.getAnonymousRole();
    boolean isAnonymous = user.getRoles().contains(anonymous);
    bean.setAnonymous(isAnonymous);
    UserRole admin = _authoritiesService.getAdministratorRole();
    boolean isAdmin = user.getRoles().contains(admin);
    bean.setAdmin(isAdmin);
    List<UserIndexBean> indices = new ArrayList<UserIndexBean>();
    bean.setIndices(indices);
    for (UserIndex index : user.getUserIndices()) {
        UserIndexKey key = index.getId();
        UserIndexBean indexBean = new UserIndexBean();
        indexBean.setType(key.getType());
        indexBean.setValue(key.getValue());
        indices.add(indexBean);
    }
    _userPropertiesService.getUserAsBean(user, bean);
    return bean;
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexBean(org.onebusaway.users.client.model.UserIndexBean) UserIndexKey(org.onebusaway.users.model.UserIndexKey) UserBean(org.onebusaway.users.client.model.UserBean) UserRole(org.onebusaway.users.model.UserRole) ArrayList(java.util.ArrayList)

Example 13 with UserRole

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

the class UserDaoImplTest method deleteUser.

@Test
public void deleteUser() {
    UserRole userRole = new UserRole("user");
    _dao.saveOrUpdateUserRole(userRole);
    User user = new User();
    user.setCreationTime(new Date());
    user.setProperties(new UserPropertiesV2());
    user.getRoles().add(userRole);
    UserIndexKey key = new UserIndexKey("phone", "2065551234");
    UserIndex index = new UserIndex();
    index.setId(key);
    index.setUser(user);
    user.getUserIndices().add(index);
    _dao.saveOrUpdateUser(user);
    assertEquals(1, _dao.getNumberOfUsers());
    UserIndex index2 = _dao.getUserIndexForId(key);
    assertEquals(key, index2.getId());
    assertEquals(user, index2.getUser());
    _dao.deleteUser(user);
    assertEquals(0, _dao.getNumberOfUsers());
    index2 = _dao.getUserIndexForId(key);
    assertNull(index2);
}
Also used : UserIndex(org.onebusaway.users.model.UserIndex) UserIndexKey(org.onebusaway.users.model.UserIndexKey) User(org.onebusaway.users.model.User) UserRole(org.onebusaway.users.model.UserRole) UserPropertiesV2(org.onebusaway.users.model.properties.UserPropertiesV2) Date(java.util.Date) Test(org.junit.Test)

Example 14 with UserRole

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

the class UserDaoImplTest method test.

@Test
public void test() {
    assertEquals(0, _dao.getNumberOfUserRoles());
    UserRole adminRole = new UserRole("admin");
    UserRole userRole = new UserRole("user");
    _dao.saveOrUpdateUserRole(adminRole);
    _dao.saveOrUpdateUserRole(userRole);
    assertEquals(2, _dao.getNumberOfUserRoles());
    assertEquals(0, _dao.getNumberOfUsersWithRole(adminRole));
    assertEquals(0, _dao.getNumberOfUsersWithRole(userRole));
    User userA = new User();
    userA.setCreationTime(new Date());
    userA.setProperties(new UserPropertiesV1());
    userA.getRoles().add(userRole);
    _dao.saveOrUpdateUser(userA);
    assertEquals(0, _dao.getNumberOfUsersWithRole(adminRole));
    assertEquals(1, _dao.getNumberOfUsersWithRole(userRole));
    User userB = new User();
    userB.setCreationTime(new Date());
    userB.setProperties(new UserPropertiesV1());
    userB.getRoles().add(adminRole);
    _dao.saveOrUpdateUser(userB);
    assertEquals(1, _dao.getNumberOfUsersWithRole(adminRole));
    assertEquals(1, _dao.getNumberOfUsersWithRole(userRole));
    userA.getRoles().add(adminRole);
    _dao.saveOrUpdateUser(userA);
    assertEquals(2, _dao.getNumberOfUsersWithRole(adminRole));
    assertEquals(1, _dao.getNumberOfUsersWithRole(userRole));
}
Also used : User(org.onebusaway.users.model.User) UserRole(org.onebusaway.users.model.UserRole) UserPropertiesV1(org.onebusaway.users.model.UserPropertiesV1) Date(java.util.Date) Test(org.junit.Test)

Example 15 with UserRole

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

the class UserManagementServiceImplTest method testUpdateUserRole.

@Test
public void testUpdateUserRole() {
    Integer userId = 1;
    UserDetail userDetail = mock(UserDetail.class);
    UserIndex userIndex = mock(UserIndex.class);
    UserRole userRole = 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, "");
    when(userService.getUserForId(userId)).thenReturn(user);
    when(user.getUserIndices()).thenReturn(userIndices);
    when(user.getRoles()).thenReturn(userRoles);
    boolean success = service.updateUser(userDetail);
    assertTrue("User's password updated successfully", success);
    verify(passwordEncoder, times(0)).encodePassword("", "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)

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