Sha256: e945597f8e991012253bda152674f09b663ea9848eec701d5a6ecc83054da221

Contents?: true

Size: 1.33 KB

Versions: 38

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Decidim
  module Elections
    module Voter
      # This class holds the data to cast a vote.
      class VoteForm < Decidim::Form
        mimic :vote

        attribute :encrypted_data, String
        attribute :encrypted_data_hash, String
        attribute :voter_id, String
        attribute :voter_token, String

        validates :encrypted_data, :encrypted_data_hash, :election, presence: true
        validate :hash_is_valid

        delegate :id, to: :election, prefix: true

        # Public: returns the associated election for the vote.
        def election
          @election ||= context.election
        end

        # Public: returns the user for the vote.
        def user
          @user ||= context.user
        end

        # Public: returns the email for the voter.
        def email
          @email ||= context.email
        end

        def bulletin_board
          @bulletin_board ||= context.bulletin_board || Decidim::Elections.bulletin_board
        end

        private

        # Private: check if the hash sent by the browser is correct.
        def hash_is_valid
          return if encrypted_data.blank? || encrypted_data_hash.blank?

          errors.add(:encrypted_data_hash, :invalid) if Digest::SHA256.hexdigest(encrypted_data) != encrypted_data_hash
        end
      end
    end
  end
end

Version data entries

38 entries across 38 versions & 1 rubygems

Version Path
decidim-elections-0.28.4 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.9 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.3 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.8 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.2 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.7 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.1 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.6 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.26.10 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.26.9 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.0 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.5 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.0.rc5 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.28.0.rc4 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.26.8 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.4 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.3 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.26.7 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.26.5 app/forms/decidim/elections/voter/vote_form.rb
decidim-elections-0.27.2 app/forms/decidim/elections/voter/vote_form.rb