Example #1
0
  @Override
  public String refreshJWT() throws JWTCreationException {

    User user = Util.getCurrentUser();
    String jwt = null;

    if (user != null) {

      Map<String, List<String>> claims = new HashMap<String, List<String>>();
      List<String> claimStrings = new ArrayList<String>();

      Set<GrantedPermission> permissions = user.getPermissions();

      for (GrantedPermission claim : permissions) {
        claimStrings.add(claim.getAuthority());
      }
      claims.put("Authorities", claimStrings);

      List<String> identity = new ArrayList<String>();

      identity.add(user.getId().toString());
      identity.add(user.getName());
      identity.add(user.getFirstName());
      identity.add(user.getLastName());

      claims.put("Identity", identity);

      jwt = jwtAuthor.createJWT(user.getSubjectName(), claims);
    } else {
      throw new JWTCreationException("Cannot generate token for Anonymous user.");
    }
    return jwt;
  }
  @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_ACB_ADMIN') or hasRole('ROLE_ACB_STAFF')")
  @Transactional(readOnly = false)
  public VendorDTO update(VendorDTO vendor)
      throws EntityRetrievalException, JsonProcessingException, EntityCreationException {

    VendorDTO before = getById(vendor.getId());
    VendorEntity result = vendorDao.update(vendor);

    // chplAdmin cannot update the transparency but any other role
    // allowed in this method can
    boolean isChplAdmin = false;
    Set<GrantedPermission> permissions = Util.getCurrentUser().getPermissions();
    for (GrantedPermission permission : permissions) {
      if (permission.getAuthority().equals("ROLE_ADMIN")) {
        isChplAdmin = true;
      }
    }

    if (!isChplAdmin) {
      List<CertificationBodyDTO> availableAcbs = acbManager.getAllForUser();
      if (availableAcbs != null && availableAcbs.size() > 0) {
        for (CertificationBodyDTO acb : availableAcbs) {
          VendorACBMapDTO existingMap =
              vendorDao.getTransparencyMapping(vendor.getId(), acb.getId());
          if (existingMap == null) {
            VendorACBMapDTO vendorMappingToUpdate = new VendorACBMapDTO();
            vendorMappingToUpdate.setAcbId(acb.getId());
            vendorMappingToUpdate.setVendorId(before.getId());
            vendorMappingToUpdate.setTransparencyAttestation(vendor.getTransparencyAttestation());
            vendorDao.createTransparencyMapping(vendorMappingToUpdate);
          } else {
            existingMap.setTransparencyAttestation(vendor.getTransparencyAttestation());
            vendorDao.updateTransparencyMapping(existingMap);
          }
        }
      }
    }
    VendorDTO after = new VendorDTO(result);
    after.setTransparencyAttestation(vendor.getTransparencyAttestation());

    activityManager.addActivity(
        ActivityConcept.ACTIVITY_CONCEPT_VENDOR,
        after.getId(),
        "Vendor " + vendor.getName() + " was updated.",
        before,
        after);

    return after;
  }