Beispiel #1
0
        public JsonResult UpdateListOnPageJson(bool listOnPage)
        {
            if (UserSession.IsKnownTeller)
            {
                var electionCacher = new ElectionCacher(Db);

                var election = UserSession.CurrentElection;
                Db.Election.Attach(election);

                election.ListForPublic       = listOnPage;
                election.ListedForPublicAsOf = listOnPage ? (DateTime?)DateTime.Now : null;

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                new PublicHub().TellPublicAboutVisibleElections();

                UpdateStatusInBrowsers();

                return(new { Saved = true }.AsJsonResult());
            }

            return(new { Saved = false }.AsJsonResult());
        }
Beispiel #2
0
        public JsonResult UpdateListOnPageJson(bool listOnPage)
        {
            if (UserSession.IsKnownTeller)
            {
                var Db = GetNewDbContext();

                var electionCacher = new ElectionCacher(Db);

                var election = UserSession.CurrentElection;
                Db.Election.Attach(election);

                election.ListForPublic       = listOnPage;
                election.ListedForPublicAsOf = listOnPage ? (DateTime?)DateTime.Now : null;

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                Startup.GetService <IPublicHubHelper>().TellPublicAboutVisibleElections();

                return(new { Saved = true }.AsJsonResult());
            }

            return(new { Saved = false }.AsJsonResult());
        }
        protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
        {
            var authorized = base.AuthorizeCore(httpContext) && (UserSession.IsGuestTeller || UserSession.IsKnownTeller);

            if (!authorized)
            {
                // The user is not authenticated
                return(false);
            }

            var electionModel = new ElectionModel();

            if (UserSession.IsGuestTeller)
            {
                if (!electionModel.GuestsAllowed())
                {
                    UserSession.ProcessLogout();
                    return(false);
                }
                return(true);
            }

            // only update visit every 5 minutes. Lasts for 1 hour, so could be only 45 minutes.
            var currentElection = UserSession.CurrentElection;
            var delayTime       = TimeSpan.FromMinutes(5);

            if (currentElection != null && currentElection.ListForPublic.AsBoolean())
            {
                LogTime("init");
                var db = UnityInstance.Resolve <IDbContextFactory>().GetNewDbContext;
                LogTime("resolve");

                db.Election.Attach(currentElection);
                LogTime("attach");

                currentElection.ListedForPublicAsOf = DateTime.Now;

                LogTime("listed");
                var electionCacher = new ElectionCacher(db);
                LogTime("cacher");

                electionCacher.UpdateItemAndSaveCache(currentElection);
                LogTime("update cache");

                if (currentElection.ListForPublic.AsBoolean() &&
                    (DateTime.Now - currentElection.ListedForPublicAsOf.GetValueOrDefault(DateTime.Now.AddMinutes(-60))).TotalMinutes > delayTime.TotalMinutes)
                {
                    db.SaveChanges();

                    LogTime("db save");
                }

                new PublicHub().TellPublicAboutVisibleElections();
                LogTime("notify");
            }

            return(true);
        }
Beispiel #4
0
        public JsonResult SaveOnlineClose(DateTime when, bool est)
        {
            var electionCacher = new ElectionCacher(Db);

            var election = UserSession.CurrentElection;

            Db.Election.Attach(election);

            election.OnlineWhenClose       = when;
            election.OnlineCloseIsEstimate = est;

            // var sendEmail = false;
            if (election.OnlineCurrentlyOpen)
            {
                election.OnlineAnnounced = null;
                // sendEmail = true;
            }

            Db.SaveChanges();

            electionCacher.UpdateItemAndSaveCache(election);

            new AllVotersHub()
            .UpdateVoters(new
            {
                election.OnlineWhenClose,
                election.OnlineWhenOpen,
                election.OnlineCloseIsEstimate,
            });
            new FrontDeskHub()
            .UpdateOnlineElection(new
            {
                election.OnlineWhenClose,
                election.OnlineWhenOpen,
                election.OnlineCloseIsEstimate,
            });

            UpdateStatusInBrowsers();

            // string emailResult = null;
            // if (sendEmail)
            // {
            //   emailResult = new EmailHelper().SendWhenOpened(election);
            // }

            return(new
            {
                success = true,
                // emailResult,
                election.OnlineWhenClose,
                election.OnlineCloseIsEstimate,
            }.AsJsonResult());
        }
