Esempio n. 1
0
        public string LoginWithFacebook(string code, string state)
        {
#if DEBUG
            GlobalProxySelection.Select = new WebProxy("127.0.0.1:1080");
#endif
            var    facebookLoginRedirectUrl      = WebConfigurationManager.AppSettings["FacebookLoginRedirectUrl"];
            var    facebookLoginAppSecret        = WebConfigurationManager.AppSettings["FacebookLoginAppSecret"];
            var    facebookLoginAppId            = WebConfigurationManager.AppSettings["FacebookLoginAppId"];
            var    facebookGetAccountDetailUrl   = WebConfigurationManager.AppSettings["FacebookGetAccountDetailUrl"];
            var    facebookGetAccessTokenAddress = WebConfigurationManager.AppSettings["FacebookGetAccessTokenAddress"];
            var    webPath = WebConfigurationManager.AppSettings["WebPath"];
            string authorization;


            using (var client = new HttpClient())
            {
                var getTokenUrl = $"{facebookGetAccessTokenAddress}client_id={facebookLoginAppId}&" +
                                  $"client_secret={facebookLoginAppSecret}&redirect_uri={facebookLoginRedirectUrl}&code={code}";
                var str     = client.GetAsync(getTokenUrl).Result;
                var content = str.Content.ReadAsStringAsync().Result;

                var result = JsonConvert.DeserializeObject <FacebookAuthResult>(content);

                if (result.AccessToken.IsNullOrEmpty())
                {
                    throw new PlanPokerException("Get access token failed - " + content);
                }

                using (var profileClient = GetFacebookHttpClient(result.AccessToken))
                {
                    var facebookUserInforequest = string.Format(facebookGetAccountDetailUrl, result.AccessToken);
                    var httpResponse            = profileClient
                                                  .GetAsync(facebookUserInforequest).Result;
                    var    profileContent = httpResponse.Content.ReadAsStringAsync().Result;
                    var    profile        = JsonConvert.DeserializeObject <dynamic>(profileContent);
                    string email          = profile.email.ToString();
                    var    user           = _userRepository.Query().FirstOrDefault(x => x.Email == email);
                    if (user != null)
                    {
                        authorization = Login(user.Email, TokenGenerator.DecodeToken(user.Password));
                    }
                    else
                    {
                        var userLogicModel = new UserLogicModel
                        {
                            Email           = profile.email.ToString(),
                            Name            = profile.first_name + " " + profile.last_name,
                            Password        = "******",
                            ComfirmPassword = "******",
                            ExpiredTime     = DateTime.Now,
                            ImagePath       = profile.picture.data.url.ToString()
                        };
                        Create(userLogicModel);
                        authorization = Login(userLogicModel.Email, userLogicModel.Password);
                    }
                }
            }

            return(webPath + "#/login?authorization=" + authorization);
        }
Esempio n. 2
0
        public void Create(UserLogicModel user, ProjectLogicModel model)
        {
            var projectModel = model.ToModel();

            projectModel.Owner = user.ToModel();
            using (var unitOfwork = _unitOfWorkFactory.GetCurrentUnitOfWork())
            {
                _projectRepository.Save(projectModel);
                unitOfwork.Commit();
            }
        }
Esempio n. 3
0
        public void Edit(UserLogicModel model)
        {
            var userModel = _userRepository.GetForUpdate(model.Id);

            using (var unitOfwork = _unitOfWorkFactory.GetCurrentUnitOfWork())
            {
                userModel.Name        = model.Name;
                userModel.ImagePath   = model.ImagePath;
                userModel.ExpiredTime = DateTime.Now;
                unitOfwork.Commit();
            }
        }
Esempio n. 4
0
 public static UserViewModel ToViewModel(this UserLogicModel user)
 {
     return(user == null ? null : new UserViewModel
     {
         Id = user.Id,
         Name = user.Name,
         Password = user.Password,
         ComfirmPassword = user.ComfirmPassword,
         Email = user.Email,
         ImagePath = user.ImagePath,
         ResetPasswordToken = user.ResetPasswordToken,
         ExpiredTime = user.ExpiredTime,
         Projects = user.Projects?.Select(x => x.ToViewModel())
     });
 }
