public static FacebookConnectionViewModel Populate(int userId, IUnitOfWork work = null)
        {
            if (work == null)
            {
                work = new UnitOfWork();
            }

            facebook_connection fbConnection = work.EntityContext.facebook_connection.Find(userId);

            if (fbConnection == null)
            {
                fbConnection = new facebook_connection()
                {
                    notifications_enabled = false, automatic_sharing_enabled = false
                };
            }

            FacebookConnectionViewModel model = new FacebookConnectionViewModel()
            {
                NotificationsEnabled    = fbConnection.notifications_enabled,
                AutomaticSharingEnabled = fbConnection.automatic_sharing_enabled,
            };

            return(model);
        }
        public ActionResult ConnectFacebook(FacebookConnectionViewModel model)
        {
            // Save user settings to db
            using (UnitOfWork work = new UnitOfWork())
            {
                user currentUser = work.UserRepository.GetUser(WebSecurity.CurrentUserId);
                work.UserRepository.AddOrUpdateFacebookSettings(currentUser, model.NotificationsEnabled, model.AutomaticSharingEnabled);
                work.SaveChanges();
            }

            // Redirect to Facebook to ask for permissions
            string redirectAfterLoginUri = JppUriInfo.GetCurrentDomain(Request) + Url.RouteUrl("Default", new { Controller = "Settings", Action = "ProcessFacebookLogin" });
            string scope = string.Empty; // NOTE: No change in scope needed for notifications; apps don't need to ask permission
            if (model.AutomaticSharingEnabled)
            {
                scope += "publish_actions,";
            }
            string appId = JPPConstants.SiteSettings.GetValue(JPPConstants.SiteSettings.FacebookAppId);
            string fbRedirectUrl = string.Format("https://www.facebook.com/dialog/oauth"
                                                 + "?client_id={0}"
                                                 + "&redirect_uri={1}"
                                                 + "&scope={2}",
                                                 appId, redirectAfterLoginUri, scope); // TODO: state, response_type: https://developers.facebook.com/docs/facebook-login/login-flow-for-web-no-jssdk/
            Response.Redirect(fbRedirectUrl);

            // Shouldn't ever get here; if we do, re-show the form
            return View(model);
        }
        public static UserSettingsViewModel Populate(int userId, IUnitOfWork work = null)
        {
            if (work == null)
            {
                work = new UnitOfWork();
            }

            UserSettingsViewModel model = new UserSettingsViewModel()
            {
                FacebookConnectionModel = FacebookConnectionViewModel.Populate(userId, work),
                PrivacySettings         = work.UserRepository.GetPrivacySettingsById(userId),
                CommunicationSettings   = work.UserRepository.GetCommunicationSettingsById(userId),
            };

            return(model);
        }
        public static FacebookConnectionViewModel Populate(int userId, IUnitOfWork work = null)
        {
            if (work == null) work = new UnitOfWork();

            facebook_connection fbConnection = work.EntityContext.facebook_connection.Find(userId);
            if (fbConnection == null)
            {
                fbConnection = new facebook_connection() { notifications_enabled = false, automatic_sharing_enabled = false };
            }

            FacebookConnectionViewModel model = new FacebookConnectionViewModel()
            {
                NotificationsEnabled = fbConnection.notifications_enabled,
                AutomaticSharingEnabled = fbConnection.automatic_sharing_enabled,
            };

            return model;
        }