use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.
the class PhoneNumberLoginInterceptor method isCurrentUserLoggedInWithKey.
private boolean isCurrentUserLoggedInWithKey(UserIndexKey key) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null)
return false;
Object principal = authentication.getPrincipal();
if (!(principal instanceof IndexedUserDetails))
return false;
IndexedUserDetails details = (IndexedUserDetails) principal;
UserIndexKey indexKey = details.getUserIndexKey();
return indexKey.equals(key);
}
use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.
the class PhoneNumberLoginInterceptor method intercept.
@Override
public String intercept(ActionInvocation invocation) throws Exception {
ActionContext context = invocation.getInvocationContext();
Map<String, Object> params = context.getParameters();
String phoneNumber = getPhoneNumber(params);
phoneNumber = PhoneNumberLibrary.normalizePhoneNumber(phoneNumber);
if (phoneNumber != null && phoneNumber.length() > 0) {
UserIndexKey key = new UserIndexKey(UserIndexTypes.PHONE_NUMBER, phoneNumber);
if (params.containsKey(RESET_USER))
_indexedUserDetailsService.resetUserForIndexKey(key);
// Ensure that we have authentication, even if it's anonymous
if (!isCurrentUserLoggedInWithKey(key)) {
IndexedUserDetails userDetails = _indexedUserDetailsService.getOrCreateUserForIndexKey(key, "", false);
DefaultUserAuthenticationToken token = new DefaultUserAuthenticationToken(userDetails);
SecurityContextHolder.getContext().setAuthentication(token);
}
}
return invocation.invoke();
}
use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.
the class UserDaoImplTest method testTransitionUserIndex.
@Test
public void testTransitionUserIndex() {
User userA = new User();
userA.setCreationTime(new Date());
userA.setProperties(new UserPropertiesV2());
UserIndex index = new UserIndex();
index.setId(new UserIndexKey("test", "A"));
index.setUser(userA);
userA.getUserIndices().add(index);
_dao.saveOrUpdateUser(userA);
User userB = new User();
userB.setCreationTime(new Date());
userB.setProperties(new UserPropertiesV2());
_dao.saveOrUpdateUser(userB);
assertEquals(1, _dao.getUserForId(userA.getId()).getUserIndices().size());
assertEquals(0, _dao.getUserForId(userB.getId()).getUserIndices().size());
index.setUser(userB);
userA.getUserIndices().remove(index);
userB.getUserIndices().add(index);
_dao.saveOrUpdateUsers(userA, userB);
assertEquals(0, _dao.getUserForId(userA.getId()).getUserIndices().size());
assertEquals(1, _dao.getUserForId(userB.getId()).getUserIndices().size());
}
use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.
the class UserServiceImplTest method testCompletePhoneNumberRegistration.
@Test
public void testCompletePhoneNumberRegistration() {
User userA = createUser(1234);
UserIndexKey key = new UserIndexKey(UserIndexTypes.PHONE_NUMBER, "12065551234");
UserDao userDao = Mockito.mock(UserDao.class);
_service.setUserDao(userDao);
Mockito.when(userDao.getUserForId(1234)).thenReturn(userA);
UserIndex migratedIndex = new UserIndex();
migratedIndex.setId(key);
migratedIndex.setUser(userA);
migratedIndex.setCredentials("");
Mockito.when(userDao.getUserIndexForId(key)).thenReturn(migratedIndex);
UserIndexRegistrationService registrationService = Mockito.mock(UserIndexRegistrationService.class);
_service.setUserIndexRegistrationService(registrationService);
UserRegistration registration = new UserRegistration(1234, "5555");
Mockito.when(registrationService.getRegistrationForUserIndexKey(key)).thenReturn(registration);
UserPropertiesService userPropertiesService = Mockito.mock(UserPropertiesService.class);
_service.setUserPropertiesService(userPropertiesService);
User userB = createUser(1235);
UserIndex index = createUserIndex(key.getType(), key.getValue(), userB);
UserIndex updated = _service.completePhoneNumberRegistration(index, "5554");
assertTrue(updated == null);
updated = _service.completePhoneNumberRegistration(index, "5555");
assertTrue(updated != null);
Mockito.verify(userPropertiesService).mergeProperties(userB, userA);
}
use of org.onebusaway.users.model.UserIndexKey in project onebusaway-application-modules by camsys.
the class UserServiceImplTest method testRegisterPhoneNumber.
@Test
public void testRegisterPhoneNumber() {
User user = createUser(1234);
double total = 0;
int n = 100;
for (int i = 0; i < n; i++) {
UserIndexRegistrationService registration = Mockito.mock(UserIndexRegistrationService.class);
_service.setUserIndexRegistrationService(registration);
String code = _service.registerPhoneNumber(user, "+12065551234");
UserIndexKey key = new UserIndexKey(UserIndexTypes.PHONE_NUMBER, "12065551234");
Mockito.verify(registration).setRegistrationForUserIndexKey(key, 1234, code);
int codeAsNumber = Integer.parseInt(code);
assertTrue(codeAsNumber >= 1000);
assertTrue(codeAsNumber <= 9999);
total += codeAsNumber;
}
double mu = total / n;
assertEquals(5500, mu, 1000);
}
Aggregations