Example #1
0
        public static void RecordNominationUpdate(IPrincipal user, VotablePosition position, bool isNowNominated)
        {
            string userId       = user.Identity.GetUserId();
            int    electionId   = position.Election.Id;
            int    positionId   = position.Id;
            string electionName = position.Election.Name;
            string positionName = position.HumanName;

            Logger.Information(
                "{userId} updated nomination status for \"{positionName}\" position for election \"{electionName}\": {isNowNominated}",
                userId, positionName, electionName, isNowNominated, positionId, electionId
                );
        }
Example #2
0
        private DisplayNomineeEntry GetNomineeForVoting(int nominationId)
        {
            NominationEntry nominationEntry = db.NominationEntries.Find(nominationId);

            if (nominationEntry == null)
            {
                return(null);
            }

            Election        election        = nominationEntry.Position.Election;
            VotablePosition position        = nominationEntry.Position;
            string          currentUsername = User.Identity.GetUserId();

            // ReSharper disable once SimplifyLinqExpression
            if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername))
            {
                // This student is not eligible for this election
                return(null);
            }

            IDictionary <VotablePosition, PositionDisplayDataForVoting> positionsData = new VotingManager()
                                                                                        .PrepareVotingDataFor(
                currentUsername, election,
                positions => positions.Where(p => p == position)
                );

            if (
                !positionsData.ContainsKey(position) ||
                positionsData[position].Status != PositionDisplayDataForVoting.PositionStatus.CanVote
                )
            {
                // Student doesn't have access to this position or cannot vote for it or has voted already
                return(null);
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            return(positionsData[position].NomineeEntries.First(entry => entry.ModelEntry == nominationEntry));
        }
        public ActionResult NewPositionEntry(NewPositionEntry model)
        {
            if (!ModelState.IsValid)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // STEP 1: Fetch everything we got as IDs

            Election           election = _db.Elections.Find(model.ElectionId);
            VotablePosition    position = _db.VotablePositions.Find(model.PositionId);
            TimetableUserEntry user     = _userRepository.GetByUsername(model.UserId);

            // STEP 2: Check that everything is correct

            List <string> errors        = new List <string>();
            bool          suggestReload = false;

            if (election == null)
            {
                errors.Add("Failed to find election");
                suggestReload = true;
            }

            if (position == null)
            {
                errors.Add("Failed to find the selected position");
                suggestReload = true;
            }

            if (election != null && position != null && position.ElectionId != election.Id)
            {
                errors.Add("The selected position doesn't belong to current election");
                suggestReload = true;
            }

            if (user == null)
            {
                errors.Add("Invalid student ID");
            }

            if (user != null && !user.IsStudentActive)
            {
                errors.Add("Only active students can participate in elections");
            }

            if (errors.Count > 0)
            {
                ViewBag.SuggestReload = suggestReload;
                string errorHtml = PartialView("_ErrorsDisplay", errors).RenderToString();

                return(Json(new
                {
                    Success = false,
                    HumanErrorHtml = errorHtml
                }));
            }

            // STEP 3: Check that there isn't an entry already for this user-position tuple

            bool isDuplicateEntry = _db.PositionEligibilityEntries
                                    .Any(entry => entry.PositionId == position.Id && entry.Username == user.UserId);

            if (isDuplicateEntry)
            {
                return(Json(new
                {
                    Success = false,
                    HumanError =
                        "Error: there is already an entry for this user-position tuple. Please edit that instead"
                }));
            }

            // STEP 4: Create a new entity and save it

            PositionEligibilityEntry newEntry = new PositionEligibilityEntry()
            {
                Username = model.UserId,
                Position = position,

                CanNominate = model.CanNominate,
                CanVote     = model.CanVote
            };

            _db.PositionEligibilityEntries.Add(newEntry);
            _db.SaveChanges();

            // STEP 5: Prepare the displayed table row

            PositionsTableRow tableRow = new PositionsTableRow(newEntry);

            PopulateUserInfo(tableRow, user);

            // STEP 6: Reply that we are done

            return(Json(new
            {
                Success = true,
                Entry = tableRow
            }));
        }
Example #4
0
 public PositionData([NotNull] VotablePosition position, bool canNominate, bool isNominated)
 {
     Position    = position;
     CanNominate = canNominate;
     IsNominated = isNominated;
 }
Example #5
0
        public ActionResult UpdateNominationsStatus(int positionId, bool newStatus)
        {
            VotablePosition position = db.VotablePositions.Find(positionId);

            if (position == null)
            {
                return(HttpNotFound());
            }

            Election election        = position.Election;
            string   currentUsername = User.Identity.GetUserId();

            // ReSharper disable once SimplifyLinqExpression
            if (!election.EligibilityEntries.Any(entry => entry.Username == currentUsername))
            {
                // This student is not eligible for this election - pretend it doesn't exist
                return(HttpNotFound());
            }

            PositionEligibilityEntry positionEligibilityEntry = db.PositionEligibilityEntries
                                                                .FirstOrDefault(entry => entry.PositionId == positionId && entry.Username == currentUsername);

            if (positionEligibilityEntry == null)
            {
                // This student is not eligible for this position - pretend it doesn't exist
                return(HttpNotFound());
            }

            // At this point we are sure that the position is visible to this student
            // Now we need to check whether (s)he can change the status (at this time)

            if (!ElectionLifecycleInfo.CanNominate(election) || !positionEligibilityEntry.CanNominate)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            // At this point we are sure that the student can change the nomination status for this position
            // Now we just need to create or delete the record in database (or do nothing if it's already matching)

            NominationEntry exitingNomination = position.NominationEntries
                                                .FirstOrDefault(entry => entry.Username == currentUsername);

            if (exitingNomination != null)
            {
                if (newStatus)
                {
                    // Do nothing
                }
                else
                {
                    // There is an existing one, remove it
                    db.NominationEntries.Remove(exitingNomination);
                    db.SaveChanges();
                }
            }
            else
            {
                if (newStatus)
                {
                    // No nomination currently, create one
                    db.NominationEntries.Add(new NominationEntry()
                    {
                        Position = position, Username = currentUsername
                    });
                    db.SaveChanges();
                }
                else
                {
                    // Do nothing
                }
            }

            AuditLogManager.RecordNominationUpdate(User, position, newStatus);

            return(RedirectToAction("Nominations", new { id = election.Id }));
        }