@Override
 public UserClientResource toResource(UserClient entity) {
   UserClientResource ret = new UserClientResource();
   ret.add(linkTo(methodOn(UserClientsResource.class).get(entity.getId())).withSelfRel());
   ret.add(
       linkTo(
               methodOn(UserClientUserClientRolesResource.class)
                   .getUserClientUserClientRoles(entity.getId(), null, null))
           .withRel("userClientRoles"));
   ret.add(UserClientUserClientTeamRoleResourceAssembler.createPossibleTeamsLink(entity));
   ret.add(
       UserClientUserClientTeamRoleResourceAssembler.createGetUserClientUserClientTeamRolesLink(
           entity));
   ret.add(
       linkTo(methodOn(UserClientPasswordResource.class).set(entity.getId(), null))
           .withRel("changePassword"));
   ret.add(
       linkTo(methodOn(UserClientsResource.class).activate(entity.getId())).withRel("activate"));
   ret.add(
       linkTo(methodOn(UserClientsResource.class).resetPassword(entity.getId()))
           .withRel("resetPassword"));
   ret.add(
       linkTo(methodOn(CasesResource.class).userClientReferenceData(entity.getId()))
           .withRel("createCase"));
   ret.setEntity(entity);
   return ret;
 }
  @Override
  public UserClientResource toResource(UserClient user) {
    if (user == null) {
      return null;
    }
    Set<String> perms = new HashSet<>();
    for (GrantedAuthority grantedAuthority : user.getAuthorities()) {
      // strip off the client and team specifics on the authorities
      Matcher matcher = specializedPermissions.matcher(grantedAuthority.getAuthority());
      if (matcher.matches()) {
        perms.add(matcher.replaceAll("$1"));
      } else {
        perms.add(grantedAuthority.getAuthority());
      }
    }

    if (user.isSuperUser()) {
      // give the super user all team permissions (not necessary but nice for the UI)
      user.setTeamRoles(superUserTeamRoleGenerator.allPermissionsOnAllTeams(user.getClient()));
    }

    // add the teams to which this user has access
    Set<TeamResource> teams = new HashSet<>();
    for (UserClientUserClientTeamRole userClientUserClientTeamRole : user.getTeamRoles()) {
      teams.add(roleTeamResourceResourceAssembler.toResource(userClientUserClientTeamRole));
    }

    boolean interruptFlow = false;

    if (user.getNotNowExpirationTime() != null) {
      LocalDateTime dateTime = LocalDateTime.now(DateTimeZone.UTC);
      interruptFlow = dateTime.isBefore(user.getNotNowExpirationTime());
    }

    UserClientResource ret =
        new UserClientResource(
            user.getId(),
            user.getVersion(),
            user.getLogin(),
            user.getFirstName(),
            user.getLastName(),
            user.getEmail(),
            user.isActive(),
            user.isAccountNonExpired(),
            user.isAccountNonLocked(),
            user.isCredentialsNonExpired(),
            user.isEmailValidated(),
            user.isSecretQuestionCreated(),
            clientResourceAssembler.toResource(user.getClient()),
            new ArrayList<>(perms),
            user.isImpersonated(),
            user.getNotNowExpirationTime(),
            user.getPasswordExpireationDateTime(),
            user.getPasswordSavedDateTime(),
            interruptFlow);

    ret.setSecurityQuestionsNotRequiredForReset(user.isSecurityQuestionsNotRequiredForReset());

    ret.add(linkTo(methodOn(UserClientsResource.class).authenticated()).withRel("authenticated"));

    if (!CollectionUtils.isEmpty(teams)) {
      ret.setTeams(teams);
    }

    if (!user.isImpersonated()) {
      // non-impersonation users only
      ret.add(linkTo(methodOn(UserClientsResource.class).getById(user.getId())).withSelfRel());
      ret.add(updateUserClientLink(user));
      ret.add(createVerifyPasswordLink(user));

      Link link =
          linkTo(
                  methodOn(UserClientSecretQuestionResponsesResource.class)
                      .secretQuestionResponses(user.getId(), null, null, null))
              .withRel("secretQuestionResponses");
      ret.add(new Link(createUriTemplate("password", link), link.getRel()));

      Link updateUserClientSecretQuestion =
          linkTo(methodOn(UserClientsResource.class).updateUserClient(user.getId(), null))
              .withRel("updateUserClientSecretQuestionFlag");
      ret.add(
          new Link(
              createUriTemplate("secretQuestionsCreated", updateUserClientSecretQuestion),
              updateUserClientSecretQuestion.getRel()));

      ret.add(
          linkTo(
                  methodOn(UserClientSecretQuestionResponsesResource.class)
                      .secretQuestionAsteriskResponse(user.getId(), null, null))
              .withRel("secretQuestionAsteriskResponses"));
      ret.add(
          linkTo(methodOn(UserClientsResource.class).sendValidationEmail(user.getId()))
              .withRel("sendValidationEmail"));
      ret.add(
          linkTo(methodOn(UserClientsPasswordResource.class).changePassword(null, null, null))
              .withRel("changePassword"));
      ret.add(linkTo(methodOn(UserClientsResource.class).notNow(user.getId())).withRel("notNow"));
    } else {
      // impersonation users
      ret.add(
          new Link(
              UriComponentsBuilder.fromHttpUrl(
                      linkTo(methodOn(UserClientsResource.class).getById(1l))
                          .withSelfRel()
                          .getHref())
                  .replacePath(adminEntryPoint)
                  .build(false)
                  .toUriString(),
              "adminApp"));
    }

    return ret;
  }