1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
44 public class ManageUsersResource extends AbstractResource {
45
46
47
48
49
50 public static final String USER_NAME_PARAMETER = "user";
51
52
53
54
55 private volatile Logger LOG;
56
57
58
59
60 private volatile String suffixProject;
61
62
63
64
65 private volatile String userName;
66
67
68
69
70
71
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
89
90
91
92
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
112
113
114
115
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
140
141
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
160
161
162
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 }