Skip to content

Commit

Permalink
Merge pull request #2 from kurzdigital/locale
Browse files Browse the repository at this point in the history
Add locale as KeycloakUser attribute
  • Loading branch information
thomasletsch authored May 17, 2018
2 parents bfbe62f + 2e65e4b commit e2520bb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class KeycloakUser {
private final String firstName;
private final String lastName;
private final String email;
private final String locale;
private final List<String> groups;
private final List<String> roles;
// ...
Expand All @@ -53,6 +54,7 @@ KeycloakUser user = KeycloakUser.builder()
.firstName("Mister")
.lastName("Test")
.email("[email protected]")
.locale("en")
.userName("test")
.groups(Arrays.asList("USER"))
.build();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/kurzdigital/keycloak/KeycloakUser.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public final class KeycloakUser {
private final String firstName;
private final String lastName;
private final String email;
private final String locale;
private final List<String> groups;
private final List<String> roles;

Expand All @@ -24,6 +25,7 @@ private KeycloakUser(Builder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.email = builder.email;
this.locale = builder.locale;
this.groups = new ArrayList<>(builder.groups);
this.roles = new ArrayList<>(builder.roles);
}
Expand All @@ -48,6 +50,10 @@ public String getEmail() {
return email;
}

public String getLocale() {
return locale;
}

public List<String> getGroups() {
return groups;
}
Expand All @@ -63,6 +69,7 @@ public Builder toBuilder() {
.firstName(firstName)
.lastName(lastName)
.email(email)
.locale(locale)
.groups(groups)
.roles(roles);
}
Expand All @@ -77,6 +84,7 @@ public static final class Builder {
private String firstName;
private String lastName;
private String email;
private String locale;
private List<String> groups = Collections.emptyList();
private List<String> roles = Collections.emptyList();

Expand Down Expand Up @@ -108,6 +116,11 @@ public Builder email(String email) {
return this;
}

public Builder locale(String locale) {
this.locale = locale;
return this;
}

public Builder groups(List<String> groups) {
this.groups = groups;
return this;
Expand Down
18 changes: 16 additions & 2 deletions src/main/java/com/kurzdigital/keycloak/KeycloakUserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* An easy to use wrapper around the keycloak admin API user management rest calls.
Expand Down Expand Up @@ -84,8 +85,10 @@ public KeycloakUser createUser(KeycloakUser user) throws MailAlreadyExistsExcept
* @throws MailAlreadyExistsException when the email given is already used by another user.
*/
@Override
public KeycloakUser createUser(KeycloakUser user, String password) throws MailAlreadyExistsException {
public KeycloakUser createUser(KeycloakUser user, String password)
throws MailAlreadyExistsException, UnsupportedLocaleException {
RealmResource realm = getRealmResource();
validateLocales(realm.toRepresentation(), user.getLocale());
UsersResource usersResource = realm.users();
UserRepresentation userRepresentation = KeycloakUserMapper.map(user);
UserRepresentation finalUserRepresentation = userRepresentation;
Expand Down Expand Up @@ -120,14 +123,16 @@ public KeycloakUser createUser(KeycloakUser user, String password) throws MailAl
* Updates the keycloak user with the new data in the given user object.
*/
@Override
public void updateUser(KeycloakUser user) {
public void updateUser(KeycloakUser user) throws UnsupportedLocaleException {
RealmResource realm = getRealmResource();
validateLocales(realm.toRepresentation(), user.getLocale());
UsersResource usersResource = realm.users();
UserResource userResource = usersResource.get(user.getId());
UserRepresentation userRepresentation = userResource.toRepresentation();
userRepresentation.setFirstName(user.getFirstName());
userRepresentation.setLastName(user.getLastName());
userRepresentation.setEmail(user.getEmail());
KeycloakUserMapper.addLocaleToUserRepresentation(user, userRepresentation);
userResource.update(userRepresentation);
List<GroupRepresentation> groups = updateGroups(user, realm, usersResource, user.getId());
}
Expand Down Expand Up @@ -215,4 +220,13 @@ private ClientRepresentation getClient() {
return getRealmResource().clients().findByClientId(keycloakConfiguration.getResource()).get(0);
}

private void validateLocales(RealmRepresentation realmRepresentation, String givenLocale)
throws UnsupportedLocaleException {
if (givenLocale != null) {
Set<String> supportedLocales = realmRepresentation.getSupportedLocales();
if (!supportedLocales.contains(givenLocale)) {
throw new UnsupportedLocaleException(supportedLocales);
}
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/kurzdigital/keycloak/KeycloakUserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserRepresentation;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class KeycloakUserMapper {
Expand All @@ -16,6 +19,9 @@ public static KeycloakUser map(UserRepresentation userRepresentation, List<Group
builder.firstName(userRepresentation.getFirstName());
builder.lastName(userRepresentation.getLastName());
builder.email(userRepresentation.getEmail());
if (userRepresentation.getAttributes() != null && userRepresentation.getAttributes().containsKey("locale")) {
builder.locale(userRepresentation.getAttributes().get("locale").get(0));
}
if (groups != null) {
builder.groups(groups.stream().map(GroupRepresentation::getName).collect(Collectors.toList()));
}
Expand All @@ -32,8 +38,18 @@ public static UserRepresentation map(KeycloakUser user) {
userRepresentation.setFirstName(user.getFirstName());
userRepresentation.setLastName(user.getLastName());
userRepresentation.setEmail(user.getEmail());
addLocaleToUserRepresentation(user, userRepresentation);
userRepresentation.setGroups(user.getGroups());
return userRepresentation;
}

public static void addLocaleToUserRepresentation(KeycloakUser source, UserRepresentation target) {
if (source.getLocale() != null) {
Map<String, List<String>> attributes = new HashMap<>(target.getAttributes());
attributes.put("locale", Collections.singletonList(source.getLocale()));

target.setAttributes(attributes);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.kurzdigital.keycloak;

import java.util.Set;

public class UnsupportedLocaleException extends RuntimeException {

public UnsupportedLocaleException(Set<String> supportedLocales) {
super("Given locale is not supported. Supported locales: " + supportedLocales);
}
}

0 comments on commit e2520bb

Please sign in to comment.