Search in sources :

Example 11 with User

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;
}
Also used : User(org.motechproject.mots.domain.security.User) UserRole(org.motechproject.mots.domain.security.UserRole) HashSet(java.util.HashSet)

Example 12 with 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;
}
Also used : User(org.motechproject.mots.domain.security.User) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UserDataBuilder(org.motechproject.mots.testbuilder.UserDataBuilder)

Example 13 with 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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) PageImpl(org.springframework.data.domain.PageImpl) User(org.motechproject.mots.domain.security.User)

Example 14 with User

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;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) User(org.motechproject.mots.domain.security.User) UserRole(org.motechproject.mots.domain.security.UserRole) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Predicate(javax.persistence.criteria.Predicate)

Example 15 with User

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;
}
Also used : User(org.motechproject.mots.domain.security.User) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UserLog(org.motechproject.mots.domain.security.UserLog)

Aggregations

User (org.motechproject.mots.domain.security.User)17 HashSet (java.util.HashSet)4 UserRole (org.motechproject.mots.domain.security.UserRole)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)2 Test (org.junit.Test)2 AssignedModules (org.motechproject.mots.domain.AssignedModules)2 CommunityHealthWorker (org.motechproject.mots.domain.CommunityHealthWorker)2 DistrictAssignmentLog (org.motechproject.mots.domain.DistrictAssignmentLog)2 Module (org.motechproject.mots.domain.Module)2 EntityNotFoundException (org.motechproject.mots.exception.EntityNotFoundException)2 IvrException (org.motechproject.mots.exception.IvrException)2 ModuleAssignmentException (org.motechproject.mots.exception.ModuleAssignmentException)2 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1