Clover icon

sunshower-sdk

  1. Project Clover database Wed Apr 4 2018 21:39:43 UTC
  2. Package io.sunshower.sdk.v1.core.security

File DefaultUserEndpoint.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
12% of files have more coverage

Code metrics

4
10
2
1
48
42
4
0.4
5
2
2

Classes

Class Line # Actions
DefaultUserEndpoint 21 10 4
0.87587.5%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    package io.sunshower.sdk.v1.core.security;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.core.security.UserService;
5    import io.sunshower.model.core.auth.Role;
6    import io.sunshower.model.core.auth.User;
7    import io.sunshower.sdk.lang.BooleanElement;
8    import io.sunshower.sdk.v1.endpoints.core.security.UserEndpoint;
9    import io.sunshower.sdk.v1.model.core.Users;
10    import io.sunshower.sdk.v1.model.core.security.PrincipalElement;
11    import org.springframework.security.access.prepost.PreAuthorize;
12    import org.springframework.transaction.annotation.Transactional;
13   
14    import javax.inject.Inject;
15    import javax.persistence.EntityManager;
16    import javax.persistence.PersistenceContext;
17    import java.util.ArrayList;
18    import java.util.List;
19    import java.util.stream.Collectors;
20   
 
21    public class DefaultUserEndpoint implements UserEndpoint {
22   
23    @Inject private Users users;
24    @Inject private UserService userService;
25    @PersistenceContext private EntityManager entityManager;
26   
 
27  1 toggle @Override
28    @Transactional
29    @PreAuthorize("hasAuthority('admin')")
30    public BooleanElement delete(Identifier userid) {
31  1 User user = entityManager.find(User.class, userid);
32  1 if(user == null) {
33  0 return BooleanElement.False;
34    }
35  1 final List<Role> roles = new ArrayList<>(user.getRoles());
36  1 for(Role role : roles) {
37  1 user.removeRole(role);
38    }
39  1 entityManager.remove(user);
40  1 return BooleanElement.True;
41    }
42   
 
43  9 toggle @Override
44    public List<PrincipalElement> list(boolean active) {
45  9 List<User> results = active ? userService.activeUsers() : userService.inactiveUsers();
46  5 return results.stream().map(users::toElement).collect(Collectors.toList());
47    }
48    }