rsanchez
2017-04-10 8d99c88af55041ff06e6b9372b6b1f66220bed38
securis/src/main/java/net/curisit/securis/services/UserResource.java
....@@ -34,6 +34,7 @@
3434 import net.curisit.integrity.commons.Utils;
3535 import net.curisit.securis.DefaultExceptionHandler;
3636 import net.curisit.securis.SeCurisException;
37
+import net.curisit.securis.db.Application;
3738 import net.curisit.securis.db.Organization;
3839 import net.curisit.securis.db.User;
3940 import net.curisit.securis.ioc.EnsureTransaction;
....@@ -131,7 +132,12 @@
131132 }
132133
133134 try {
134
- this.setUserOrg(user, user.getOrgsIds(), em);
135
+ this.setUserOrgs(user, user.getOrgsIds(), em);
136
+ } catch (SeCurisException e) {
137
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
138
+ }
139
+ try {
140
+ this.setUserApps(user, user.getAppsIds(), em);
135141 } catch (SeCurisException e) {
136142 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
137143 }
....@@ -149,7 +155,7 @@
149155 return Response.ok(user).build();
150156 }
151157
152
- private void setUserOrg(User user, Set<Integer> orgsIds, EntityManager em) throws SeCurisException {
158
+ private void setUserOrgs(User user, Set<Integer> orgsIds, EntityManager em) throws SeCurisException {
153159 Set<Organization> orgs = null;
154160 if (orgsIds != null && !orgsIds.isEmpty()) {
155161 orgs = new HashSet<>();
....@@ -165,6 +171,23 @@
165171
166172 user.setOrganizations(orgs);
167173
174
+ }
175
+
176
+ private void setUserApps(User user, Set<Integer> appsIds, EntityManager em) throws SeCurisException {
177
+ Set<Application> apps = null;
178
+ if (appsIds != null && !appsIds.isEmpty()) {
179
+ apps = new HashSet<>();
180
+ for (Integer appId : appsIds) {
181
+ Application o = em.find(Application.class, appId);
182
+ if (o == null) {
183
+ LOG.error("User application with id {} not found in DB", appId);
184
+ throw new SeCurisException("User's application not found with ID: " + appId);
185
+ }
186
+ apps.add(o);
187
+ }
188
+ }
189
+
190
+ user.setApplications(apps);
168191 }
169192
170193 @PUT
....@@ -185,7 +208,12 @@
185208 }
186209
187210 try {
188
- this.setUserOrg(currentUser, user.getOrgsIds(), em);
211
+ this.setUserOrgs(currentUser, user.getOrgsIds(), em);
212
+ } catch (SeCurisException e) {
213
+ return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
214
+ }
215
+ try {
216
+ this.setUserApps(currentUser, user.getAppsIds(), em);
189217 } catch (SeCurisException e) {
190218 return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build();
191219 }
....@@ -199,8 +227,6 @@
199227 } else {
200228 // Password has not been modified
201229 // return
202
- // Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER,
203
- // "User password is mandatory").build();
204230 }
205231
206232 currentUser.setLastLogin(user.getLastLogin());