コード例 #1
0
ファイル: AccountService.java プロジェクト: rishiHPE/keycloak
  /**
   * Get account information.
   *
   * @return
   */
  @Path("/")
  @GET
  public Response accountPage() {
    List<MediaType> types = headers.getAcceptableMediaTypes();
    if (types.contains(MediaType.WILDCARD_TYPE) || (types.contains(MediaType.TEXT_HTML_TYPE))) {
      return forwardToPage(null, AccountPages.ACCOUNT);
    } else if (types.contains(MediaType.APPLICATION_JSON_TYPE)) {
      requireOneOf(AccountRoles.MANAGE_ACCOUNT, AccountRoles.VIEW_PROFILE);

      UserRepresentation rep = ModelToRepresentation.toRepresentation(auth.getUser());
      if (rep.getAttributes() != null) {
        Iterator<String> itr = rep.getAttributes().keySet().iterator();
        while (itr.hasNext()) {
          if (itr.next().startsWith("keycloak.")) {
            itr.remove();
          }
        }
      }

      return Cors.add(request, Response.ok(rep)).auth().allowedOrigins(auth.getToken()).build();
    } else {
      return Response.notAcceptable(
              Variant.VariantListBuilder.newInstance()
                  .mediaTypes(MediaType.TEXT_HTML_TYPE, MediaType.APPLICATION_JSON_TYPE)
                  .build())
          .build();
    }
  }
コード例 #2
0
  @Test
  public void attributes() {
    UserRepresentation user1 = new UserRepresentation();
    user1.setUsername("user1");
    user1.singleAttribute("attr1", "value1user1");
    user1.singleAttribute("attr2", "value2user1");

    Response response = realm.users().create(user1);
    String user1Id = ApiUtil.getCreatedId(response);
    response.close();

    UserRepresentation user2 = new UserRepresentation();
    user2.setUsername("user2");
    user2.singleAttribute("attr1", "value1user2");
    List<String> vals = new ArrayList<>();
    vals.add("value2user2");
    vals.add("value2user2_2");
    user2.getAttributesAsListValues().put("attr2", vals);

    response = realm.users().create(user2);
    String user2Id = ApiUtil.getCreatedId(response);
    response.close();
    user1 = realm.users().get(user1Id).toRepresentation();
    assertEquals(2, user1.getAttributesAsListValues().size());
    assertAttributeValue("value1user1", user1.getAttributesAsListValues().get("attr1"));
    assertAttributeValue("value2user1", user1.getAttributesAsListValues().get("attr2"));

    user2 = realm.users().get(user2Id).toRepresentation();
    assertEquals(2, user2.getAttributesAsListValues().size());
    assertAttributeValue("value1user2", user2.getAttributesAsListValues().get("attr1"));
    vals = user2.getAttributesAsListValues().get("attr2");
    assertEquals(2, vals.size());
    assertTrue(vals.contains("value2user2") && vals.contains("value2user2_2"));

    user1.singleAttribute("attr1", "value3user1");
    user1.singleAttribute("attr3", "value4user1");

    realm.users().get(user1Id).update(user1);

    user1 = realm.users().get(user1Id).toRepresentation();
    assertEquals(3, user1.getAttributesAsListValues().size());
    assertAttributeValue("value3user1", user1.getAttributesAsListValues().get("attr1"));
    assertAttributeValue("value2user1", user1.getAttributesAsListValues().get("attr2"));
    assertAttributeValue("value4user1", user1.getAttributesAsListValues().get("attr3"));

    user1.getAttributes().remove("attr1");
    realm.users().get(user1Id).update(user1);

    user1 = realm.users().get(user1Id).toRepresentation();
    assertEquals(2, user1.getAttributesAsListValues().size());
    assertAttributeValue("value2user1", user1.getAttributesAsListValues().get("attr2"));
    assertAttributeValue("value4user1", user1.getAttributesAsListValues().get("attr3"));

    user1.getAttributes().clear();
    realm.users().get(user1Id).update(user1);

    user1 = realm.users().get(user1Id).toRepresentation();
    assertNull(user1.getAttributes());
  }
コード例 #3
0
  /**
   * Update the user
   *
   * @param id User id
   * @param rep
   * @return
   */
  @Path("{id}")
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public Response updateUser(final @PathParam("id") String id, final UserRepresentation rep) {
    auth.requireManage();

    try {
      UserModel user = session.users().getUserById(id, realm);
      if (user == null) {
        throw new NotFoundException("User not found");
      }

      Set<String> attrsToRemove;
      if (rep.getAttributes() != null) {
        attrsToRemove = new HashSet<>(user.getAttributes().keySet());
        attrsToRemove.removeAll(rep.getAttributes().keySet());
      } else {
        attrsToRemove = Collections.emptySet();
      }

      if (rep.isEnabled() != null && rep.isEnabled()) {
        UsernameLoginFailureModel failureModel =
            session.sessions().getUserLoginFailure(realm, rep.getUsername().toLowerCase());
        if (failureModel != null) {
          failureModel.clearFailures();
        }
      }

      updateUserFromRep(user, rep, attrsToRemove, realm, session);
      adminEvent
          .operation(OperationType.UPDATE)
          .resourcePath(uriInfo)
          .representation(rep)
          .success();

      if (session.getTransaction().isActive()) {
        session.getTransaction().commit();
      }
      return Response.noContent().build();
    } catch (ModelDuplicateException e) {
      return ErrorResponse.exists("User exists with same username or email");
    } catch (ModelReadOnlyException re) {
      return ErrorResponse.exists("User is read only!");
    }
  }