Skip to content

Commit

Permalink
feat: Add required_actions attribute to keycloak_user
Browse files Browse the repository at this point in the history
  • Loading branch information
scheying committed Aug 11, 2023
1 parent bfee4d7 commit d78f0be
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ resource "keycloak_user" "user_with_initial_password" {
- `first_name` - (Optional) The user's first name.
- `last_name` - (Optional) The user's last name.
- `attributes` - (Optional) A map representing attributes for the user. In order to add multivalue attributes, use `##` to seperate the values. Max length for each value is 255 chars
- `required_actions` - (Optional) A list of required user actions.
- `federated_identity` - (Optional) When specified, the user will be linked to a federated identity provider. Refer to the [federated user example](https://github.com/mrparkers/terraform-provider-keycloak/blob/master/example/federated_user_example.tf) for more details.
- `identity_provider` - (Required) The name of the identity provider
- `user_id` - (Required) The ID of the user defined in the identity provider
Expand Down
20 changes: 11 additions & 9 deletions keycloak/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type User struct {
Enabled bool `json:"enabled"`
Attributes map[string][]string `json:"attributes"`
FederatedIdentities FederatedIdentities `json:"federatedIdentities"`
RequiredActions []string `json:"requiredActions"`
}

type PasswordCredentials struct {
Expand All @@ -35,15 +36,16 @@ type PasswordCredentials struct {

func (keycloakClient *KeycloakClient) NewUser(ctx context.Context, user *User) error {
newUser := User{
Id: user.Id,
RealmId: user.RealmId,
Username: user.Username,
Email: user.Email,
EmailVerified: user.EmailVerified,
FirstName: user.FirstName,
LastName: user.LastName,
Enabled: user.Enabled,
Attributes: user.Attributes,
Id: user.Id,
RealmId: user.RealmId,
Username: user.Username,
Email: user.Email,
EmailVerified: user.EmailVerified,
FirstName: user.FirstName,
LastName: user.LastName,
Enabled: user.Enabled,
Attributes: user.Attributes,
RequiredActions: user.RequiredActions,
}
_, location, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/users", user.RealmId), newUser)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
Expand Down Expand Up @@ -47,6 +48,11 @@ func dataSourceKeycloakOpenidClientServiceAccountUser() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},
"required_actions": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"federated_identity": {
Type: schema.TypeList,
Computed: true,
Expand Down
6 changes: 6 additions & 0 deletions provider/data_source_keycloak_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
Expand Down Expand Up @@ -39,6 +40,11 @@ func dataSourceKeycloakUser() *schema.Resource {
Type: schema.TypeMap,
Computed: true,
},
"required_actions": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
"federated_identity": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Expand Down
17 changes: 16 additions & 1 deletion provider/resource_keycloak_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"context"
"errors"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mrparkers/terraform-provider-keycloak/keycloak"
"strings"
)

const MULTIVALUE_ATTRIBUTE_SEPARATOR = "##"
Expand Down Expand Up @@ -63,6 +64,11 @@ func resourceKeycloakUser() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
},
"required_actions": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"federated_identity": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -118,6 +124,13 @@ func onlyDiffOnCreate(_, _, _ string, d *schema.ResourceData) bool {

func mapFromDataToUser(data *schema.ResourceData) *keycloak.User {
attributes := map[string][]string{}
var requiredActions []string

if v, ok := data.GetOk("required_actions"); ok {
for _, requiredAction := range v.(*schema.Set).List() {
requiredActions = append(requiredActions, requiredAction.(string))
}
}
if v, ok := data.GetOk("attributes"); ok {
for key, value := range v.(map[string]interface{}) {
attributes[key] = strings.Split(value.(string), MULTIVALUE_ATTRIBUTE_SEPARATOR)
Expand All @@ -141,6 +154,7 @@ func mapFromDataToUser(data *schema.ResourceData) *keycloak.User {
Enabled: data.Get("enabled").(bool),
Attributes: attributes,
FederatedIdentities: *federatedIdentities,
RequiredActions: requiredActions,
}
}

Expand Down Expand Up @@ -182,6 +196,7 @@ func mapFromUserToData(data *schema.ResourceData, user *keycloak.User) {
data.Set("enabled", user.Enabled)
data.Set("attributes", attributes)
data.Set("federated_identity", federatedIdentities)
data.Set("required_actions", user.RequiredActions)
}

func resourceKeycloakUserCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down

0 comments on commit d78f0be

Please sign in to comment.