Clover icon

sunshower-core

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

File DefaultApplicationService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
14% of files have more coverage

Code metrics

8
51
14
1
179
157
19
0.37
3.64
14
1.36

Classes

Class Line # Actions
DefaultApplicationService 25 51 19
0.7534246475.3%
 

Contributing tests

This file is covered by 8 tests. .

Source view

1    package io.sunshower.service.application;
2   
3    import io.sunshower.core.security.RoleService;
4    import io.sunshower.core.security.crypto.EncryptionService;
5    import io.sunshower.model.core.Application;
6    import io.sunshower.model.core.ApplicationInitializationException;
7    import io.sunshower.model.core.auth.Activation;
8    import io.sunshower.model.core.auth.Role;
9    import io.sunshower.model.core.auth.User;
10    import io.sunshower.service.security.ActivationService;
11    import io.sunshower.service.security.ApplicationService;
12    import io.sunshower.service.security.DefaultRoles;
13    import io.sunshower.service.signup.SignupService;
14    import java.util.*;
15    import java.util.stream.Collectors;
16    import javax.inject.Inject;
17    import javax.persistence.EntityManager;
18    import javax.persistence.PersistenceContext;
19    import org.springframework.security.access.prepost.PreAuthorize;
20    import org.springframework.stereotype.Service;
21    import org.springframework.transaction.annotation.Transactional;
22   
23    @Service
24    @Transactional
 
25    public class DefaultApplicationService implements ApplicationService, ActivationService {
26   
27    @Inject private RoleService roleService;
28   
29    @PersistenceContext private EntityManager entityManager;
30   
31    @Inject private SignupService signupService;
32   
33    @Inject private EncryptionService encryptionService;
34   
 
35  0 toggle @Override
36    public Application instance() {
37  0 return entityManager
38    .createQuery("select a from Application a", Application.class)
39    .getSingleResult();
40    }
41   
 
42  4 toggle @Override
43    public Boolean isInitialized() {
44  4 return entityManager
45    .createQuery(
46    "select count(a) from Application a " + "where a.enabled = true", Long.class)
47    .getSingleResult()
48    == 1;
49    }
50   
 
51  2 toggle @Override
52    public Application initialize(Application application) {
53  2 if (!isInitialized()) {
54  2 Collection<User> administrators = application.getAdministrators();
55  2 if (administrators == null || administrators.size() == 0) {
56  0 throw new ApplicationInitializationException(
57    "You must add at least one administrator "
58    + "or the application will not be accessible");
59    }
60   
61  2 for (User u : administrators) {
62  2 addAdministrator(u);
63    }
64  2 application.setEnabled(true);
65  2 entityManager.persist(application);
66  2 entityManager.flush();
67  2 return application;
68    } else {
69  0 return instance();
70    }
71    }
72   
 
73  2 toggle @Override
74    public Set<User> getAdministrators() {
75  2 return new HashSet<>(
76    entityManager
77    .createQuery(
78    "select u from User u "
79    + "left join fetch u.roles as r"
80    + " where r.authority = 'admin'",
81    User.class)
82    .getResultList());
83    }
84   
 
85  2 toggle @Override
86    public Boolean addAdministrator(User user) {
87  2 final Role userRole = roleService.findOrCreate(DefaultRoles.SITE_ADMINISTRATOR.toRole());
88  2 user.addRole(userRole);
89  2 user.setPassword(encryptionService.encrypt(user.getPassword()));
90  2 entityManager.merge(user);
91  2 entityManager.flush();
92  2 return true;
93    }
94   
 
95  0 toggle @Override
96    public Boolean removeAdministrator(User user) {
97  0 return null;
98    }
99   
 
100  4 toggle @Override
101    @PreAuthorize("hasAuthority('admin')")
102    public Activation getActivation() {
103  4 return entityManager
104    .createQuery("select a from Activation a where a.active = true", Activation.class)
105    .getSingleResult();
106    }
107   
 
108  8 toggle @Override
109    public boolean isActive() {
110  8 return entityManager
111    .createQuery("select count(a) from Activation a where a.active = true", Long.class)
112    .getSingleResult()
113    > 0;
114    }
115   
 
116  3 toggle @Override
117    @PreAuthorize("hasAuthority('admin')")
118    public List<Activation> list() {
119  3 return entityManager
120    .createQuery("select a from Activation a", Activation.class)
121    .getResultList();
122    }
123   
 
124  1 toggle @Override
125    public User delete(Activation activation) {
126  1 final Activation persisted = entityManager.find(Activation.class, activation.getId());
127  1 if (persisted == null) {
128  0 throw new NoSuchElementException("No no valid activation for that found");
129    }
130  1 entityManager.remove(persisted);
131  1 entityManager.flush();
132  1 return persisted.getActivator();
133    }
134   
 
135  0 toggle @PreAuthorize("@applicationService.isActive() && hasAuthority('admin')")
136    public Activation deactivate() {
137  0 final Activation activation = getActivation();
138  0 activation.getActivator().clearRoles();
139  0 entityManager.remove(activation);
140  0 entityManager.remove(activation.getActivator());
141  0 entityManager.remove(activation.getApplication());
142  0 entityManager.flush();
143  0 return activation;
144    }
145   
 
146  5 toggle public Activation activate(User activator) {
147  5 checkActive();
148  4 final Activation activation = new Activation();
149  4 activation.setActive(true);
150  4 final Application application = new Application();
151  4 application.setName("Sunshower");
152  4 application.setAdministrators(Collections.singletonList(activator));
153  4 activation.setActivator(activator);
154  4 activation.setApplication(application);
155  4 activation
156    .getActivator()
157    .setAuthorities(
158    getRoles(
159    DefaultRoles.SITE_ADMINISTRATOR,
160    DefaultRoles.TENANT_USER,
161    DefaultRoles.TENANT_ADMINISTRATOR));
162    // activation.getActivator().addRole(getAdminRole());
163  4 entityManager.persist(activation);
164  4 return activation;
165    }
166   
 
167  4 toggle private Set<Role> getRoles(DefaultRoles... roles) {
168  4 return Arrays.stream(roles)
169    .map(r -> roleService.findOrCreate(r.toRole()))
170    .collect(Collectors.toSet());
171    }
172   
 
173  5 toggle private void checkActive() {
174  5 if (isActive()) {
175  1 throw new IllegalStateException(
176    String.format("%s is already active!", getActivation().getApplication().getName()));
177    }
178    }
179    }