Clover icon

sunshower-core

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

File DefaultSignupService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
24% of files have more coverage

Code metrics

10
40
7
1
143
125
13
0.32
5.71
7
1.86

Classes

Class Line # Actions
DefaultSignupService 25 40 13
0.666666766.7%
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    package io.sunshower.service.security;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.core.security.RoleService;
5    import io.sunshower.core.security.crypto.EncryptionService;
6    import io.sunshower.model.core.auth.Role;
7    import io.sunshower.model.core.auth.User;
8    import io.sunshower.service.signup.RegistrationRequest;
9    import io.sunshower.service.signup.SignupService;
10    import java.util.List;
11    import java.util.Map;
12    import javax.inject.Inject;
13    import javax.persistence.EntityManager;
14    import javax.persistence.EntityNotFoundException;
15    import javax.persistence.PersistenceContext;
16    import org.springframework.aop.support.AopUtils;
17    import org.springframework.context.ApplicationContext;
18    import org.springframework.security.access.prepost.PreAuthorize;
19    import org.springframework.stereotype.Service;
20    import org.springframework.transaction.annotation.Transactional;
21    import org.springframework.util.ReflectionUtils;
22   
23    @Service
24    @Transactional
 
25    public class DefaultSignupService implements SignupService {
26   
27    @Inject private RoleService roleService;
28   
29    @PersistenceContext private EntityManager entityManager;
30   
31    @Inject private EncryptionService encryptionService;
32   
33    @Inject private ApplicationContext applicationContext;
34   
 
35  16 toggle @Override
36    public RegistrationRequest signup(User user) {
37  16 if (user.getRoles() != null) {
38  16 user.getRoles().clear();
39    }
40  16 user.setActive(false);
41  16 final RegistrationRequest registrationRequest = new RegistrationRequest(user);
42  16 entityManager.persist(registrationRequest);
43   
44  16 final Role role = roleService.findOrCreate(DefaultRoles.TENANT_USER.toRole());
45   
46  16 user.setPassword(encryptionService.encrypt(user.getPassword()));
47  16 user.addRole(role);
48   
49  16 entityManager.merge(user);
50  16 entityManager.flush();
51   
52  15 return registrationRequest;
53    }
54   
 
55  5 toggle private void executePostSignup(User user) {
56  5 Map<String, Object> beansWithAnnotation =
57    applicationContext.getBeansWithAnnotation(PostSignupListener.class);
58  5 for (Map.Entry<String, Object> bean : beansWithAnnotation.entrySet()) {
59  0 performPostSignup(bean, user);
60    }
61    }
62   
 
63  0 toggle private void performPostSignup(Map.Entry<String, Object> bean, User user) {
64  0 Class<?> type = AopUtils.getTargetClass(bean.getValue());
65  0 ReflectionUtils.doWithMethods(
66    type,
67    m -> {
68  0 ReflectionUtils.invokeMethod(m, bean.getValue(), user);
69    },
70    t -> {
71  0 if (t.isAnnotationPresent(PostSignup.class)) {
72  0 if (t.getParameterCount() == 1 && t.getParameterTypes()[0].equals(User.class)) {
73  0 return true;
74    } else {
75  0 throw new IllegalStateException(
76    "PostSignup method requires " + "exactly 1 parameter of type user");
77    }
78    }
79  0 return false;
80    });
81    }
82   
 
83  2 toggle @Override
84    @PreAuthorize("hasAuthority('admin')")
85    public List<User> list() {
86  2 return entityManager
87    .createQuery("select u from User u left join fetch u.roles ", User.class)
88    .getResultList();
89    }
90   
 
91  2 toggle @Override
92    @PreAuthorize("hasAuthority('admin')")
93    public User revoke(Identifier uuid) {
94  2 User user = entityManager.find(User.class, uuid);
95  2 if (user != null) {
96  2 user.setActive(false);
97  2 signup(user);
98  2 return user;
99    } else {
100  0 throw new EntityNotFoundException("No user identified by '" + uuid + "' was found!");
101    }
102    }
103   
 
104  5 toggle @Override
105    @PreAuthorize("hasAuthority('admin')")
106    public User approve(String s) {
107  5 final List<RegistrationRequest> u =
108    entityManager
109    .createQuery(
110    "select r from "
111    + "RegistrationRequest r "
112    + "inner join r.user u "
113    + "inner join u.roles "
114    + "inner join u.details "
115    + "where r.requestId = :id",
116    RegistrationRequest.class)
117    .setParameter("id", s)
118    .getResultList();
119  5 if (u.size() == 1) {
120   
121  5 final RegistrationRequest request = u.get(0);
122  5 final User user = request.getUser();
123  5 user.setActive(true);
124  5 entityManager.remove(request);
125  5 entityManager.flush();
126  5 executePostSignup(user);
127  5 return user;
128    }
129  0 throw new EntityNotFoundException("No registration request exists");
130    }
131   
 
132  9 toggle @Override
133    @PreAuthorize("hasAuthority('admin')")
134    public List<RegistrationRequest> pendingRegistrations() {
135  9 return entityManager
136    .createQuery(
137    "select r from RegistrationRequest r "
138    + "inner join r.user as u "
139    + "inner join u.roles as roles",
140    RegistrationRequest.class)
141    .getResultList();
142    }
143    }