Пример #1
0
  /**
   * Create a new user
   *
   * <p>Username must be unique.
   *
   * @param uriInfo
   * @param rep
   * @return
   */
  @POST
  @Consumes(MediaType.APPLICATION_JSON)
  public Response createUser(final @Context UriInfo uriInfo, final UserRepresentation rep) {
    auth.requireManage();

    // Double-check duplicated username and email here due to federation
    if (session.users().getUserByUsername(rep.getUsername(), realm) != null) {
      return ErrorResponse.exists("User exists with same username");
    }
    if (rep.getEmail() != null && session.users().getUserByEmail(rep.getEmail(), realm) != null) {
      return ErrorResponse.exists("User exists with same email");
    }

    try {
      UserModel user = session.users().addUser(realm, rep.getUsername());
      Set<String> emptySet = Collections.emptySet();
      updateUserFromRep(user, rep, emptySet, realm, session);

      adminEvent
          .operation(OperationType.CREATE)
          .resourcePath(uriInfo, user.getId())
          .representation(rep)
          .success();

      if (session.getTransaction().isActive()) {
        session.getTransaction().commit();
      }

      return Response.created(uriInfo.getAbsolutePathBuilder().path(user.getId()).build()).build();
    } catch (ModelDuplicateException e) {
      if (session.getTransaction().isActive()) {
        session.getTransaction().setRollbackOnly();
      }
      return ErrorResponse.exists("User exists with same username or email");
    }
  }
Пример #2
0
  @Test
  public void logInAsUserInIDP() {
    driver.navigate().to(getAccountUrl(consumerRealmName()));

    log.debug("Clicking social " + getIDPAlias());
    accountLoginPage.clickSocial(getIDPAlias());

    waitForPage("log in to");

    Assert.assertTrue(
        "Driver should be on the provider realm page right now",
        driver.getCurrentUrl().contains("/auth/realms/" + providerRealmName() + "/"));

    log.debug("Logging in");
    accountLoginPage.login(getUserLogin(), getUserPassword());

    waitForPage("update account information");

    Assert.assertTrue(updateAccountInformationPage.isCurrent());
    Assert.assertTrue(
        "We must be on correct realm right now",
        driver.getCurrentUrl().contains("/auth/realms/" + consumerRealmName() + "/"));

    log.debug("Updating info on updateAccount page");
    updateAccountInformationPage.updateAccountInformation("Firstname", "Lastname");

    UsersResource consumerUsers = adminClient.realm(consumerRealmName()).users();

    int userCount = consumerUsers.count();
    Assert.assertTrue("There must be at least one user", userCount > 0);

    List<UserRepresentation> users = consumerUsers.search("", 0, userCount);

    boolean isUserFound = false;
    for (UserRepresentation user : users) {
      if (user.getUsername().equals(getUserLogin()) && user.getEmail().equals(getUserEmail())) {
        isUserFound = true;
        break;
      }
    }

    Assert.assertTrue(
        "There must be user " + getUserLogin() + " in realm " + consumerRealmName(), isUserFound);

    testSingleLogout();
  }
Пример #3
0
  public static void updateUserFromRep(
      UserModel user,
      UserRepresentation rep,
      Set<String> attrsToRemove,
      RealmModel realm,
      KeycloakSession session) {
    if (realm.isEditUsernameAllowed()) {
      user.setUsername(rep.getUsername());
    }
    user.setEmail(rep.getEmail());
    user.setFirstName(rep.getFirstName());
    user.setLastName(rep.getLastName());

    if (rep.isEnabled() != null) user.setEnabled(rep.isEnabled());
    if (rep.isTotp() != null) user.setOtpEnabled(rep.isTotp());
    if (rep.isEmailVerified() != null) user.setEmailVerified(rep.isEmailVerified());

    List<String> reqActions = rep.getRequiredActions();

    if (reqActions != null) {
      Set<String> allActions = new HashSet<>();
      for (ProviderFactory factory :
          session.getKeycloakSessionFactory().getProviderFactories(RequiredActionProvider.class)) {
        allActions.add(factory.getId());
      }
      for (String action : allActions) {
        if (reqActions.contains(action)) {
          user.addRequiredAction(action);
        } else {
          user.removeRequiredAction(action);
        }
      }
    }

    if (rep.getAttributesAsListValues() != null) {
      for (Map.Entry<String, List<String>> attr : rep.getAttributesAsListValues().entrySet()) {
        user.setAttribute(attr.getKey(), attr.getValue());
      }

      for (String attr : attrsToRemove) {
        user.removeAttribute(attr);
      }
    }
  }
Пример #4
0
 public void setValues(UserRepresentation user) {
   setUsername(user.getUsername());
   setEmail(user.getEmail());
   setFirstName(user.getFirstName());
   setLastName(user.getLastName());
 }