private async Task SeedTestUsersAsync() { if (!await _userService.Users.AnyAsync()) { foreach (var testUser in Users) { if (testUser.Id != AppAdministrator.Id) { await _userService.CreateAsync(testUser); foreach (var testUserClaim in UserClaims.Where(userClaim => userClaim.UserId == testUser.Id)) { await _userService.AddClaimAsync(testUser, testUserClaim.ToClaim()); } foreach (var testUserRole in UserRoles.Where(userRole => userRole.UserId == testUser.Id)) { await _userService.AddToRoleAsync(testUser, Roles.Single(role => role.Id == testUserRole.RoleId).Name); } var user = await _userService.FindByIdAsync(testUser.Id.ToString()); await _doxatagService.ChangeDoxatagAsync(user, Doxatags.Single(doxatag => doxatag.UserId == user.Id.ConvertTo <UserId>()).Name); } } Logger.LogInformation("The users being populated..."); } else { Logger.LogInformation("The users already populated."); } }
private async Task SeedAdministratorAsync() { if (!await _userService.Users.AnyAsync(user => user.Id == AppAdministrator.Id)) { var administrator = Users.Single(user => user.Id == AppAdministrator.Id); await _userService.CreateAsync(administrator, Options.Administrator.Password); foreach (var claim in UserClaims.Where(userClaim => userClaim.UserId == administrator.Id)) { await _userService.AddClaimAsync(administrator, claim.ToClaim()); } foreach (var role in UserRoles.Where(userRole => userRole.UserId == administrator.Id)) { await _userService.AddToRoleAsync(administrator, Roles.Single(roleModel => roleModel.Id == role.RoleId).Name); } Logger.LogInformation("The administrator as been created."); } else { var administrator = await _userService.FindByIdAsync(AppAdministrator.Id.ToString()); if (!await _userService.CheckPasswordAsync(administrator, Options.Administrator.Password)) { await _userService.RemovePasswordAsync(administrator); await _userService.AddPasswordAsync(administrator, Options.Administrator.Password); Logger.LogInformation("The administrator password as been updated."); } } }
/// <summary> /// Get the claims associated with the specified <paramref name="user"/> as an asynchronous operation. /// </summary> /// <param name="user">The user whose claims should be retrieved.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> /// <returns>A <see cref="Task{TResult}"/> that contains the claims granted to a user.</returns> public async override Task <IList <Claim> > GetClaimsAsync(User user, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } return(await UserClaims.Where(uc => uc.UserId.Equals(user.Id)).Select(c => c.ToClaim()).ToListAsync(cancellationToken)); }
public override async Task ReplaceClaimAsync(User user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default) { ThrowIfDisposed(); user.CheakArgument(); claim.CheakArgument(); newClaim.CheakArgument(); var matchedClaims = await UserClaims.Where(uc => uc.UserId.Equals(user.Id) && uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync(); foreach (var matchedClaim in matchedClaims) { matchedClaim.ClaimValue = newClaim.Value; matchedClaim.ClaimType = newClaim.Type; } }
public override async Task ReplaceClaimAsync(ApplicationUser <TKey> user, Claim claim, Claim newClaim, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); user.ThrowIfNull(nameof(user)); claim.ThrowIfNull(nameof(claim)); newClaim.ThrowIfNull(nameof(newClaim)); UserClaims ??= (await UserClaimsTable.GetClaimsAsync(user.Id)).ToList(); var matchedClaims = UserClaims.Where(x => x.UserId.Equals(user.Id) && x.ClaimType == claim.Type && x.ClaimValue == claim.Value); foreach (var matchedClaim in matchedClaims) { matchedClaim.ClaimType = newClaim.Type; matchedClaim.ClaimValue = newClaim.Value; } }
/// <inheritdoc/> public override async Task RemoveClaimsAsync(TUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken) { ThrowIfDisposed(); user.ThrowIfNull(nameof(user)); claims.ThrowIfNull(nameof(claims)); UserClaims ??= (await UserClaimsTable.GetClaimsAsync(user.Id)).ToList(); foreach (var claim in claims) { var matchedClaims = UserClaims.Where(x => x.UserId.Equals(user.Id) && x.ClaimType == claim.Type && x.ClaimValue == claim.Value); foreach (var matchedClaim in matchedClaims) { UserClaims.Remove(matchedClaim); } } }
public override async Task RemoveClaimsAsync(User user, IEnumerable <Claim> claims, CancellationToken cancellationToken = default) { ThrowIfDisposed(); user.CheakArgument(); claims.CheakArgument(); foreach (var claim in claims) { var matchedClaims = await UserClaims.Where(uc => uc.UserId.Equals(user.Id) && uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync(); foreach (var c in matchedClaims) { UserClaims.Remove(c); } } }
public async Task <IList <Claim> > GetClaimsAsync(ApplicationUser user, CancellationToken cancellationToken) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } var userClaims = await UserClaims.Where(uc => uc.UserId.Equals(user.Id)).ToListAsync(); List <Claim> result = new List <Claim>(); foreach (var c in userClaims) { result.Add(MakeClaim(c)); } return(result); }
/// <summary> /// Removes the <paramref name="claims"/> given from the specified <paramref name="user"/>. /// </summary> /// <param name="user">The user to remove the claims from.</param> /// <param name="claims">The claim to remove.</param> /// <param name="cancellationToken">The <see cref="CancellationToken"/> used to propagate notifications that the operation should be canceled.</param> /// <returns>The <see cref="Task"/> that represents the asynchronous operation.</returns> public async override Task RemoveClaimsAsync(User user, IEnumerable <Claim> claims, CancellationToken cancellationToken = default(CancellationToken)) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } if (claims == null) { throw new ArgumentNullException(nameof(claims)); } foreach (var claim in claims) { var matchedClaims = await UserClaims.Where(uc => uc.UserId.Equals(user.Id) && uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync(cancellationToken); foreach (var c in matchedClaims) { UserClaims.Remove(c); } } }
public override async Task ReplaceClaimAsync(IdentityUserNav <TKey> user, Claim claim, Claim newClaim, CancellationToken cancellationToken = default) { ThrowIfDisposed(); if (user == null) { throw new ArgumentNullException(nameof(user)); } if (claim == null) { throw new ArgumentNullException(nameof(claim)); } if (newClaim == null) { throw new ArgumentNullException(nameof(newClaim)); } var matchedClaims = await UserClaims.Where(uc => uc.UserId.Equals(user.Id) && uc.ClaimValue == claim.Value && uc.ClaimType == claim.Type).ToListAsync(cancellationToken); foreach (var matchedClaim in matchedClaims) { matchedClaim.ClaimValue = newClaim.Value; matchedClaim.ClaimType = newClaim.Type; } }
public override async Task <IList <Claim> > GetClaimsAsync(User user, CancellationToken cancellationToken = default) { ThrowIfDisposed(); user.CheakArgument(); return(await UserClaims.Where(uc => uc.UserId.Equals(user.Id)).Select(c => c.ToClaim()).ToListAsync()); }