Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
// ------------------------ Breaking changes ------------------------ //
== Breaking changes

=== Method `UserProfile#toRepresentation(boolean)` added

The `UserProfile` interface has a new method `toRepresentation(boolean)`. This method allows clients to specify whether to include
only the basic attributes in representations or all of them.

The `UserProfile` interface is a private API and should not be implemented by custom code. However, if you have extensions that
implement this interface, you will need to update your code to accommodate this new method.

// ------------------------ Notable changes ------------------------ //
== Notable changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ private Stream<BruteUser> toRepresentation(RealmModel realm, UserPermissionEvalu

return userModels.map(user -> {
UserProfile profile = provider.create(UserProfileContext.USER_API, user);
UserRepresentation rep = profile.toRepresentation();
UserRepresentation rep = profile.toRepresentation(!briefRepresentationB);
UserRepresentation userRep = briefRepresentationB ?
ModelToRepresentation.toBriefRepresentation(user, rep, false) :
ModelToRepresentation.toRepresentation(session, realm, user, rep, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,12 @@ public Attributes getAttributes() {
}

@Override
public <R extends AbstractUserRepresentation> R toRepresentation() {
public <R extends AbstractUserRepresentation> R toRepresentation(boolean full) {
if (user == null) {
throw new IllegalStateException("Can not create the representation because the user is not yet created");
}

R rep = createUserRepresentation();
R rep = createUserRepresentation(full);
Map<String, List<String>> readable = attributes.getReadable();
Map<String, List<String>> attributesRep = new HashMap<>(readable);

Expand Down Expand Up @@ -290,13 +290,18 @@ public <R extends AbstractUserRepresentation> R toRepresentation() {
}

@SuppressWarnings("unchecked")
private <R extends AbstractUserRepresentation> R createUserRepresentation() {
private <R extends AbstractUserRepresentation> R createUserRepresentation(boolean full) {
UserProfileContext context = metadata.getContext();
R rep;

if (context.isAdminContext()) {
RealmModel realm = session.getContext().getRealm();
rep = (R) ModelToRepresentation.toRepresentation(session, realm, user);

if (full) {
rep = (R) ModelToRepresentation.toRepresentation(session, realm, user);
} else {
rep = (R) new org.keycloak.representations.idm.UserRepresentation();
}
} else {
// by default, we build the simplest representation without exposing much information about users
rep = (R) new org.keycloak.representations.account.UserRepresentation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,20 @@ default void update(AttributeChangeListener... changeListener) throws Validation
*/
Attributes getAttributes();

<R extends AbstractUserRepresentation> R toRepresentation();
/**
* Returns the full user representation
*
* @return the user representation
*/
default <R extends AbstractUserRepresentation> R toRepresentation() {
return toRepresentation(true);
}

/**
* Returns the user representation
*
* @param full if the full representation should be returned
* @return the user representation
*/
<R extends AbstractUserRepresentation> R toRepresentation(boolean full);
}
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ private Stream<UserRepresentation> toRepresentation(RealmModel realm, UserPermis
return userModels
.map(user -> {
UserProfile profile = provider.create(UserProfileContext.USER_API, user);
UserRepresentation rep = profile.toRepresentation();
UserRepresentation rep = profile.toRepresentation(!briefRepresentationB);
UserRepresentation userRep = briefRepresentationB ?
ModelToRepresentation.toBriefRepresentation(user, rep, false) :
ModelToRepresentation.toRepresentation(session, realm, user, rep, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;

@KeycloakIntegrationTest
public class UsersTest {
Expand All @@ -62,6 +63,20 @@ public void searchUserWithWildcards() {
assertThat(realm.admin().users().search("Us*e", null, null), hasSize(1));
}

@Test
public void testFullRepresentationOnSearches() {
createUser("user", "firstName", "lastName", "[email protected]");

List<UserRepresentation> users = realm.admin().users().search("user", null, null, false);
UserRepresentation user = users.get(0);
assertThat(user.getRequiredActions(), empty());

users = realm.admin().users().search("user", null, null, true);
user = users.get(0);
assertThat(user.getRequiredActions(), nullValue());
assertThat(user.isTotp(), nullValue());
}

@Test
public void searchUserDefaultSettings() throws Exception {
createUser("User", "firstName", "lastName", "[email protected]");
Expand Down
Loading