public ApplicationUserManager(PortfolioContext portfolioContext, IUserStore <ApplicationUser> store, IRoleService roleManager, string accessToken) : base(store) { this.portfolioContext = portfolioContext; this.roleManager = roleManager; if (accessToken != null) { this.accessToken = accessToken; graph = new MicrosoftGraphUserStoreService(roleManager); } }
public async Task SyncPeople(string viewKey = "odd", bool forceADSync = false) { log.Add("Syncing people..."); using (var source = new MigratePortfolioContext <oddproject>("odd")) { var dest = ServiceContext.PortfolioContext; var portfolio = await dest.Portfolios.Include(p => p.Teams).SingleAsync(p => p.ViewKey == "odd"); // Sync the people foreach (var sourcePerson in source.odd_people.AsEnumerable().Where(p => !string.IsNullOrWhiteSpace(p.email))) { var destPerson = dest.People.Local.SingleOrDefault(u => u.Email == sourcePerson.email) ?? dest.People.Include(p => p.Team).SingleOrDefault(u => u.Email == sourcePerson.email); if (destPerson == null) { destPerson = new Person() { Firstname = sourcePerson.firstname, Surname = sourcePerson.surname, Email = sourcePerson.email, Timestamp = sourcePerson.timestamp }; dest.People.Add(destPerson); log.Add($"Added person {destPerson.Firstname} {destPerson.Surname}"); } if (SyncMaps.emailMap.ContainsKey(destPerson.Email)) { destPerson.Email = SyncMaps.emailMap[destPerson.Email]; } // Set the active directory user MicrosoftGraphUserModel adUser = null; if (destPerson.ActiveDirectoryId == null || forceADSync) { if (destPerson.Email != null) { var graph = new MicrosoftGraphUserStoreService(roleManager); adUser = await graph.GetUserForPrincipalNameAsync(destPerson.Email); if (adUser != null) { destPerson.ActiveDirectoryPrincipalName = adUser.userPrincipalName; destPerson.ActiveDirectoryId = adUser.id; destPerson.ActiveDirectoryDisplayName = adUser.displayName; } else { log.Add($"USER NOT FOUND! {destPerson.Email}"); } } } var teamViewKey = sourcePerson.g6team; if (destPerson.Team == null && destPerson.Team?.ViewKey != teamViewKey) { var team = portfolio.Teams.SingleOrDefault(t => t.ViewKey == teamViewKey); if (team == null) { team = dest.Teams.Local.SingleOrDefault(t => t.ViewKey == teamViewKey) ?? await dest.Teams.SingleOrDefaultAsync(t => t.ViewKey == teamViewKey); if (team == null) { team = new Team() { ViewKey = teamViewKey, Name = SyncMaps.teamNameMap[teamViewKey].Item1 }; dest.Teams.Add(team); } portfolio.Teams.Add(team); } if (team.Order == 0) { team.Order = SyncMaps.teamNameMap[teamViewKey].Item2; } destPerson.Team = team; } } dest.SaveChanges(); log.Add("Sync people complete."); } }