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.resource.admin;
20  
21  import java.util.List;
22  
23  import org.apache.logging.log4j.Level;
24  import org.apache.logging.log4j.Logger;
25  import org.restlet.data.Form;
26  import org.restlet.data.Status;
27  import org.restlet.resource.Get;
28  import org.restlet.resource.Post;
29  import org.restlet.resource.ResourceException;
30  
31  import fr.cnes.doi.application.AdminApplication;
32  import fr.cnes.doi.db.AbstractUserRoleDBHelper;
33  import fr.cnes.doi.db.model.DOIUser;
34  import fr.cnes.doi.exception.DOIDbException;
35  import fr.cnes.doi.plugin.PluginFactory;
36  import fr.cnes.doi.resource.AbstractResource;
37  import java.util.ArrayList;
38  
39  /**
40   * Provide a resource to get all super users and add a user to the super user
41   * group.
42   */
43  public class ManageSuperUsersResource extends AbstractResource {
44  
45      /**
46       * Parameter for the SUPERUSER name {@value #SUPERUSER_NAME_PARAMETER}. This
47       * parameter is send to create a new identifier for the SUPERUSER.
48       */
49      public static final String SUPERUSER_NAME_PARAMETER = "superUserName";
50  
51      /**
52       * Logger.
53       */
54      private volatile Logger LOG;
55  
56      /**
57       * Set-up method that can be overridden in order to initialize the state of
58       * the resource.
59       *
60       * @throws ResourceException - if a problem happens
61       */
62      @Override
63      protected void doInit() throws ResourceException {
64          super.doInit();
65          final AdminApplication app = (AdminApplication) getApplication();
66          LOG = app.getLog();
67          LOG.traceEntry();
68          setDescription("This resource handles super users");
69          LOG.traceExit();
70      }
71  
72      // TODO requirement
73      /**
74       * Rename the SUPERUSER from the SUPERUSER id sent in url.
75       *
76       * @param mediaForm Data sent with request containing the user name to be
77       * super user.
78       */
79      // @Requirement(reqId = Requirement.DOI_SRV_140, reqName =
80      // Requirement.DOI_SRV_140_NAME)
81      @Post
82      public void createSuperUser(final Form mediaForm) {
83          LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
84          checkInputs(mediaForm);
85          final String newSuperUserName = mediaForm.getFirstValue(SUPERUSER_NAME_PARAMETER);
86          final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
87          if (!manageUsers.isUserExist(newSuperUserName)) {
88              throw LOG.throwing(new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
89                      "Can't find user " + newSuperUserName));
90          } else if (manageUsers.setUserToAdminGroup(newSuperUserName)) {
91              setStatus(Status.SUCCESS_NO_CONTENT);
92          } else {
93              throw LOG.throwing(new ResourceException(Status.SERVER_ERROR_INTERNAL,
94                      "Can't create user " + newSuperUserName));
95          }
96      }
97  
98      // TODO requirement
99      /**
100      * Returns the list of superusers as an array format.
101      *
102      * @return the list of superusers as an array format
103      */
104     // @Requirement(reqId = Requirement.DOI_SRV_140, reqName =
105     // Requirement.DOI_SRV_140_NAME)
106     @Get
107     public List<String> getSuperUsersAsJson() {
108         LOG.traceEntry();
109         try {
110             final ArrayList<String> result = new ArrayList<>();
111             final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
112             final List<DOIUser> users = manageUsers.getUsers();
113             for (final DOIUser doiUser : users) {
114                 if (doiUser.isAdmin()) {
115                     result.add(doiUser.getUsername());
116                 }
117             }
118             return LOG.traceExit(result);
119         } catch (DOIDbException ex) {
120             throw LOG.throwing(new ResourceException(Status.SERVER_ERROR_INTERNAL, ex.getMessage()));
121         }
122     }
123 
124     /**
125      * Tests if the {@link #SUPERUSER_NAME_PARAMETER} is set.
126      *
127      * @param mediaForm the parameters
128      * @throws ResourceException - if SUPERUSER_NAME_PARAMETER is not set
129      */
130     private void checkInputs(final Form mediaForm) throws ResourceException {
131         LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
132         if (isValueNotExist(mediaForm, SUPERUSER_NAME_PARAMETER)) {
133             throw LOG.throwing(Level.ERROR, new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
134                     SUPERUSER_NAME_PARAMETER + " parameter must be set"));
135         }
136         LOG.debug("The form is valid");
137         LOG.traceExit();
138     }
139 }