diff --git a/db/db.go b/db/db.go index 190bb8893..21bf201bd 100644 --- a/db/db.go +++ b/db/db.go @@ -8407,3 +8407,49 @@ func RemoveRoleAndPermissions(roleID []int, tenantName string) error { return nil } + +func RemovePermissionsByTenant(tenantName string) error { + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + tenantName = strings.ToLower(tenantName) + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return err + } + defer conn.Release() + + query := `DELETE FROM permissions WHERE tenant_name = $1` + stmt, err := conn.Conn().Prepare(ctx, "remove_permissions_by_tenant", query) + if err != nil { + return err + } + _, err = conn.Conn().Query(ctx, stmt.Name, tenantName) + if err != nil { + return err + } + + return nil +} + +func RemoveRolesByTenant(tenantName string) error { + ctx, cancelfunc := context.WithTimeout(context.Background(), DbOperationTimeout*time.Second) + defer cancelfunc() + tenantName = strings.ToLower(tenantName) + conn, err := MetadataDbClient.Client.Acquire(ctx) + if err != nil { + return err + } + defer conn.Release() + + query := `DELETE FROM roles WHERE tenant_name = $1` + stmt, err := conn.Conn().Prepare(ctx, "remove_roles_by_tenant", query) + if err != nil { + return err + } + _, err = conn.Conn().Query(ctx, stmt.Name, tenantName) + if err != nil { + return err + } + + return nil +} diff --git a/server/memphis_handlers_user_mgmt.go b/server/memphis_handlers_user_mgmt.go index 1ba87ef91..36194423a 100644 --- a/server/memphis_handlers_user_mgmt.go +++ b/server/memphis_handlers_user_mgmt.go @@ -176,6 +176,16 @@ func removeTenantResources(tenantName string, user models.User) error { return err } + err = db.RemovePermissionsByTenant(tenantName) + if err != nil { + return err + } + + err = db.RemoveRolesByTenant(tenantName) + if err != nil { + return err + } + err = db.RemoveStationsByTenant(tenantName) if err != nil { return err