Exemple #1
0
        protected override ICollection <Entity.UserLogin> GetCollection(Entity.User user)
        {
            if (user.UserLogins == null)
            {
                user.UserLogins = new List <Entity.UserLogin>();
            }

            return(user.UserLogins);
        }
Exemple #2
0
        protected override ICollection <Entity.UserClaim> GetCollection(Entity.User user)
        {
            if (user.UserClaims == null)
            {
                user.UserClaims = new List <Entity.UserClaim>();
            }

            return(user.UserClaims);
        }
        public async Task DeleteAsync_should_remove_entity_id_from_parent()
        {
            using var store = new RavenDbTestDriverWrapper().GetDocumentStore();

            var entity = new TEntity
            {
                Id     = Guid.NewGuid().ToString(),
                UserId = "test"
            };
            var user = new Entity.User
            {
                Id = "test",
            };
            var collection = GetCollection(user);

            collection.Add(new TEntity
            {
                Id = $"{typeof(TEntity).Name.ToLowerInvariant()}/{entity.Id}"
            });

            using var s1 = store.OpenAsyncSession();
            await s1.StoreAsync(user, $"{nameof(Entity.User).ToLowerInvariant()}/{user.Id}");

            await s1.StoreAsync(entity, $"{typeof(TEntity).Name.ToLowerInvariant()}/{entity.Id}");

            await s1.SaveChangesAsync();

            var loggerMock = new Mock <ILogger <AdminStore <TEntity> > >();

            using var session = store.OpenAsyncSession();

            var sut = CreateSut(session, loggerMock.Object);


            await sut.DeleteAsync(entity.Id);

            using var s2 = store.OpenAsyncSession();
            var updated = await s2.LoadAsync <Entity.User>($"{nameof(Entity.User).ToLowerInvariant()}/test");

            var updatedCollection = GetCollection(updated);

            Assert.DoesNotContain(updatedCollection, i => i.Id == $"{typeof(TEntity).Name.ToLowerInvariant()}/{entity.Id}");
        }
 public static TUser ToUser <TUser>(this Entity.User entity) where TUser : IdentityUser, new()
 {
     return(new TUser
     {
         Id = entity.Id,
         AccessFailedCount = entity.AccessFailedCount,
         Email = entity.Email,
         EmailConfirmed = entity.EmailConfirmed,
         LockoutEnabled = entity.LockoutEnabled,
         LockoutEnd = entity.LockoutEnd,
         PhoneNumber = entity.PhoneNumber,
         PhoneNumberConfirmed = entity.PhoneNumberConfirmed,
         TwoFactorEnabled = entity.TwoFactorEnabled,
         UserName = entity.UserName,
         ConcurrencyStamp = entity.ConcurrencyStamp,
         NormalizedEmail = entity.NormalizedEmail,
         NormalizedUserName = entity.NormalizedUserName,
         PasswordHash = entity.PasswordHash,
         SecurityStamp = entity.SecurityStamp
     });
 }
Exemple #5
0
 public static User FromEntity(Entity.User user)
 {
     return(new User
     {
         AccessFailedCount = user.AccessFailedCount,
         Email = user.Email,
         EmailConfirmed = user.EmailConfirmed,
         Id = user.Id,
         LockoutEnabled = user.LockoutEnabled,
         LockoutEnd = user.LockoutEnd,
         PhoneNumber = user.PhoneNumber,
         PhoneNumberConfirmed = user.PhoneNumberConfirmed,
         TwoFactorEnabled = user.TwoFactorEnabled,
         UserName = user.UserName,
         PasswordHash = user.PasswordHash,
         ConcurrencyStamp = user.ConcurrencyStamp,
         NormalizedEmail = user.NormalizedEmail,
         NormalizedUserName = user.NormalizedUserName,
         SecurityStamp = user.SecurityStamp
     });
 }
        public async Task <Entity.User> UpdateAsync(Entity.User entity, CancellationToken cancellationToken = default)
        {
            var user = await GetUserAsync(entity.Id)
                       .ConfigureAwait(false);

            user.Email            = entity.Email;
            user.EmailConfirmed   = entity.EmailConfirmed;
            user.TwoFactorEnabled = entity.TwoFactorEnabled;
            user.LockoutEnabled   = entity.LockoutEnabled;

            if (entity.LockoutEnd.HasValue)
            {
                var lockoutEnd = entity.LockoutEnd.Value;
                user.LockoutEnd = lockoutEnd != DateTime.MinValue ? new DateTimeOffset(lockoutEnd) : DateTimeOffset.MinValue;
            }
            else
            {
                user.LockoutEnd = null;
            }
            user.PhoneNumber          = entity.PhoneNumber;
            user.PhoneNumberConfirmed = entity.PhoneNumberConfirmed;
            user.SecurityStamp        = entity.SecurityStamp;
            user.UserName             = entity.UserName;
            user.NormalizedUserName   = entity.NormalizedUserName;
            user.NormalizedEmail      = entity.NormalizedEmail;
            user.ConcurrencyStamp     = entity.ConcurrencyStamp;
            user.PasswordHash         = entity.PasswordHash;
            var result = await _userManager.UpdateAsync(user)
                         .ConfigureAwait(false);

            if (result.Succeeded)
            {
                _logger.LogInformation("Entity {EntityId} updated", entity.Id, entity);
                return(entity);
            }
            throw new IdentityException
                  {
                      Errors = result.Errors
                  };
        }
        public async Task <Entity.User> CreateAsync(Entity.User entity, CancellationToken cancellationToken = default)
        {
            var user = entity.ToUser <TUser>();

            user.Id = Guid.NewGuid().ToString();
            var result = string.IsNullOrEmpty(entity.Password)
                ? await _userManager.CreateAsync(user)
                         .ConfigureAwait(false)
                : await _userManager.CreateAsync(user, entity.Password)
                         .ConfigureAwait(false);

            if (result.Succeeded)
            {
                entity.Id = user.Id;
                _logger.LogInformation("Entity {EntityId} created", entity.Id, entity);
                return(entity);
            }
            throw new IdentityException
                  {
                      Errors = result.Errors
                  };
        }
 protected abstract ICollection <TEntity> GetCollection(Entity.User user);