Ejemplo n.º 1
0
 public LoginServices(IServiceBaseParameter <AspNetUsers> serviceBaseParameter, ITokenBusiness tokenBusiness, IIdentityUnitOfWork <AspNetRoles> roleUnitOfWork, IIdentityUnitOfWork <AspNetUsersRoles> userRolesUnitOfWork)
 {
     _tokenBusiness        = tokenBusiness;
     _serviceBaseParameter = serviceBaseParameter;
     _roleUnitOfWork       = roleUnitOfWork;
     _userRolesUnitOfWork  = userRolesUnitOfWork;
 }
Ejemplo n.º 2
0
        public async Task CreateUserAsyncMethod_WithFirstUser_ShouldBeReturnedSuccessfulIdentityOperation()
        {
            UserDTO user = new UserDTO
            {
                Email    = "*****@*****.**",
                FName    = "Josh",
                LName    = "Collins",
                Password = "******",
                Role     = "Worker",
                UserName = "******"
            };

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            IList <ApplicationUser> applicationUsers = new List <ApplicationUser>();

            applicationUsers.Insert(0, new ApplicationUser());
            var expected = new IdentityOperation(true, "Sign up is success", "");

            mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null);
            mock.Setup(x => x.Users.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).Returns(Task.FromResult(new IdentityResult()));
            mock.Setup(x => x.Users.GetUsersInRoleAsync("Admin")).Returns(Task.FromResult(applicationUsers));
            mock.Setup(x => x.People.Create(It.IsAny <Person>()));


            var actual = await service.CreateUserAsync(user);


            Assert.AreSame(expected.Message, actual.Message);
            Assert.AreSame(expected.Property, actual.Property);
            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 3
0
        public async Task CreateUserAsyncMethod_UserAlreadyExists_ShouldBeReturnedFailedIdentityOperation()
        {
            UserDTO user = new UserDTO
            {
                Email    = "*****@*****.**",
                FName    = "Josh",
                LName    = "Collins",
                Password = "******",
                Role     = "Worker",
                UserName = "******"
            };

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            IList <ApplicationUser> applicationUsers = new List <ApplicationUser>(1);
            var expected = new IdentityOperation(false, "", "");

            mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());


            var actual = await service.CreateUserAsync(user);


            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 4
0
        private static void DIManagerDemo()
        {
            Console.WriteLine("\nUnityManager Demo\n");

            try
            {
                IDIManager diManager = DIHelper.GetService <IDIManager>();
                Console.WriteLine(diManager.ToString());

                IIdentityUnitOfWork unitOfWork =
                    diManager.GetService <IIdentityUnitOfWork>();
                Console.WriteLine(unitOfWork.ToString());

                IIdentityGenericApplication <User> application =
                    diManager.GetService <IIdentityGenericApplication <User> >();
                Console.WriteLine(application.ToString());

                ZOperationResult operationResult = new ZOperationResult();
                User             user            = application.Get(operationResult, x => x.UserName.ToLower() == "administrator");
                Console.WriteLine(user.UserName);
            }
            catch (Exception exception)
            {
                WriteException(exception);
            }
        }
Ejemplo n.º 5
0
        public async Task UpdateUserRoleAsyncMethod_ManagerWithTeamToWorker_ShouldBeReturnedFailedIdentityOperation()
        {
            string         userId = "1a", rolename = "Worker";
            IList <string> roles = new List <string>();

            roles.Add("Manager");

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            var expected = new IdentityOperation(false, "", "");

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());
            mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles);
            mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync(new Person {
                TeamId = 1
            });


            var actual = await service.UpdateUserRoleAsync(userId, rolename);


            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 6
0
        public async Task CreateUserAsyncMethod_CannotCreateUser_ShouldBeReturnedFailedIdentityOperation()
        {
            UserDTO user = new UserDTO
            {
                Email    = "*****@*****.**",
                FName    = "Josh",
                LName    = "Collins",
                Password = "******",
                Role     = "Worker",
                UserName = "******"
            };

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            IList <ApplicationUser> applicationUsers = new List <ApplicationUser>();

            applicationUsers.Insert(0, new ApplicationUser());

            mock.Setup(x => x.Users.FindByEmailAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null);
            mock.Setup(x => x.Users.CreateAsync(It.IsAny <ApplicationUser>(), It.IsAny <string>())).Returns(Task.FromResult(new IdentityResult()));
            mock.Setup(x => x.Users.GetUsersInRoleAsync("Admin")).Returns(Task.FromResult(applicationUsers));
            // If we already have duplicate of username in the database, for example.
            mock.Setup(x => x.People.Create(It.IsAny <Person>())).Throws(new ArgumentException());


            var actual = await service.CreateUserAsync(user);


            Assert.AreEqual(false, actual.Succeeded);
        }
