@@ -67,6 +67,7 @@ func (s *Bridge) Run(ctx context.Context) (created, updated, deleted, deactivate
|
||||
}
|
||||
|
||||
scimUsersByEmail := make(map[string]*scimclient.User)
|
||||
|
||||
for i := range scimUsers {
|
||||
email := strings.ToLower(scimUsers[i].UserName)
|
||||
scimUsersByEmail[email] = &scimUsers[i]
|
||||
@@ -86,6 +87,7 @@ func (s *Bridge) Run(ctx context.Context) (created, updated, deleted, deactivate
|
||||
errs = append(errs, fmt.Errorf("cannot create user %q: %w", pu.ExternalID, err))
|
||||
continue
|
||||
}
|
||||
|
||||
created++
|
||||
} else {
|
||||
needsUpdate := existingSCIM.Active != pu.Active ||
|
||||
@@ -107,6 +109,7 @@ func (s *Bridge) Run(ctx context.Context) (created, updated, deleted, deactivate
|
||||
errs = append(errs, fmt.Errorf("cannot update user %q: %w", pu.ExternalID, err))
|
||||
continue
|
||||
}
|
||||
|
||||
updated++
|
||||
} else {
|
||||
skipped++
|
||||
@@ -124,7 +127,9 @@ func (s *Bridge) Run(ctx context.Context) (created, updated, deleted, deactivate
|
||||
errs = append(errs, fmt.Errorf("cannot delete user %q: %w", scimUser.ExternalID, err))
|
||||
continue
|
||||
}
|
||||
|
||||
deleted++
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -136,6 +141,7 @@ func (s *Bridge) Run(ctx context.Context) (created, updated, deleted, deactivate
|
||||
errs = append(errs, fmt.Errorf("cannot deactivate user %q: %w", scimUser.ExternalID, err))
|
||||
continue
|
||||
}
|
||||
|
||||
deactivated++
|
||||
}
|
||||
|
||||
@@ -148,5 +154,6 @@ func (s *Bridge) isExcluded(email string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -72,6 +72,7 @@ func NewClient(httpClient *http.Client, endpoint, token string) *Client {
|
||||
|
||||
func (c *Client) ListUsers(ctx context.Context) (Users, error) {
|
||||
var allUsers Users
|
||||
|
||||
startIndex := 1
|
||||
count := 100
|
||||
|
||||
@@ -107,6 +108,7 @@ func (c *Client) listUsersPage(ctx context.Context, startIndex, count int) (User
|
||||
if err != nil {
|
||||
return nil, 0, fmt.Errorf("cannot fetch users: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
@@ -131,6 +133,7 @@ func (c *Client) CreateUser(ctx context.Context, user *User) error {
|
||||
}
|
||||
|
||||
reqURL := fmt.Sprintf("%s/Users", c.endpoint)
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create request: %w", err)
|
||||
@@ -143,6 +146,7 @@ func (c *Client) CreateUser(ctx context.Context, user *User) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create user: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
|
||||
@@ -162,6 +166,7 @@ func (c *Client) UpdateUser(ctx context.Context, userID string, user *User) erro
|
||||
}
|
||||
|
||||
reqURL := fmt.Sprintf("%s/Users/%s", c.endpoint, url.PathEscape(userID))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, reqURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create request: %w", err)
|
||||
@@ -174,6 +179,7 @@ func (c *Client) UpdateUser(ctx context.Context, userID string, user *User) erro
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot update user: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
@@ -244,6 +250,7 @@ func (c *Client) DeactivateUser(ctx context.Context, userID string) error {
|
||||
}
|
||||
|
||||
reqURL := fmt.Sprintf("%s/Users/%s", c.endpoint, url.PathEscape(userID))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, reqURL, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create request: %w", err)
|
||||
@@ -256,6 +263,7 @@ func (c *Client) DeactivateUser(ctx context.Context, userID string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot deactivate user: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
|
||||
@@ -268,6 +276,7 @@ func (c *Client) DeactivateUser(ctx context.Context, userID string) error {
|
||||
|
||||
func (c *Client) DeleteUser(ctx context.Context, userID string) error {
|
||||
reqURL := fmt.Sprintf("%s/Users/%s", c.endpoint, url.PathEscape(userID))
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, reqURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create request: %w", err)
|
||||
@@ -279,6 +288,7 @@ func (c *Client) DeleteUser(ctx context.Context, userID string) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete user: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent && resp.StatusCode != http.StatusNotFound {
|
||||
|
||||
@@ -57,6 +57,7 @@ func TestUser_UnmarshalJSON(t *testing.T) {
|
||||
}`)
|
||||
|
||||
var user scimclient.User
|
||||
|
||||
err := json.Unmarshal(data, &user)
|
||||
|
||||
require.NoError(t, err)
|
||||
@@ -96,6 +97,7 @@ func TestUser_UnmarshalJSON(t *testing.T) {
|
||||
}`)
|
||||
|
||||
var user scimclient.User
|
||||
|
||||
err := json.Unmarshal(data, &user)
|
||||
|
||||
require.NoError(t, err)
|
||||
@@ -123,6 +125,7 @@ func TestUser_UnmarshalJSON(t *testing.T) {
|
||||
}`)
|
||||
|
||||
var user scimclient.User
|
||||
|
||||
err := json.Unmarshal(data, &user)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -55,6 +55,7 @@ func (p *Provider) isExcluded(email string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -65,6 +66,7 @@ func (p *Provider) ListUsers(ctx context.Context) (scimclient.Users, error) {
|
||||
}
|
||||
|
||||
var allUsers scimclient.Users
|
||||
|
||||
pageToken := ""
|
||||
|
||||
for {
|
||||
@@ -128,12 +130,16 @@ func (p *Provider) extractOrganizationFields(raw any, user *scimclient.User) {
|
||||
return
|
||||
}
|
||||
|
||||
var primary *admin.UserOrganization
|
||||
var first *admin.UserOrganization
|
||||
var (
|
||||
primary *admin.UserOrganization
|
||||
first *admin.UserOrganization
|
||||
)
|
||||
|
||||
for i := range orgs {
|
||||
if first == nil {
|
||||
first = &orgs[i]
|
||||
}
|
||||
|
||||
if orgs[i].Primary {
|
||||
primary = &orgs[i]
|
||||
break
|
||||
@@ -144,6 +150,7 @@ func (p *Provider) extractOrganizationFields(raw any, user *scimclient.User) {
|
||||
if org == nil {
|
||||
org = first
|
||||
}
|
||||
|
||||
if org == nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ func (p *Provider) isExcluded(email string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -115,6 +116,7 @@ func (p *Provider) ListUsers(ctx context.Context) (scimclient.Users, error) {
|
||||
}
|
||||
|
||||
var allUsers scimclient.Users
|
||||
|
||||
for range graphMaxPages {
|
||||
users, next, err := p.fetchPage(ctx, endpoint)
|
||||
if err != nil {
|
||||
@@ -126,9 +128,11 @@ func (p *Provider) ListUsers(ctx context.Context) (scimclient.Users, error) {
|
||||
if email == "" {
|
||||
email = u.UserPrincipalName
|
||||
}
|
||||
|
||||
if email == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if p.isExcluded(email) {
|
||||
continue
|
||||
}
|
||||
@@ -151,6 +155,7 @@ func (p *Provider) ListUsers(ctx context.Context) (scimclient.Users, error) {
|
||||
if next == "" {
|
||||
return allUsers, nil
|
||||
}
|
||||
|
||||
endpoint = next
|
||||
}
|
||||
|
||||
@@ -177,12 +182,14 @@ func (p *Provider) fetchPage(ctx context.Context, endpoint string) ([]graphUser,
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot create graph users request: %w", err)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
resp, err := p.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot list graph users: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = resp.Body.Close() }()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
|
||||
Reference in New Issue
Block a user