Esempio n. 5
0
        public void Check_token_will_check_email_and_password_is_pair()
        {
            //Arrange
            var userLogicModel = new UserLogicModel
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var userLogic = new UserLogic(_iuserRepositoryMock.Object, _unitOfWorkFactory.Object);

            //Act
            userLogic.CheckToken(userLogicModel.Email, userLogicModel.Password);
            //Assert
            _iuserRepositoryMock.Verify(x => x.Query(), Times.Once);
        }
Esempio n. 6
0
        public void Create_user_when_email_does_not_exist_check_email_should_return_false()
        {
            //Arrange
            var userLogicModel = new UserLogicModel
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var userLogic = new UserLogic(_iuserRepositoryMock.Object, _unitOfWorkFactory.Object);

            _unitOfWorkFactory.Setup(x => x.GetCurrentUnitOfWork()).Returns(_uniteOfWorkMock.Object);
            //Act
            userLogic.Create(userLogicModel);
            //Assert
        }
Esempio n. 7
0
        public void Login_will_return_token_if_email_and_password_are_pair()
        {
            //Arrange
            var userLogic      = new UserLogic(_iuserRepositoryMock.Object, _unitOfWorkFactory.Object);
            var userLogicModel = new UserLogicModel
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            //Act
            var result = userLogic.Login(userLogicModel.Email, userLogicModel.Password);

            //Assert
            _iuserRepositoryMock.Verify(x => x.Query(), Times.Once);
        }
Esempio n. 8
0
        public void EditPassword(UserLogicModel model)
        {
            var userModel = _userRepository.GetForUpdate(model.Id);

            if (model.ComfirmPassword != null && model.Password != model.ComfirmPassword)
            {
                throw new PlanPokerException("Confirm is not match with password.");
            }

            using (var unitOfwork = _unitOfWorkFactory.GetCurrentUnitOfWork())
            {
                userModel.Password    = TokenGenerator.EncodeToken(model.Password);
                userModel.ExpiredTime = DateTime.Now;
                unitOfwork.Commit();
            }
        }
Esempio n. 9
0
        public void Create_will_create_project_by_call_Create_method_in_project_logic()
        {
            //Arrange
            var projectLogicModel = new ProjectLogicModel
            {
                Name = "project1"
            };
            var userModel = new UserLogicModel
            {
                Id    = 2,
                Email = "test",
                Name  = "joy"
            };

            //Act
            _projectLogic.Create(userModel, projectLogicModel);
            //Assert
            _projectRepository.Verify(x => x.Save(It.IsAny <ProjectModel>()), Times.Once);
        }
Esempio n. 10
0
        public void Edit_will_edit_user_name_and_image()
        {
            //Arrange
            var userLogic      = new UserLogic(_iuserRepositoryMock.Object, _unitOfWorkFactory.Object);
            var userLogicModel = new UserLogicModel
            {
                Id        = 1,
                Email     = "*****@*****.**",
                Password  = "******",
                Name      = "Joy1",
                ImagePath = "Location"
            };

            //Act
            _iuserRepositoryMock.Setup(x => x.GetForUpdate(userLogicModel.Id)).Returns(userLogicModel.ToModel);
            _unitOfWorkFactory.Setup(x => x.GetCurrentUnitOfWork()).Returns(_uniteOfWorkMock.Object);
            userLogic.Edit(userLogicModel);
            //Assert
            _iuserRepositoryMock.Verify(x => x.GetForUpdate(It.IsAny <int>()), Times.Once);
        }
Esempio n. 11
0
        public void Create(UserLogicModel model)
        {
            var userModel = model.ToModel();

            if (_userRepository.Query().Any(x => x.Email == model.Email))
            {
                throw new PlanPokerException("The email is already exist.");
            }

            if (model.Password != model.ComfirmPassword)
            {
                throw new PlanPokerException("Confirm is not match with password.");
            }

            userModel.Password    = TokenGenerator.EncodeToken(model.Password);
            userModel.ExpiredTime = DateTime.Now;
            using (var unitOfwork = _unitOfWorkFactory.GetCurrentUnitOfWork())
            {
                _userRepository.Save(userModel);
                unitOfwork.Commit();
            }
        }
