user_credentials_controller.rb 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. class UserCredentialsController < ApplicationController
  2. include SortableTable
  3. def index
  4. set_table_sort sorts: %w[credential_name credential_value], default: { credential_name: :asc }
  5. @user_credentials = current_user.user_credentials.reorder(table_sort).page(params[:page])
  6. respond_to do |format|
  7. format.html
  8. format.json {
  9. send_data Utils.pretty_jsonify(@user_credentials.limit(nil).as_json), disposition: 'attachment'
  10. }
  11. end
  12. end
  13. def import
  14. if params[:file]
  15. file = params[:file]
  16. content = JSON.parse(file.read)
  17. new_credentials = content.map do |hash|
  18. current_user.user_credentials.build(hash.slice("credential_name", "credential_value", "mode"))
  19. end
  20. respond_to do |format|
  21. if new_credentials.map(&:save).all?
  22. format.html { redirect_to user_credentials_path, notice: "The file was successfully uploaded."}
  23. else
  24. 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?'}
  25. end
  26. end
  27. else
  28. redirect_to user_credentials_path, notice: "No file was chosen to be uploaded."
  29. end
  30. end
  31. def new
  32. @user_credential = current_user.user_credentials.build
  33. respond_to do |format|
  34. format.html
  35. format.json { render json: @user_credential }
  36. end
  37. end
  38. def edit
  39. @user_credential = current_user.user_credentials.find(params[:id])
  40. end
  41. def create
  42. @user_credential = current_user.user_credentials.build(user_credential_params)
  43. respond_to do |format|
  44. if @user_credential.save
  45. format.html { redirect_to user_credentials_path, notice: 'Your credential was successfully created.' }
  46. format.json { render json: @user_credential, status: :created, location: @user_credential }
  47. else
  48. format.html { render action: "new" }
  49. format.json { render json: @user_credential.errors, status: :unprocessable_entity }
  50. end
  51. end
  52. end
  53. def update
  54. @user_credential = current_user.user_credentials.find(params[:id])
  55. respond_to do |format|
  56. if @user_credential.update(user_credential_params)
  57. format.html { redirect_to user_credentials_path, notice: 'Your credential was successfully updated.' }
  58. format.json { head :no_content }
  59. else
  60. format.html { render action: "edit" }
  61. format.json { render json: @user_credential.errors, status: :unprocessable_entity }
  62. end
  63. end
  64. end
  65. def destroy
  66. @user_credential = current_user.user_credentials.find(params[:id])
  67. @user_credential.destroy
  68. respond_to do |format|
  69. format.html { redirect_to user_credentials_path }
  70. format.json { head :no_content }
  71. end
  72. end
  73. private
  74. def user_credential_params
  75. params.require(:user_credential).permit(:credential_name, :credential_value, :mode)
  76. end
  77. end