1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.services;
20
21 import fr.cnes.doi.db.AbstractUserRoleDBHelper;
22 import fr.cnes.doi.db.IAuthenticationDBHelper;
23 import java.util.List;
24
25 import fr.cnes.doi.exception.AuthenticationAccessException;
26 import fr.cnes.doi.db.model.AuthSystemUser;
27 import fr.cnes.doi.exception.DOIDbException;
28 import fr.cnes.doi.db.model.DOIUser;
29 import org.apache.logging.log4j.LogManager;
30 import org.apache.logging.log4j.Logger;
31 import fr.cnes.doi.plugin.PluginFactory;
32
33
34
35
36
37
38 public class DOIUsersUpdate implements Runnable {
39
40
41
42
43 private static final Logger LOG = LogManager.getLogger(DOIUsersUpdate.class.getName());
44
45
46
47
48 private static final IAuthenticationDBHelper AUTHENTICATION_SERVICE = PluginFactory.
49 getAuthenticationSystem();
50
51
52
53
54
55
56
57
58
59 private void updateDoiServerDataBaseFromAuthSystem() throws AuthenticationAccessException,
60 DOIDbException {
61 LOG.traceEntry();
62 final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
63 final List<AuthSystemUser> authMembers = AUTHENTICATION_SERVICE.getDOIProjectMembers();
64 LOG.debug("Authentication system members: {}", authMembers);
65 final List<DOIUser> dbusers = manageUsers.getUsers();
66 LOG.debug("Users from database: {}", dbusers);
67 LOG.debug("remove from database, users that are no longer members of doi_server project");
68 for (final DOIUser dbuser : dbusers) {
69 if (!isContained(dbuser, authMembers)) {
70 LOG.debug("User {} is removed from database", dbuser.getUsername());
71 manageUsers.removeDOIUser(dbuser.getUsername());
72 }
73 }
74 LOG.debug("add to database users that are new members of doi_server project");
75 for (final AuthSystemUser authMember : authMembers) {
76 if (!isContained(authMember, dbusers)) {
77
78 LOG.debug("authSystemUser {} is added to database as simple user", authMember.
79 getUsername());
80 manageUsers.addDOIUser(authMember.getUsername(), false, authMember.getEmail());
81 }
82 }
83 LOG.traceExit();
84 }
85
86
87
88
89
90
91
92
93
94
95 private boolean isContained(final AuthSystemUser authMember, final List<DOIUser> dbusers) {
96 LOG.traceEntry("Parameters {},", authMember, dbusers);
97 boolean isContained = false;
98 for (final DOIUser dbuser : dbusers) {
99 if (dbuser.getUsername().equals(authMember.getUsername())) {
100 isContained = true;
101 break;
102 }
103 }
104 return LOG.traceExit(isContained);
105 }
106
107
108
109
110
111
112
113
114
115
116 private boolean isContained(final DOIUser dbuser, final List<AuthSystemUser> authMembers) {
117 LOG.traceEntry("Parameters {},", dbuser, authMembers);
118 boolean isContained = false;
119 for (final AuthSystemUser authenticationUser : authMembers) {
120 if (authenticationUser.getUsername().equals(dbuser.getUsername())) {
121 isContained = true;
122 break;
123 }
124 }
125 return LOG.traceExit(isContained);
126 }
127
128
129
130
131 @Override
132 public void run() {
133 LOG.info("executing task that updates database from the authentication system !");
134 try {
135 this.updateDoiServerDataBaseFromAuthSystem();
136 } catch (AuthenticationAccessException | DOIDbException e) {
137 LOG.error("error occured when calling DOIUsersUpdate job", e);
138 }
139 }
140
141 }