Ejemplo n.º 1
0
        public ContentResult Save(string settingsJSON)
        {
            //var model = new InstantArticlesPageModel()
            //{
            //    InstantArticles = _GetViewModels()
            //};

            UserSettingModel model;

            try
            {
                model = JSONSerializer.Deserialize <UserSettingModel>(settingsJSON);

                var context = new DataContext();

                var userSettingRepos = new EFDataRepository <UserSetting>(context)
                {
                    ExcludeDeleted  = true,
                    IncludeRelated  = true,
                    SaveChanges     = true,
                    DeletePermanent = false
                };

                var userSettings =
                    (UserSetting)AutoMapper.Mapper.Map(model, typeof(UserSettingModel), typeof(UserSetting));

                userSettingRepos.AddOrUpdate((IDataEntity)userSettings);

                SessionManager.UserSettings = model;


                var fbPageRepos = new EFDataRepository <FacebookPage>(context)
                {
                    ExcludeDeleted  = true,
                    IncludeRelated  = true,
                    SaveChanges     = true,
                    DeletePermanent = false
                };

                var page = fbPageRepos.Query(u => u.UserId == SessionManager.User.Id).FirstOrDefault();


                SessionManager.FacebookPage =
                    (FacebookPageModel)AutoMapper.Mapper.Map(page, typeof(FacebookPage), typeof(FacebookPageModel));
            }

            catch (Exception ex)
            {
                return(_ConvertToJSON(new { Success = false }));
            }
            return(_ConvertToJSON(new { Success = true, Settings = model }));
        }
Ejemplo n.º 2
0
        public async Task <ContentResult> CreateAdmin()
        {
            if (UserManager.FindByName("admin") == null)
            {
                var user = new ApplicationUser {
                    UserName = Settings.AdminUser, Email = Settings.AdminEmail
                };
                var result = await UserManager.CreateAsync(user, Settings.AdminPassword);

                if (result.Succeeded)
                {
                    result = UserManager.AddToRole(user.Id, "Admin");

                    var context = new DataContext();

                    var userRepos = new EFDataRepository <User>(context)
                    {
                        ExcludeDeleted  = true,
                        IncludeRelated  = true,
                        SaveChanges     = true,
                        DeletePermanent = false
                    };

                    var savedUser = userRepos.Query(u => u.UserName == user.UserName).FirstOrDefault();

                    if (savedUser == null)
                    {
                        var newUser = new User()
                        {
                            UserName  = Settings.AdminUser,
                            Email     = Settings.AdminEmail,
                            Password  = Settings.AdminPassword,
                            FirstName = Settings.AdminFirstName,
                            LastName  = Settings.AdminLastName,
                            UserId    = user.Id
                        };

                        userRepos.AddOrUpdate(newUser);
                    }

                    return(Content(String.Format("Successfully created admin user\n\rUser Id: {0}.", user.Id)));
                }
            }


            return(Content(String.Format("Error creating admin user account.")));
        }
Ejemplo n.º 3
0
        public static bool UpdateFacebookSessionInfo(string accessToken, string userId)
        {
            try
            {
                SessionManager.FacebookUserAccessToken = accessToken;
                SessionManager.FacebookUserId          = userId;

                //get/save pages info to db
                var     fb     = new FacebookClient(accessToken);
                dynamic result = fb.Get($"{userId}/accounts");

                var pageList = new List <FacebookPageModel>();

                foreach (dynamic page in result.data)
                {
                    pageList.Add(new FacebookPageModel()
                    {
                        PageId         = page.id,
                        Title          = page.name,
                        AccessToken    = page.access_token,
                        FacebookUserId = userId,
                        UserId         = SessionManager.User.Id
                    });
                }

                var db = new DataContext();

                var fbPageRepos = new EFDataRepository <FacebookPage>(db)
                {
                    ExcludeDeleted  = true,
                    IncludeRelated  = true,
                    SaveChanges     = true,
                    DeletePermanent = true
                };


                //remove old pages
                var oldIds = fbPageRepos.Query(p => p.UserId == SessionManager.User.Id).Select(p => p.Id).ToList();
                fbPageRepos.Remove(oldIds);

                //add new
                fbPageRepos.AddOrUpdate(
                    pageList.Select(
                        p =>
                        (FacebookPage)
                        AutoMapper.Mapper.Map(p, typeof(FacebookPageModel),
                                              typeof(FacebookPage))).Cast <IDataEntity>());


                //get/save app access token to session
                fb = new FacebookClient(accessToken);
                dynamic parameters = new ExpandoObject();
                parameters.grant_type        = "fb_exchange_token";
                parameters.client_id         = Settings.FacebookAppId;
                parameters.client_secret     = Settings.FacebookAppSecret;
                parameters.fb_exchange_token = accessToken;
                result = fb.Get($"oauth/access_token", parameters);

                SessionManager.FacebookAppAccessToken = result.access_token;
            }

            catch (Exception ex)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            ViewBag.HideNavBars = true;

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true

            ApplicationUser user = UserManager.FindByEmail(model.Email);

            var result = await SignInManager.PasswordSignInAsync(user.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:

                //save user info
                var context = new DataContext();

                var userRepos = new EFDataRepository <User>(context)
                {
                    ExcludeDeleted  = true,
                    IncludeRelated  = true,
                    SaveChanges     = true,
                    DeletePermanent = false
                };

                var savedUser = userRepos.Query(u => u.UserName == user.UserName).FirstOrDefault();

                if (savedUser != null)
                {
                    SessionManager.User = (UserModel)AutoMapper.Mapper.Map(savedUser, typeof(User), typeof(UserModel));
                }

                //create/retrieve user settings
                var userSettingRepos = new EFDataRepository <UserSetting>(context)
                {
                    ExcludeDeleted  = true,
                    IncludeRelated  = true,
                    SaveChanges     = true,
                    DeletePermanent = false
                };

                var savedUserSettings = userSettingRepos.Query(u => u.UserId == savedUser.Id).FirstOrDefault();

                if (savedUserSettings == null)
                {
                    var newUserSettings = new UserSetting()
                    {
                        UserId = savedUser.Id,
                    };

                    SessionManager.UserSettings =
                        (UserSettingModel)
                        AutoMapper.Mapper.Map(newUserSettings, typeof(UserSetting), typeof(UserSettingModel));

                    userSettingRepos.AddOrUpdate(newUserSettings);
                }
                else
                {
                    SessionManager.UserSettings = (UserSettingModel)
                                                  AutoMapper.Mapper.Map(savedUserSettings, typeof(UserSetting), typeof(UserSettingModel));
                }

                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt. Email/Password incorrect or missing.");
                return(View(model));
            }
        }