private static void ValidatePollAnswer(PollAnswer answer)
        {
            if (answer.Id == Guid.Empty)
            {
                answer.Id = Guid.NewGuid();
            }

            answer.Name = TEApi.Html.Sanitize(TEApi.Html.EnsureEncoded(answer.Name));
            if (string.IsNullOrEmpty(answer.Name))
            {
                throw new PollException("The name of the poll answer must be defined.");
            }

            Poll poll = GetPoll(answer.PollId);

            if (poll == null)
            {
                throw new PollException("The poll associated to the answer does not exist.");
            }

            var group = TEApi.Groups.Get(poll.ApplicationId);

            if (group == null || group.HasErrors())
            {
                throw new PollException("The group identified on the poll is invalid.");
            }

            if (poll.AuthorUserId != TEApi.Users.AccessingUser.Id.Value && !PollingPermissionService.CanCreatePolls(TEApi.Groups.ContentTypeId, group.ApplicationId))
            {
                throw new PollException("The user does not have permission to create/edit this poll. The user must be the original creator or an have create poll permissions in the group.");
            }
        }
        private static void ValidatePollVote(PollVote vote)
        {
            if (vote.CreatedDateUtc == DateTime.MinValue)
            {
                vote.CreatedDateUtc = DateTime.UtcNow;
            }

            vote.LastUpdatedDateUtc = DateTime.UtcNow;

            Poll poll = GetPoll(vote.PollId);

            if (poll == null)
            {
                throw new PollException("The poll associated to the vote does not exist.");
            }

            if (poll.VotingEndDateUtc.HasValue && poll.VotingEndDateUtc.Value < DateTime.UtcNow)
            {
                throw new PollException("Voting has ended. Votes cannot be added or changed.");
            }

            if (!poll.Answers.Any(x => x.Id == vote.PollAnswerId))
            {
                throw new PollException("The poll answer doesn't exist on this poll.");
            }

            var group = TEApi.Groups.Get(poll.ApplicationId);

            if (group == null || group.HasErrors())
            {
                throw new PollException("The group identified on the poll is invalid.");
            }

            if (TEApi.Users.AccessingUser.IsSystemAccount.Value)
            {
                throw new PollException("You must be logged in to vote on a poll");
            }

            if (!PollingPermissionService.CanVoteOnPolls(TEApi.Groups.ContentTypeId, poll.ApplicationId))
            {
                throw new PollException("The user does not have permission to vote on polls in this group.");
            }
        }
        private static void ValidatePoll(Poll poll)
        {
            if (poll.Id == Guid.Empty)
            {
                poll.CreatedDateUtc = DateTime.UtcNow;
                poll.Id             = Guid.NewGuid();
            }

            poll.LastUpdatedDateUtc = DateTime.UtcNow;
            poll.Name        = TEApi.Html.Sanitize(TEApi.Html.EnsureEncoded(poll.Name));
            poll.Description = TEApi.Html.Sanitize(poll.Description ?? string.Empty);

            if (poll.HideResultsUntilVotingComplete && !poll.VotingEndDateUtc.HasValue)
            {
                poll.HideResultsUntilVotingComplete = false;
            }

            if (string.IsNullOrEmpty(poll.Name))
            {
                throw new PollException("The name of the poll must be defined.");
            }

            var group = TEApi.Groups.Get(poll.ApplicationId);

            if (group == null || group.HasErrors())
            {
                throw new PollException("The group identified on the poll is invalid.");
            }

            if (!PollingPermissionService.CanCreatePolls(TEApi.Groups.ContentTypeId, group.ApplicationId))
            {
                throw new PollException("The user does not have permission to create polls in this group.");
            }

            if (poll.AuthorUserId <= 0)
            {
                poll.AuthorUserId = TEApi.Users.AccessingUser.Id.Value;
            }
            else if (poll.AuthorUserId != TEApi.Users.AccessingUser.Id.Value && !PollingPermissionService.CanCreatePolls(TEApi.Groups.ContentTypeId, group.ApplicationId))
            {
                throw new PollException("The user does not have permission to create/edit this poll. The user must be the original creator or an admin in the group.");
            }
        }
        internal static Poll GetPoll(Guid pollId)
        {
            Poll poll = (Poll)CacheService.Get(PollCacheKey(pollId), CacheScope.All);

            if (poll == null)
            {
                poll = PollingDataService.GetPoll(pollId);
                if (poll != null)
                {
                    CacheService.Put(PollCacheKey(pollId), poll, CacheScope.All, new string[] { PollTag(poll.ApplicationId) });
                }
            }

            if (poll != null && PollingPermissionService.CanReadPolls(TEApi.Groups.ContentTypeId, poll.ApplicationId))
            {
                return(poll);
            }

            return(null);
        }