Ejemplo n.º 1
0
        private static CouncilElectionForm GenerateFormForCouncil(Election election, CouncilElectionData councilData)
        {
            // Construct the form using the base election
            CouncilElectionForm form = Mapper.Map <CouncilElectionForm>(election);

            // Add the council-specific data
            Mapper.Map(councilData, form);

            return(form);
        }
Ejemplo n.º 2
0
        protected CouncilElectionData GetCouncilData(Election election, string exceptionMessageIfNotFound)
        {
            CouncilElectionData data = Db.CouncilElectionData.Find(election.Id);

            if (data == null)
            {
                throw new Exception(exceptionMessageIfNotFound);
            }

            return(data);
        }
Ejemplo n.º 3
0
        public ActionResult NewCouncil(CouncilElectionForm form)
        {
            ModelFieldsAccessibility fieldsInfo = NewCouncilFieldsInfo;

            // Ignore stuff that isn't supposed to be in new election
            fieldsInfo.ReplaceUneditableWithOldValues(form, new CouncilElectionForm());
            this.RemoveIgnoredErrors(fieldsInfo);

            if (ModelState.IsValid)
            {
                Election election = Mapper.Map <Election>(form);
                election.Type = ElectionType.StudentCouncil;

                CouncilElectionData councilData = Mapper.Map <CouncilElectionData>(form);
                councilData.Election = election;

                ElectionStateChange createChangeInfo = new ElectionStateChange
                {
                    Election           = election,
                    PreviousState      = null,
                    TargetState        = election.State,
                    IsCausedByUser     = true,
                    InstigatorUsername = User.Identity.GetUserId(),
                    CompletedAt        = DateTime.Now
                };

                db.Elections.Add(election);
                db.CouncilElectionData.Add(councilData);
                db.ElectionStateChanges.Add(createChangeInfo);

                using (DbContextTransaction transaction = db.Database.BeginTransaction())
                {
                    db.SaveChanges();

                    // This needs to be after the first big transaction - otherwise EF gets confused about order of actions
                    election.PopulateAutomaticStateTransitions();
                    db.SaveChanges();

                    transaction.Commit();
                }

                AuditLogManager.RecordNewElection(createChangeInfo);

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

            ViewData[FormConstants.FieldsInfoKey] = fieldsInfo;

            return(View(form));
        }
Ejemplo n.º 4
0
        public ActionResult Edit(int id)
        {
            Election election = db.Elections.Find(id);

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

            CouncilElectionData councilData = null;
            CouncilElectionForm councilForm = null;
            ElectionForm        form;

            if (election.Type == ElectionType.StudentCouncil)
            {
                councilData = db.CouncilElectionData.First(data => data.ElectionId == election.Id);
                form        = councilForm = GenerateFormForCouncil(election, councilData);
            }
            else
            {
                form = GenerateFormForCourseRep(election);
            }

            ModelFieldsAccessibility fieldsInfo = ElectionLifecycleInfo.GetWhatCanBeEditedCouncil(election);

            ViewData[FormConstants.FieldsInfoKey] = fieldsInfo;
            ViewBag.Election = election;

            fieldsInfo.EnsureAllowedDefaultKind(
                ModelFieldsAccessibility.Kind.Editable,
                nameof(AdminElectionsController) + "." + nameof(Edit)
                );

            if (Request.HttpMethod.ToUpper() != "POST")
            {
                // Just show the template
                return(View("Edit", form));
            }

            AntiForgery.Validate();

            // Update the form based on data that we received
            // ReSharper disable once ConvertIfStatementToNullCoalescingExpression - we need the compiler to specify different generic arguments
            if (councilForm != null)
            {
                TryUpdateModel(councilForm);
            }
            else
            {
                TryUpdateModel(form);
            }

            // Get the original form so that we use old values for uneditable fields
            CouncilElectionForm councilOriginalForm = null;
            ElectionForm        originalForm;

            if (councilForm != null)
            {
                originalForm = councilOriginalForm = GenerateFormForCouncil(election, councilData);
            }
            else
            {
                originalForm = GenerateFormForCourseRep(election);
            }

            // Replace all uneditable values with old ones
            fieldsInfo.ReplaceUneditableWithOldValues(form, originalForm);

            // As the role IDs are sent from user, we need to make sure that they weren't changed
            if (councilForm != null && fieldsInfo.CanBeChangedByUser(nameof(CouncilElectionForm.Roles)))
            {
                IEnumerable <int?> initialRoleIds = councilOriginalForm.Roles.Select(role => role.Id);
                IEnumerable <int?> newRoleIds     = councilForm.Roles.Select(role => role.Id);

                if (!initialRoleIds.SequenceEqual(newRoleIds))
                {
                    throw new Exception("The IDs of roles were changed by user input");
                }
            }

            // Validate again (since some rules are relative to other fields and can be affected by operations above)
            TryValidateModel(form);

            // Ignore the failures from uneditable fields
            this.RemoveIgnoredErrors(fieldsInfo);

            if (!ModelState.IsValid)
            {
                // The validation failed so we just display the form again
                return(View("Edit", form));
            }

            // Record the admin action
            AdminActionRecord actionRecord = CreateActionRecord(election, AdminActionRecord.RecordType.Edit);

            actionRecord.SetFormChangeSet(FormChangeSet.Generate(form, originalForm));
            db.AdminActionRecords.Add(actionRecord);

            // Validation passed with the fields that are allowed to change. Persist the changes
            Mapper.Map(form, election);
            if (councilData != null)
            {
                Mapper.Map(form, councilData);
            }

            db.SaveChanges();

            BackgroundJob.Enqueue <SynchronizeDelayedJobsJob>(job => job.Execute(election.Id));
            AuditLogManager.RecordElectionEdit(User, election);

            return(RedirectToAction("Details", new { id }));
        }