Skip to content

Commit

Permalink
fix: allow definition of multivalued user profile attributes (keycloa…
Browse files Browse the repository at this point in the history
…k#1071)

In order to replicate the existing userprofile configuration, we need to support the multivalued attribute.

We now support the `multi_valued = bool` attribute.

Example:
```hcl
  attribute {
    name = "myAttr"

    multi_valued = true

    validator {
      name = "options"
      config = {
        options = jsonencode(["opt1", "opt2", "opt3"])
      }
    }

    annotations = {
      foo = jsonencode({ "key" : "val" })
    }
  }
 ```

 Fixes keycloak#1064

Signed-off-by: Thomas Darimont <[email protected]>
Signed-off-by: angeloxx <[email protected]>
  • Loading branch information
thomasdarimont authored and angeloxx committed Jan 26, 2025
1 parent 01153e9 commit e0833b1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/resources/realm_user_profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "keycloak_realm_user_profile" "userprofile" {
display_name = "Field 1"
group = "group1"
multi_valued = false
enabled_when_scope = ["offline_access"]
required_for_roles = ["user"]
Expand Down Expand Up @@ -98,6 +99,7 @@ resource "keycloak_realm_user_profile" "userprofile" {

- `name` - (Required) The name of the attribute.
- `display_name` - (Optional) The display name of the attribute.
- `multi_valued` - (Optional) If the attribute supports multiple values. Defaults to `false`.
- `group` - (Optional) The group that the attribute belong to.
- `enabled_when_scope` - (Optional) A list of scopes. The attribute will only be enabled when these scopes are requested by clients.
- `required_for_roles` - (Optional) A list of roles for which the attribute will be required.
Expand Down
1 change: 1 addition & 0 deletions keycloak/realm_user_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type RealmUserProfileAttribute struct {
DisplayName string `json:"displayName,omitempty"`
Group string `json:"group,omitempty"`
Name string `json:"name"`
MultiValued bool `json:"multivalued,omitempty"`
Permissions *RealmUserProfilePermissions `json:"permissions,omitempty"`
Required *RealmUserProfileRequired `json:"required,omitempty"`
Selector *RealmUserProfileSelector `json:"selector,omitempty"`
Expand Down
13 changes: 13 additions & 0 deletions provider/resource_keycloak_realm_user_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func resourceKeycloakRealmUserProfile() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"multi_valued": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"group": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -153,6 +158,12 @@ func getRealmUserProfileAttributeFromData(m map[string]interface{}) *keycloak.Re
Group: m["group"].(string),
}

if v, ok := m["multivalued"].(bool); ok {
attribute.MultiValued = v
} else {
attribute.MultiValued = false
}

if v, ok := m["permissions"]; ok && len(v.([]interface{})) > 0 {
permissions := keycloak.RealmUserProfilePermissions{
Edit: make([]string, 0),
Expand Down Expand Up @@ -329,6 +340,8 @@ func getRealmUserProfileAttributeData(attr *keycloak.RealmUserProfileAttribute)
attributeData["name"] = attr.Name

attributeData["display_name"] = attr.DisplayName
attributeData["multi_valued"] = attr.MultiValued

attributeData["group"] = attr.Group
if attr.Selector != nil && len(attr.Selector.Scopes) != 0 {
attributeData["enabled_when_scope"] = attr.Selector.Scopes
Expand Down
1 change: 1 addition & 0 deletions provider/resource_keycloak_realm_user_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func TestAccKeycloakRealmUserProfile_basicFull(t *testing.T) {
{
Name: "attribute2",
DisplayName: "attribute 2",
MultiValued: false,
Group: "group",
Selector: &keycloak.RealmUserProfileSelector{Scopes: []string{"roles"}},
Required: &keycloak.RealmUserProfileRequired{
Expand Down

0 comments on commit e0833b1

Please sign in to comment.