Inheritance: IUserProfileValidation
Ejemplo n.º 1
0
        public ActionResult RegisterUser(FacebookRegisterModel model, string returnUrl)
        {
            // TODO: Rebuild functionality using CoffeeScript implementation and new C# API
            if (ModelState.IsValid)
            {
                var userProfile = Mapper.Map<FacebookRegisterModel, UserProfile>(model);

                using (new UnitOfWorkScope())
                {
                    var userProfileService = new UserProfileService(userProfileRepository);
                    var organization = OrganizationRepository.GetDefaultOrganization(readOnly: false);

                    if (!userProfileService.IsFacebookAccountUnique(userProfile.FacebookID))
                    {
                        TempData["ModelErrors"] = new List<string> { "This FacebookID is already in use by another user account. Please sign in with a different Facebook account." };
                        return RedirectToAction("Register", new { returnUrl = returnUrl });
                    }

                    if (organization.UserProfiles == null)
                    {
                        organization.UserProfiles = new List<UserProfile>();
                    }

                    userProfile.Active = true;
                    userProfile.IsActivated = false;
                    var service = new GrassrootsMembershipService();
                    userProfile.ActivationHash = service.GetUserAuthorizationHash();
                    userProfile.LastActivationAttempt = DateTime.Now;
                    organization.UserProfiles.Add(userProfile);
                    OrganizationRepository.Save();
                    accountMailer.Authorize(new AuthorizeModel
                                                {
                                                    Email = userProfile.Email,
                                                    FirstName = userProfile.FirstName,
                                                    LastName = userProfile.LastName,
                                                    SenderEmail = organization.ContactEmail,
                                                    SenderName = organization.Name,
                                                    Url = Url.ToPublicUrl(Url.Action("Activate", "Account", new { hash = userProfile.ActivationHash }))
                                                }).SendAsync();

                    return RedirectToAction("AwaitingActivation", "Account");
                }
            }

            return RedirectToAction("Register");
        }
Ejemplo n.º 2
0
        public ActionResult RegisterUser(FacebookRegisterModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var userProfile = Mapper.Map<FacebookRegisterModel, UserProfile>(model);

                using (new UnitOfWorkScope())
                {
                    var organization = OrganizationRepository.GetDefaultOrganization(readOnly: false);

                    if (organization.UserProfiles == null)
                    {
                        organization.UserProfiles = new List<UserProfile>();
                    }

                    userProfile.Active = true;
                    userProfile.IsActivated = false;
                    var service = new GrassrootsMembershipService();
                    userProfile.ActivationHash = service.GetUserAuthorizationHash();
                    userProfile.LastActivationAttempt = DateTime.Now;
                    organization.UserProfiles.Add(userProfile);
                    OrganizationRepository.Save();
                    accountMailer.Authorize(new AuthorizeModel
                                                {
                                                    Email = userProfile.Email,
                                                    FirstName = userProfile.FirstName,
                                                    LastName = userProfile.LastName,
                                                    SenderEmail = organization.ContactEmail,
                                                    SenderName = organization.Name,
                                                    Url = Url.ToPublicUrl(Url.Action("Activate", "Account", new { hash = userProfile.ActivationHash }))
                                                }).SendAsync();

                    return RedirectToAction("AwaitingActivation", "Account");
                }
            }

            return RedirectToAction("Register");
        }
Ejemplo n.º 3
0
        private static FacebookRegisterModel MapFacebookUser(dynamic me)
        {
            var viewModel = new FacebookRegisterModel
                                {
                                    FirstName = me.first_name,
                                    LastName = me.last_name,
                                    FacebookID = me.id,
                                    Gender = me.gender,
                                    Email = me.email,
                                    Birthdate = DateTime.Parse(me.birthday)
                                };

            // If the user's FB location is not set, we can't do anything.
            if ( me.location != null )
            {
                string location = me.location.name;
                var locArray = location.Split( new[] { ',' } );
                viewModel.City = locArray[0].Trim();

                var pair = UIHelpers.StateDictionary.FirstOrDefault( s => s.Key.ToLower() == locArray[1].Trim().ToLower() );
                viewModel.State = pair.Value;
            }
            return viewModel;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Attempts to populate registration ViewModel with data from Facebook and present to user for verification.
        /// </summary>
        /// <param name="code">Code sent to Facebook to obtain access token</param>
        /// <param name="state">Redirect Url</param>
        /// <returns>Registration view pre-populated w/ data from Facebook</returns>
        public ActionResult NewAccount(string code, string state)
        {
            FacebookOAuthResult oAuthResult;
            var viewModel = new FacebookRegisterModel();

            if (FacebookOAuthResult.TryParse(Request.Url, out oAuthResult))
            {
                if (oAuthResult.IsSuccess)
                {
                    var oAuthClient = new FacebookOAuthClient(FacebookApplication.Current) { RedirectUri = new Uri(GetNewAccountUrl()) };
                    dynamic tokenResult = oAuthClient.ExchangeCodeForAccessToken(code);
                    string accessToken = tokenResult.access_token;

                    FacebookClient fbClient = new FacebookClient(accessToken);
                    dynamic me = fbClient.Get("me");
                    viewModel = MapFacebookUser(me);
                }
            }

            return View("FacebookRegister", viewModel);
        }