public void UserRepository_AddTest_ValidUser_NewAddress()
 {
     IUserRepository userRepository = IoCFactory.Instance.CurrentContainer.Resolve<IUserRepository>();
     IoCFactory.Instance.CurrentContainer.Resolve<IAddressRepository>();
     User user1 = new User()
     {
         Id = new Guid("514c5873-bee8-cc78-3de8-08d1b9e806d0"),
         UserName = "******",
         FirstName = "test2",
         LastName = "test2",
         PasswordHash = "test2",
         BirthDate = DateTime.UtcNow,
         LastActivityDate = DateTime.UtcNow,
         Address = new Address()
         {
             Id = Guid.NewGuid(),
             AddressLine1 = "test2",
             ZipCode = "12352",
             City = "Test2"
         }
     };
    
     userRepository.Add(user1);
     userRepository.UnitOfWork.Commit();
     User byId = userRepository.GetElementById(user1.Id);
     Assert.IsNotNull((object)byId);
     Assert.IsTrue(byId.Id == user1.Id);
 }
        public async Task UserManagementServices_AddAsyncTest()
        {
            IUserManagementServices manager = IoCFactory.Instance.CurrentContainer.Resolve<IUserManagementServices>();
            User user = new User()
            {
                Id = Guid.NewGuid(),
                UserName = "******",
                FirstName = "test",
                LastName = "test",
                PasswordHash="test",
                BirthDate = DateTime.UtcNow,
                Address=new Address(){
                    Id = Guid.NewGuid(),
                    AddressLine1 = "test",
                    ZipCode = "1235",
                    City = "Test",
                }
            };

            await manager.AddAsync(user);
            User usr = manager.GetById(user.Id);
            Assert.IsNotNull((object)usr);
            Assert.IsTrue(usr.Id == user.Id);
            Assert.IsTrue(usr.FirstName == user.FirstName);
        }
 public async Task RemoveAsync(User user, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (user == (User)null)
         throw new ArgumentNullException("user");
     this._userRepository.Remove(user);
     await this._userRepository.UnitOfWork.CommitAsync(cancellationToken);
 }
        public async Task AddAsync(User user, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == (User)null)
                throw new ArgumentNullException("user");

            IMainBCUnitOfWork unitOfWork = this._userRepository.UnitOfWork as IMainBCUnitOfWork;
            this._userRepository.Add(user);
            await unitOfWork.CommitAsync(cancellationToken);
        }
        public static User ToDomainUser(this ApplicationUser model)
        {
            User user = null;
            if (model != null)
            {
                user = new User()
                {
                    UserName = model.UserName,
                    Email = model.Email,
                    PasswordHash = model.PasswordHash,
                    SecurityStamp = model.SecurityStamp,
                    LockoutEnabled = model.LockoutEnabled,
                    LastName = "test",
                    FirstName = "test",
                    BirthDate = DateTime.Now,
                    TwoFactorEnabled = model.TwoFactorEnabled,
                    EmailConfirmed = model.EmailConfirmed,
                    PhoneNumberConfirmed = model.PhoneNumberConfirmed,
                    Address = new Address()
                    {
                        Id = Guid.NewGuid(),
                        AddressLine1 = "test",
                        ZipCode = "test",
                        City = "test",
                    },

                    Roles = new List<Role>()
                                    {
                                       new Role(){
                                           Id = Guid.NewGuid(),
                                           Name = "test"
                                       }
                                    },
                    Claims = new List<Claim>(),
                    Logins = new List<Login>(),
                };

                if (!string.IsNullOrEmpty(model.Id))
                {
                    user.Id = new Guid(model.Id);
                }
            }

            return user;
        }
        private async Task<User> GetUserWithClaims(User user, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Claims));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException,("User id=" + user.Id)));
            else
                return dbUser;
        }
        public async Task RemoveRoleAsync(User user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (string.IsNullOrEmpty(roleName))
                throw new ArgumentNullException("roleName");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Roles));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
            if (dbUser.Roles != null)
            {
                Role role = dbUser.Roles.FirstOrDefault((x => string.Equals(x.Name, roleName, StringComparison.InvariantCultureIgnoreCase)));
                if (role == (Role)null)
                    throw new Exception(Messages.RemoveNonExistingRoleException);
                dbUser.Roles.Remove(role);
                this._userRepository.SetModified(dbUser);
                await this._userRepository.UnitOfWork.CommitAsync();
            }
        }
        public async Task<bool> IsInRoleAsync(User user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (string.IsNullOrEmpty(roleName))
                throw new ArgumentNullException("roleName");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken,  x => x.Roles);
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
            else
                return dbUser.Roles != null && dbUser.Roles.Any<Role>(x => string.Equals(x.Name, roleName, StringComparison.InvariantCultureIgnoreCase));
        }
        public async Task AddRoleAsync(User user, string roleName, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (string.IsNullOrEmpty(roleName))
                throw new ArgumentNullException("roleName");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Roles));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + (object)user.Id)));
            if (dbUser.Roles == null)
                dbUser.Roles = new List<Role>();

            if (dbUser.Roles.Any(x => string.Equals(x.Name, roleName, StringComparison.InvariantCultureIgnoreCase)))
                throw new Exception(string.Format(Messages.RoleAlreadyExistException, roleName));
            dbUser.Roles.Add(new Role()
            {
                Name = roleName
            });
            this._userRepository.SetModified(dbUser);
            await this._userRepository.UnitOfWork.CommitAsync();
        }
        public async Task RemoveClaimAsync(User user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (claim == (Claim)null)
                throw new ArgumentNullException("claim");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Claims));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
            if (dbUser.Claims != null)
            {
                Claim clm = (Claim)null;
                clm = (claim.Id == Guid.Empty) ?
                     dbUser.Claims.FirstOrDefault(x => x.ClaimType != null && x.ClaimType == claim.ClaimType && x.ClaimValue != null && x.ClaimValue == claim.ClaimValue) :
                     dbUser.Claims.FirstOrDefault(x => x.Id == claim.Id);
                if (clm == (Claim)null)
                    throw new Exception(Messages.RemoveNonExistingClaimException);
                dbUser.Claims.Remove(clm);
                this._userRepository.SetModified(dbUser);
                await this._userRepository.UnitOfWork.CommitAsync();
            }
        }
 public async Task AddClaimAsync(User user, Claim claim, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (user == (User)null)
         throw new ArgumentNullException("user");
     if (claim == (Claim)null)
         throw new ArgumentNullException("claim");
     User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Claims));
     if (dbUser == (User)null)
         throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
     if (dbUser.Claims == null)
         dbUser.Claims = (ICollection<Claim>)new List<Claim>();
     dbUser.Claims.Add(claim);
     this._userRepository.SetModified(dbUser);
     await this._userRepository.UnitOfWork.CommitAsync();
 }
 public async Task<IEnumerable<Login>> GetLoginsAsync(User user, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (user == (User)null)
         throw new ArgumentNullException("user");
     User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Logins));
     if (dbUser == (User)null)
         throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));
     else
         return dbUser.Logins;
 }
        public async Task RemoveLoginAsync(User user, Login login, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (login == (Login)null)
                throw new ArgumentNullException("login");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Logins));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));

            if (dbUser.Logins != null)
            {
                Login log = (Login)null;
                log = (login.Id == Guid.Empty) ?
                    dbUser.Logins.FirstOrDefault(x => x.ProviderKey != null && x.ProviderKey == login.ProviderKey && x.LoginProvider != null && x.LoginProvider == login.LoginProvider) :
                    dbUser.Logins.FirstOrDefault(x => x.Id == login.Id);
                if (log == (Login)null)
                    throw new Exception(Messages.RemoveNonExistingException);
                dbUser.Logins.Remove(log);
                this._userRepository.SetModified(dbUser);

                await this._userRepository.UnitOfWork.CommitAsync();
            }
        }
        public async Task AddLoginAsync(User user, Login login, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (login == (Login)null)
                throw new ArgumentNullException("login");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), new CancellationToken(), (x => x.Logins));

            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));

            if (dbUser.Logins == null)
                dbUser.Logins = new List<Login>();

            dbUser.Logins.Add(login);

            this._userRepository.SetModified(dbUser);


            await this._userRepository.UnitOfWork.CommitAsync();
        }
        public void UserRepository_AddTest_InValidUser()
        {
            IUserRepository userRepository = IoCFactory.Instance.CurrentContainer.Resolve<IUserRepository>();
            IoCFactory.Instance.CurrentContainer.Resolve<IAddressRepository>();
            User user1 = new User()
            {
                Id = Guid.NewGuid(),
                FirstName = "test",
                LastName = "test",
                BirthDate = DateTime.UtcNow,
                LastActivityDate = DateTime.UtcNow,
                Address = new Address()
                {
                    Id = Guid.NewGuid(),
                    AddressLine1 = "test",
                    ZipCode = "1235",
                    City = "Test"
                }
            };

            userRepository.Add(user1);
            userRepository.UnitOfWork.CommitAndRefreshChanges();
            userRepository.GetElementById(user1.Id);
        }