diff --git a/src/pkg/services/m365/m365.go b/src/pkg/services/m365/m365.go index ca2c48fa2..f4851e9ef 100644 --- a/src/pkg/services/m365/m365.go +++ b/src/pkg/services/m365/m365.go @@ -33,6 +33,14 @@ type User struct { Info api.UserInfo } +// UserNoInfo is the minimal information required to identify and display a user. +// TODO: Remove this once `UsersCompatNoInfo` is removed +type UserNoInfo struct { + PrincipalName string + ID string + Name string +} + // UsersCompat returns a list of users in the specified M365 tenant. // TODO(ashmrtn): Remove when upstream consumers of the SDK support the fault // package. @@ -47,6 +55,54 @@ func UsersCompat(ctx context.Context, acct account.Account) ([]*User, error) { return users, errs.Failure() } +// UsersCompatNoInfo returns a list of users in the specified M365 tenant. +// TODO: Remove this once `Info` is removed from the `User` struct and callers +// have switched over +func UsersCompatNoInfo(ctx context.Context, acct account.Account) ([]*UserNoInfo, error) { + errs := fault.New(true) + + users, err := usersNoInfo(ctx, acct, errs) + if err != nil { + return nil, err + } + + return users, errs.Failure() +} + +// usersNoInfo returns a list of users in the specified M365 tenant - with no info +// TODO: Remove this once we remove `Info` from `Users` and instead rely on the `GetUserInfo` API +// to get user information +func usersNoInfo(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*UserNoInfo, error) { + uapi, err := makeUserAPI(acct) + if err != nil { + return nil, clues.Wrap(err, "getting users").WithClues(ctx) + } + + users, err := discovery.Users(ctx, uapi, errs) + if err != nil { + return nil, err + } + + ret := make([]*UserNoInfo, 0, len(users)) + + for _, u := range users { + pu, err := parseUser(u) + if err != nil { + return nil, clues.Wrap(err, "formatting user data") + } + + puNoInfo := &UserNoInfo{ + PrincipalName: pu.PrincipalName, + ID: pu.ID, + Name: pu.Name, + } + + ret = append(ret, puNoInfo) + } + + return ret, nil +} + // Users returns a list of users in the specified M365 tenant func Users(ctx context.Context, acct account.Account, errs *fault.Bus) ([]*User, error) { uapi, err := makeUserAPI(acct) diff --git a/src/pkg/services/m365/m365_test.go b/src/pkg/services/m365/m365_test.go index 0353d4e36..46028ee3f 100644 --- a/src/pkg/services/m365/m365_test.go +++ b/src/pkg/services/m365/m365_test.go @@ -52,6 +52,30 @@ func (suite *M365IntegrationSuite) TestUsers() { } } +func (suite *M365IntegrationSuite) TestUsersCompat_HasNoInfo() { + ctx, flush := tester.NewContext() + defer flush() + + var ( + t = suite.T() + acct = tester.NewM365Account(suite.T()) + ) + + users, err := m365.UsersCompatNoInfo(ctx, acct) + assert.NoError(t, err, clues.ToCore(err)) + assert.NotEmpty(t, users) + + for _, u := range users { + suite.Run("user_"+u.ID, func() { + t := suite.T() + + assert.NotEmpty(t, u.ID) + assert.NotEmpty(t, u.PrincipalName) + assert.NotEmpty(t, u.Name) + }) + } +} + func (suite *M365IntegrationSuite) TestGetUserInfo() { ctx, flush := tester.NewContext() defer flush()