From a48b992ec049eeb3fe3c980f8ebf36fcd0ad7346 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 18 Mar 2016 14:28:47 +1100 Subject: [PATCH] Shops can require users to login Enterprise users have a new option to restrict their shopfronts to logged in users only. If a guest visits one of these shopfornts, the guest is prompted to login and is not shown any products. Closes #849. --- app/helpers/shop_helper.rb | 4 ++++ .../form/_primary_details.html.haml | 17 +++++++++------ app/views/enterprises/shop.html.haml | 5 +++-- app/views/shop/_messages.html.haml | 9 +++++++- config/locales/en.yml | 14 +++++++++++++ ...6051131_add_require_login_to_enterprise.rb | 5 +++++ db/schema.rb | 3 ++- spec/features/admin/enterprises_spec.rb | 6 ++++++ .../consumer/shopping/shopping_spec.rb | 21 +++++++++++++++++++ 9 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20160316051131_add_require_login_to_enterprise.rb diff --git a/app/helpers/shop_helper.rb b/app/helpers/shop_helper.rb index 3066bfbe05..eb2643820f 100644 --- a/app/helpers/shop_helper.rb +++ b/app/helpers/shop_helper.rb @@ -7,4 +7,8 @@ module ShopHelper ] end end + + def require_login? + current_distributor.require_login? && spree_current_user.nil? + end end diff --git a/app/views/admin/enterprises/form/_primary_details.html.haml b/app/views/admin/enterprises/form/_primary_details.html.haml index 437a61e866..ccf59c8a12 100644 --- a/app/views/admin/enterprises/form/_primary_details.html.haml +++ b/app/views/admin/enterprises/form/_primary_details.html.haml @@ -22,7 +22,6 @@ %a What's this? .five.columns.omega = f.check_box :is_primary_producer, 'ng-model' => 'Enterprise.is_primary_producer' -   = f.label :is_primary_producer, 'Producer' - if spree_current_user.admin? .row @@ -33,15 +32,12 @@ %a What's this? .two.columns = f.radio_button :sells, "none", 'ng-model' => 'Enterprise.sells' -   = f.label :sells, "None", value: "none" .two.columns = f.radio_button :sells, "own", 'ng-model' => 'Enterprise.sells' -   = f.label :sells, "Own", value: "own" .four.columns.omega = f.radio_button :sells, "any", 'ng-model' => 'Enterprise.sells' -   = f.label :sells, "Any", value: "any" .row .three.columns.alpha @@ -50,12 +46,21 @@ %a What's this? .two.columns = f.radio_button :visible, true -   = f.label :visible, "Visible", :value => "true" .five.columns.omega = f.radio_button :visible, false -   = f.label :visible, "Not Visible", :value => "false" +.row + .three.columns.alpha + %label= t '.shopfront_requires_login' + %div{'ofn-with-tip' => t('.shopfront_requires_login_tip')} + %a= t 'admin.whats_this' + .two.columns + = f.radio_button :require_login, false + = f.label :require_login, t('.shopfront_requires_login_false'), value: :false + .five.columns.omega + = f.radio_button :require_login, true + = f.label :require_login, t('.shopfront_requires_login_true'), value: :true .permalink{ ng: { controller: "permalinkCtrl" } } .row{ ng: { show: "Enterprise.sells == 'own' || Enterprise.sells == 'any'" } } .three.columns.alpha diff --git a/app/views/enterprises/shop.html.haml b/app/views/enterprises/shop.html.haml index f2063494b4..980cb2b3b0 100644 --- a/app/views/enterprises/shop.html.haml +++ b/app/views/enterprises/shop.html.haml @@ -22,6 +22,7 @@ %select.avenir#order_cycle_id{"ng-model" => "order_cycle.order_cycle_id", "ofn-change-order-cycle" => true, + "disabled" => require_login?, "ng-options" => "oc.id as oc.time for oc in #{@order_cycles.map {|oc| {time: pickup_time(oc), id: oc.id}}.to_json}", "popover-placement" => "left", "popover" => t(:enterprises_choose), "popover-trigger" => "openTrigger"} @@ -31,7 +32,7 @@ = render partial: 'shop/messages' - .row - = render partial: "shop/products/form" + - unless require_login? + .row= render partial: "shop/products/form" = render partial: "shared/footer" diff --git a/app/views/shop/_messages.html.haml b/app/views/shop/_messages.html.haml index 1e56aba468..3082d05c12 100644 --- a/app/views/shop/_messages.html.haml +++ b/app/views/shop/_messages.html.haml @@ -1,5 +1,12 @@ -- if @order_cycles and @order_cycles.empty? +- if require_login? + .row.footer-pad + .small-12.columns + .shopfront_closed_message + = t '.require_login_html', + {login: link_to(t('.login'), login_path), + register: link_to(t('.register'), '/register')} +- elsif @order_cycles and @order_cycles.empty? - if current_distributor.preferred_shopfront_closed_message.present? .row .small-12.columns diff --git a/config/locales/en.yml b/config/locales/en.yml index 323288edc2..6db986d7b5 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -105,6 +105,20 @@ en: enterprise: select_outgoing_oc_products_from: Select outgoing OC products from + enterprises: + form: + primary_details: + shopfront_requires_login: "Shopfront requires login?" + shopfront_requires_login_tip: "Choose whether viewing the shopfront requires to login or not." + shopfront_requires_login_false: "Public" + shopfront_requires_login_true: "Require customers to login" + + shop: + messages: + login: "login" + register: "register" + require_login_html: "Please %{login} if you have an account already. Otherwise, %{register} to become a customer." + # Printable Invoice Columns invoice_column_item: "Item" invoice_column_qty: "Qty" diff --git a/db/migrate/20160316051131_add_require_login_to_enterprise.rb b/db/migrate/20160316051131_add_require_login_to_enterprise.rb new file mode 100644 index 0000000000..68de642b62 --- /dev/null +++ b/db/migrate/20160316051131_add_require_login_to_enterprise.rb @@ -0,0 +1,5 @@ +class AddRequireLoginToEnterprise < ActiveRecord::Migration + def change + add_column :enterprises, :require_login, :boolean, default: false, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index cdab93834c..9994617a5f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20160302044850) do +ActiveRecord::Schema.define(:version => 20160316051131) do create_table "account_invoices", :force => true do |t| t.integer "user_id", :null => false @@ -348,6 +348,7 @@ ActiveRecord::Schema.define(:version => 20160302044850) do t.string "permalink", :null => false t.boolean "charges_sales_tax", :default => false, :null => false t.string "email_address" + t.boolean "require_login", :default => false, :null => false end add_index "enterprises", ["address_id"], :name => "index_enterprises_on_address_id" diff --git a/spec/features/admin/enterprises_spec.rb b/spec/features/admin/enterprises_spec.rb index 92ac89015e..b72258f7f9 100644 --- a/spec/features/admin/enterprises_spec.rb +++ b/spec/features/admin/enterprises_spec.rb @@ -82,6 +82,10 @@ feature %q{ page.should have_selector '.available' choose 'Own' + # Require login to view shopfront + expect(page).to have_checked_field "enterprise_require_login_false" + choose "Require customers to login" + within (".side_menu") { click_link "Users" } select2_search user.email, from: 'Owner' @@ -162,6 +166,8 @@ feature %q{ page.should have_field 'enterprise_name', :with => 'Eaterprises' @enterprise.reload expect(@enterprise.owner).to eq user + expect(page).to have_checked_field "enterprise_visible_true" + expect(page).to have_checked_field "enterprise_require_login_true" click_link "Business Details" page.should have_checked_field "enterprise_charges_sales_tax_true" diff --git a/spec/features/consumer/shopping/shopping_spec.rb b/spec/features/consumer/shopping/shopping_spec.rb index e81a792881..5b8bb5881f 100644 --- a/spec/features/consumer/shopping/shopping_spec.rb +++ b/spec/features/consumer/shopping/shopping_spec.rb @@ -253,5 +253,26 @@ feature "As a consumer I want to shop with a distributor", js: true do page.should have_content "The next cycle opens in 10 days" end end + + context "when shopping requires to login" do + let(:exchange) { Exchange.find(oc1.exchanges.to_enterprises(distributor).outgoing.first.id) } + let(:product) { create(:simple_product) } + let(:variant) { create(:variant, product: product) } + + before do + add_product_and_variant_to_order_cycle(exchange, product, variant) + set_order_cycle(order, oc1) + distributor.require_login = true + distributor.save! + visit shop_path + end + + it "tells us to login" do + expect(page).to have_content "Please login" + end + it "does not show products" do + expect(page).to have_no_content product.name + end + end end end