use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.
the class UserDataBuilder method buildAsNew.
/**
* Builds instance of {@link User} without id.
*/
public User buildAsNew() {
User user = new User();
user.setName(name);
user.setUsername(username);
user.setEmail(email);
user.setPassword(password);
Set<UserRole> roles = new HashSet<>();
roles.add(role);
user.setRoles(roles);
user.setEnabled(enabled);
return user;
}
use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.
the class TestUtils method createNewUserAndAddToSecurityContext.
/**
* Mock current user.
*/
public static User createNewUserAndAddToSecurityContext() {
final User user = new UserDataBuilder().build();
SecurityContext securityContext = PowerMockito.mock(SecurityContext.class);
Authentication authentication = PowerMockito.mock(Authentication.class);
PowerMockito.mockStatic(SecurityContextHolder.class);
BDDMockito.given(SecurityContextHolder.getContext()).willReturn(securityContext);
Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
Mockito.when(authentication.getPrincipal()).thenReturn(user.getUsername());
return user;
}
use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.
the class UserRepositoryImpl method search.
/**
* Finds Users matching all of the provided parameters.
* If there are no parameters, return all Users.
*/
@Override
public Page<User> search(String username, String email, String name, String role, Pageable pageable) throws IllegalArgumentException {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<User> query = builder.createQuery(User.class);
query = prepareQuery(query, username, email, name, role, false, pageable);
CriteriaQuery<Long> countQuery = builder.createQuery(Long.class);
countQuery = prepareQuery(countQuery, username, email, name, role, true, pageable);
Long count = entityManager.createQuery(countQuery).getSingleResult();
int pageSize = getPageSize(pageable);
int firstResult = getFirstResult(pageable, pageSize);
List<User> users = entityManager.createQuery(query).setMaxResults(pageSize).setFirstResult(firstResult).getResultList();
return new PageImpl<>(users, pageable, count);
}
use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.
the class UserRepositoryImpl method prepareQuery.
private <T> CriteriaQuery<T> prepareQuery(CriteriaQuery<T> query, String username, String email, String name, String role, boolean count, Pageable pageable) throws IllegalArgumentException {
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
Root<User> root = query.from(User.class);
if (count) {
CriteriaQuery<Long> countQuery = (CriteriaQuery<Long>) query;
query = (CriteriaQuery<T>) countQuery.select(builder.count(root));
}
Predicate predicate = builder.conjunction();
if (username != null) {
predicate = builder.and(predicate, builder.like(root.get(USERNAME), '%' + username + '%'));
}
if (email != null) {
predicate = builder.and(predicate, builder.like(root.get(EMAIL), '%' + email + '%'));
}
if (name != null) {
predicate = builder.and(predicate, builder.like(root.get(NAME), '%' + name + '%'));
}
if (role != null) {
Join<User, UserRole> roleJoin = root.join(ROLES);
predicate = builder.and(predicate, builder.like(roleJoin.get(NAME), '%' + role + '%'));
}
query.where(predicate);
if (!count && pageable != null && pageable.getSort() != null) {
query = addSortProperties(query, root, pageable);
}
return query;
}
use of org.motechproject.mots.domain.security.User in project mots by motech-implementations.
the class CustomTokenServices method refreshAccessToken.
@Override
public OAuth2AccessToken refreshAccessToken(String refreshTokenValue, TokenRequest tokenRequest) throws AuthenticationException {
OAuth2AccessToken accessToken = super.refreshAccessToken(refreshTokenValue, tokenRequest);
OAuth2Authentication authentication = tokenStore.readAuthenticationForRefreshToken(accessToken.getRefreshToken());
User user = getUserFromPrincipal(authentication.getUserAuthentication().getPrincipal());
UserLog existingUserLog = userLogService.getUserLog(user);
if (existingUserLog != null) {
existingUserLog.setLogoutDate(accessToken.getExpiration());
userLogService.updateUserLog(existingUserLog);
} else {
userLogService.createNewUserLog(user, accessToken.getExpiration());
}
return accessToken;
}
Aggregations