Skip to content

Commit

Permalink
Do not sync users when there is no need (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
enahum authored Feb 29, 2024
1 parent 4a0643f commit 62d0309
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func (p *Plugin) syncUsers() {
if msUser.Type == msteamsUserTypeGuest {
// Check if syncing of MS Teams guest users is disabled.
if !syncGuestUsers {
if isUserPresent && isRemoteUser(mmUser) {
if isUserPresent && isRemoteUser(mmUser) && mmUser.DeleteAt == 0 {
// Deactivate the Mattermost user corresponding to the MS Teams guest user.
p.API.LogInfo("Deactivating the guest user account", "user_id", mmUser.Id, "teams_user_id", msUser.ID)
if err := p.API.UpdateUserActive(mmUser.Id, false); err != nil {
Expand Down Expand Up @@ -767,23 +767,34 @@ func (p *Plugin) syncUsers() {
if err = p.store.SetUserInfo(newUser.Id, msUser.ID, nil); err != nil {
p.API.LogWarn("Unable to set user info during sync user job", "user_id", newUser.Id, "teams_user_id", msUser.ID, "error", err.Error())
}
} else if (username != mmUser.Username || msUser.DisplayName != mmUser.FirstName) && mmUser.RemoteId != nil {
mmUser.Username = username
mmUser.FirstName = msUser.DisplayName
for {
_, err := p.API.UpdateUser(mmUser)
if err != nil {
if err.Id == "app.user.save.username_exists.app_error" {
mmUser.Username += "-" + fmt.Sprint(userSuffixID)
userSuffixID++
continue
} else if mmUser.RemoteId != nil {
shouldUpdate := false
if !strings.HasPrefix(mmUser.Username, "msteams_") && username != mmUser.Username {
mmUser.Username = username
shouldUpdate = true
}

if mmUser.FirstName != msUser.DisplayName {
mmUser.FirstName = msUser.DisplayName
shouldUpdate = true
}

if shouldUpdate {
for {
_, err := p.API.UpdateUser(mmUser)
if err != nil {
if err.Id == "app.user.save.username_exists.app_error" {
mmUser.Username = username + "-" + fmt.Sprint(userSuffixID)
userSuffixID++
continue
}

p.API.LogWarn("Unable to update user during sync user job", "user_id", mmUser.Id, "teams_user_id", msUser.ID, "error", err.Error())
break
}

p.API.LogWarn("Unable to update user during sync user job", "user_id", mmUser.Id, "teams_user_id", msUser.ID, "error", err.Error())
break
}

break
}
}
}
Expand Down

0 comments on commit 62d0309

Please sign in to comment.