public virtual async Task SetOrganizationUnitsAsync(IdentityUser user, params Guid[] organizationUnitIds) { Check.NotNull(user, nameof(user)); Check.NotNull(organizationUnitIds, nameof(organizationUnitIds)); await CheckMaxUserOrganizationUnitMembershipCountAsync(organizationUnitIds.Length); await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); //Remove from removed OUs foreach (var ouId in user.OrganizationUnits.Select(uou => uou.OrganizationUnitId).ToArray()) { if (!organizationUnitIds.Contains(ouId)) { user.RemoveOrganizationUnit(ouId); } } //Add to added OUs foreach (var organizationUnitId in organizationUnitIds) { if (!user.IsInOrganizationUnit(organizationUnitId)) { user.AddOrganizationUnit(organizationUnitId); } } }
public virtual async Task <List <OrganizationUnit> > GetOrganizationUnitsAsync(IdentityUser user) { await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); return(await OrganizationUnitRepository.GetListAsync( user.OrganizationUnits.Select(t => t.OrganizationUnitId), cancellationToken : CancellationToken )); }
public virtual async Task AddToOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) { await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); if (user.OrganizationUnits.Any(cou => cou.OrganizationUnitId == ou.Id)) { return; } await CheckMaxUserOrganizationUnitMembershipCountAsync(user.OrganizationUnits.Count + 1); user.AddOrganizationUnit(ou.Id); }
public virtual async Task UpdateUserAsync(IdentityUser user, string providerName) { await IdentityOptions.SetAsync(); var externalUser = await GetUserInfoAsync(user); NormalizeExternalLoginUserInfo(externalUser, user.UserName); if (!externalUser.Name.IsNullOrWhiteSpace()) { user.Name = externalUser.Name; } if (!externalUser.Surname.IsNullOrWhiteSpace()) { user.Surname = externalUser.Surname; } if (user.PhoneNumber != externalUser.PhoneNumber) { if (!externalUser.PhoneNumber.IsNullOrWhiteSpace()) { await UserManager.SetPhoneNumberAsync(user, externalUser.PhoneNumber); user.SetPhoneNumberConfirmed(externalUser.PhoneNumberConfirmed == true); } } else { if (!user.PhoneNumber.IsNullOrWhiteSpace() && user.PhoneNumberConfirmed == false && externalUser.PhoneNumberConfirmed == true) { user.SetPhoneNumberConfirmed(true); } } if (!string.Equals(user.Email, externalUser.Email, StringComparison.OrdinalIgnoreCase)) { (await UserManager.SetEmailAsync(user, externalUser.Email)).CheckErrors(); user.SetEmailConfirmed(externalUser.EmailConfirmed ?? false); } if (externalUser.TwoFactorEnabled != null) { (await UserManager.SetTwoFactorEnabledAsync(user, externalUser.TwoFactorEnabled.Value)).CheckErrors(); } await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.Logins); var userLogin = user.Logins.FirstOrDefault(l => l.LoginProvider == providerName); if (userLogin != null) { if (userLogin.ProviderKey != externalUser.ProviderKey) { (await UserManager.RemoveLoginAsync(user, providerName, userLogin.ProviderKey)).CheckErrors(); (await UserManager.AddLoginAsync(user, new UserLoginInfo(providerName, externalUser.ProviderKey, providerName))).CheckErrors(); } } else { (await UserManager.AddLoginAsync(user, new UserLoginInfo(providerName, externalUser.ProviderKey, providerName))).CheckErrors(); } user.IsExternal = true; (await UserManager.UpdateAsync(user)).CheckErrors(); }
public virtual async Task RemoveFromOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) { await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); user.RemoveOrganizationUnit(ou.Id); }
public virtual async Task <bool> IsInOrganizationUnitAsync(IdentityUser user, OrganizationUnit ou) { await IdentityUserRepository.EnsureCollectionLoadedAsync(user, u => u.OrganizationUnits, CancellationTokenProvider.Token); return(user.IsInOrganizationUnit(ou.Id)); }