PageRenderTime 2088ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/app/controllers/spree/admin/users_controller.rb

https://gitlab.com/shinvdu/spree
Ruby | 145 lines | 119 code | 23 blank | 3 comment | 16 complexity | a2ea87b57f0aac2e104d290a538f2ed6 MD5 | raw file
  1. module Spree
  2. module Admin
  3. class UsersController < ResourceController
  4. rescue_from Spree::Core::DestroyWithOrdersError, with: :user_destroy_with_orders_error
  5. after_action :sign_in_if_change_own_password, only: :update
  6. # http://spreecommerce.com/blog/2010/11/02/json-hijacking-vulnerability/
  7. before_action :check_json_authenticity, only: :index
  8. def index
  9. respond_with(@collection) do |format|
  10. format.html
  11. format.json { render json: json_data }
  12. end
  13. end
  14. def show
  15. redirect_to edit_admin_user_path(@user)
  16. end
  17. def create
  18. @user = Spree.user_class.new(user_params)
  19. if @user.save
  20. flash.now[:success] = flash_message_for(@user, :successfully_created)
  21. render :edit
  22. else
  23. render :new
  24. end
  25. end
  26. def update
  27. if params[:user][:password].blank? && params[:user][:password_confirmation].blank?
  28. params[:user].delete(:password)
  29. params[:user].delete(:password_confirmation)
  30. end
  31. if @user.update_attributes(user_params)
  32. flash.now[:success] = Spree.t(:account_updated)
  33. end
  34. render :edit
  35. end
  36. def addresses
  37. if request.put?
  38. if @user.update_attributes(user_params)
  39. flash.now[:success] = Spree.t(:account_updated)
  40. end
  41. render :addresses
  42. end
  43. end
  44. def orders
  45. params[:q] ||= {}
  46. @search = Spree::Order.reverse_chronological.ransack(params[:q].merge(user_id_eq: @user.id))
  47. @orders = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
  48. end
  49. def items
  50. params[:q] ||= {}
  51. @search = Spree::Order.includes(
  52. line_items: {
  53. variant: [:product, { option_values: :option_type }]
  54. }).ransack(params[:q].merge(user_id_eq: @user.id))
  55. @orders = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
  56. end
  57. def generate_api_key
  58. if @user.generate_spree_api_key!
  59. flash[:success] = Spree.t('api.key_generated')
  60. end
  61. redirect_to edit_admin_user_path(@user)
  62. end
  63. def clear_api_key
  64. if @user.clear_spree_api_key!
  65. flash[:success] = Spree.t('api.key_cleared')
  66. end
  67. redirect_to edit_admin_user_path(@user)
  68. end
  69. def model_class
  70. Spree.user_class
  71. end
  72. protected
  73. def collection
  74. return @collection if @collection.present?
  75. @collection = super
  76. if request.xhr? && params[:q].present?
  77. @collection = @collection.includes(:bill_address, :ship_address)
  78. .where("spree_users.email #{LIKE} :search
  79. OR (spree_addresses.firstname #{LIKE} :search AND spree_addresses.id = spree_users.bill_address_id)
  80. OR (spree_addresses.lastname #{LIKE} :search AND spree_addresses.id = spree_users.bill_address_id)
  81. OR (spree_addresses.firstname #{LIKE} :search AND spree_addresses.id = spree_users.ship_address_id)
  82. OR (spree_addresses.lastname #{LIKE} :search AND spree_addresses.id = spree_users.ship_address_id)",
  83. { search: "#{params[:q].strip}%" })
  84. .limit(params[:limit] || 100)
  85. else
  86. @search = @collection.ransack(params[:q])
  87. @collection = @search.result.page(params[:page]).per(Spree::Config[:admin_products_per_page])
  88. end
  89. end
  90. private
  91. def user_params
  92. params.require(:user).permit(permitted_user_attributes |
  93. [spree_role_ids: [],
  94. ship_address_attributes: permitted_address_attributes,
  95. bill_address_attributes: permitted_address_attributes])
  96. end
  97. # handling raise from Spree::Admin::ResourceController#destroy
  98. def user_destroy_with_orders_error
  99. invoke_callbacks(:destroy, :fails)
  100. render status: :forbidden, text: Spree.t(:error_user_destroy_with_orders)
  101. end
  102. # Allow different formats of json data to suit different ajax calls
  103. def json_data
  104. json_format = params[:json_format] || 'default'
  105. case json_format
  106. when 'basic'
  107. collection.map { |u| { 'id' => u.id, 'name' => u.email } }.to_json
  108. else
  109. address_fields = [:firstname, :lastname, :address1, :address2, :city, :zipcode, :phone, :state_name, :state_id, :country_id]
  110. includes = { only: address_fields, include: { state: { only: :name }, country: { only: :name } } }
  111. collection.to_json(only: [:id, :email], include:
  112. { bill_address: includes, ship_address: includes })
  113. end
  114. end
  115. def sign_in_if_change_own_password
  116. if try_spree_current_user == @user && @user.password.present?
  117. sign_in(@user, event: :authentication, bypass: true)
  118. end
  119. end
  120. end
  121. end
  122. end