Beispiel #5
0
        public void SetTallyStatus(string status)
        {
            if (UserSession.IsGuestTeller)
            {
                return;
            }
            var Db = GetNewDbContext();

            var electionCacher = new ElectionCacher(Db);
            var election       = UserSession.CurrentElection;

            if (election.TallyStatus != status)
            {
                Db.Election.Attach(election);

                election.TallyStatus = status;

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                //new ElectionStatusSharer().SetStateFor(election);
                //        var menuHelper = new MenuHelper(controller.Url);
                var info = new
                {
                    //          QuickLinks = menuHelper.QuickLinks(),
                    //          Name = UserSession.CurrentElectionStatusName,
                    StateName = UserSession.CurrentElectionStatus,
                };
                //
                //        // should always be true... but usage could change in future
                //        var currentIsKnown = UserSession.IsKnownTeller;
                //        UserSession.IsKnownTeller = false;
                ////        menuHelper = new MenuHelper(controller.Url);
                //        var infoForGuest = new
                //        {
                ////          QuickLinks = menuHelper.QuickLinks(),
                ////          QuickSelector = menuHelper.StateSelectorItems().ToString(),
                ////          Name = UserSession.CurrentElectionStatusName,
                //          State = UserSession.CurrentElectionStatus,
                //        };
                //        UserSession.IsKnownTeller = currentIsKnown;

                Startup.GetService <IMainHubHelper>().StatusChanged(info, info);
            }
        }
Beispiel #6
0
        /// <summary>
        ///   Based on the 2 flags, get a default reason (may be null)
        /// </summary>
        /// <returns></returns>
        // public IneligibleReasonEnum GetDefaultIneligibleReason()
        // {
        //   return GetDefaultIneligibleReason(UserSession.CurrentElection);
        // }

        /// <summary>
        ///   Based on the 2 flags, get a default reason (may be null)
        /// </summary>
        /// <returns></returns>
        // public static IneligibleReasonEnum GetDefaultIneligibleReason(Election election)
        // {
        //   if (election == null)
        //   {
        //     return null;
        //   }
        //
        //   var canVote = election.CanVote == CanVoteOrReceive.All;
        //   var canReceiveVotes = election.CanReceive == CanVoteOrReceive.All;
        //
        //   if (canVote && canReceiveVotes)
        //   {
        //     return null;
        //   }
        //
        //   if (!canVote && !canReceiveVotes)
        //   {
        //     return IneligibleReasonEnum.Ineligible_Other;
        //   }
        //
        //   if (!canVote)
        //   {
        //     return IneligibleReasonEnum.IneligiblePartial2_Not_a_Delegate;
        //   }
        //
        //   return IneligibleReasonEnum.IneligiblePartial1_Not_in_TieBreak;
        // }


        /// <summary>
        ///   Will be either email or sms
        /// </summary>
        /// <returns></returns>
        public JsonResult SaveNotification(string emailSubject, string emailText, string smsText)
        {
            var electionCacher = new ElectionCacher(Db);
            var election       = UserSession.CurrentElection;

            Db.Election.Attach(election);

            var changed = false;

            if (emailSubject != null) // don't use HasContent - if empty string, save it
            {
                election.EmailSubject = Uri.UnescapeDataString(emailSubject);
                changed = true;
            }

            if (emailText != null) // don't use HasContent - if empty string, save it
            {
                election.EmailText = Uri.UnescapeDataString(emailText);
                changed            = true;
            }

            if (smsText != null)
            {
                election.SmsText = Uri.UnescapeDataString(smsText);
                changed          = true;
            }

            if (changed)
            {
                Db.SaveChanges();
                electionCacher.UpdateItemAndSaveCache(election);
                return(new
                {
                    success = true,
                    Status = "Saved",
                }.AsJsonResult());
            }

            return(new
            {
                success = false,
                Status = "Nothing to save",
            }.AsJsonResult());
        }
