Account theme - add new field populated with the groups of the current user

Hi,

I created a new account theme and I want to display a select input populated with current user’ groups.
I don’t know how to retrieve the user groups inside the template account.ftl. Is it possibile? If yes, how?

This is what I tried until now:

         <div class="form-group">
            <div class="col-sm-2 col-md-2">
                <label for="default_group" class="control-label">${msg("defaultGroupLabel")}</label>
            </div>
              <div class="col-sm-10 col-md-10">
                <select name="user.attributes.default_group">
                    <#list account.groups() as group>
                        <option value="${group.id}">${group.name}</option>
                    </#list>
                </select>
            </div>
        </div>

The goal is to provide a way to the end user which allow him to configure an attribute that contains the id of the desired group. This attribute will be then used when login in the application to display the default group.

Other tries:

 <div class="form-group">
            <div class="col-sm-2 col-md-2">
                <label for="default_group" class="control-label">${msg("defaultGroupLabel")}</label>
            </div>
              <div class="col-sm-10 col-md-10">
                <select name="user.attributes.default_group">
                    <#list realm.users().get(user.getId()).groups() as group>
                        <option value="${group.id}">${group.name}</option>
                    </#list>
                </select>
            </div>
        </div>
 <div class="form-group">
            <div class="col-sm-2 col-md-2">
                <label for="default_group" class="control-label">${msg("defaultGroupLabel")}</label>
            </div>
              <div class="col-sm-10 col-md-10">
                <select name="user.attributes.default_group">
                    <#list groups() as group>
                        <option value="${group.id}">${group.name}</option>
                    </#list>
                </select>
            </div>
        </div>

Afraid groups is not available to the templates right now. Feel free to open a request for it in our JIRA and a contribution would also be welcome.

The following feature request https://issues.redhat.com/browse/KEYCLOAK-12721 has been issued.