I have implemented this example for user storage in JPA in Keycloak. But I can not figure out how to add a group and role information to the user so it is visible in the JWT token. I guess it has to be set into this UserAdapter or the 'UserEntity entity' object. How exactly to set group info?
I tried to add
protected List<GroupModel> groups = new ArrayList<>();
protected List<RoleModel> roles = new ArrayList<>();
to UserAdapter class but it does not reflect in the JWT token content.
Do like this:
@Override
public UserModel getUserById(RealmModel realm, String id) {
logger.info("getUserById: " + id);
String persistenceId = StorageId.externalId(id);
User user = userRepository.findByDn(persistenceId);
if (Objects.isNull(user)) {
logger.info("could not find user by id: " + id);
return null;
}
UserAdapter ua = new UserAdapter(session, realm, model, user);
RoleModel rol = realm.getRole("user");
if (!ua.hasRole(rol)) {
ua.grantRole(rol);
}
return ua;
}