diff --git a/app/controllers/user_credentials_controller.rb b/app/controllers/user_credentials_controller.rb index e9228a33..d2d3f9bf 100644 --- a/app/controllers/user_credentials_controller.rb +++ b/app/controllers/user_credentials_controller.rb @@ -14,6 +14,26 @@ class UserCredentialsController < ApplicationController end end + def import + if params[:file] + file = params[:file] + content = JSON.parse(file.read) + new_credentials = content.map do |hash| + current_user.user_credentials.build(hash.slice("credential_name", "credential_value", "mode")) + end + + respond_to do |format| + if new_credentials.map(&:save).all? + format.html { redirect_to user_credentials_path, notice: "The file was successfully uploaded."} + else + format.html { redirect_to user_credentials_path, notice: 'One or more of the uploaded credentials was not imported due to an error. Perhaps an existing credential had the same name?'} + end + end + else + redirect_to user_credentials_path, notice: "No file was chosen to be uploaded." + end + end + def new @user_credential = current_user.user_credentials.build diff --git a/app/views/user_credentials/index.html.erb b/app/views/user_credentials/index.html.erb index db1d8f13..cf72c6ae 100644 --- a/app/views/user_credentials/index.html.erb +++ b/app/views/user_credentials/index.html.erb @@ -39,6 +39,28 @@