Beispiel #7
0
        public JsonResult UpdateElectionShowAllJson(bool showFullReport)
        {
            var electionCacher = new ElectionCacher(Db);

            var election = UserSession.CurrentElection;

            if (election.ShowFullReport != showFullReport)
            {
                Db.Election.Attach(election);

                election.ShowFullReport = showFullReport;

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);
            }

            return(new { Saved = true }.AsJsonResult());
        }
Beispiel #8
0
        public void SetTallyStatus(string status)
        {
            if (UserSession.IsGuestTeller)
            {
                return;
            }

            var electionCacher = new ElectionCacher(Db);
            var election       = UserSession.CurrentElection;

            if (election.TallyStatus != status)
            {
                Db.Election.Attach(election);

                election.TallyStatus = status;

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                UpdateStatusInBrowsers();
            }
        }
Beispiel #9
0
        /// <Summary>Gets directly from the database, not session. Stores in session.</Summary>
        /// <Summary>Saves changes to this election</Summary>
        public JsonResult SaveElection(Election electionFromBrowser)
        {
            var electionCacher = new ElectionCacher(Db);

            var election = UserSession.CurrentElection;

            Db.Election.Attach(election);

            var beforeChanges = election.GetAllProperties();

            var currentType = election.ElectionType;
            var currentMode = election.ElectionMode;
            var currentNum  = election.NumberToElect;
            // var currentCan = election.CanVote;
            // var currentReceive = election.CanReceive;
            var currentListed = election.ListForPublic;



            // List of fields to allow edit from setup page
            var editableFields = new
            {
                election.Name,
                election.DateOfElection,
                election.Convenor,
                election.ElectionType,
                election.ElectionMode,
                election.NumberToElect,
                election.NumberExtra,
                // election.CanVote,
                // election.CanReceive,
                election.ListForPublic,
                election.ShowAsTest,
                election.ElectionPasscode,
                //election.UseCallInButton, - use VotingMethods now
                election.HidePreBallotPages,
                election.MaskVotingMethod,
                election.BallotProcessRaw,
                election.EnvNumModeRaw,
                election.T24,
                election.OnlineWhenOpen,
                election.OnlineWhenClose,
                election.OnlineCloseIsEstimate,
                election.OnlineSelectionProcess,
                election.EmailFromAddress,
                election.EmailFromName,
                election.VotingMethods,
                election.CustomMethods,
            }.GetAllPropertyInfos().Select(pi => pi.Name).ToList();

            // if (election.ElectionType == ElectionTypeEnum.Oth)
            // {
            //   editableFields.Add(election.CanVote);
            //   editableFields.Add(election.CanReceive);
            // }

            if (!currentListed.AsBoolean() && election.ListForPublic.AsBoolean())
            {
                // just turned on
                election.ListedForPublicAsOf = DateTime.Now;
            }

            var changed = electionFromBrowser.CopyPropertyValuesTo(election, editableFields);

            var coreSettingsChanged = currentMode != election.ElectionMode ||
                                      currentType != election.ElectionType ||
                                      currentNum != election.NumberToElect;

            // || currentCan != election.CanVote
            // || currentReceive != election.CanReceive;

            if (coreSettingsChanged)
            {
                var setupModel = new SetupModel();
                if (setupModel.HasBallots || setupModel.HasOnlineBallots)
                {
                    return(new
                    {
                        success = false,
                        Status = "Cannot change type of election after ballots are entered."
                    }.AsJsonResult());
                }
            }

            var locationModel  = new LocationModel();
            var onlineLocation = locationModel.GetOnlineLocation();

            if (changed)
            {
                // check if trying to turn off online elections
                if (!election.OnlineEnabled)
                {
                    if (onlineLocation != null)
                    {
                        if (locationModel.IsLocationInUse(onlineLocation.LocationGuid))
                        {
                            beforeChanges.CopyPropertyValuesTo(election);

                            // we have ballots from Online... can't remove Open date!
                            return(new
                            {
                                success = false,
                                Status = "Online ballots received. Cannot disable online voting.",
                                Election = election,
                                displayName = UserSession.CurrentElectionDisplayNameAndInfo
                            }.AsJsonResult());
                        }
                    }
                }

                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                new PublicHub()
                .TellPublicAboutVisibleElections(); // in case the name, or ListForPublic, etc. has changed
            }


            if (coreSettingsChanged)
            {
                // reset flags
                // new PeopleModel().SetInvolvementFlagsToDefault(); --> must re-import list to apply settings to everyone

                // update analysis
                new ResultsModel().GenerateResults();
            }

            // adjust for Online
            if (election.OnlineEnabled)
            {
                if (onlineLocation == null)
                {
                    // need a new location for online!
                    locationModel.EditLocation(0, LocationModel.OnlineLocationName, true);
                }
            }
            else
            {
                // not enabled
                if (onlineLocation != null)
                {
                    // remove it (already checked that it is not in use)
                    locationModel.EditLocation(onlineLocation.C_RowId, "", true);
                }
            }

            new AllVotersHub()
            .UpdateVoters(new
            {
                changed = true,
                election.OnlineWhenClose,
                election.OnlineWhenOpen,
                election.OnlineCloseIsEstimate,
                election.OnlineSelectionProcess
            });
            new FrontDeskHub()
            .UpdateOnlineElection(new
            {
                election.OnlineWhenClose,
                election.OnlineWhenOpen,
                election.OnlineCloseIsEstimate,
            });

            // alert will go out when the scheduled job runs

            return(new
            {
                success = true,
                Status = "Saved",
                defaultFromAddress = UserSession.CurrentElection.EmailFromAddressWithDefault,
                Election = election,
                displayName = UserSession.CurrentElectionDisplayNameAndInfo
            }.AsJsonResult());
        }