Ejemplo n.º 7
0
        public async Task GetUserByIdAsyncMethod_UserInTeam_ShouldBeReturnedUser()
        {
            var expected = new UserDTO {
                Role = "Manager", TeamName = "TestTeam1"
            };

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());
            mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(new List <string>()
            {
                "Manager"
            });
            mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync(new Person()
            {
                TeamId = 1
            });
            mock.Setup(x => x.Teams.GetByIdAsync(It.IsAny <int>())).ReturnsAsync(new Team()
            {
                TeamName = "TestTeam1"
            });


            var actual = await service.GetUserByIdAsync(It.IsAny <string>());


            Assert.AreSame(expected.Role, actual.Role);
            Assert.AreSame(expected.TeamName, actual.TeamName);
        }
Ejemplo n.º 8
0
 public MenuServices(IServiceBaseParameter <Menus> businessBaseParameter, IIdentityUnitOfWork <AspNetRoles> roleUnitOfWork, IIdentityUnitOfWork <MenuRoles> menuRoleUnitOfWork, IIdentityUnitOfWork <AspNetUsersRoles> userRoleUnitOfWork) : base(businessBaseParameter)
 {
     _userRoleUnitOfWork = userRoleUnitOfWork;
     _roleUnitOfWork     = roleUnitOfWork;
     _menuRoleUnitOfWork = menuRoleUnitOfWork;
     _menuIdList         = new List <string>();
     _menuIdSelectedList = new List <string>();
 }
Ejemplo n.º 9
0
 public UserService(IIdentityUnitOfWork uow, IMapper mapper, IJwtFactory jwtFactory, JWTIssuerOptions jwtOptions, IHttpContextAccessor httpContextAccessor)
 {
     db                       = uow;
     this.mapper              = mapper;
     this.jwtFactory          = jwtFactory;
     this.jwtOptions          = jwtOptions;
     this.httpContextAccessor = httpContextAccessor;
 }
Ejemplo n.º 10
0
        private static void PersistenceIdentityData <TEntity>(IIdentityUnitOfWork unitOfWork)
            where TEntity : ZDataBase
        {
            IGenericRepository <TEntity> repository = unitOfWork.GetRepository <TEntity>();
            TEntity entity = repository.Query().FirstOrDefault();

            Console.WriteLine(typeof(TEntity).Name + ": " + repository.CountAll());
        }
Ejemplo n.º 11
0
        public RoleService(IIdentityUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));

            _mapper = new Mapper(new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <ApplicationRole, RoleDto>();
            }));
        }
