Clover icon

sunshower-sdk

  1. Project Clover database Sat May 12 2018 05:15:40 UTC
  2. Package io.sunshower.sdk.v1.core.security

File DefaultSignupEndpoint.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
18
6
1
95
84
8
0.44
3
6
1.33

Classes

Class Line # Actions
DefaultSignupEndpoint 27 18 8
0.923076992.3%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    package io.sunshower.sdk.v1.core.security;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.model.core.auth.User;
5    import io.sunshower.sdk.lang.IdentifierElement;
6    import io.sunshower.sdk.v1.endpoints.core.security.SignupEndpoint;
7    import io.sunshower.sdk.v1.model.core.Registrations;
8    import io.sunshower.sdk.v1.model.core.Users;
9    import io.sunshower.sdk.v1.model.core.faults.DuplicateElementException;
10    import io.sunshower.sdk.v1.model.core.security.PrincipalElement;
11    import io.sunshower.sdk.v1.model.core.security.RegistrationConfirmationElement;
12    import io.sunshower.sdk.v1.model.core.security.RegistrationRequestElement;
13    import io.sunshower.service.signup.RegistrationRequest;
14    import io.sunshower.service.signup.SignupService;
15    import org.springframework.security.access.prepost.PreAuthorize;
16    import org.springframework.transaction.annotation.Transactional;
17   
18    import javax.inject.Inject;
19    import javax.persistence.EntityManager;
20    import javax.persistence.PersistenceContext;
21    import javax.persistence.PersistenceException;
22    import java.util.List;
23    import java.util.NoSuchElementException;
24    import java.util.stream.Collectors;
25   
26    @Transactional
 
27    public class DefaultSignupEndpoint implements SignupEndpoint {
28   
29    @PersistenceContext private EntityManager entityManager;
30    @Inject private Registrations registrations;
31    @Inject private SignupService signupService;
32    @Inject private Users users;
33   
 
34  8 toggle @Override
35    public RegistrationConfirmationElement signup(RegistrationRequestElement request) {
36  8 try {
37  8 final User user = registrations.toUser(request);
38  8 final RegistrationRequest signup = signupService.signup(user);
39   
40  6 return registrations.toConfirmation(signup);
41    } catch (PersistenceException ex) {
42  1 throw new DuplicateElementException("username and e-mail address must be unique");
43    }
44    }
45   
 
46  0 toggle @Override
47    @PreAuthorize("hasAuthority('admin')")
48    public List<PrincipalElement> approvedUsers() {
49  0 return entityManager
50    .createQuery("select e from User e where e.active = true", User.class)
51    .getResultList()
52    .stream()
53    .map(users::toElement)
54    .collect(Collectors.toList());
55    }
56   
 
57  6 toggle @Override
58    public List<RegistrationRequestElement> list() {
59  6 return signupService
60    .pendingRegistrations()
61    .stream()
62    .map(registrations::toElement)
63    .collect(Collectors.toList());
64    }
65   
 
66  3 toggle @Override
67    public IdentifierElement approve(String id) {
68  3 User user = signupService.approve(id);
69  3 return new IdentifierElement(user.getId());
70    }
71   
 
72  1 toggle @Override
73    public String revoke(Identifier id) {
74  1 signupService.revoke(id);
75  1 return id.toString();
76    }
77   
 
78  5 toggle @Override
79    public RegistrationRequestElement delete(String id) {
80  5 List<RegistrationRequest> requests =
81    entityManager
82    .createQuery(
83    "select r from RegistrationRequest r where r.requestId = :id",
84    RegistrationRequest.class)
85    .setParameter("id", id)
86    .getResultList();
87  5 if (!requests.isEmpty()) {
88  3 RegistrationRequest request = requests.get(0);
89  3 entityManager.remove(request);
90  3 entityManager.flush();
91  3 return registrations.toElement(request);
92    }
93  2 throw new NoSuchElementException("No registration request with that id");
94    }
95    }