private void UpdateItem(Certificate certificate) { ListViewItem item = this.certificates[certificate]; item.SubItems[3].Text = certificate.Validate(controller.Status.CertificateStorage).Text(); DateTime validFrom = certificate.ExpectedValidFrom(controller.Status.CertificateStorage); if (validFrom == DateTime.MinValue) { item.SubItems[4].Text = Resources.CertificateManagerValidIndefinite; } else if (validFrom == DateTime.MaxValue) { item.SubItems[4].Text = string.Empty; } else { item.SubItems[4].Text = validFrom.ToShortDateString(); } DateTime validUntil = certificate.ExpectedValidUntil(controller.Status.CertificateStorage, DateTime.Now); if (validUntil == DateTime.MinValue) { item.SubItems[5].Text = string.Empty; } else if (validUntil == DateTime.MaxValue) { item.SubItems[5].Text = Resources.CertificateManagerValidIndefinite; } else { item.SubItems[5].Text = validUntil.ToShortDateString(); } }
private void AddItem(Certificate certificate) { ListViewItem item = new ListViewItem(certificate.Id.ToString()); item.SubItems.Add(certificate.TypeText); if (certificate is VoterCertificate) { item.SubItems.Add(controller.Status.GetGroupName(((VoterCertificate)certificate).GroupId)); } else if (certificate is AuthorityCertificate) { item.SubItems.Add(((AuthorityCertificate)certificate).FullName); } else if (certificate is AdminCertificate) { item.SubItems.Add(((AdminCertificate)certificate).FullName); } else if (certificate is NotaryCertificate) { item.SubItems.Add(((NotaryCertificate)certificate).FullName); } else { item.SubItems.Add(string.Empty); } item.SubItems.Add(certificate.Validate(controller.Status.CertificateStorage).Text()); DateTime validFrom = certificate.ExpectedValidFrom(controller.Status.CertificateStorage); if (validFrom == DateTime.MinValue) { item.SubItems.Add(Resources.CertificateManagerValidIndefinite); } else if (validFrom == DateTime.MaxValue) { item.SubItems.Add(string.Empty); } else { item.SubItems.Add(validFrom.ToShortDateString()); } DateTime validUntil = certificate.ExpectedValidUntil(controller.Status.CertificateStorage, DateTime.Now); if (validUntil == DateTime.MinValue) { item.SubItems.Add(string.Empty); } else if (validUntil == DateTime.MaxValue) { item.SubItems.Add(Resources.CertificateManagerValidIndefinite); } else { item.SubItems.Add(validUntil.ToShortDateString()); } item.Tag = certificate; this.certificateList.Items.Add(item); this.certificates.Add(certificate, item); }
/// <summary> /// Add an authority. /// </summary> /// <param name="certificate">Authority to be added.</param> /// <returns>Index of the authority.</returns> public int AddAuthority( IRpcConnection connection, Certificate certificate) { if (certificate == null) throw new ArgumentNullException("certificate"); if (certificate.Validate(this.certificateStorage) != CertificateValidationResult.Valid) throw new PiSecurityException(ExceptionCode.InvalidCertificate, "Authority certificate not valid."); if (!(certificate is AuthorityCertificate)) throw new PiSecurityException(ExceptionCode.NoAuthorizedAuthority, "No an authority certificate."); MySqlTransaction transaction = DbConnection.BeginTransaction(); MySqlCommand countCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId", DbConnection, transaction); countCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); if ((long)countCommand.ExecuteScalar() >= this.parameters.AuthorityCount) throw new PiArgumentException(ExceptionCode.AlreadyEnoughAuthorities, "Already enough authorities."); MySqlCommand addedCommand = new MySqlCommand("SELECT count(*) FROM authority WHERE VotingId = @VotingId AND AuthorityId = @AuthorityId", DbConnection, transaction); addedCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); addedCommand.Add("@AuthorityId", certificate.Id.ToByteArray()); if (addedCommand.ExecuteHasRows()) throw new PiArgumentException(ExceptionCode.AuthorityAlreadyInVoting, "Already an authority of the voting."); MySqlCommand indexCommand = new MySqlCommand("SELECT max(AuthorityIndex) + 1 FROM authority WHERE VotingId = @VotingId", DbConnection, transaction); indexCommand.Add("@VotingId", this.parameters.VotingId.ToByteArray()); object authorityIndexNull = indexCommand.ExecuteScalar(); int authorityIndex = authorityIndexNull == DBNull.Value ? 1 : Convert.ToInt32((long)authorityIndexNull); MySqlCommand insertCommand = new MySqlCommand("INSERT INTO authority (VotingId, AuthorityIndex, AuthorityId, Certificate) VALUES (@VotingId, @AuthorityIndex, @AuthorityId, @Certificate)", DbConnection, transaction); insertCommand.Parameters.AddWithValue("@VotingId", this.parameters.VotingId.ToByteArray()); insertCommand.Parameters.AddWithValue("@AuthorityIndex", authorityIndex); insertCommand.Parameters.AddWithValue("@AuthorityId", certificate.Id.ToByteArray()); insertCommand.Parameters.AddWithValue("@Certificate", certificate.ToBinary()); insertCommand.ExecuteNonQuery(); Logger.Log(LogLevel.Info, "Connection {0}: Authority id {1} added to voting id {2}", connection.Id, certificate.Id.ToString(), Id.ToString()); transaction.Commit(); return authorityIndex; }