func ProcessUserProfile()

in controllers/keycloakrealm/chain/user_profile.go [43:99]


func ProcessUserProfile(ctx context.Context, realm string, userProfileSpec *common.UserProfileConfig, kClient keycloak.Client) error {
	userProfile, err := kClient.GetUsersProfile(ctx, realm)
	if err != nil {
		return fmt.Errorf("unable to get current user profile: %w", err)
	}

	userProfileToUpdate := userProfileConfigSpecToModel(userProfileSpec)
	attributesToUpdate := userProfileConfigAttributeToMap(&userProfileToUpdate)

	if userProfile.Attributes == nil {
		userProfile.Attributes = &[]keycloakgoclient.UserProfileAttribute{}
	}

	for i := 0; i < len(*userProfile.Attributes); i++ {
		attribute := (*userProfile.Attributes)[i]
		if v, ok := attributesToUpdate[*attribute.Name]; ok {
			(*userProfile.Attributes)[i] = v

			delete(attributesToUpdate, *attribute.Name)
		}
	}

	for _, v := range attributesToUpdate {
		*userProfile.Attributes = append(*userProfile.Attributes, v)
	}

	groupsToUpdate := userProfileConfigGroupToMap(&userProfileToUpdate)

	if userProfile.Groups == nil {
		userProfile.Groups = &[]keycloakgoclient.UserProfileGroup{}
	}

	for i := 0; i < len(*userProfile.Groups); i++ {
		group := (*userProfile.Groups)[i]
		if v, ok := groupsToUpdate[*group.Name]; ok {
			(*userProfile.Groups)[i] = v

			delete(groupsToUpdate, *group.Name)
		}
	}

	for _, v := range groupsToUpdate {
		*userProfile.Groups = append(*userProfile.Groups, v)
	}

	userProfile.UnmanagedAttributePolicy = userProfileToUpdate.UnmanagedAttributePolicy

	if _, err = kClient.UpdateUsersProfile(
		ctx,
		realm,
		*userProfile,
	); err != nil {
		return fmt.Errorf("unable to update user profile: %w", err)
	}

	return nil
}