Пример #1
0
        public string GetDecryptedListPool(OnlineVotingInfo onlineVotingInfo, out string errorMessage)
        {
            var rawText = onlineVotingInfo.ListPool;

            if (!EncryptionHelper.IsEncrypted(rawText))
            {
                errorMessage = null;
                return(rawText);
            }

            var salt     = onlineVotingInfo.C_RowId.ToString();
            var listPool = EncryptionHelper.Decrypt(rawText, salt, out errorMessage);

            if (errorMessage.HasContent())
            {
                // DecryptionError: The provided payload could not be decrypted. Refer to the inner exception for more information.
                // (The key {8bcd2320-f17b-4831-a4b3-f24d74f50e5c} was not found in the key ring.)

                if (errorMessage.Contains("not found in the key ring"))
                {
                    new LogHelper().Add("DecryptionError: " + errorMessage, true, UserSession.VoterId);

                    errorMessage = "The server has been changed. You must create your ballot again.";
                    return(null);
                }

                new LogHelper().Add("DecryptionError: " + errorMessage, true, UserSession.VoterId);
                return(null);
            }

            return(listPool);
        }
Пример #2
0
        private void LoadOnlineVotingInfo()
        {
            XmlNodeList nodes;

            try
            {
                nodes = _xmlRoot.SelectNodes("t:onlineVoterInfo", _nsm);
                if (nodes != null)
                {
                    var toLoad = new List <OnlineVotingInfo>();
                    foreach (XmlElement element in nodes)
                    {
                        var ovi = new OnlineVotingInfo();
                        element.CopyAttributeValuesTo(ovi);
                        ovi.ElectionGuid = _electionGuid;
                        UpdateGuidFromMapping(ovi, v => v.PersonGuid, Guid.Empty);
                        toLoad.Add(ovi);
                    }

                    Db.BulkInsert(toLoad);
                    _hub.StatusUpdate("Loaded {0} online voter info{1}".FilledWith(toLoad.Count, toLoad.Count.Plural("es")));
                }
            }
            catch (Exception ex)
            {
                _hub.StatusUpdate("Failed to load online voter infos. (Error: {0})".FilledWith(ex.LastException().Message));
            }
        }
Пример #3
0
        public JsonResult JoinElection(Guid electionGuid)
        {
            var voterId = UserSession.VoterId;

            if (voterId.HasNoContent())
            {
                return(new
                {
                    Error = "Invalid request"
                }.AsJsonResult());
            }

            // confirm that this person is in the election
            var personQuery = Db.Person.Where(p => p.ElectionGuid == electionGuid);

            if (UserSession.VoterIdType == VoterIdTypeEnum.Email)
            {
                personQuery = personQuery.Where(p => p.Email == voterId);
            }
            else if (UserSession.VoterIdType == VoterIdTypeEnum.Phone)
            {
                personQuery = personQuery.Where(p => p.Phone == voterId);
            }
            else
            {
                return(new
                {
                    Error = "Invalid request"
                }.AsJsonResult());
            }

            var electionInfo = personQuery
                               .Join(Db.Election, p => p.ElectionGuid, e => e.ElectionGuid, (p, e) => new { e, p })
                               .SingleOrDefault();

            if (electionInfo == null)
            {
                return(new
                {
                    Error = "Invalid election"
                }.AsJsonResult());
            }

            var now = DateTime.Now;

            // get voting info
            var votingInfo = Db.OnlineVotingInfo
                             .SingleOrDefault(ovi => ovi.ElectionGuid == electionGuid && ovi.PersonGuid == electionInfo.p.PersonGuid);

            // if (votingInfo == null)
            // {
            //   var existingByEmail = Db.OnlineVotingInfo
            //     .SingleOrDefault(ovi => ovi.ElectionGuid == electionGuid && ovi.PersonGuid == electionInfo.p.Email);
            //
            //   if (existingByEmail != null)
            //   {
            //     return new
            //     {
            //       Error = "This email address was used for another person."
            //     }.AsJsonResult();
            //   }
            //
            //   var existingByPhone = Db.OnlineVotingInfo
            //      .SingleOrDefault(ovi => ovi.ElectionGuid == electionGuid && ovi.Phone == electionInfo.p.Phone);
            //
            //   if (existingByPhone != null)
            //   {
            //     return new
            //     {
            //       Error = "This phone number was used for another person."
            //     }.AsJsonResult();
            //   }
            // }

            if (electionInfo.e.OnlineWhenOpen <= now && electionInfo.e.OnlineWhenClose > now)
            {
                // put election in session
                UserSession.CurrentElectionGuid       = electionInfo.e.ElectionGuid;
                UserSession.VoterInElectionPersonGuid = electionInfo.p.PersonGuid;
                // UserSession.VoterInElectionPersonName = electionInfo.p.C_FullNameFL;
                string poolDecryptError = null;

                if (votingInfo == null)
                {
                    votingInfo = new OnlineVotingInfo
                    {
                        ElectionGuid  = electionInfo.e.ElectionGuid,
                        PersonGuid    = electionInfo.p.PersonGuid,
                        Status        = OnlineBallotStatusEnum.New,
                        WhenStatus    = now,
                        HistoryStatus = "New|{0}".FilledWith(now.ToJSON())
                    };
                    Db.OnlineVotingInfo.Add(votingInfo);
                    Db.SaveChanges();
                }
                else
                {
                    if (EncryptionHelper.IsEncrypted(votingInfo.ListPool))
                    {
                        votingInfo.ListPool = new OnlineVoteHelper().GetDecryptedListPool(votingInfo, out poolDecryptError);
                    }
                }

                // okay
                return(new
                {
                    open = true,
                    voterName = electionInfo.p.C_FullName,
                    electionInfo.e.NumberToElect,
                    OnlineSelectionProcess = electionInfo.e.OnlineSelectionProcess.DefaultTo(OnlineSelectionProcessEnum.Random.ToString().Substring(0, 1)),
                    registration = VotingMethodEnum.TextFor(electionInfo.p.VotingMethod),
                    votingInfo,
                    poolDecryptError
                }.AsJsonResult());
            }

            return(new
            {
                closed = true,
                votingInfo
            }.AsJsonResult());
        }
Пример #4
0
        /// <summary>
        /// Encrypt <param name="newListPoolToEncrypt"></param> and put it into the <param name="onlineVotingInfo"></param>.
        /// </summary>
        /// <param name="onlineVotingInfo"></param>
        /// <param name="newListPoolToEncrypt"></param>
        public void SetListPoolEncrypted(OnlineVotingInfo onlineVotingInfo, string newListPoolToEncrypt = null)
        {
            var encrypted = EncryptionHelper.Encrypt(newListPoolToEncrypt ?? onlineVotingInfo.ListPool, onlineVotingInfo.C_RowId.ToString());

            onlineVotingInfo.ListPool = encrypted;
        }