-
Notifications
You must be signed in to change notification settings - Fork 23
/
users.go
123 lines (100 loc) · 3.28 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package m365
import (
"context"
"fmt"
"github.com/alcionai/clues"
"github.com/microsoftgraph/msgraph-sdk-go/models"
"github.com/alcionai/corso/src/internal/common/ptr"
"github.com/alcionai/corso/src/internal/m365/service/exchange"
"github.com/alcionai/corso/src/internal/m365/service/onedrive"
"github.com/alcionai/corso/src/pkg/fault"
"github.com/alcionai/corso/src/pkg/services/m365/api"
)
// UserNoInfo is the minimal information required to identify and display a user.
// TODO(pandeyabs): Rename this to User now that `Info` support has been removed.
type UserNoInfo struct {
PrincipalName string
ID string
Name string
}
// UsersCompatNoInfo returns a list of users in the specified M365 tenant.
// TODO(pandeyabs): Rename this to Users now that `Info` support has been removed. Would
// need corresponding changes in SDK consumers.
func (c client) UsersCompatNoInfo(ctx context.Context) ([]*UserNoInfo, error) {
errs := fault.New(true)
us, err := usersNoInfo(ctx, c.AC, errs)
if err != nil {
return nil, err
}
return us, errs.Failure()
}
// UserHasMailbox returns true if the user has an exchange mailbox enabled
// false otherwise, and a nil pointer and an error in case of error
func (c client) UserHasMailbox(ctx context.Context, userID string) (bool, error) {
return exchange.IsServiceEnabled(ctx, c.AC.Users(), userID)
}
func (c client) UserGetMailboxInfo(
ctx context.Context,
userID string,
) (api.MailboxInfo, error) {
return exchange.GetMailboxInfo(ctx, c.AC.Users(), userID)
}
// UserHasDrives returns true if the user has any drives
// false otherwise, and a nil pointer and an error in case of error
func (c client) UserHasDrives(ctx context.Context, userID string) (bool, error) {
return onedrive.IsServiceEnabled(ctx, c.AC.Users(), userID)
}
// usersNoInfo returns a list of users in the specified M365 tenant - with no info
func usersNoInfo(
ctx context.Context,
ac api.Client,
errs *fault.Bus,
) ([]*UserNoInfo, error) {
us, err := ac.Users().GetAll(ctx, errs)
if err != nil {
return nil, err
}
ret := make([]*UserNoInfo, 0, len(us))
for _, u := range us {
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
}
func (c client) UserAssignedLicenses(ctx context.Context, userID string) (int, error) {
us, err := c.AC.Users().GetByID(
ctx,
userID,
api.CallConfig{Select: api.SelectProps("assignedLicenses")})
if err != nil {
return 0, err
}
if us.GetAssignedLicenses() != nil {
for _, license := range us.GetAssignedLicenses() {
fmt.Println(license.GetSkuId())
}
return len(us.GetAssignedLicenses()), nil
}
return 0, clues.New("user missing assigned licenses")
}
// parseUser extracts information from `models.Userable` we care about
func parseUser(item models.Userable) (*UserNoInfo, error) {
if item.GetUserPrincipalName() == nil {
return nil, clues.New("user missing principal name").
With("user_id", ptr.Val(item.GetId()))
}
u := &UserNoInfo{
PrincipalName: ptr.Val(item.GetUserPrincipalName()),
ID: ptr.Val(item.GetId()),
Name: ptr.Val(item.GetDisplayName()),
}
return u, nil
}