Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Readonly permissions field to user #572

Merged
merged 13 commits into from
May 10, 2023
Merged
9 changes: 9 additions & 0 deletions docs/data-sources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ data "auth0_user" "my_user" {
- `name` (String) Name of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `nickname` (String) Preferred nickname or alias of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
- `password` (String) Initial password for this user. Required for non-passwordless connections (SMS and email).
- `permissions` (List of Object) Configuration settings for the credentials for the email provider. (see [below for nested schema](#nestedatt--permissions))
- `phone_number` (String) Phone number for the user; follows the E.164 recommendation. Used for SMS connections.
- `phone_verified` (Boolean) Indicates whether the phone number has been verified.
- `picture` (String) Picture of the user. This value can only be updated if the connection is a database connection (using the Auth0 store), a passwordless connection (email or sms) or has disabled 'Sync user profile attributes at each login'. For more information, see: [Configure Identity Provider Connection for User Profile Updates](https://auth0.com/docs/manage-users/user-accounts/user-profiles/configure-connection-sync-with-auth0).
Expand All @@ -45,4 +46,12 @@ data "auth0_user" "my_user" {
- `username` (String) Username of the user. Only valid if the connection requires a username.
- `verify_email` (Boolean) Indicates whether the user will receive a verification email after creation. Overrides behavior of `email_verified` parameter.

<a id="nestedatt--permissions"></a>
### Nested Schema for `permissions`

Read-Only:

- `name` (String)
- `resource_server_identifier` (String)


5 changes: 1 addition & 4 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ better alternative.
<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `domain` (String) Your Auth0 domain name. It can also be sourced from the `AUTH0_DOMAIN` environment variable.
willvedd marked this conversation as resolved.
Show resolved Hide resolved

### Optional

- `api_token` (String) Your Auth0 [management api access token](https://auth0.com/docs/security/tokens/access-tokens/management-api-access-tokens). It can also be sourced from the `AUTH0_API_TOKEN` environment variable. It can be used instead of `client_id` + `client_secret`. If both are specified, `api_token` will be used over `client_id` + `client_secret` fields.
- `audience` (String) Your Auth0 audience when using a custom domain. It can also be sourced from the `AUTH0_AUDIENCE` environment variable.
- `client_id` (String) Your Auth0 client ID. It can also be sourced from the `AUTH0_CLIENT_ID` environment variable.
- `client_secret` (String) Your Auth0 client secret. It can also be sourced from the `AUTH0_CLIENT_SECRET` environment variable.
- `debug` (Boolean) Indicates whether to turn on debug mode.
- `domain` (String) Your Auth0 domain name. It can also be sourced from the `AUTH0_DOMAIN` environment variable.

## Environment Variables

Expand Down
9 changes: 9 additions & 0 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ resource "auth0_role" "admin" {
### Read-Only

- `id` (String) The ID of this resource.
- `permissions` (List of Object) Configuration settings for the credentials for the email provider. (see [below for nested schema](#nestedatt--permissions))

<a id="nestedatt--permissions"></a>
### Nested Schema for `permissions`

Read-Only:

- `name` (String)
- `resource_server_identifier` (String)

## Import

Expand Down
1 change: 1 addition & 0 deletions internal/auth0/user/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestAccDataSourceUser(t *testing.T) {
resource.TestCheckResourceAttr("data.auth0_user.test", "nickname", strings.ToLower(t.Name())),
resource.TestCheckResourceAttr("data.auth0_user.test", "picture", "https://www.example.com/picture.jpg"),
resource.TestCheckResourceAttr("data.auth0_user.test", "roles.#", "2"),
resource.TestCheckResourceAttr("data.auth0_user.test", "permissions.#", "0"),
resource.TestCheckResourceAttr("data.auth0_user.test", "user_metadata", `{"baz":"qux","foo":"bar"}`),
resource.TestCheckResourceAttr("data.auth0_user.test", "app_metadata", `{"baz":"qux","foo":"bar"}`),
),
Expand Down
36 changes: 36 additions & 0 deletions internal/auth0/user/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,25 @@ func NewResource() *schema.Resource {
Elem: &schema.Schema{Type: schema.TypeString},
Description: "Set of IDs of roles assigned to the user.",
},
"permissions": {
Type: schema.TypeList,
Computed: true,
Description: "Configuration settings for the credentials for the email provider.",
willvedd marked this conversation as resolved.
Show resolved Hide resolved
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of permission.",
willvedd marked this conversation as resolved.
Show resolved Hide resolved
},
"resource_server_identifier": {
Type: schema.TypeString,
Computed: true,
Description: "Resource server identifier associated with permission.",
willvedd marked this conversation as resolved.
Show resolved Hide resolved
},
},
},
},
},
}
}
Expand Down Expand Up @@ -209,6 +228,12 @@ func readUser(ctx context.Context, d *schema.ResourceData, m interface{}) diag.D
}
result = multierror.Append(result, d.Set("roles", flattenUserRoles(roleList)))

permissions, err := api.User.Permissions(*user.ID)
willvedd marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return diag.FromErr(err)
}
result = multierror.Append(result, d.Set("permissions", flattenUserPermissions(permissions)))

return diag.FromErr(result.ErrorOrNil())
}

Expand Down Expand Up @@ -377,6 +402,17 @@ func flattenUserRoles(roleList *management.RoleList) []interface{} {
return roles
}

func flattenUserPermissions(permissionList *management.PermissionList) []interface{} {
var permissions []interface{}
for _, p := range permissionList.Permissions {
permissions = append(permissions, map[string]string{
"name": p.GetName(),
"resource_server_identifier": p.GetResourceServerIdentifier(),
})
}
return permissions
}

func validateUser(user *management.User) error {
validations := []validateUserFunc{
validateNoUsernameAndPasswordSimultaneously(),
Expand Down
2 changes: 2 additions & 0 deletions internal/auth0/user/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func TestAccUser(t *testing.T) {
resource.TestCheckResourceAttr("auth0_user.user", "user_metadata", ""),
resource.TestCheckResourceAttr("auth0_user.user", "app_metadata", ""),
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "0"),
resource.TestCheckResourceAttr("auth0_user.user", "permissions.#", "0"),
),
},
{
Expand Down Expand Up @@ -204,6 +205,7 @@ func TestAccUser(t *testing.T) {
Config: acctest.ParseTestName(testAccUserUpdateRemovingMetadata, strings.ToLower(t.Name())),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_user.user", "roles.#", "0"),
resource.TestCheckResourceAttr("auth0_user.user", "permissions.#", "0"),
resource.TestCheckResourceAttr("auth0_user.user", "user_metadata", ""),
resource.TestCheckResourceAttr("auth0_user.user", "app_metadata", ""),
),
Expand Down