Beispiel #10
0
        //    public Election GetFreshFromDb(Guid electionGuid)
        //    {
        //      return Election.ThisElectionCached;// Db.Election.FirstOrDefault(e => e.ElectionGuid == electionGuid);
        //    }
        /// <Summary>Gets directly from the database, not session. Stores in session.</Summary>
        /// <Summary>Saves changes to this election</Summary>
        public JsonResult SaveElection(Election electionFromBrowser)
        {
            var Db = GetNewDbContext();
            //      var election = Db.Election.SingleOrDefault(e => e.Id == electionFromBrowser.Id);
            var electionCacher = new ElectionCacher(Db);

            var election = UserSession.CurrentElection;

            Db.Election.Attach(election);

            var currentType    = election.ElectionType;
            var currentMode    = election.ElectionMode;
            var currentCan     = election.CanVote;
            var currentReceive = election.CanReceive;
            var currentListed  = election.ListForPublic;

            // List of fields to allow edit from setup page
            var editableFields = new
            {
                election.Name,
                election.DateOfElection,
                election.Convenor,
                election.ElectionType,
                election.ElectionMode,
                election.NumberToElect,
                election.NumberExtra,
                election.CanVote,
                election.CanReceive,
                election.ListForPublic,
                election.ShowAsTest,
                election.ElectionPasscode,
                election.UseCallInButton,
                election.HidePreBallotPages,
                election.MaskVotingMethod,
                election.BallotProcess,
                election.EnvNumMode,
                election.T24,
            }.GetAllPropertyInfos().Select(pi => pi.Name).ToArray();

            if (!currentListed.AsBoolean() && election.ListForPublic.AsBoolean())
            {
                // just turned on
                election.ListedForPublicAsOf = DateTime.Now;
            }

            var changed = electionFromBrowser.CopyPropertyValuesTo(election, editableFields);

            if (changed)
            {
                Db.SaveChanges();

                electionCacher.UpdateItemAndSaveCache(election);

                Startup.GetService <IPublicHubHelper>().TellPublicAboutVisibleElections(); // in case the name, or ListForPublic, etc. has changed
            }

            if (currentMode != election.ElectionMode ||
                currentType != election.ElectionType ||
                currentCan != election.CanVote ||
                currentReceive != election.CanReceive
                )
            {
                // reset flags
                new PeopleModel().SetInvolvementFlagsToDefault();

                // update analysis
                new ResultsModel().GenerateResults();
            }

            var displayName = UserSession.CurrentElectionDisplayNameAndInfo;

            return(new
            {
                Status = "Saved",
                Election = election,
                displayName
            }.AsJsonResult());
        }