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.Delete;
28  import org.restlet.resource.Get;
29  import org.restlet.resource.Post;
30  import org.restlet.resource.ResourceException;
31  
32  import fr.cnes.doi.application.AdminApplication;
33  import fr.cnes.doi.db.AbstractUserRoleDBHelper;
34  import fr.cnes.doi.db.model.DOIUser;
35  import fr.cnes.doi.exception.DOIDbException;
36  import fr.cnes.doi.plugin.PluginFactory;
37  import fr.cnes.doi.resource.AbstractResource;
38  import fr.cnes.doi.utils.spec.Requirement;
39  import java.util.ArrayList;
40  
41  /**
42   * Provide resources to get all users, bind or delete a user to/from a project.
43   */
44  public class ManageUsersResource extends AbstractResource {
45  
46      /**
47       * Parameter for the user name {@value #USER_NAME_PARAMETER}. This parameter
48       * is send to associate an user to a project.
49       */
50      public static final String USER_NAME_PARAMETER = "user";
51  
52      /**
53       * Logger.
54       */
55      private volatile Logger LOG;
56  
57      /**
58       * Suffix of the project.
59       */
60      private volatile String suffixProject;
61  
62      /**
63       * User name.
64       */
65      private volatile String userName;
66  
67      /**
68       * Set-up method that can be overridden in order to initialize the state of
69       * the resource.
70       *
71       * @throws ResourceException - if a problem happens
72       */
73      @Override
74      protected void doInit() throws ResourceException {
75          super.doInit();
76          final AdminApplication app = (AdminApplication) getApplication();
77          LOG = app.getLog();
78          LOG.traceEntry();
79          setDescription("This resource handles association between users and projects");
80          this.suffixProject = getAttribute("suffixProject");
81          this.userName = getAttribute("userName");
82          LOG.debug(this.suffixProject);
83          LOG.debug(this.userName);
84  
85          LOG.traceExit();
86      }
87  
88      //TODO requirement
89      /**
90       * Get users.
91       *
92       * @return users.
93       */
94      @Get
95      public List<String> getUsers() {
96          LOG.traceEntry();
97          try {
98              final int idProject = Integer.parseInt(suffixProject);
99              final List<String> users = new ArrayList<>();
100             final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
101             final List<DOIUser> doiUsers = manageUsers.getUsersFromRole(idProject);
102             for (final DOIUser doiUser : doiUsers) {
103                 users.add(doiUser.getUsername());
104             }
105             return LOG.traceExit(users);
106         } catch (DOIDbException ex) {
107             throw LOG.throwing(new ResourceException(Status.SERVER_ERROR_INTERNAL, ex.getMessage()));
108         }
109     }
110 
111     //TODO requirement 
112     /**
113      * Adds user to project
114      *
115      * @param mediaForm form
116      */
117     @Requirement(reqId = Requirement.DOI_SRV_140, reqName = Requirement.DOI_SRV_140_NAME)
118     @Post
119     public void addUserToProject(final Form mediaForm) {
120         LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
121         checkInputs(mediaForm);
122         final String user = mediaForm.getFirstValue(USER_NAME_PARAMETER);
123         final int idProject = Integer.parseInt(suffixProject);
124         final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
125         if (manageUsers.isUserExist(user)) {
126             if (manageUsers.addUserToRole(user, idProject)) {
127                 setStatus(Status.SUCCESS_NO_CONTENT);
128             } else {
129                 throw LOG.throwing(new ResourceException(
130                         Status.SERVER_ERROR_INTERNAL, "Can't add user to project"));
131             }
132         } else {
133             throw LOG.throwing(new ResourceException(
134                     Status.CLIENT_ERROR_BAD_REQUEST, "User " + user + " not found"));
135         }
136         LOG.traceExit();
137     }
138 
139     //TODO requirement
140     /**
141      * Delete the project
142      *
143      */
144     @Requirement(reqId = Requirement.DOI_SRV_140, reqName = Requirement.DOI_SRV_140_NAME)
145     @Delete
146     public void deleteProject() {
147         LOG.traceEntry();
148         final int idProject = Integer.parseInt(suffixProject);
149         final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
150         if (manageUsers.removeUserToRole(userName, idProject)) {
151             setStatus(Status.SUCCESS_NO_CONTENT);
152         } else {
153             throw LOG.throwing(new ResourceException(Status.SERVER_ERROR_INTERNAL,
154                     "Can't remove user " + userName));
155         }
156     }
157 
158     /**
159      * Tests if the {@link #USER_NAME_PARAMETER} is set.
160      *
161      * @param mediaForm the parameters
162      * @throws ResourceException - if USER_NAME_PARAMETER is not set
163      */
164     private void checkInputs(final Form mediaForm) throws ResourceException {
165         LOG.traceEntry("Parameters\n\tmediaForm : {}", mediaForm);
166         if (isValueNotExist(mediaForm, USER_NAME_PARAMETER)) {
167             throw LOG.throwing(Level.DEBUG, new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
168                     USER_NAME_PARAMETER + " parameter must be set"));
169         }
170         LOG.debug("The form is valid");
171         LOG.traceExit();
172     }
173 
174 }