use of org.motechproject.security.domain.MotechUser in project motech by motech.
the class PasswordRecoveryServiceBundleIT method setUp.
@Before
@Override
public void setUp() throws Exception {
super.setUp();
recoveriesDataService.deleteAll();
usersDataService.create(new MotechUser(USERNAME, "test", EMAIL, null, null, null, Locale.ENGLISH));
usersDataService.create(new MotechUser(OTHER_USERNAME, "test", OTHER_EMAIL, null, null, null, Locale.ENGLISH));
// Fake time
DateTimeUtils.setCurrentMillisFixed(DateTime.now().minusHours(1).getMillis());
}
use of org.motechproject.security.domain.MotechUser in project motech by motech.
the class MotechOpenIdUserDetailsService method loadUserDetails.
/**
* Adds user for given OpenId to {@link MotechUsersDao}
* and return his {@link org.springframework.security.core.userdetails.UserDetails}
*
* @param token for OpenId
* @return details of added user
*/
@Override
@Transactional
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) {
MotechUser user = motechUsersDao.findUserByOpenId(token.getName());
if (user == null) {
List<String> roles = new ArrayList<>();
if (motechUsersDao.getOpenIdUsers().isEmpty()) {
for (RoleDto role : motechRoleService.getRoles()) {
roles.add(role.getRoleName());
}
}
user = new MotechUser(getAttribute(token.getAttributes(), "Email"), "", getAttribute(token.getAttributes(), "Email"), "", roles, token.getName(), Locale.getDefault());
motechUsersDao.addOpenIdUser(user);
}
return new User(user.getUserName(), user.getPassword(), user.isActive(), true, !UserStatus.MUST_CHANGE_PASSWORD.equals(user.getUserStatus()), !UserStatus.BLOCKED.equals(user.getUserStatus()), authoritiesService.authoritiesFor(user));
}
use of org.motechproject.security.domain.MotechUser in project motech by motech.
the class PasswordRecoveryServiceImpl method passwordRecoveryRequest.
@Override
@Transactional
public String passwordRecoveryRequest(String email, DateTime expiration, boolean notify) throws UserNotFoundException {
MotechUser user = motechUsersDao.findUserByEmail(email);
DateTime expirationDate = expiration;
if (expirationDate == null) {
expirationDate = DateTime.now().plusHours(DEFAULT_EXPIRATION_HOURS);
} else if (expirationDate.isBefore(DateTime.now())) {
throw new IllegalArgumentException("The expiration date shouldn't be a past date!");
}
if (user == null) {
throw new UserNotFoundException("User with email not found: " + email);
}
String token = RandomStringUtils.randomAlphanumeric(TOKEN_LENGTH);
PasswordRecovery recovery = createRecovery(user.getUserName(), user.getEmail(), token, expirationDate, user.getLocale());
if (notify) {
emailSender.sendRecoveryEmail(recovery);
}
LOGGER.info("Created a password recovery for user " + user.getUserName());
return token;
}
use of org.motechproject.security.domain.MotechUser in project motech by motech.
the class MotechUsersDaoTest method shouldNotAllowDuplicateEmails.
@Test(expected = EmailExistsException.class)
public void shouldNotAllowDuplicateEmails() {
MotechUser motechUser = new MotechUser("testuser", "testpassword", "test@test.com", "id", asList("ADMIN"), "", Locale.ENGLISH);
when(usersDataService.findByUserName("testuser")).thenReturn(null);
when(usersDataService.findByEmail("test@test.com")).thenReturn(motechUser);
try {
motechUsersDao.add(motechUser);
} finally {
verify(usersDataService).findByUserName("testuser");
verify(usersDataService).findByEmail("test@test.com");
verify(usersDataService, never()).create(motechUser);
}
}
use of org.motechproject.security.domain.MotechUser in project motech by motech.
the class MotechUsersDaoTest method shouldCreateANewUser.
@Test
public void shouldCreateANewUser() {
MotechUser motechUser = new MotechUser("testuser", "testpassword", "test@test.com", "id", asList("ADMIN"), "", Locale.ENGLISH);
when(usersDataService.findByUserName("testuser")).thenReturn(null);
when(usersDataService.findByEmail("test@test.com")).thenReturn(null);
motechUsersDao.add(motechUser);
verify(usersDataService).findByUserName("testuser");
verify(usersDataService).findByEmail("test@test.com");
verify(usersDataService).create(motechUser);
}
Aggregations