public ActionResult CreateUserByFb(string data)
        {
            JsonViewData viewData = Validate <JsonViewData>(
                new Validation(() => string.IsNullOrEmpty(data.NullSafe()), "Nie udało nam się uzyskać Twoich danych z serwisu Facebook, spróbuj ponownie.")
                );


            if (viewData == null)
            {
                try
                {
                    var fbUserViewData = new JavaScriptSerializer().Deserialize <FbUserDataView>(data);
                    fbUserViewData = AssignViewData <FbUserDataView>(fbUserViewData);

                    using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                    {
                        IUser user = _userRepository.FindByFbId(fbUserViewData.Id);

                        if (user == null)
                        {
                            var email = fbUserViewData.Email;
                            if (string.IsNullOrEmpty(email))
                            {
                                email = fbUserViewData.Id + "@facebook.dotnetomaniak.pl";
                            }
                            user = _factory.CreateUser(fbUserViewData.Name, email, null);

                            user.FbId = fbUserViewData.Id;
                            unitOfWork.Commit();

                            user.FbId           = fbUserViewData.Id;
                            user.LastActivityAt = SystemTime.Now();
                            UserRepository.Add(user);

                            _eventAggregator.GetEvent <UserActivateEvent>().Publish(new UserActivateEventArgs(user));

                            unitOfWork.Commit();
                            viewData = LogUserByFb(viewData, unitOfWork, user);
                        }
                        else
                        {
                            return(Json(new { isSuccessful = false, errorMessage = "Użytkownik o podanym koncie Facebook'a istnieje już na dotnetomaniak.pl." }));
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Exception(e);
                    viewData = new JsonViewData {
                        errorMessage = FormatStrings.UnknownError.FormatWith("logowania")
                    };
                }
            }

            return(Json(new { isSuccessful = true, redirectUrl = Url.Action("Published", "Story", null, "http") }));
        }
        public void FindPromotedByUser_Should_Return_Correct_Result()
        {
            var story1 = CreateStory();
            var story2 = CreateStory();
            var story3 = CreateStory();

            Stories.AddRange(new[] { story1 as Story, story2 as Story, story3 as Story });

            var user = _factory.CreateUser("promoter", "*****@*****.**", "xxxxxx");

            Stories.ForEach(s => Votes.Add(new StoryVote {
                User = (User)user, Story = s, IPAddress = "192.168.0.1", Timestamp = SystemTime.Now().AddDays(-3)
            }));

            var pagedResult = _storyRepository.FindPromotedByUser(user.Id, 0, 10);

            Assert.Equal(3, pagedResult.Result.Count);
            Assert.Equal(3, pagedResult.Total);
        }
Beispiel #3
0
        private void CreateUserIfNotExists(DefaultUser defaultUser)
        {
            if (_userRepository.FindByUserName(defaultUser.UserName) == null)
            {
                IUser user = _factory.CreateUser(defaultUser.UserName, defaultUser.Email, defaultUser.Password);

                user.Role     = defaultUser.Role;
                user.IsActive = true;

                _userRepository.Add(user);
            }
        }
        public ActionResult OpenId(string identifier, bool?rememberMe)
        {
            string errorMessage = null;

            string url = string.Concat(Settings.RootUrl, Url.Content("~/xrds.axd"));

            HttpContext.Response.Headers.Add("X-XRDS-Location", url);

            try
            {
                if (OpenIdRelyingParty.Response == null)
                {
                    if (!string.IsNullOrEmpty(identifier))
                    {
                        Identifier id;

                        if (Identifier.TryParse(identifier, out id))
                        {
                            var realm = new Realm(new Uri(string.Concat(Settings.RootUrl, Url.RouteUrl("OpenId"))));

                            IAuthenticationRequest request = OpenIdRelyingParty.CreateRequest(identifier, realm);

                            var fetch = new ClaimsRequest
                            {
                                Email = DemandLevel.Require
                            };

                            request.AddExtension(fetch);

                            OpenIdRememberMe = rememberMe ?? false;
                            ReturnUrl        = (HttpContext.Request.UrlReferrer != null) ? HttpContext.Request.UrlReferrer.ToString() : string.Empty;

                            return(request.RedirectingResponse.AsActionResult()); //.RedirectToProvider();
                        }
                    }

                    return(new EmptyResult());
                }

                if (OpenIdRelyingParty.Response.Status == AuthenticationStatus.Authenticated)
                {
                    string userName = OpenIdRelyingParty.Response.ClaimedIdentifier;

                    using (IUnitOfWork unitOfWork = UnitOfWork.Begin())
                    {
                        IUser user = UserRepository.FindByUserName(userName);

                        if ((user != null) && user.IsLockedOut)
                        {
                            errorMessage = "Twoje konto jest obecnie zablokowane. Skontaktuj się z nami w tej sprawie.";
                        }
                        else
                        {
                            var fetch = OpenIdRelyingParty.Response.GetExtension <ClaimsResponse>();

                            // Some of the Provider does not return Email
                            // Such as Yahoo, Blogger, Bloglines etc, in that case we will assign a default
                            // email
                            string email = ((fetch == null) || string.IsNullOrEmpty(fetch.Email)) ? Settings.DefaultEmailOfOpenIdUser : fetch.Email;

                            if (user == null)
                            {
                                user = _factory.CreateUser(userName, email, null);
                                UserRepository.Add(user);
                                _eventAggregator.GetEvent <UserActivateEvent>().Publish(new UserActivateEventArgs(user));
                            }
                            else
                            {
                                //Sync the email from OpenID provider.
                                if ((string.Compare(email, user.Email, StringComparison.OrdinalIgnoreCase) != 0) &&
                                    (string.Compare(email, Settings.DefaultEmailOfOpenIdUser, StringComparison.OrdinalIgnoreCase) != 0))
                                {
                                    user.ChangeEmail(email);
                                }
                            }

                            user.LastActivityAt = SystemTime.Now();

                            unitOfWork.Commit();
                            FormsAuthentication.SetAuthCookie(userName, OpenIdRememberMe);
                        }
                    }
                }
                else if ((OpenIdRelyingParty.Response.Status == AuthenticationStatus.Failed) || (OpenIdRelyingParty.Response.Status == AuthenticationStatus.Canceled))
                {
                    if (OpenIdRelyingParty.Response.Exception != null)
                    {
                        errorMessage = OpenIdRelyingParty.Response.Exception.Message;
                    }
                    if (String.IsNullOrEmpty(errorMessage))
                    {
                        errorMessage = "Nie udało się zalogować przez wybranego dostawcę OpenID.";
                    }
                }
            }
            catch (Exception oid)
            {
                errorMessage = oid.Message;
            }

            if (!string.IsNullOrEmpty(errorMessage))
            {
                GenerateMessageCookie(errorMessage, true);
            }

            string returnUrl = ReturnUrl;

            if (!string.IsNullOrEmpty(returnUrl))
            {
                return(Redirect(returnUrl));
            }

            return(RedirectToRoute("Published"));
        }
Beispiel #5
0
        public void Add_Should_Use_Database()
        {
            database.Expect(d => d.Insert(It.IsAny <User>())).Verifiable();

            _userRepository.Add(_factory.CreateUser("dummy", "*****@*****.**", "xxxx"));
        }