public bool Register(UserDTO user)
        {
            var userExist = _uow.Users.SingleOrDefault(p => p.Email == user.Email);

            if (userExist == null)
            {
                var entity = user.Map();
                entity.Password = entity.Password.getSHA1();
                _uow.Users.Add(entity);
                _uow.Save();

                var permission = _uow.Permissions.Single(p => p.Value == (int)PermissionType.User && p.Deleted == false);
                UserPermission userPermission = new UserPermission
                {
                    IdUser = entity.Id,
                    IdPermission = permission.Id,
                    LastModifyDate = DateTime.Now,
                };
                _uow.UserPermissions.Add(userPermission);
                _uow.Save();

                return false;
            }
            else
            {
                return true;
            }
        }
Esempio n. 2
0
        public static UserDTO Map(this User source)
        {
            UserDTO target = new UserDTO();
            target.Id = source.Id;
            target.FirstName = source.FirstName;
            target.LastName = source.LastName;
            target.Password = source.Password;
            target.Email = source.Email;

            return target;
        }
        public void NinjectFirstMethod()
        {
            var uow = IoCNinjectProvider.Instance.Get<IUnitOfWork>();

            var user = new UserDTO
            {
                Email = "*****@*****.**",
                FirstName = "darek4",
                LastName = "darek4",
                Password = "******"
            };

            user.Password = user.Password.getSHA1();
            var entity = user.Map();
            uow.Users.Add(entity);
            uow.Save();

            Assert.IsNotNull(entity.Id);
        }
Esempio n. 4
0
        public UserDTO UpdateUser(UserDTO userDTO)
        {
            var entity = _uow.Users.SingleOrDefault(p => p.Id == userDTO.Id);

            if (entity != null)
            {
                if (!_uow.Users.Any(p => p.Email == userDTO.Email))
                {
                    entity.Email = userDTO.Email;
                }

                entity.LastName = userDTO.LastName;
                entity.FirstName = userDTO.FirstName;
                _uow.Save();

                return entity.Map();
            }

            return null;
        }
        public void TestMapping()
        {
            //var test = context.Users.FirstOrDefault(p => p.Login.Contains("test"));
            using (Hobby.SimpleInjector.SimpleInjectorConsole.Instance.BeginLifetimeScope())
            {
                var uow = IoCCProvider.Container.GetInstance<IUnitOfWork>();

                var user = new UserDTO
                {
                    Email = "*****@*****.**",
                    Password = "******"
                };

                uow.Users.Add(user.Map());
                uow.Save();

                var test = uow.Users.Single(p => p.Id == 1);

                Assert.IsNull(user.Id);
            }
        }
        public void UserService_CheckUser()
        {
            //Arrange
            var userDTO = new UserDTO
            {
                CreateDate = DateTime.Now,
                Email = "*****@*****.**",
                Id = 1,
                FirstName = "test",
                LastName = "test",
                Password = "******".getSHA1()
            };

            Mock<IAuthenticateService> mockService = new Mock<IAuthenticateService>();
            mockService.Setup(_test => _test.CheckUser("test", "2")).Returns(userDTO);

            //Act
            var result = mockService.Object.CheckUser("test", "2").Id;

            //Arrange
            Assert.AreEqual(1, result);
        }
        public JsonResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    UserDTO entity = new UserDTO()
                    {
                        Email = model.Email,
                        FirstName = model.FirstName,
                        LastName = model.LastName,
                        Password = model.Password
                    };
                    bool userExist = _authService.Register(entity);

                    return Json(new
                    {
                        userExist = userExist,
                        userModel = model
                    });
                }
                catch (Exception ex)
                {
                    logger.LogException(LogLevel.Error, "Error to register a new user", ex);

                    return Json("Error save");
                }
            }

            return Json(new { });
        }
        public void TestUserPermissions()
        {
            using (Hobby.SimpleInjector.SimpleInjectorConsole.Instance.BeginLifetimeScope())
            {
                var uow = IoCCProvider.Container.GetInstance<IUnitOfWork>();

                var categorie = new CategoryDTO
                {
                    Name = "testCategorie1"
                };

                var permissions = new PermissionDTO
                {
                    Name = "testPermission1",
                    Deleted = false,
                    Description = "TEST"
                };

                var user = new UserDTO
                {
                    FirstName = "testlogin1",
                    Password = "******"
                };
                //Trzeba przypsiac do obiektu
                var entityPer = permissions.Map();
                var entityUse = user.Map();

                uow.Permissions.Add(entityPer);
                uow.Users.Add(entityUse);
                uow.Save();

                var userPermission = new UserPermissionDTO()
                {
                    IdPermission = entityPer.Id,
                    IdUser = entityUse.Id
                };
                uow.UserPermissions.Add(userPermission.Map());
                uow.Save();
            }
        }
        public JsonResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                UserDTO entity = new UserDTO()
                {
                    Email = model.Email,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Password = model.Password
                };

                try
                {
                    _authService.Register(entity);
                }
                catch (Exception)
                {
                    logger.Error("Error to register a new user");
                    return Json("Error save");
                }

                return Json(model);
            }

            return Json(new { });
        }