GC: setTenants not checking *users.GetMail() (#629)

User query updated from `*user.GetMail()` -> `*user.GetPrincipalName()`. Guard checks added and added GetUsers as one of the GraphQueryFunctions
This commit is contained in:
Danny 2022-08-22 17:07:57 -04:00 committed by GitHub
parent b573882746
commit d7abed1406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 18 deletions

View File

@ -234,6 +234,10 @@ func (suite *ExchangeServiceSuite) TestGraphQueryFunctions() {
name: "GraphQuery: Get All Folders", name: "GraphQuery: Get All Folders",
function: GetAllFolderNamesForUser, function: GetAllFolderNamesForUser,
}, },
{
name: "GraphQuery: Get All Users",
function: GetAllUsersForTenant,
},
} }
for _, test := range tests { for _, test := range tests {
suite.T().Run(test.name, func(t *testing.T) { suite.T().Run(test.name, func(t *testing.T) {

View File

@ -6,6 +6,7 @@ import (
absser "github.com/microsoft/kiota-abstractions-go/serialization" absser "github.com/microsoft/kiota-abstractions-go/serialization"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
msuser "github.com/microsoftgraph/msgraph-sdk-go/users"
mscontacts "github.com/microsoftgraph/msgraph-sdk-go/users/item/contacts" mscontacts "github.com/microsoftgraph/msgraph-sdk-go/users/item/contacts"
msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/events" msevents "github.com/microsoftgraph/msgraph-sdk-go/users/item/events"
msfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders" msfolder "github.com/microsoftgraph/msgraph-sdk-go/users/item/mailfolders"
@ -51,6 +52,8 @@ var (
"displayName": 6, "displayName": 6,
"employeeId": 7, "employeeId": 7,
"id": 8, "id": 8,
"mail": 9,
"userPrincipalName": 10,
} }
fieldsForMessages = map[string]int{ fieldsForMessages = map[string]int{
@ -132,6 +135,17 @@ func GetAllFolderNamesForUser(gs graph.Service, user string) (absser.Parsable, e
return gs.Client().UsersById(user).MailFolders().GetWithRequestConfigurationAndResponseHandler(options, nil) return gs.Client().UsersById(user).MailFolders().GetWithRequestConfigurationAndResponseHandler(options, nil)
} }
// GetAllUsersForTenant is a GraphQuery for retrieving all the UserCollectionResponse with
// that contains the UserID and email for each user. All other information is omitted
func GetAllUsersForTenant(gs graph.Service, user string) (absser.Parsable, error) {
selecting := []string{"userPrincipalName"}
options, err := optionsForUsers(selecting)
if err != nil {
return nil, err
}
return gs.Client().Users().GetWithRequestConfigurationAndResponseHandler(options, nil)
}
// GetAllEvents for User. Default returns EventResponseCollection for future events. // GetAllEvents for User. Default returns EventResponseCollection for future events.
// of the time that the call was made. There a // of the time that the call was made. There a
func GetAllEventsForUser(gs graph.Service, user string) (absser.Parsable, error) { func GetAllEventsForUser(gs graph.Service, user string) (absser.Parsable, error) {
@ -544,6 +558,20 @@ func optionsForContacts(moreOps []string) (*mscontacts.ContactsRequestBuilderGet
return options, nil return options, nil
} }
func optionsForUsers(moreOps []string) (*msuser.UsersRequestBuilderGetRequestConfiguration, error) {
selecting, err := buildOptions(moreOps, users)
if err != nil {
return nil, err
}
requestParams := &msuser.UsersRequestBuilderGetQueryParameters{
Select: selecting,
}
options := &msuser.UsersRequestBuilderGetRequestConfiguration{
QueryParameters: requestParams,
}
return options, nil
}
// buildOptions - Utility Method for verifying if select options are valid for the m365 object type // buildOptions - Utility Method for verifying if select options are valid for the m365 object type
// @return is a pair. The first is a string literal of allowable options based on the object type, // @return is a pair. The first is a string literal of allowable options based on the object type,
// the second is an error. An error is returned if an unsupported option or optionIdentifier was used // the second is an error. An error is returned if an unsupported option or optionIdentifier was used

View File

@ -5,6 +5,7 @@ package connector
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"strings" "strings"
"sync/atomic" "sync/atomic"
@ -12,7 +13,6 @@ import (
msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go" msgraphsdk "github.com/microsoftgraph/msgraph-sdk-go"
msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core" msgraphgocore "github.com/microsoftgraph/msgraph-sdk-go-core"
"github.com/microsoftgraph/msgraph-sdk-go/models" "github.com/microsoftgraph/msgraph-sdk-go/models"
msuser "github.com/microsoftgraph/msgraph-sdk-go/users"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/alcionai/corso/internal/connector/exchange" "github.com/alcionai/corso/internal/connector/exchange"
@ -116,14 +116,7 @@ func (gs *graphService) EnableFailFast() {
// workspace. The users field is updated during this method // workspace. The users field is updated during this method
// iff the return value is true // iff the return value is true
func (gc *GraphConnector) setTenantUsers() error { func (gc *GraphConnector) setTenantUsers() error {
selecting := []string{"id, mail"} response, err := exchange.GetAllUsersForTenant(gc.graphService, "")
requestParams := &msuser.UsersRequestBuilderGetQueryParameters{
Select: selecting,
}
options := &msuser.UsersRequestBuilderGetRequestConfiguration{
QueryParameters: requestParams,
}
response, err := gc.Client().Users().GetWithRequestConfigurationAndResponseHandler(options, nil)
if err != nil { if err != nil {
return errors.Wrapf( return errors.Wrapf(
err, err,
@ -151,9 +144,20 @@ func (gc *GraphConnector) setTenantUsers() error {
err = support.WrapAndAppend(gc.graphService.adapter.GetBaseUrl(), errors.New("user iteration failure"), err) err = support.WrapAndAppend(gc.graphService.adapter.GetBaseUrl(), errors.New("user iteration failure"), err)
return true return true
} }
gc.Users[*user.GetMail()] = *user.GetId() if user.GetUserPrincipalName() == nil {
err = support.WrapAndAppend(
gc.graphService.adapter.GetBaseUrl(),
fmt.Errorf("no email address for User: %s", *user.GetId()),
err,
)
return true return true
} }
// *user.GetId() is populated for every M365 entityable object by M365 backstore
gc.Users[*user.GetUserPrincipalName()] = *user.GetId()
return true
}
iterateError = userIterator.Iterate(callbackFunc) iterateError = userIterator.Iterate(callbackFunc)
if iterateError != nil { if iterateError != nil {
err = support.WrapAndAppend(gc.graphService.adapter.GetBaseUrl(), iterateError, err) err = support.WrapAndAppend(gc.graphService.adapter.GetBaseUrl(), iterateError, err)