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.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
41
42
43 public class ManageSuperUsersResource extends AbstractResource {
44
45
46
47
48
49 public static final String SUPERUSER_NAME_PARAMETER = "superUserName";
50
51
52
53
54 private volatile Logger LOG;
55
56
57
58
59
60
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
73
74
75
76
77
78
79
80
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
99
100
101
102
103
104
105
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
126
127
128
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 }