Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.security.user

File DefaultUserService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
46% of files have more coverage

Code metrics

2
11
6
1
87
75
7
0.64
1.83
6
1.17

Classes

Class Line # Actions
DefaultUserService 18 11 7
0.3157894631.6%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    package io.sunshower.service.security.user;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.core.security.UserService;
5    import io.sunshower.model.core.auth.User;
6    import io.sunshower.model.core.vault.KeyProvider;
7    import java.util.List;
8    import javax.inject.Inject;
9    import javax.persistence.EntityManager;
10    import javax.persistence.PersistenceContext;
11    import org.springframework.security.core.userdetails.UserDetails;
12    import org.springframework.security.core.userdetails.UserDetailsService;
13    import org.springframework.security.core.userdetails.UsernameNotFoundException;
14    import org.springframework.transaction.annotation.Transactional;
15   
16    /** Created by haswell on 10/18/16. */
17    @Transactional
 
18    public class DefaultUserService implements UserService, UserDetailsService {
19   
20    @Inject private KeyProvider keyProvider;
21   
22    @PersistenceContext private EntityManager entityManager;
23   
 
24  0 toggle @Override
25    public User save(User u) {
26  0 entityManager.persist(u);
27  0 entityManager.flush();
28  0 return u;
29    }
30   
 
31  0 toggle @Override
32    public User get(Identifier id) {
33  0 return entityManager.find(User.class, id);
34    }
35   
 
36  7 toggle @Override
37    public User findByUsername(String username) {
38  7 return entityManager
39    .createQuery(
40    "select u from User u " + "left join fetch u.roles r1 " + "where u.username = :name",
41    User.class)
42    .setParameter("name", username)
43    .getSingleResult();
44    }
45   
 
46  3 toggle @Override
47    public List<User> activeUsers() {
48  3 return entityManager
49    .createQuery(
50    "select distinct(u) from User u "
51    + "left join fetch u.roles "
52    + "left join fetch u.details deets "
53    + "where u.active = true",
54    User.class)
55    .getResultList();
56    }
57   
 
58  1 toggle @Override
59    public List<User> inactiveUsers() {
60  1 return entityManager
61    .createQuery(
62    "select distinct(u) from User u "
63    + "left join fetch u.roles "
64    + "left join fetch u.details deets "
65    + "where u.active = false",
66    User.class)
67    .getResultList();
68    }
69   
 
70  0 toggle @Override
71    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
72  0 List<User> users =
73    entityManager
74    .createQuery(
75    "select u from User u "
76    + "left join fetch u.roles r1 "
77    + "where u.username = :name",
78    User.class)
79    .setParameter("name", username)
80    .getResultList();
81   
82  0 if (users.size() > 0) {
83  0 return users.get(0);
84    }
85  0 throw new UsernameNotFoundException("Failed to locate user: " + username);
86    }
87    }