private async Task <Person> EnsurePersonForPrincipalName(string name, Portfolio portfolio = null) { Person person = null; if (!string.IsNullOrWhiteSpace(name)) { var context = ServiceContext.PortfolioContext; // Check if there is already a matching person MicrosoftGraphUserModel user = null; person = context.People.Local.SingleOrDefault(p => p.ActiveDirectoryPrincipalName == name || p.Email == name) ?? await context.People.SingleOrDefaultAsync(p => p.ActiveDirectoryPrincipalName == name || p.Email == name); // There is no person with a AD Id, so we need to find the AD user if (person == null || person.ActiveDirectoryId == null) { if (name.Contains("@")) { // Assume this is an email address: error if it is an external entry if (name.Contains("#EXT#")) { name = name.Substring(0, name.IndexOf("#EXT#")); throw new PortfolioUserException($"Cannot add external user, {name}, to the project. If you are trying to add a supplier as a team member, consider using the 'Supplier' field."); } user = await msgraphService.GetUserForPrincipalNameAsync(name); } else { var result = await msgraphService.GetUsersAsync(name); var users = result.value.Where(u => u.companyName == "Food Standards Agency" && u.department != null).ToList(); if (users.Count == 1) { user = users[0]; } } if (user != null) { // Need to check for an existing person with the same ActiveDirectory Id (this happens when the user has a name change). person = context.People.Local.SingleOrDefault(p => p.ActiveDirectoryId == user.id) ?? await context.People.SingleOrDefaultAsync(p => p.ActiveDirectoryId == user.id); if (person == null) { // Assume an email was passed in person = new Person() { Email = name }; person.Timestamp = DateTime.Now; context.People.Add(person); } PortfolioMapper.ActiveDirectoryMapper.Map(user, person); } else { throw new PortfolioUserException($"User not found: {name}"); } } // Set the team if (person != null && person.Team_Id == null) { // Get the AD user if haven't already if (user == null && !string.IsNullOrWhiteSpace(person.ActiveDirectoryId)) { user = await msgraphService.GetUserForPrincipalNameAsync(name); } // Look up the team from the user's department if (user != null && user.department != null) { // Get the viewkey from the map (or create one from the department) string teamViewKey; if (!lazyTeamViewKeyMap.Value.TryGetValue(user.department, out teamViewKey)) { // Strip none alpha characters from dept to get a viewkey, and lowercase it Regex rgx = new Regex("[^a-zA-Z0-9]"); teamViewKey = rgx.Replace(user.department, "").ToLower(); } // Find the team (or create one) var team = await GetExistingTeamAsync(teamViewKey); if (team == null) { int order = await GetNextOrderAsync(); team = new Team() { ViewKey = teamViewKey, Name = user.department, Order = order }; } person.Team = team; if (portfolio?.Teams != null) { if (!portfolio.Teams.Contains(team)) { portfolio.Teams.Add(team); } } } } } return(person); }
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."); } }