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

[3/X] DXCDT-455: Fix permissions update issue in auth0_user_permissions resource #630

Merged
merged 5 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 35 additions & 33 deletions internal/auth0/user/resource_permissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,46 +66,56 @@ func NewPermissionsResource() *schema.Resource {
}

func upsertUserPermissions(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
if !data.HasChange("permissions") {
return nil
}

api := meta.(*config.Config).GetAPI()
mutex := meta.(*config.Config).GetMutex()

userID := data.Get("user_id").(string)

if !data.HasChange("permissions") {
return nil
}

mutex.Lock(userID)
defer mutex.Unlock(userID)

toAdd, toRemove := value.Difference(data, "permissions")

var addPermissions []*management.Permission
for _, addPermission := range toAdd {
permission := addPermission.(map[string]interface{})
addPermissions = append(addPermissions, &management.Permission{
var rmPermissions []*management.Permission
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is swapping around the removal and additions. We need to always perform removals first.

for _, rmPermission := range toRemove {
permission := rmPermission.(map[string]interface{})
rmPermissions = append(rmPermissions, &management.Permission{
Name: auth0.String(permission["name"].(string)),
ResourceServerIdentifier: auth0.String(permission["resource_server_identifier"].(string)),
})
}

if len(addPermissions) > 0 {
if err := api.User.AssignPermissions(userID, addPermissions); err != nil {
if len(rmPermissions) > 0 {
if err := api.User.RemovePermissions(userID, rmPermissions); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}

return diag.FromErr(err)
}
}

var rmPermissions []*management.Permission
for _, rmPermission := range toRemove {
permission := rmPermission.(map[string]interface{})
rmPermissions = append(rmPermissions, &management.Permission{
var addPermissions []*management.Permission
for _, addPermission := range toAdd {
permission := addPermission.(map[string]interface{})
addPermissions = append(addPermissions, &management.Permission{
Name: auth0.String(permission["name"].(string)),
ResourceServerIdentifier: auth0.String(permission["resource_server_identifier"].(string)),
})
}

if len(rmPermissions) > 0 {
if err := api.User.RemovePermissions(userID, rmPermissions); err != nil {
if len(addPermissions) > 0 {
if err := api.User.AssignPermissions(userID, addPermissions); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}

return diag.FromErr(err)
}
}
Expand All @@ -124,6 +134,7 @@ func readUserPermissions(_ context.Context, data *schema.ResourceData, meta inte
data.SetId("")
return nil
}

return diag.FromErr(err)
}

Expand All @@ -144,33 +155,24 @@ func deleteUserPermissions(_ context.Context, data *schema.ResourceData, meta in
mutex.Lock(userID)
defer mutex.Unlock(userID)

permissions, err := api.User.Permissions(userID)
if err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}
return diag.FromErr(err)
}
permissions := data.Get("permissions").(*schema.Set).List()

var rmPermissions []*management.Permission
for _, rmPermission := range permissions.Permissions {
for _, rmPermission := range permissions {
permission := rmPermission.(map[string]interface{})
rmPermissions = append(rmPermissions, &management.Permission{
Name: auth0.String(rmPermission.GetName()),
ResourceServerIdentifier: auth0.String(rmPermission.GetResourceServerIdentifier()),
Name: auth0.String(permission["name"].(string)),
ResourceServerIdentifier: auth0.String(permission["resource_server_identifier"].(string)),
})
}
if err := api.User.RemovePermissions(
userID,
rmPermissions,
); err != nil {

if err := api.User.RemovePermissions(userID, rmPermissions); err != nil {
if mErr, ok := err.(management.Error); ok && mErr.Status() == http.StatusNotFound {
data.SetId("")
return nil
}

return diag.FromErr(err)
}

data.SetId("")
return nil
}
Loading