9
9
import org .gridsuite .useradmin .server .UserAdminApplicationProps ;
10
10
import org .gridsuite .useradmin .server .UserAdminException ;
11
11
import org .gridsuite .useradmin .server .repository .ConnectionEntity ;
12
- import org .gridsuite .useradmin .server .repository .ConnectionRepository ;
13
12
import org .gridsuite .useradmin .server .repository .UserAdminRepository ;
14
13
import org .gridsuite .useradmin .server .repository .UserInfosEntity ;
15
14
import org .slf4j .Logger ;
16
15
import org .slf4j .LoggerFactory ;
17
16
import org .springframework .beans .factory .annotation .Autowired ;
18
- import org .springframework .dao .DataIntegrityViolationException ;
19
17
import org .springframework .stereotype .Service ;
20
18
21
- import java .time .LocalDateTime ;
22
19
import java .util .*;
23
20
import java .util .concurrent .ConcurrentHashMap ;
24
21
import java .util .function .Function ;
25
22
import java .util .function .Predicate ;
26
- import java .util .stream .Collectors ;
27
23
28
24
import static org .gridsuite .useradmin .server .UserAdminException .Type .FORBIDDEN ;
29
25
@@ -35,14 +31,14 @@ public class UserAdminService {
35
31
private static final Logger LOGGER = LoggerFactory .getLogger (UserAdminService .class );
36
32
private UserAdminRepository userAdminRepository ;
37
33
38
- private ConnectionRepository connectionRepository ;
34
+ private ConnectionsService connectionsService ;
39
35
40
36
@ Autowired
41
37
private UserAdminApplicationProps applicationProps ;
42
38
43
- public UserAdminService (UserAdminRepository userAdminRepository , ConnectionRepository connectionRepository ) {
39
+ public UserAdminService (UserAdminRepository userAdminRepository , ConnectionsService connectionsService ) {
44
40
this .userAdminRepository = Objects .requireNonNull (userAdminRepository );
45
- this .connectionRepository = Objects .requireNonNull (connectionRepository );
41
+ this .connectionsService = Objects .requireNonNull (connectionsService );
46
42
}
47
43
48
44
public List <UserInfosEntity > getUsers (String userId ) {
@@ -56,23 +52,14 @@ public List<ConnectionEntity> getConnections(String userId) {
56
52
if (!isAdmin (userId )) {
57
53
throw new UserAdminException (FORBIDDEN );
58
54
}
59
- return removeDuplicates (connectionRepository . findAll () );
55
+ return connectionsService . removeDuplicates ();
60
56
}
61
57
62
58
public static <T > Predicate <T > distinctByKey (Function <? super T , ?> keyExtractor ) {
63
59
Set <Object > seen = ConcurrentHashMap .newKeySet ();
64
60
return t -> seen .add (keyExtractor .apply (t ));
65
61
}
66
62
67
-
68
- private List <ConnectionEntity > removeDuplicates (List <ConnectionEntity > connections ) {
69
- List <ConnectionEntity > connectionsWithoutDuplicates = connections .stream ().filter (distinctByKey (ConnectionEntity ::getSub )).collect (Collectors .toList ());
70
- connectionsWithoutDuplicates .stream ().forEach (c -> c .setLastConnexionDate (connectionRepository .findBySub (c .getSub ()).stream ().max (Comparator .comparing (ConnectionEntity ::getLastConnexionDate )).get ().getLastConnexionDate ()));
71
- connectionRepository .deleteAll ();
72
- connectionRepository .saveAll (connectionsWithoutDuplicates );
73
- return connectionsWithoutDuplicates ;
74
- }
75
-
76
63
public void createUser (String sub , String userId ) {
77
64
if (!isAdmin (userId )) {
78
65
throw new UserAdminException (FORBIDDEN );
@@ -90,26 +77,10 @@ public void delete(UUID id, String userId) {
90
77
91
78
public boolean subExists (String sub ) {
92
79
Boolean isAllowed = (applicationProps .getAdmins ().isEmpty () && userAdminRepository .count () == 0 ) || !userAdminRepository .findAllBySub (sub ).isEmpty ();
93
- recordConnectionAttempt (sub , isAllowed );
80
+ connectionsService . recordConnectionAttempt (sub , isAllowed );
94
81
return isAllowed .booleanValue ();
95
82
}
96
83
97
- public void recordConnectionAttempt (String sub , Boolean isAllowed ) {
98
- ConnectionEntity connectionEntity = connectionRepository .findBySub (sub ).stream ().max (Comparator .comparing (ConnectionEntity ::getLastConnexionDate )).orElse (null );
99
- if (connectionEntity == null ) {
100
- connectionEntity = new ConnectionEntity (sub , LocalDateTime .now (), LocalDateTime .now (), isAllowed );
101
- try {
102
- connectionRepository .save (connectionEntity );
103
- } catch (DataIntegrityViolationException e ) {
104
- LOGGER .info ("User connection already recorded." );
105
- }
106
- } else {
107
- connectionEntity .setLastConnexionDate (LocalDateTime .now ());
108
- connectionEntity .setConnectionAccepted (isAllowed );
109
- connectionRepository .save (connectionEntity );
110
- }
111
- }
112
-
113
84
private boolean isAdmin (String sub ) {
114
85
return applicationProps .getAdmins ().contains (sub );
115
86
}
0 commit comments