canvas-lms/lib/microsoft_sync
Evan Battaglia ce25d3d508 Refresh MS Sync user mappings after email changes
closes INTEROP-6817
flag=none

Why:
- When a user's primary email changes, if the root account is set up to
  use email as the login attribute, mapping which maps the user to the
  Microsoft 365 user will be out-of-date.
- We never delete UserMappings except when changing Microsoft Sync
  settings at an account level so currently this is almost impossible
  for customers to rectify

I didn't want to actually immediately delete a mapping, because if a
CommunicationChannel changes in the middle of a sync, the missing
UserMapping could cause us to remove the user from the Microsoft 365
group, even if the user's primary email has not even really changed (for
instance, if the user added a non-primary email). So instead I use a
needs_updating field.

Test plan:
- Make sure you have an account set up with Microsoft Sync using email
  as the login attribute.
- Make sure you have a course set up with Microsoft Sync, ideally with
  2+ users. Make sure it is successfully synced so that there are 2+
  users in the group visible in Microsoft 365 admin console.
- Look at one of the users who has a account in the Microsoft 365
  tenant and change their primary email address to another email address
  of a another user on the Microsoft 365 side
- Manually sync the course
  (course.microsoft_sync_group.syncer_job.run_synchronously)
- Look in the Microsoft 365 admin console and check that the old user
  has been removed and the new user has been added to the group.
- Change the email address again to something not corresponding to any
  user on the Microsoft side
- Sync again and see that the user has been removed from the Microsoft
  group
- Make sure one of your tests used the API to update the email address
  - add a communication channel with the POST API endpoint, e.g.:
    tok http://canvas-lms.docker/api/v1/users/123/communication_channels \
      communication_channel[type]=email \
      communication_channel[address]=someaddress@example.com \
      skip_confirmation=true
  - remove the old communication channel
    tok http://canvas-lms.docker/api/v1/users/123/communication_channels
    tok delete http://canvas-lms.docker/api/v1/users/123/communication_channels/456
- Repeat the steps above but update a primary email address from a
  SIS import. You should be able to do this by creating a users.csv file
  like this and uploading with "SIS Import" in the account menu (and
  choose the "Override UI changes" checkbox):
    user_id,login_id,first_name,last_name,email,status
    sisuser1,sisuser1login,FirstNameOne,LastNameOne,myemailaddress@instructure.com,active
- from a console, set the needs_updating on user's UserMapping to false,
  then destroy one of the user's email communication channels with
  commchannel.destroy_permanently!
- make sure the UserMapping has gotten needs_updating set to true
- destroy all email communication channels for a user, or make all their
  communication channels something without a user on the Microsoft 365
  side. Sync the course. The user should be removed from the course
  (note that you cannot remove the last course owner), and the
  UserMapping should no longer exist at all.

Change-Id: I9e2f3d5e439bebc8c9eaf89b1595908213dc1981
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/294820
Product-Review: Alexis Nast <alexis.nast@instructure.com>
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
Migration-Review: Ben Rinaca <brinaca@instructure.com>
Reviewed-by: Tucker Mcknight <tmcknight@instructure.com>
QA-Review: Tucker Mcknight <tmcknight@instructure.com>
2022-07-18 19:53:35 +00:00
..
graph_service MS Sync: don't send error report if group deleted 2022-06-10 19:25:46 +00:00
canvas_models_helpers.rb Disallow enabling MSFT Sync if too many users 2021-10-05 23:05:50 +00:00
errors.rb MS Sync: don't send error report if group deleted 2022-06-10 19:25:46 +00:00
graph_service.rb MSFT Graph Service split up endpoints refactor 5 2021-11-15 19:54:35 +00:00
graph_service_helpers.rb RuboCop: Style/StringLiterals, Style/StringLiteralsInInterpolation 2021-11-25 14:03:06 +00:00
login_service.rb RuboCop: Style/StringLiterals, Style/StringLiteralsInInterpolation 2021-11-25 14:03:06 +00:00
membership_diff.rb DRY up a couple constants by using JS ENV 2021-10-25 16:39:21 +00:00
partial_membership_diff.rb RuboCop: Style/StringLiterals, Style/StringLiteralsInInterpolation 2021-11-25 14:03:06 +00:00
settings_validator.rb RuboCop: Style/PercentLiteralDelimiters 2021-11-18 23:05:50 +00:00
state_machine_job.rb MS Sync: don't send error report if group deleted 2022-06-10 19:25:46 +00:00
syncer_steps.rb Refresh MS Sync user mappings after email changes 2022-07-18 19:53:35 +00:00
users_uluvs_finder.rb Microsoft Sync: Cross-shard communication channels 2022-01-27 16:46:45 +00:00