Ejemplo n.º 12
0
        public AccountService(IIdentityUnitOfWork uow)
        {
            Database = uow;
            Database.UserManager.EmailService = new EmailService();

            var provider = new DpapiDataProtectionProvider("VariousTests");

            Database.UserManager.UserTokenProvider = new DataProtectorTokenProvider <AppUser>(provider.Create("EmailConfirmation"));
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Create units and mappers for work.
 /// </summary>
 /// <param name="mapperDTO">Mapper for converting database entities to DTO entities</param>
 /// <param name="rentUnit">Rent unit of work</param>
 /// <param name="identityUnit">Udentity unit of work</param>
 /// <param name="identityMapper">Mapper for converting identity entities to BLL classes</param>
 /// <param name="logService">Service for logging</param>
 public Service(IRentMapperDTO mapperDTO, IRentUnitOfWork rentUnit,
                IIdentityUnitOfWork identityUnit, IIdentityMapperDTO identityMapper, ILogService logService)
 {
     RentMapperDTO      = mapperDTO;
     RentUnitOfWork     = rentUnit;
     IdentityMapperDTO  = identityMapper;
     IdentityUnitOfWork = identityUnit;
     LogService         = logService;
 }
Ejemplo n.º 14
0
        /// <summary>
        /// Dependency Injection to database repositories.
        /// </summary>
        /// <param name="unitOfWork"> Point to context of dataBase </param>
        public UserService(IIdentityUnitOfWork unitOfWork)
        {
            Database = unitOfWork;

            // Using Factory Method.
            MapperCreator  creator       = new IdentityCreator();
            IWrappedMapper wrappedMapper = creator.FactoryMethod();

            mapper = wrappedMapper.CreateMapping();
        }
Ejemplo n.º 15
0
        public async Task GetUserByIdAsyncMethod_NameNotExist_ShouldBeThrownUserNotFoundException()
        {
            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            mock.Setup(x => x.Users.FindByNameAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null);


            await service.GetUserByIdAsync(It.IsAny <string>());
        }
Ejemplo n.º 16
0
        public TokenService(IIdentityUnitOfWork unitOfWork, IConfiguration configuration)
        {
            Database      = unitOfWork;
            Configuration = configuration;

            // Using Factory Method.
            MapperCreator  creator       = new IdentityCreator();
            IWrappedMapper wrappedMapper = creator.FactoryMethod();

            mapper = wrappedMapper.CreateMapping();
        }
Ejemplo n.º 17
0
 public AuthorizationApp(
     IIdentityUnitOfWork unitOfWork,
     IUserRepository userRepository,
     IRoleRepository roleRepository,
     IPermissionRepository permissionRepository)
 {
     _unitOfWork           = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
     _userRepository       = userRepository ?? throw new ArgumentNullException(nameof(userRepository));
     _roleRepository       = roleRepository ?? throw new ArgumentNullException(nameof(roleRepository));
     _permissionRepository = permissionRepository ?? throw new ArgumentNullException(nameof(permissionRepository));
 }
Ejemplo n.º 18
0
        public UserService(IIdentityUnitOfWork identityUnitOfWork, IUnitOfWork unitOfWork)
        {
            _identityUnitOfWork = identityUnitOfWork ?? throw new ArgumentNullException(nameof(identityUnitOfWork));
            _unitOfWork         = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));

            _mapper = new Mapper(new MapperConfiguration(cfg => {
                cfg.CreateMap <ApplicationUser, UserDto>().
                ForMember(x => x.RolesId, opt => opt.MapFrom(x => x.Roles.Select(u => u.RoleId)));

                cfg.CreateMap <ApplicationRole, RoleDto>().
                ForMember(x => x.Name, opt => opt.MapFrom(x => x.Name));
            }));
        }
Ejemplo n.º 19
0
        public PhotoService(IUnitOfWork unitOfWork, IIdentityUnitOfWork identityUnitOfWork)
        {
            _unitOfWork         = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
            _identityUnitOfWork = identityUnitOfWork ?? throw new ArgumentNullException(nameof(identityUnitOfWork));

            _mapper = new Mapper(new MapperConfiguration(cfg => {
                cfg.CreateMap <Photo, PhotoDto>()
                .ForMember(x => x.ClientProfileDtoId, opt => opt.MapFrom(x => x.ClientProfileId))
                .ForMember(x => x.Data, opt => opt.MapFrom(x => Convert.ToBase64String(x.Data)));
                cfg.CreateMap <Like, LikeDto>()
                .ForMember(x => x.PhotoDtoId, opt => opt.MapFrom(x => x.PhotoId));
            }));
        }
Ejemplo n.º 20
0
        public LotService(IAuctionUnitOfWork database, IIdentityUnitOfWork identityDb)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database");
            }

            if (identityDb == null)
            {
                throw new ArgumentNullException("identityDb");
            }
            IdentityDb = identityDb;
            Database   = database;
        }
Ejemplo n.º 21
0
        private static void PersistenceIdentityDemo()
        {
            Console.WriteLine("\nPersistence Identity Demo\n");

            IIdentityUnitOfWork unitOfWork = DIHelper.DIManager.GetService <IIdentityUnitOfWork>();

            Console.WriteLine(unitOfWork.GetType().FullName + " with " + unitOfWork.DBMS.ToString() + "\n");

            PersistenceIdentityData <Role>(unitOfWork);
            PersistenceIdentityData <UserClaim>(unitOfWork);
            PersistenceIdentityData <UserLogin>(unitOfWork);
            PersistenceIdentityData <UserRole>(unitOfWork);
            PersistenceIdentityData <User>(unitOfWork);
        }
