protected void Page_Load(object sender, EventArgs e) { string position = Request.QueryString["position"]; int positionID; if (position != null && int.TryParse(position, out positionID)) { positionTitle = dbLogic.selectPositionFromID(positionID.ToString()); LabelHeader.Text = positionTitle; HiddenFieldPosition.Value = positionTitle; ISession session = DatabaseEntities.NHibernateHelper.CreateSessionFactory().OpenSession(); userObject = DatabaseEntities.User.FindUser(session, Page.User.Identity.Name.ToString()); id = userObject.ID.ToString(); if(dbLogic.isUserWTS(int.Parse(id), HiddenFieldPosition.Value)) { Fieldset2.Visible = false; Confirm.Visible = true; return; } //ds = dbLogic.getResults(); info = new string[3] { id, id, HiddenFieldPosition.Value }; if (!String.IsNullOrEmpty(positionTitle)) Submit.Enabled = true; } else throw new HttpException(400, "Invalid position ID"); }
protected void createUser_Click(object sender, EventArgs e) { if (!Page.IsValid) return; ISession session = DatabaseEntities.NHibernateHelper.CreateSessionFactory().OpenSession(); ITransaction transaction = session.BeginTransaction(); DatabaseEntities.User user = new DatabaseEntities.User(); user.FirstName = "Default"; user.LastName = "Admin"; user.Email = email.Text; user.Password = DatabaseEntities.User.Hash(password.Text); user.PasswordHint = ""; user.CanVote = false; user.CurrentCommittee = -1; user.Department = DepartmentType.Staff; user.IsAdmin = true; user.IsBargainingUnit = false; user.IsNEC = false; user.IsTenured = false; user.IsUnion = false; user.LastLogin = DateTime.Now; user.CanVote = false; DatabaseEntities.NHibernateHelper.UpdateDatabase(session, user); DatabaseEntities.NHibernateHelper.Finished(transaction); createUserStatus.Visible = true; createUser.Enabled = false; }
protected void Certify_Click(Object sender, EventArgs e) { ITransaction transaction = session.BeginTransaction(); user = DatabaseEntities.User.FindUser(session, User.Identity.Name); // If the confirmation box is ticked, submit the certification if (CertifyCheckBox.Checked == true) { Certification certification = new Certification(); certification.Election = election.ID; certification.User = user.ID; session.SaveOrUpdate(certification); session.Flush(); CertifyCheckBox.Visible = false; CertifyButton.Visible = false; CertifyWarning.Visible = false; NECCertifyAgreement.Visible = false; NECCertificationComplete.Visible = true; } else // otherwise display the error label CertifyWarning.Visible = true; NHibernateHelper.Finished(transaction); }
private User CreateUser(String email, String firstName, String lastName, String department) { DatabaseEntities.User user = new DatabaseEntities.User(); user.FirstName = firstName; user.LastName = lastName; user.Email = email; user.Password = DatabaseEntities.User.Hash(""); user.PasswordHint = ""; user.LastLogin = DateTime.Now; user.CurrentCommittee = Convert.ToInt32(CurrentCommittee.SelectedValue); user.Department = (DepartmentType)Enum.Parse(typeof(DepartmentType), department); return user; }
/// <summary> /// This function takes the specified user off the committee with the /// specified ID. /// </summary> /// <param name="session">A valid session.</param> /// <param name="user">The user to remove</param> /// <param name="id">The ID of the committee the user is being removed from.</param> public static void RemoveMember(ISession session, User user, int id) { user.CurrentCommittee = User.NoCommittee; session.SaveOrUpdate(user); session.Flush(); }
public static void AddMembersToSingleCommitteeOnly() { Console.Write("Add Members to Single Committee Only:\n"); ISessionFactory factory = NHibernateHelper.CreateSessionFactory(); // Clear out database using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { Clear(session, transaction); } } // Pre-requisites using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { // create 1st committee Console.Write("Create committee with 2 positions.\n"); Committee com = new Committee(); com.Name = "Acommittee"; com.PositionCount = 2; session.SaveOrUpdate(com); // add 2 users who are tenured and union members who are on // the committee Console.Write("Create 2 users and add them to the first committee.\n"); User[] users = new User[2]; for (int i = 0; i < 2; i++) { users[i] = User.CreateUser("e", i.ToString() + "F", i.ToString() + "L", "p", "h", false, false, true, true, false, DepartmentType.CSC, OfficerPositionType.None, true, User.NoCommittee); session.SaveOrUpdate(users[i]); users[i].AddToCommittee(session, "Acommittee"); } // create 2nd committee Console.Write("Create another committee with 2 positions.\n"); Committee bcom = new Committee(); bcom.Name = "Bcommittee"; bcom.PositionCount = 2; session.SaveOrUpdate(bcom); transaction.Commit(); } } // More pre-requisites using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { Console.Write("Try to add one of the users to the second committee.\n"); List<User> users = User.GetAllUsers(session); User toAdd = users[0]; toAdd.AddToCommittee(session, "Bcommittee"); transaction.Commit(); } } // Assertions using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { // assert that both users' committee fields reference the // original committee List<User> users = User.GetAllUsers(session); Committee a = Committee.FindCommittee(session, "Acommittee"), b = Committee.FindCommittee(session, "Bcommittee"); Assert.AreEqual(a.ID, users[0].CurrentCommittee); Assert.AreEqual(a.ID, users[1].CurrentCommittee); Assert.AreEqual(0, a.NumberOfVacancies(session)); Assert.AreEqual(2, b.NumberOfVacancies(session)); Assert.AreEqual(2, a.NumberOfPositions(session)); Assert.AreEqual(0, b.NumberOfPositions(session)); } } }
public static void TestCommitteeCreationFailure() { Console.Write("Test Committee Election Failure:\n"); ISessionFactory factory = NHibernateHelper.CreateSessionFactory(); // Clear out database using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { Clear(session, transaction); } } // Pre-requisites using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { // create a committee: 3 positions, 3 filled Console.Write("Create committee with 3 positions.\n"); Committee com = new Committee(); com.Name = "Acommittee"; com.PositionCount = 3; session.SaveOrUpdate(com); // add 3 users who are tenured and union members who are on // the committee Console.Write("Create 3 users and give them positions in the committee.\n"); User[] users = new User[3]; for (int i = 0; i < 3; i++) { users[i] = User.CreateUser("e", i.ToString() + "F", i.ToString() + "L", "p", "h", false, false, true, true, false, DepartmentType.CSC, OfficerPositionType.None, true, com.ID); session.SaveOrUpdate(users[i]); } transaction.Commit(); } // Assertions using (ITransaction transaction = session.BeginTransaction()) { Console.Write("Attempting to create an election based off the committee.\n"); Committee com = Committee.FindCommittee(session, "Acommittee"); CommitteeElection election = CommitteeElection.CreateElection(session, com); Console.Write("CommitteeElection value (should be null): "); Assert.AreEqual(null, election); } } }
public static void InitiateCommitteeElection() { Console.Write("Initial Committee Election:\n"); // Set up pre-conditions ISessionFactory factory = NHibernateHelper.CreateSessionFactory(); using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { // clear the databse first Clear(session, transaction); } using (ITransaction transaction = session.BeginTransaction()) { // add an admin user Console.Write("Create admin user.\n"); User user = User.CreateUser("e", "f", "l", "p", "h", true, false, false, false, false, DepartmentType.CSC, OfficerPositionType.None, false, -1); session.SaveOrUpdate(user); // create a committee: 4 positions, 3 filled Console.Write("Create committee with 4 positions.\n"); Committee com = new Committee(); com.Name = "Acommittee"; com.PositionCount = 4; session.SaveOrUpdate(com); // add 3 users who are tenured and union members who are on // the committee Console.Write("Create 3 users and give them positions in the committee.\n"); User[] users = new User[3]; for (int i = 0; i < 3; i++) { users[i] = User.CreateUser("e", i.ToString() + "F", i.ToString() + "L", "p", "h", false, false, true, true, false, DepartmentType.CSC, OfficerPositionType.None, true, com.ID); session.SaveOrUpdate(users[i]); } transaction.Commit(); } using (ITransaction transaction = session.BeginTransaction()) { Console.Write("Create an election based off the committee.\n"); Committee com = Committee.FindCommittee(session, "Acommittee"); CommitteeElection election = CommitteeElection.CreateElection(session, com); session.SaveOrUpdate(election); transaction.Commit(); } } // Assertions using (ISession session = factory.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { Console.Write("Election object's phase is WTSPhase: "); Assert.AreEqual(ElectionPhase.WTSPhase, CommitteeElection.FindElection(session, "Acommittee").Phase); int vacancies = CommitteeElection.FindElection(session, "Acommittee").VacanciesToFill; Assert.AreEqual(1, vacancies); // Put email stuff here } } }
protected void Page_Load(object sender, EventArgs e) { ISession session = DatabaseEntities.NHibernateHelper.CreateSessionFactory().OpenSession(); user = GetUser(session); is_admin = user.IsAdmin; }
/// <summary> /// Hashes and then updates a user's password. /// </summary> /// <param name="session">A valid session.</param> /// <param name="ID">The ID of the user whose password is to be set.</param> /// <param name="password">The new password.</param> /// <param name="passwordHint">The new password hint.</param> public static void UpdatePassword(ISession session, User user, string password, string passwordHint) { user.Password = Hash(password); user.PasswordHint = passwordHint; session.SaveOrUpdate(user); session.Flush(); }
/// <summary> /// This function imports all the the users in a .accdb file which is /// formatted like the example file Karen gave to us. The .accdb /// will just be on the server. /// </summary> /// <param name="session">A valid session.</param> /// <param name="filePath"></param> /// <returns>True if the import was successful, false otherwise.</returns> public static bool ImportUsers(ISession session, string filePath) { DataSet data = new DataSet(); using (OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Persist Security Info=False;")) { OleDbCommand command = new OleDbCommand("SELECT * FROM Faculty",connection); OleDbDataAdapter adapter = new OleDbDataAdapter(command); try { connection.Open(); adapter.Fill(data, "Faculty"); } catch(Exception e) { return false; } connection.Close(); } DataRowCollection rows = data.Tables["Faculty"].Rows; foreach (DataRow row in rows) { User user = new User(); user.LastName = row.Field<string>("Last"); user.FirstName = row.Field<string>("First"); user.Department = User.GetDepartment(row.Field<string>("Department")); if (user.Department == DepartmentType.None) return false; // Im assuming N = not tenured // T = tenured // O = I don't know but im marking it in as not tenured for now. string tenured = row.Field<string>("Tenure"); user.IsTenured = (tenured == "T") ? true : false; // this field is either 1 or 2. Im assuming 2 means they aren't tenured user.IsUnion = (row.Field<Int16?>("Union") == 1) ? true : false; user.Email = row.Field<string>("Email"); if(string.IsNullOrEmpty(user.Email)) user.Email = "Please retrieve the email for this user."; user.Password = User.Hash(User.DefaultPassword); user.PasswordHint = "Please see the email you recieved in regards to signing up on the iVote system."; user.LastLogin = DateTime.Now; user.OfficerPosition = OfficerPositionType.None; user.IsFaculty = true; user.IsAdmin = false; user.IsBargainingUnit = false; user.IsNEC = false; user.CanVote = true; user.CurrentCommittee = User.NoCommittee; session.SaveOrUpdate(user); } session.Flush(); return true; }
// Helper functions /// <summary> /// Creates a new user object with the specified values /// </summary> /// <param name="email">The user's email</param> /// <param name="first">The user's first name</param> /// <param name="last">The user's last name</param> /// <param name="password">The user's password to be hashed</param> /// <param name="passwordHint">The user's password hint</param> /// <param name="isAdmin">Whether ot not the user is an admin</param> /// <param name="isNEC">Whether or not the user is an NEC member</param> /// <param name="isFaculty">Whether or not the user is an faculty member</param> /// <param name="isTenured">Whether or not the user is tenured</param> /// <param name="isUnion">Whether or not the user is in APSCUF</param> /// <param name="isBargainingUnit">Whether or not the user is in a bargainingunit committee</param> /// <param name="department">The department the faculty member is in</param> /// <param name="officerPosition">The officer position of the user</param> /// <param name="canVote">Whether or not the user can vote</param> /// <param name="currentCommittee">The committee this user serves on</param> /// <returns>Returns a user object with the specified officer position</returns> public static User CreateUser(string email, string first, string last, string password, string passwordHint, bool isAdmin, bool isNEC, bool isFaculty,bool isTenured, bool isUnion, bool isBargainingUnit, DepartmentType department, OfficerPositionType officerPosition, bool canVote, int currentCommittee) { User ret = new User(); ret.Email = email; ret.FirstName = first; ret.LastName = last; ret.Password = Hash(password); ret.PasswordHint = passwordHint; ret.IsAdmin = isAdmin; ret.IsNEC = isNEC; ret.IsFaculty = isFaculty; ret.IsTenured = isTenured; ret.IsUnion = isUnion; ret.IsBargainingUnit = isBargainingUnit; ret.Department = department; ret.OfficerPosition = officerPosition; ret.CanVote = canVote; ret.CurrentCommittee = currentCommittee; return ret; }
protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["id"] == null || Request.QueryString["id"] == "") throw new HttpException(400, "Invalid election ID"); ElectionID = int.Parse(Request.QueryString["id"]); session = NHibernateHelper.CreateSessionFactory().OpenSession(); // grab the objects based off the committee ID election = CommitteeElection.FindElection(session, ElectionID); if (election == null) Response.Redirect("home.aspx#election_not_found"); committee = Committee.FindCommittee(session, election.PertinentCommittee); if (committee == null) Response.Redirect("home.aspx#committee_not_found"); VacancyCount.Text = election.VacanciesToFill.ToString(); CommitteeNameLiteral.Text = committee.Name; CommitteeNameLiteral2.Text = committee.Name; CommitteeDescription.Text = committee.Description; must_be_tenured.Visible = committee.TenureRequired; user = DatabaseEntities.User.FindUser(session, User.Identity.Name); MakeElectionSpecial.Visible = user.IsAdmin; // expose the pertinent panel based on the state of the election. switch (election.Phase) { case ElectionPhase.WTSPhase: //******************************* //****** Faculty WTS Load ******* //******************************* //Check if WTS already exists List<DatabaseEntities.CommitteeWTS> wtsList = DatabaseEntities.CommitteeWTS.FindCommitteeWTS(session, election.ID); bool wtsAlreadySubmitted = false; foreach (DatabaseEntities.CommitteeWTS wts in wtsList) { if (wts.Election == election.ID && wts.User == user.ID && (!committee.TenureRequired || user.IsTenured) && (!committee.BargainingUnitRequired || user.IsBargainingUnit)) wtsAlreadySubmitted = true; } if(user.CurrentCommittee == committee.ID) { wtsPanelServing.Visible = true; wtsPanelNew.Visible = false; } else if (wtsAlreadySubmitted && (!committee.TenureRequired || user.IsTenured) && (!committee.BargainingUnitRequired || user.IsBargainingUnit)) { wtsPanelExisting.Visible = true; wtsPanelNew.Visible = false; } if ((!committee.TenureRequired || user.IsTenured) && (!committee.BargainingUnitRequired || user.IsBargainingUnit)) FacultyWTS.Visible = true; //Prevent a user from submitting a WTS if a member from their department is on the committee if(DatabaseEntities.Committee.DepartmentRepresented(session, committee, user.Department)) FacultyWTS.Visible = false; break; case ElectionPhase.NominationPhase: if(!user.CanVote) break; if (CommitteeWTSNomination.FindCommitteeWTSNomination(session, election.ID, user.ID).Count == 0) { FacultyNomination.Visible = true; BuildUserNominationOptions(); } else FacultyNominationComplete.Visible = true; break; case ElectionPhase.VotePhase: if(!user.CanVote) break; if (BallotFlag.FindBallotFlag(session, election.ID, user.ID) == null) { FacultyVote.Visible = true; BuildUserVoteOptions(); } else FacultyVoteComplete.Visible = true; break; case ElectionPhase.ClosedPhase: if (!user.IsNEC && !user.IsAdmin) FacultyClosed.Visible = true; break; } JulioButtonHider.Visible = user.IsAdmin; CancelElection.Visible = user.IsAdmin; if (user.IsNEC && election.Phase == ElectionPhase.CertificationPhase) { ActivateTab("CertificationPhase"); NECCertificationPanel.Visible = true; BuildNECVoteTable(); if(Certification.FindCertification(session, election.ID, user.ID) != null) { NECCertifyAgreement.Visible = false; CertifyCheckBox.Visible = false; CertifyButton.Visible = false; CertifyWarning.Visible = false; NECCertificationComplete.Visible = true; } } DaysLeftInPhase(); JulioButton.Text = "Switch to Next Phase"; switch(election.Phase) { case ElectionPhase.WTSPhase: PhaseLiteral.Text = "WTS Phase"; break; case ElectionPhase.NominationPhase: PhaseLiteral.Text = "Nomination Phase"; break; case ElectionPhase.VotePhase: PhaseLiteral.Text = "Voting Phase"; break; case ElectionPhase.CertificationPhase: PhaseLiteral.Text = "Certification Phase"; if(ElectionConflict.FindElectionConflicts(session, election.ID).Count == 0) JulioButton.Text = "Close Election"; break; case ElectionPhase.ConflictPhase: PhaseLiteral.Text = "Conflict Resolution Phase"; JulioButton.Text = "Close Election"; break; case ElectionPhase.ClosedPhase: PhaseLiteral.Text = "Closed"; CancelElection.Visible = false; JulioButtonHider.Visible = false; break; } if(user.IsAdmin) { ActivateTab(election.Phase.ToString()); JulioButton.Visible = true; JulioButtonSpacerText.Visible = true; if(election.Phase >= ElectionPhase.ClosedPhase) closed_tab.Visible = true; if (election.Phase == ElectionPhase.ConflictPhase) { List<ElectionConflict> conflicts = ElectionConflict.FindElectionConflicts(session, election.ID); foreach (ElectionConflict conflict in conflicts) { DatabaseEntities.User conflictUser1 = DatabaseEntities.User.FindUser(session, conflict.FirstUser); if (conflict.Type == ConflictType.ElectedToMultipleCommittees) BuildMultipleCommitteesConflictPanel(conflictUser1, conflict.ID); if (conflict.Type == ConflictType.TooManyDeptMembers) { DatabaseEntities.User conflictUser2 = DatabaseEntities.User.FindUser(session, conflict.SecUser); BuildTooManyDeptConflictPanel(conflictUser1, conflictUser2, conflictUser2.Department, conflict.ID); } } JulioButton.Visible = conflicts.Count == 0; JulioButtonSpacerText.Visible = conflicts.Count == 0; if (conflicts.Count == 0) AdminNoConflicts.Visible = true; else DaysRemaining.Text = "The election cannot be closed while conflicts are present."; conflicts_tab.Visible = true; } if (election.Phase >= ElectionPhase.CertificationPhase) { int numberCertifications = Certification.FindCertifications(session, election.ID).Count; string req_certs = System.Configuration.ConfigurationManager.AppSettings["required_nec_certs"]; int nec_certs = req_certs != null ? int.Parse(req_certs) : 3; AdminCertCount.Text = "There are currently " + numberCertifications.ToString(); if (numberCertifications >= nec_certs) // TODO: Add a button to advance to the next phase. AdminCertCount.Text += " certifications, which is enough to proceed to the next stage."; else AdminCertCount.Text += " certification(s). More NEC members must certify the results before proceeding."; certifications_tab.Visible = true; necprogressbar.Attributes.Add("style", "width: " + Math.Min(100, numberCertifications * (100 / nec_certs)).ToString() + "%"); if(numberCertifications < nec_certs) { HtmlGenericControl pretext = new HtmlGenericControl("span"); pretext.InnerText = certifications_tab_link.Text; certifications_tab_link.Controls.Add(pretext); HtmlGenericControl badge = new HtmlGenericControl("span"); badge.Attributes["class"] = "badge badge-info"; badge.Attributes["style"] = "margin-left: 0.5em;"; badge.InnerText = numberCertifications.ToString(); certifications_tab_link.Controls.Add(badge); } } if (election.Phase >= ElectionPhase.VotePhase) { votes_tab.Visible = true; BuildAdminVoteTable(); } if (election.Phase >= ElectionPhase.NominationPhase) { nominations_tab.Visible = true; BuildAdminNominationTable(); } if(election.Phase >= ElectionPhase.WTSPhase && election.Phase < ElectionPhase.ClosedPhase) wts_tab.Visible = true; //******************************* //******** Admin WTS Load ******* //******************************* List<DatabaseEntities.CommitteeWTS> wtsList = DatabaseEntities.CommitteeWTS.FindCommitteeWTS(session, election.ID); foreach (DatabaseEntities.CommitteeWTS wts in wtsList) { DatabaseEntities.User wtsUser = DatabaseEntities.User.FindUser(session, wts.User); if(wtsUser == null) continue; TableRow tr = new TableRow(); Label revokeNameLabel = new Label(); revokeNameLabel.Text = wtsUser.FirstName + " " + wtsUser.LastName; TableCell td1 = new TableCell(); td1.Controls.Add(revokeNameLabel); Label revokeDeptLabel = new Label(); revokeDeptLabel.Text = wtsUser.Department.ToString(); TableCell td2 = new TableCell(); td2.Controls.Add(revokeDeptLabel); Button revokeButton = new Button(); revokeButton.Text = "Revoke"; revokeButton.CssClass = "btn btn-danger btn-small"; revokeButton.CommandArgument = wts.User.ToString(); revokeButton.Click += new System.EventHandler(this.wtsRevoke_Click); TableCell td3 = new TableCell(); td3.Controls.Add(revokeButton); tr.Cells.Add(td1); tr.Cells.Add(td2); tr.Cells.Add(td3); wtsAdminTable.Rows.Add(tr); } if(wtsList.Count == 0) { TableRow tr = new TableRow(); TableCell td1 = new TableCell(); td1.Controls.Add(new LiteralControl("No WTS forms have been submitted yet.")); td1.ColumnSpan = 3; tr.Controls.Add(td1); wtsAdminTable.Rows.Add(tr); } } }