| .. | .. |
|---|
| 27 | 27 | import net.curisit.integrity.exception.CurisException; |
|---|
| 28 | 28 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 29 | 29 | import net.curisit.securis.db.License; |
|---|
| 30 | +import net.curisit.securis.db.LicenseHistory; |
|---|
| 30 | 31 | import net.curisit.securis.db.Pack; |
|---|
| 31 | 32 | import net.curisit.securis.db.User; |
|---|
| 32 | 33 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| .. | .. |
|---|
| 119 | 120 | return Response.ok(lic).build(); |
|---|
| 120 | 121 | } |
|---|
| 121 | 122 | |
|---|
| 123 | + /** |
|---|
| 124 | + * |
|---|
| 125 | + * @return The license file, only of license is active |
|---|
| 126 | + */ |
|---|
| 127 | + @GET |
|---|
| 128 | + @Path("/{licId}/download") |
|---|
| 129 | + @Securable |
|---|
| 130 | + @Produces( |
|---|
| 131 | + { MediaType.APPLICATION_OCTET_STREAM }) |
|---|
| 132 | + public Response download(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) { |
|---|
| 133 | + log.info("Getting license data for id: {}: ", licId); |
|---|
| 134 | + if (licId == null || licId.equals("")) { |
|---|
| 135 | + log.error("License ID is mandatory"); |
|---|
| 136 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 137 | + } |
|---|
| 138 | + |
|---|
| 139 | + EntityManager em = emProvider.get(); |
|---|
| 140 | + License lic = em.find(License.class, licId); |
|---|
| 141 | + if (lic == null) { |
|---|
| 142 | + log.error("License with id {} not found in DB", licId); |
|---|
| 143 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build(); |
|---|
| 144 | + } |
|---|
| 145 | + if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 146 | + if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) { |
|---|
| 147 | + log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 148 | + return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build(); |
|---|
| 149 | + } |
|---|
| 150 | + } |
|---|
| 151 | + if (lic.getLicenseData() == null) { |
|---|
| 152 | + log.error("License with id {} has not license file generated", licId, bsc.getUserPrincipal()); |
|---|
| 153 | + return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License has not contain data to generate license file").build(); |
|---|
| 154 | + } |
|---|
| 155 | + if (lic.getStatus() != License.Status.ACTIVE) { |
|---|
| 156 | + log.error("License with id {} is not active, so It can not downloaded", licId, bsc.getUserPrincipal()); |
|---|
| 157 | + return Response.status(Status.FORBIDDEN).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License is not active, so It can not be downloaded").build(); |
|---|
| 158 | + } |
|---|
| 159 | + return Response.ok(lic.getLicenseData()).build(); |
|---|
| 160 | + } |
|---|
| 161 | + |
|---|
| 162 | + @PUT |
|---|
| 163 | + @POST |
|---|
| 164 | + @Path("/{licId}/activate") |
|---|
| 165 | + @Securable |
|---|
| 166 | + @Transactional |
|---|
| 167 | + @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 168 | + @Produces( |
|---|
| 169 | + { MediaType.APPLICATION_JSON }) |
|---|
| 170 | + public Response activate(@PathParam("licId") Integer licId, @Context BasicSecurityContext bsc) { |
|---|
| 171 | + log.info("Getting license data for id: {}: ", licId); |
|---|
| 172 | + if (licId == null || licId.equals("")) { |
|---|
| 173 | + log.error("License ID is mandatory"); |
|---|
| 174 | + return Response.status(Status.NOT_FOUND).build(); |
|---|
| 175 | + } |
|---|
| 176 | + |
|---|
| 177 | + EntityManager em = emProvider.get(); |
|---|
| 178 | + License lic = em.find(License.class, licId); |
|---|
| 179 | + if (lic == null) { |
|---|
| 180 | + log.error("License with id {} not found in DB", licId); |
|---|
| 181 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "License not found for ID: " + licId).build(); |
|---|
| 182 | + } |
|---|
| 183 | + if (!bsc.isUserInRole(BasicSecurityContext.ROL_ADMIN)) { |
|---|
| 184 | + if (!bsc.getOrganizationsIds().contains(lic.getPack().getOrganization().getId())) { |
|---|
| 185 | + log.error("License with id {} is not accesible by user {}", licId, bsc.getUserPrincipal()); |
|---|
| 186 | + return Response.status(Status.UNAUTHORIZED).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Unathorized access to license data").build(); |
|---|
| 187 | + } |
|---|
| 188 | + } |
|---|
| 189 | + |
|---|
| 190 | + User user = null; |
|---|
| 191 | + try { |
|---|
| 192 | + user = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 193 | + } catch (CurisException ex) { |
|---|
| 194 | + return Response.status(Status.NOT_FOUND).header(DefaultExceptionHandler.ERROR_MESSAGE_HEADER, "Current user not found in DB: " + bsc.getUserPrincipal()).build(); |
|---|
| 195 | + } |
|---|
| 196 | + |
|---|
| 197 | + lic.setStatus(License.Status.ACTIVE); |
|---|
| 198 | + lic.setModificationTimestamp(new Date()); |
|---|
| 199 | + em.persist(lic); |
|---|
| 200 | + LicenseHistory lh = new LicenseHistory(); |
|---|
| 201 | + lh.setLicense(lic); |
|---|
| 202 | + lh.setUser(user); |
|---|
| 203 | + lh.setTimestamp(new Date()); |
|---|
| 204 | + lh.setAction(LicenseHistory.Actions.ACTIVATE); |
|---|
| 205 | + em.persist(lh); |
|---|
| 206 | + return Response.ok(lic).build(); |
|---|
| 207 | + } |
|---|
| 208 | + |
|---|
| 122 | 209 | @POST |
|---|
| 123 | 210 | @Path("/") |
|---|
| 124 | 211 | @Consumes(MediaType.APPLICATION_JSON) |
|---|
| .. | .. |
|---|
| 145 | 232 | } |
|---|
| 146 | 233 | } |
|---|
| 147 | 234 | |
|---|
| 235 | + User createdBy = null; |
|---|
| 148 | 236 | try { |
|---|
| 149 | | - User createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 150 | | - lic.setCreatedBy(createdBy); |
|---|
| 237 | + createdBy = getUser(bsc.getUserPrincipal().getName(), em); |
|---|
| 151 | 238 | } catch (CurisException ex) { |
|---|
| 152 | 239 | String createdByUsername = lic.getCreatedById(); |
|---|
| 153 | 240 | log.error("License created by user with id {} not found in DB", createdByUsername); |
|---|
| .. | .. |
|---|
| 155 | 242 | } |
|---|
| 156 | 243 | |
|---|
| 157 | 244 | // ODO: Manage status if request data is set |
|---|
| 158 | | - lic.setCanceledBy(null); |
|---|
| 245 | + lic.setCreatedBy(createdBy); |
|---|
| 159 | 246 | lic.setStatus(License.Status.CREATED); |
|---|
| 160 | 247 | lic.setCreationTimestamp(new Date()); |
|---|
| 161 | 248 | lic.setModificationTimestamp(lic.getCreationTimestamp()); |
|---|
| 162 | 249 | em.persist(lic); |
|---|
| 250 | + LicenseHistory lh = new LicenseHistory(); |
|---|
| 251 | + lh.setLicense(lic); |
|---|
| 252 | + lh.setUser(createdBy); |
|---|
| 253 | + lh.setTimestamp(new Date()); |
|---|
| 254 | + lh.setAction(LicenseHistory.Actions.CREATE); |
|---|
| 255 | + em.persist(lh); |
|---|
| 163 | 256 | |
|---|
| 164 | 257 | return Response.ok(lic).build(); |
|---|
| 165 | 258 | } |
|---|