Ejemplo n.º 22
0
        public async Task UpdateUserRoleAsyncMethod_UserIdNotExists_ShouldBeReturnedFailedIdentityOperation()
        {
            string userId = "1a", rolename = "Worker";

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            var expected = new IdentityOperation(false, "", "");

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync((ApplicationUser)null);


            var actual = await service.UpdateUserRoleAsync(userId, rolename);


            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 23
0
        public async Task UpdateUserRoleAsyncMethod_WithoutChanges_ShouldBeReturnedSuccessfulIdentityOperation()
        {
            string         userId = "1a", rolename = "Worker";
            IList <string> roles = new List <string>();

            roles.Add("Worker");

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            var expected = new IdentityOperation(true, "", "");

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());
            mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles);


            var actual = await service.UpdateUserRoleAsync(userId, rolename);


            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 24
0
        public MappingIdentityProfile(IIdentityUnitOfWork uow)
        {
            Config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <UserProfile, UserBLL>()
                .ForMember(dto => dto.Email, m => m.MapFrom(cp => cp.ApplicationUser.Email))
                .ForMember(dto => dto.Password, m => m.MapFrom(cp => cp.ApplicationUser.PasswordHash))
                .ForMember(dto => dto.UserName, m => m.MapFrom(cp => cp.ApplicationUser.UserName))
                .ForMember(dto => dto.Birthday, m => m.MapFrom(cp => cp.Birthday.Date))

                //.ForMember(dto => dto.Avatar, m => m.MapFrom(cp => uow.UserRepository
                //.Find(p=>p.Photos.Where(p0 => p0.IsAvatar == true)
                //.Select(p1=>p1.PhotoAddress)
                //.FirstOrDefault())))
                .ForMember(dto => dto.Role,
                           m =>
                           m.MapFrom(
                               cp =>
                               uow.RoleManager.FindById(
                                   cp.ApplicationUser.Roles.First(p2 => p2.UserId == cp.Id).RoleId).Name));
            });
        }
Ejemplo n.º 25
0
        public async Task GetUserByIdAsyncMethod_OnlyAdminExists_ShouldBeReturnedUser()
        {
            var expected = new UserDTO {
                Role = "Admin"
            };

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());
            mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(new List <string>()
            {
                "Admin"
            });
            mock.Setup(x => x.People.GetSingleAsync(It.IsAny <Expression <Func <Person, bool> > >())).ReturnsAsync((Person)null);


            var actual = await service.GetUserByIdAsync(It.IsAny <string>());


            Assert.AreSame(expected.Role, actual.Role);
        }
Ejemplo n.º 26
0
        public async Task UpdateUserRoleAsyncMethod_FromAdmin_ShouldBeReturnedFailedIdentityOperation()
        {
            string         userId = "1a", rolename = "Manager";
            IList <string> roles = new List <string>();

            roles.Add("Admin");

            Mock <IIdentityUnitOfWork> mock = new Mock <IIdentityUnitOfWork>();
            IIdentityUnitOfWork        uow  = mock.Object;
            UserService service             = new UserService(uow);

            var expected = new IdentityOperation(false, "Only the one administrator have to be in database", "userRole");

            mock.Setup(x => x.Users.FindByIdAsync(It.IsAny <string>())).ReturnsAsync(new ApplicationUser());
            mock.Setup(x => x.Users.GetRolesAsync(It.IsAny <ApplicationUser>())).ReturnsAsync(roles);


            var actual = await service.UpdateUserRoleAsync(userId, rolename);


            Assert.AreSame(expected.Message, actual.Message);
            Assert.AreSame(expected.Property, actual.Property);
            Assert.AreEqual(expected.Succeeded, actual.Succeeded);
        }
Ejemplo n.º 27
0
 public EmployeeCreatedHandler(IIdentityUnitOfWork unitOfWork)
 {
     this.unitOfWork = unitOfWork;
 }
Ejemplo n.º 28
0
 public IdentityUserRepository(IIdentityUnitOfWork unitOfWork)
     : base(unitOfWork)
 {
 }
 /// <summary>Starts unit of work.</summary>
 /// <param name="other">    The other. </param>
 /// <returns>An IIdentityUnitOfWork.</returns>
 public IIdentityUnitOfWork StartUnitOfWork(IIdentityUnitOfWork other)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 30
0
 public UserService(IIdentityUnitOfWork uow)
 {
     database = uow;
 }