View Javadoc

1   /*
2    * Copyright (C) 2017-2019 Centre National d'Etudes Spatiales (CNES).
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 3.0 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17   * MA 02110-1301  USA
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   * Updates the database from the authentication system.
35   *
36   * @author Jean-Christophe Malapert (jean-christophe.malapert@cnes.fr)
37   */
38  public class DOIUsersUpdate implements Runnable {
39  
40      /**
41       * Logger.
42       */
43      private static final Logger LOG = LogManager.getLogger(DOIUsersUpdate.class.getName());
44  
45      /**
46       * Access to the authentication system.
47       */
48      private static final IAuthenticationDBHelper AUTHENTICATION_SERVICE = PluginFactory.
49              getAuthenticationSystem();
50  
51      /**
52       * Fills the DOI users database from the members of the authentication
53       * system.
54       *
55       * @throws AuthenticationAccessException When an authentication problem
56       * occurs
57       * @throws DOIDbException When a SQL problem occurs
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                  // add authMember to doi database
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       * Tests if the authMember of the authentication mechanism is contained in
88       * the users from the database.
89       *
90       * @param authMember authMember of the authentication mechanism
91       * @param dbusers users database
92       * @return True when the authMember is contained in the users from database
93       * otherwise false
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      * Tests if a user from the database is contained in the members from the
109      * authentication system.
110      *
111      * @param dbuser the user from the database
112      * @param authMembers the users from the authentication system
113      * @return True when the user from the database is contained from the users
114      * from the authentication system
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      * {@inheritDoc}
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 }