lillianchou94/csua169

View on GitHub
app/views/elections/show_vote.html.erb

Summary

Maintainability
Test Coverage
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/core-min.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<!-- left election panel -->
  <script>
  function getCookie(name) {
    var value = "; " + document.cookie;
    var parts = value.split("; " + name + "=");
    if (parts.length == 2) { return parts.pop().split(";").shift(); }
  };
  
  function setCookie(name,value,expires){
      document.cookie = name + "=" + value + ((expires==null) ? "" : ";expires=" + expires.toGMTString());
  };
  
  function check_cookie(){
    $.ajax({
          type: 'POST',
          url: '/get_user_prime',
          data: {'user_email' : $("#nom_user_email").val(),
                'election_id' : $("#election_selected_id").val(),
                'increment_vote' : true,
          },
          cache: false,
          success: function(result) {
                cookie_val = getCookie("password");
                alert(document.cookie)
                alert(cookie_val);
                
                if (cookie_val !== undefined){
                  render_modal3(cookie_val, result.prime); // Popup is not show
                }
                else {
                  render_modal(); // Popup is shown
                }
              }
        });
  };
  
  function render_modal2(prime) {
    var election_id = $('#election_selected_id').val();
    var position_name_id = $('#position_name_id').val();
    var user_email = $("#user_email").val();
    var is_cached;
    var current_user_votes = $('#curr_user_votes').val();
    var user_prime = prime;
    var input_val = $("#password_id").val();
    var encodeuri = btoa(input_val);
    var key = CryptoJS.enc.Base64.parse(encodeuri.toString());
    var iv  = CryptoJS.enc.Base64.parse("EBESExQVFhcYGRobHB0eHw==");
    var text = current_user_votes;
    var step_text = CryptoJS.AES.decrypt(text, key, {iv: iv});
    var decrypted_text = step_text.toString(CryptoJS.enc.Utf8);
    decrypted_text = decrypted_text + "{" + election_id + "{" + position_name_id + " : " + user_prime + ","; // Need to do cleanup in the backend
    var encrypted;
    var encrypted = CryptoJS.AES.encrypt(decrypted_text, key, {iv : iv});
    // alert("this is encrypted: "+encrypted);
    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv});
    // alert("This is the decrypted: "+decrypted.toString(CryptoJS.enc.Utf8));
    console.log("ending decryption");
    var expirydate=new Date();
    expirydate.setTime(expirydate.getTime()+(100*60*60*24*100));
    setCookie("password", input_val, expirydate);
    $.ajax({
      type: 'POST',
      url: '/encryption_save',
      data: {'encrypted_text' : encrypted.toString(),
            'election_id' : election_id,
            'user_email' : user_email,
      },
      cache: false
    });
  };
  
  function render_modal3(password, prime) {
    var election_id = $('#election_selected_id').val();
    var position_name_id = $('#position_name_id').val();
    var user_email = $("#user_email").val();
    var is_cached;
    var current_user_votes = $('#curr_user_votes').val();
    var user_prime = prime;
    var input_val = password;
    console.log(input_val)
    var encodeuri = btoa(input_val);
    var key = CryptoJS.enc.Base64.parse(encodeuri.toString());
    var iv  = CryptoJS.enc.Base64.parse("EBESExQVFhcYGRobHB0eHw==");
    var text = current_user_votes;
    var step_text = CryptoJS.AES.decrypt(text, key, {iv: iv});
    var decrypted_text = step_text.toString(CryptoJS.enc.Utf8);
    decrypted_text = decrypted_text + "{" + election_id + "{" + position_name_id + " : " + user_prime + ","; // Need to do cleanup in the backend
    var encrypted;
    var encrypted = CryptoJS.AES.encrypt(decrypted_text, key, {iv : iv});
    // alert("this is encrypted: "+encrypted);
    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv});
    // alert("This is the decrypted: "+decrypted.toString(CryptoJS.enc.Utf8));
    console.log("ending decryption");
    $.ajax({
      type: 'POST',
      url: '/encryption_save',
      data: {'encrypted_text' : encrypted.toString(),
            'election_id' : election_id,
            'user_email' : user_email,
      },
      cache: false
    });
  };
  
  function render_modal() {
            var opt = {
          autoOpen: false,
          modal: true,
          width: 300,
          height:150,
          title: 'Enter Password'
            };
            var theDialog = $("#confirmVoteModal").dialog(opt);
            theDialog.dialog("open");
  };
  function get_election_dashboard() {
      $.ajax({
        type: 'GET',
        url: '/election_dashboard',
        data: {},
        cache: false,
        success: function(result) {
            $('#election_dashboard').html(result);
        }
    });
    };
  function goto_next_phase(election_id) {
    $.ajax({
      type: 'GET',
      url: '/get_current_phase',
      data: {'election_id':election_id
      },
      cache: false,
      success: function(data) {
        if (data.phase == "2") {
          $.ajax({
            type: 'POST',
            url: '/goto_next_phase',
            data: {'election_id':election_id
            },
            cache: false,
            success: function() {
              get_election_dashboard();
                }
          });
            }
      }
    });
  }
  </script>
  
    <% if @current_user and @election_id != '' and @position_id != ''%>
    <p>Voting for <%=@position_id%></p>
    <p>You can only vote once per position. Once you vote for candidate for a position, you cannot change your selection after submitting.</p>
    <input id="election_selected_id" type="text" value="<%=@election_id%>" hidden>
    <input id="position_name_id" type="text" value="<%=@position_id%>" hidden>
    <input id="curr_user_votes" type="text" value="<%=@current_user_votes%>" hidden>
    <input id="user_email" type="text" value="<%=@current_user_email%>" hidden>
    
    <form id="vote_form_id"></form>
      <% for @nom in @user_list %>
        <div class="radio">
          <label><input type="radio" name="nom" id="id_<%=@nom.user_email%>" value="<%=@nom.user_email%>"><%=@nom.user_name%> (<%=@nom.user_email%>)</label>
        </div>
      <% end %>
      <input id="nom_user_email" type="text" value="<%=@nom.user_email%>" hidden>
      <button value="submit_button" id="vote_form_submit_id" onclick="check_cookie()">Vote!</button>
    </form>
    <% end %>
  
  <% if @election_id == '' or @position_id == '' %>
  <p> You've reached this page in error</p>
  <% end %>
  <br> <br>
    <div id="confirmVoteModal" title="Confirm Vote" style="display:none">
    <script>
      function set_vote_position() {
        var user_selected = $("input:radio[name='nom']:checked").val();
        if (user_selected == null){
          alert("You need to select a user");
        }
        else{
          $.ajax({
          type: 'POST',
          url: '/get_user_prime',
          data: {'user_email' : $("#nom_user_email").val(),
                'election_id' : $("#election_selected_id").val(),
                'increment_vote' : true
          },
          cache: false,
          success: function(result) {
            render_modal2(result.prime);
              }
        });
        }
        };
      </script>
    <!-- <form id="voteFrame_id"> -->
          <input type='hidden' name='<%=request_forgery_protection_token.to_s%>' value='<%=form_authenticity_token%>'>
        <label>Password</label>
        <input id="password_id" name="password_value" type="password"><br>
        <button value="Submit" id="voteFrame_submit_id" onclick="set_vote_position()">Submit</button>
        <!-- <input id="voting_submit" type="submit" value="Submit">
       </form> -->
    </div>

  
  <% if @current_user.is_admin_at_all? %>
        <button type="button" onclick="goto_next_phase('<%=@election_id%>')"><i class="fa fa-gabel" aria-hidden="true"></i> End voting</button>
    <% end %>