Esempio n. 12
0
        public void Check_email_when_email_does_not_exist_should_return_false()
        {
            //Arrange
            const string email          = "*****@*****.**";
            var          users          = new List <UserModel>();
            var          userLogicModel = new UserLogicModel
            {
                Email    = "*****@*****.**",
                Password = "******"
            };
            var userLogic = new UserLogic(_iuserRepositoryMock.Object, _unitOfWorkFactory.Object);
            var user      = userLogicModel.ToModel();

            users.Add(user);
            _unitOfWorkFactory.Setup(x => x.GetCurrentUnitOfWork()).Returns(_uniteOfWorkMock.Object);
            _iuserRepositoryMock.Setup(x => x.Query()).Returns(users.AsQueryable());
            //Act
            var result = userLogic.CheckEmailExist(email);

            //Assert
            Assert.IsFalse(result);
        }
Esempio n. 13
0
        public string LoginWithLinkedIn(string code, string state)
        {
#if DEBUG
            GlobalProxySelection.Select = GlobalProxySelection.GetEmptyWebProxy();
#endif
            var    linkedInLoginRedirectUrl      = WebConfigurationManager.AppSettings["LinkedInLoginRedirectUrl"];
            var    linkedInLoginAppSecret        = WebConfigurationManager.AppSettings["LinkedInLoginAppSecret"];
            var    linkedInLoginAppId            = WebConfigurationManager.AppSettings["LinkedInLoginAppId"];
            var    linkedInGetAccountDetailUrl   = WebConfigurationManager.AppSettings["LinkedInGetAccountDetailUrl"];
            var    linkedInGetAccessTokenAddress = WebConfigurationManager.AppSettings["LinkedInGetAccessTokenAddress"];
            var    webPath = WebConfigurationManager.AppSettings["WebPath"];
            string authorization;

            using (var client = new HttpClient())
            {
                var values = new Dictionary <string, string>
                {
                    { "grant_type", "authorization_code" },
                    { "code", code },
                    { "redirect_uri", linkedInLoginRedirectUrl },
                    { "client_id", linkedInLoginAppId },
                    { "client_secret", linkedInLoginAppSecret }
                };

                var body    = new FormUrlEncodedContent(values);
                var str     = client.PostAsync(linkedInGetAccessTokenAddress, body).Result;
                var content = str.Content.ReadAsStringAsync().Result;

                var result = JsonConvert.DeserializeObject <LinkedInAuthResult>(content);

                if (result.AccessToken.IsNullOrEmpty())
                {
                    throw new PlanPokerException("Get access token failed - " + content);
                }

                using (var profileClient = GetLinkedInHttpClient(result.AccessToken))
                {
                    var httpResponse = profileClient
                                       .GetAsync(linkedInGetAccountDetailUrl).Result;
                    var profileContent = httpResponse.Content.ReadAsStringAsync().Result;
                    var profile        = JsonConvert.DeserializeObject <LinkedInLogicModel>(profileContent);
                    var user           = _userRepository.Query().FirstOrDefault(x => x.Email == profile.Email);
                    if (user != null)
                    {
                        authorization = Login(user.Email, TokenGenerator.DecodeToken(user.Password));
                    }
                    else
                    {
                        var userLogicModel = new UserLogicModel
                        {
                            Email           = profile.Email,
                            Name            = profile.FirstName + " " + profile.LastName,
                            Password        = "******",
                            ComfirmPassword = "******",
                            ExpiredTime     = DateTime.Now,
                            ImagePath       = profile.Picture
                        };
                        Create(userLogicModel);
                        authorization = Login(userLogicModel.Email, userLogicModel.Password);
                    }
                }
            }

            return(webPath + "#/login?authorization=" + authorization);
        }
 public ChatMessageRoomModel(UserLogicModel user, string content, DateTime time)
 {
     User = user;
     Content = content;
     Time = time;
 }
 public DebugJoinGameRequestModel(string roomId, UserLogicModel user)
 {
     RoomID = roomId;
     User = user;
 }