| .. | .. |
|---|
| 34 | 34 | import net.curisit.integrity.commons.Utils; |
|---|
| 35 | 35 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 36 | 36 | import net.curisit.securis.SeCurisException; |
|---|
| 37 | +import net.curisit.securis.db.Application; |
|---|
| 37 | 38 | import net.curisit.securis.db.Organization; |
|---|
| 38 | 39 | import net.curisit.securis.db.User; |
|---|
| 39 | 40 | import net.curisit.securis.ioc.EnsureTransaction; |
|---|
| .. | .. |
|---|
| 131 | 132 | } |
|---|
| 132 | 133 | |
|---|
| 133 | 134 | 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); |
|---|
| 135 | 141 | } catch (SeCurisException e) { |
|---|
| 136 | 142 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 137 | 143 | } |
|---|
| .. | .. |
|---|
| 149 | 155 | return Response.ok(user).build(); |
|---|
| 150 | 156 | } |
|---|
| 151 | 157 | |
|---|
| 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 { |
|---|
| 153 | 159 | Set<Organization> orgs = null; |
|---|
| 154 | 160 | if (orgsIds != null && !orgsIds.isEmpty()) { |
|---|
| 155 | 161 | orgs = new HashSet<>(); |
|---|
| .. | .. |
|---|
| 165 | 171 | |
|---|
| 166 | 172 | user.setOrganizations(orgs); |
|---|
| 167 | 173 | |
|---|
| 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); |
|---|
| 168 | 191 | } |
|---|
| 169 | 192 | |
|---|
| 170 | 193 | @PUT |
|---|
| .. | .. |
|---|
| 185 | 208 | } |
|---|
| 186 | 209 | |
|---|
| 187 | 210 | 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); |
|---|
| 189 | 217 | } catch (SeCurisException e) { |
|---|
| 190 | 218 | return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, e.getMessage()).build(); |
|---|
| 191 | 219 | } |
|---|
| .. | .. |
|---|
| 199 | 227 | } else { |
|---|
| 200 | 228 | // Password has not been modified |
|---|
| 201 | 229 | // return |
|---|
| 202 | | - // Response.status(DefaultExceptionHandler.DEFAULT_APP_ERROR_STATUS_CODE).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, |
|---|
| 203 | | - // "User password is mandatory").build(); |
|---|
| 204 | 230 | } |
|---|
| 205 | 231 | |
|---|
| 206 | 232 | currentUser.setLastLogin(user.getLastLogin()); |
|---|