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.
This commit is contained in:
Maikel Linke
2016-03-18 14:28:47 +11:00
parent 599e39ce16
commit a48b992ec0
9 changed files with 74 additions and 10 deletions

View File

@@ -7,4 +7,8 @@ module ShopHelper
]
end
end
def require_login?
current_distributor.require_login? && spree_current_user.nil?
end
end

View File

@@ -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

View File

@@ -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"

View File

@@ -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

View File

@@ -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"

View File

@@ -0,0 +1,5 @@
class AddRequireLoginToEnterprise < ActiveRecord::Migration
def change
add_column :enterprises, :require_login, :boolean, default: false, null: false
end
end

View File

@@ -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"

View File

@@ -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"

View File

@@ -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