Skip to content

Commit d830181

Browse files
authored
Add aggregate attributes flag to keycloak_saml_user_attribute_protocol_mapper (#1080)
Signed-off-by: Robin Meese <[email protected]>
1 parent e8ccbc6 commit d830181

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

docs/resources/saml_user_attribute_protocol_mapper.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ resource "keycloak_saml_user_attribute_protocol_mapper" "saml_user_attribute_map
4747
- `client_id` - (Optional) The client this protocol mapper should be attached to. Conflicts with `client_scope_id`. One of `client_id` or `client_scope_id` must be specified.
4848
- `client_scope_id` - (Optional) The client scope this protocol mapper should be attached to. Conflicts with `client_id`. One of `client_id` or `client_scope_id` must be specified.
4949
- `friendly_name` - (Optional) An optional human-friendly name for this attribute.
50+
- `aggregate_attributes`- (Optional) Indicates whether this attribute is a single value or an array of values. Defaults to `false`.
5051

5152
## Import
5253

keycloak/saml_user_attribute_protocol_mapper.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keycloak
33
import (
44
"context"
55
"fmt"
6+
"strconv"
67
)
78

89
type SamlUserAttributeProtocolMapper struct {
@@ -12,10 +13,11 @@ type SamlUserAttributeProtocolMapper struct {
1213
ClientId string
1314
ClientScopeId string
1415

15-
UserAttribute string
16-
FriendlyName string
17-
SamlAttributeName string
18-
SamlAttributeNameFormat string
16+
UserAttribute string
17+
FriendlyName string
18+
SamlAttributeName string
19+
SamlAttributeNameFormat string
20+
AggregateAttributeValues bool
1921
}
2022

2123
func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper() *protocolMapper {
@@ -25,27 +27,34 @@ func (mapper *SamlUserAttributeProtocolMapper) convertToGenericProtocolMapper()
2527
Protocol: "saml",
2628
ProtocolMapper: "saml-user-attribute-mapper",
2729
Config: map[string]string{
28-
attributeNameField: mapper.SamlAttributeName,
29-
attributeNameFormatField: mapper.SamlAttributeNameFormat,
30-
friendlyNameField: mapper.FriendlyName,
31-
userAttributeField: mapper.UserAttribute,
30+
attributeNameField: mapper.SamlAttributeName,
31+
attributeNameFormatField: mapper.SamlAttributeNameFormat,
32+
friendlyNameField: mapper.FriendlyName,
33+
userAttributeField: mapper.UserAttribute,
34+
aggregateAttributeValuesField: strconv.FormatBool(mapper.AggregateAttributeValues),
3235
},
3336
}
3437
}
3538

36-
func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) *SamlUserAttributeProtocolMapper {
39+
func (protocolMapper *protocolMapper) convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId string) (*SamlUserAttributeProtocolMapper, error) {
40+
aggregateAttributeValues, err := parseBoolAndTreatEmptyStringAsFalse(protocolMapper.Config[addToAccessTokenField])
41+
if err != nil {
42+
return nil, err
43+
}
44+
3745
return &SamlUserAttributeProtocolMapper{
3846
Id: protocolMapper.Id,
3947
Name: protocolMapper.Name,
4048
RealmId: realmId,
4149
ClientId: clientId,
4250
ClientScopeId: clientScopeId,
4351

44-
UserAttribute: protocolMapper.Config[userAttributeField],
45-
FriendlyName: protocolMapper.Config[friendlyNameField],
46-
SamlAttributeName: protocolMapper.Config[attributeNameField],
47-
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
48-
}
52+
UserAttribute: protocolMapper.Config[userAttributeField],
53+
FriendlyName: protocolMapper.Config[friendlyNameField],
54+
SamlAttributeName: protocolMapper.Config[attributeNameField],
55+
SamlAttributeNameFormat: protocolMapper.Config[attributeNameFormatField],
56+
AggregateAttributeValues: aggregateAttributeValues,
57+
}, nil
4958
}
5059

5160
func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) (*SamlUserAttributeProtocolMapper, error) {
@@ -56,7 +65,7 @@ func (keycloakClient *KeycloakClient) GetSamlUserAttributeProtocolMapper(ctx con
5665
return nil, err
5766
}
5867

59-
return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId), nil
68+
return protocolMapper.convertToSamlUserAttributeProtocolMapper(realmId, clientId, clientScopeId)
6069
}
6170

6271
func (keycloakClient *KeycloakClient) DeleteSamlUserAttributeProtocolMapper(ctx context.Context, realmId, clientId, clientScopeId, mapperId string) error {

provider/resource_keycloak_saml_user_attribute_protocol_mapper.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ func resourceKeycloakSamlUserAttributeProtocolMapper() *schema.Resource {
6262
Required: true,
6363
ValidateFunc: validation.StringInSlice(keycloakSamlUserAttributeProtocolMapperNameFormats, false),
6464
},
65+
"aggregate_attributes": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
Default: false,
69+
Description: "Indicates if attribute values should be aggregated within the group attributes",
70+
},
6571
},
6672
}
6773
}
@@ -96,6 +102,7 @@ func mapFromSamlUserAttributeMapperToData(mapper *keycloak.SamlUserAttributeProt
96102
data.Set("friendly_name", mapper.FriendlyName)
97103
data.Set("saml_attribute_name", mapper.SamlAttributeName)
98104
data.Set("saml_attribute_name_format", mapper.SamlAttributeNameFormat)
105+
data.Set("aggregate_attributes", mapper.AggregateAttributeValues)
99106
}
100107

101108
func resourceKeycloakSamlUserAttributeProtocolMapperCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {

0 commit comments

Comments
 (0)