From e5720cfca7d798dcf5e98c4916de0907b6a9bd09 Mon Sep 17 00:00:00 2001 From: Niklaus Giger Date: Tue, 9 May 2017 14:05:38 +0200 Subject: [PATCH 2/2] BBMB: Changes needed for rack, viral modules and KnownUser --- lib/bbmb/html/state/customers.rb | 4 ++- lib/bbmb/html/state/global.rb | 6 +++-- lib/bbmb/html/state/init.rb | 1 + lib/bbmb/html/state/login.rb | 6 ++++- lib/bbmb/html/state/viral/admin.rb | 5 ++-- lib/bbmb/html/state/viral/customer.rb | 10 +++----- lib/bbmb/html/util/known_user.rb | 46 +++++++++++++++++++++++++++++------ lib/bbmb/html/util/session.rb | 44 ++++++++++++++++++++++++++++----- lib/bbmb/html/view/customers.rb | 14 ++++++++--- lib/bbmb/persistence/odba.rb | 8 +++--- lib/bbmb/util/server.rb | 13 +++++++++- 11 files changed, 122 insertions(+), 35 deletions(-) diff --git a/lib/bbmb/html/state/customers.rb b/lib/bbmb/html/state/customers.rb index 3ec3959..28cae94 100644 --- a/lib/bbmb/html/state/customers.rb +++ b/lib/bbmb/html/state/customers.rb @@ -51,7 +51,7 @@ module BBMB def page(model) index = @session.user_input(:index).to_i - step = @session.user.pagestep || BBMB.config.pagestep + step = @session.user.get_preference(:pagestep) || BBMB.config.pagestep page = OpenStruct.new page.index = index page.first = index + 1 @@ -72,10 +72,12 @@ module BBMB end def last_login + binding.pry @last_login ||= @session.user.last_login(email) end def valid + binding.pry @valid ||= @session.user.entity_valid?(email).to_s end end diff --git a/lib/bbmb/html/state/global.rb b/lib/bbmb/html/state/global.rb index 8ba91d3..1f983f6 100644 --- a/lib/bbmb/html/state/global.rb +++ b/lib/bbmb/html/state/global.rb @@ -22,10 +22,12 @@ module BBMB module Html module State class Global < SBSM::State - def initialize(first, second) - super(first, second) + def initialize(session, model) + SBSM.info "BBMB::Html::State Global session #{session.class} model #{model.class}" + super(session, model) end def logout + SBSM.info "BBMB::Html::State Global logout" @session.logout State::Login.new(@session, nil) end diff --git a/lib/bbmb/html/state/init.rb b/lib/bbmb/html/state/init.rb index fe8ce43..be0a035 100644 --- a/lib/bbmb/html/state/init.rb +++ b/lib/bbmb/html/state/init.rb @@ -10,6 +10,7 @@ module BBMB class Init < SBSM::State VIEW = Html::View::LoginForm def login; require 'pry'; binding.pry + SBSM.info "BBMB::Html::Init login #{user_input(:email)} #{user_input(:pass)} #{@user.class}" if(res = @session.login) Customers.new(@session, nil) end diff --git a/lib/bbmb/html/state/login.rb b/lib/bbmb/html/state/login.rb index 333a655..5434834 100644 --- a/lib/bbmb/html/state/login.rb +++ b/lib/bbmb/html/state/login.rb @@ -14,7 +14,9 @@ module BBMB module State class Login < SBSM::State VIEW = View::Login - def login; require 'pry'; binding.pry + def login + # is called from sbsm + SBSM.info "BBMB::Html::State Login login #{user_input(:email)} #{user_input(:pass)} #{@user.class}" reconsider_permissions(@session.login) trigger(:home) rescue Yus::UnknownEntityError @@ -65,7 +67,9 @@ TVS/Virbac-Nr: #{input[:customer_id]} ['.Admin', State::Viral::Admin], ['.Customer', State::Viral::Customer], ].each { |key, mod| + puts "viral_modules trying #{key} #{mod}" if(user.allowed?("login", BBMB.config.auth_domain + key)) + puts "Was allowed via user #{user} for #{mod}" yield mod end } diff --git a/lib/bbmb/html/state/viral/admin.rb b/lib/bbmb/html/state/viral/admin.rb index bfbced8..88559a8 100644 --- a/lib/bbmb/html/state/viral/admin.rb +++ b/lib/bbmb/html/state/viral/admin.rb @@ -24,8 +24,8 @@ module Admin Model::Customer.find_by_customer_id(customer_id) end def home - binding.pry - trigger(@session.user.home || :customers) + home = @session.user.get_preference(:home) || :customers + trigger(home) end def order binding.pry @@ -35,7 +35,6 @@ module Admin end end def zone_navigation - binding.pry [:customers] end end diff --git a/lib/bbmb/html/state/viral/customer.rb b/lib/bbmb/html/state/viral/customer.rb index b8a8710..660e1d1 100644 --- a/lib/bbmb/html/state/viral/customer.rb +++ b/lib/bbmb/html/state/viral/customer.rb @@ -23,8 +23,7 @@ module Customer :search_favorites => State::FavoritesResult, } def _customer - binding.pry - @customer ||= Model::Customer.find_by_email(@session.user.name) + @customer ||= Model::Customer.find_by_email(@session.auth_session.name) end def _increment_order(order) quantities = user_input(:quantity) @@ -59,7 +58,7 @@ module Customer false else quantities.each { |article_number, quantity| - order.add(quantity.to_i, + order.add(quantity.to_i, Model::Product.find_by_article_number(article_number)) } BBMB.persistence.save(order, _customer) @@ -92,8 +91,8 @@ module Customer _transfer(_customer.favorites) end def home - binding.pry - trigger(@session.user.home || :current_order) + home = @session.user.get_preference(:home) || :current_order + trigger(home) end def increment_order if(_increment_order(_customer.current_order)) @@ -136,7 +135,6 @@ module Customer State::Json.new(@session, {:success => success}) end def zone_navigation - binding.pry [ :current_order, :orders, :favorites ] end end diff --git a/lib/bbmb/html/util/known_user.rb b/lib/bbmb/html/util/known_user.rb index c280457..fea39e0 100644 --- a/lib/bbmb/html/util/known_user.rb +++ b/lib/bbmb/html/util/known_user.rb @@ -21,32 +21,64 @@ class KnownUser < SBSM::User define_method(key) { remote_call(:get_preference, key) } - } + } if false def initialize(session) - puts "BBMB::Html::Util::KnownUser.new" - @auth_session = session + puts "BBMB::Html::Util::KnownUser.new object_id is #{self.object_id} SBSM::Session ? #{self.is_a? SBSM::Session} auth_session is #{session.class}" + @auth_session = session.auth_session + # puts "backtrace #{caller.join("\n")}" + # @auth_session.auth.allowed?('edit', 'yus.entities') end def allowed?(action, key=nil) + if @auth_session + return @auth_session.allowed?(action, key) + return @auth_session.remote_call(:allowed?, action, key) + end + SBSM.debug('User ' + sprintf('allowed?(%s, %s)', action, key)) + if defined?(yus_user) && yus_user + allowed = yus_user.send(:allowed?, action, key) + end if false + binding.pry # if action.to_s.eql?('yus_user') + return true + # SBSM.debug('User'+ sprintf('allowed?(%s, %s) -> %s', action, key, allowed)) + return allowed + # session.rb:25:in `login' BBMB::Html::Util::Session login claude.meier@gmx.net 5972659ce6c7f9b2356c0e650c7c40a3 allowed = remote_call(:allowed?, action, key) SBSM.debug('User') { sprintf('%s: allowed?(%s, %s) -> %s', name, action, key, allowed) } allowed + rescue => error + binding.pry end def entity_valid?(email) - !!(allowed?('edit', 'yus.entities') \ - && (entity = remote_call(:find_entity, email)) && entity.valid?) + !!(@auth_session.allowed?('edit', 'yus.entities') \ + && (entity = @auth_session.find_entity(email)) && entity.valid?) end def navigation puts "BBMB::Html::Util::KnownUser navigation returning [ :logout ]" [ :logout ] end + def get_preference(key) + return @auth_session.get_preference(key.to_s) + binding.pry + remote_call(:get_preference, key) + end def remote_call(method, *args, &block) - @auth_session.send(method, *args, &block) + # binding.pry # auth.login ArgumentError: wrong number of arguments (given 0, expected 3) + SBSM.debug("remote_call #{method} args #{args} block.nil? #{block.nil?}") + if defined?(@auth_session) && @auth_session.is_a?(DRb::DRbObject) + return @auth_session.send(method, *args, &block) + else + return false + return super(method, *args, &block) + end rescue RangeError, DRb::DRbError => e SBSM.info('auth') { e } + rescue error + puts error + require 'pry'; binding.pry end - alias :method_missing :remote_call + # alias :method_missing :remote_call end end end diff --git a/lib/bbmb/html/util/session.rb b/lib/bbmb/html/util/session.rb index 5c85f72..7de6af9 100644 --- a/lib/bbmb/html/util/session.rb +++ b/lib/bbmb/html/util/session.rb @@ -1,6 +1,7 @@ #!/usr/bin/env ruby # encoding: utf-8 +require 'uri' require 'bbmb/config' require 'sbsm/session' require 'bbmb/html/state/global' @@ -16,15 +17,41 @@ class Session < SBSM::Session DEFAULT_STATE = State::Login EXPIRES = BBMB.config.session_timeout PERSISTENT_COOKIE_NAME = "bbmb-barcodereader" - def login; require 'pry'; binding.pry - SBSM.info "BBMB::Html::Util::Session login #{user_input(:email)} #{user_input(:pass)}" - @user = @app.auth.login(user_input(:email), user_input(:pass)) + if uri = URI.parse(BBMB.config.http_server) + SERVER_NAME = uri.host + end + attr_reader :email, :pass, :auth_session + def initialize(app:, cookie_name:, trans_handler:, validator:, unknown_user:) + super + # @user = BBMB::Html::Util::KnownUser.new(self) + end + def login + @email = user_input(:email) + @password = user_input(:pass) @user.session = self if(@user.respond_to?(:session=)) + # Before rack: @user = @app.login(user_input(:email), user_input(:pass)) + # gets now NoMethodError: undefined method `login' for nil:NilClass + + # undefined + # @user = @app.auth.login(user_input(:email), user_input(:pass)) + # ArgumentError: wrong number of arguments (given 2, expected 3) + # from (druby://virbac.bbmb.ngiger.ch:12003) /usr/local/ruby-2.4.0/lib/ruby/gems/2.4.0/gems/yus-1.0.4/lib/yus/server.rb:24:in `login' + puts "session= defined? #{@user.respond_to?(:session=)}" + # @auth_session = @app.auth.login(user_input(:email), user_input(:pass), BBMB.config.auth_domain) + @auth_session = @app.auth.login(user_input(:email), user_input(:pass), BBMB.config.auth_domain) # logs in claude meier without problem, but not admin + if @auth_session.valid? + @user = BBMB::Html::Util::KnownUser.new(self) # TODO:Should we set it already in the initialize method? + else + @user = SBSM::UnknownUser + end + SBSM.info "BBMB::Html::Util::Session login #{user_input(:email)} #{user_input(:pass)} #{@user.class} auth_session #{@auth_session.class}" + #unless @user.is_a?(BBMB::Html::Util::KnownUser) @user end - def logout; require 'pry'; binding.pry - SBSM.info "BBMB::Html::Util::Session logout " - @app.auth.logout(@user.auth_session) if(@user.respond_to?(:auth_session)) + def logout + SBSM.info "BBMB::Html::Util::Session logout @auth_session #{@auth_session.class}" + $stdout.sync = true + @app.logout(@user.auth_session) if(@user.respond_to?(:auth_session)) super end @@ -50,6 +77,11 @@ class Session < SBSM::Session def validate(key, value) @validator.validate(key, value) end + def remote_call(method, *args, &block) + @yus_user.send(method, *args, &block) + rescue RangeError, DRb::DRbError => e + BBMB.logger.error('auth') { e } + end end end end diff --git a/lib/bbmb/html/view/customers.rb b/lib/bbmb/html/view/customers.rb index d412a5e..2ecccfd 100644 --- a/lib/bbmb/html/view/customers.rb +++ b/lib/bbmb/html/view/customers.rb @@ -64,14 +64,14 @@ class CustomersList < HtmlGrid::List def customer_id(model) link = HtmlGrid::Link.new(:customer_id, model, @session, self) link.value = model.customer_id - link.href = @lookandfeel._event_url(:customer, + link.href = @lookandfeel._event_url(:customer, {:customer_id => model.customer_id}) link end def organisation(model) link = HtmlGrid::Link.new(:organisation, model, @session, self) link.value = model.organisation - link.href = @lookandfeel._event_url(:customer, + link.href = @lookandfeel._event_url(:customer, {:customer_id => model.customer_id}) link end @@ -93,20 +93,26 @@ class CustomersList < HtmlGrid::List def active(model) model.value(:active) end -=end +=end def last_login(model) if model.respond_to?(:last_login) model.last_login else - @session.user.last_login(model.email) + @session.auth_session.last_login(model.email) end end def valid(model) + # old = @session.user.entity_valid?(model.email).to_s) if model.respond_to?(:valid) @lookandfeel.lookup(model.valid) + elsif @session.auth_session && defined?(@session.auth_session.entity_valid?) + @session.auth_session.entity_valid?(model.email) else @lookandfeel.lookup(@session.user.entity_valid?(model.email).to_s) end + rescue => error + puts error + binding.pry end private def sort_link(header_key, matrix, component) diff --git a/lib/bbmb/persistence/odba.rb b/lib/bbmb/persistence/odba.rb index d82ca00..ac07518 100644 --- a/lib/bbmb/persistence/odba.rb +++ b/lib/bbmb/persistence/odba.rb @@ -24,12 +24,12 @@ module BBMB objs.each { |obj| obj.odba_delete } end def ODBA.migrate_to_subject - all(Model::Product) { |product| - product.migrate_to_subject && product.odba_store + all(Model::Product) { |product| + product.migrate_to_subject && product.odba_store } all(Model::Order) { |order| - order.each { |position| - position.migrate_to_subject && position.odba_store + order.each { |position| + position.migrate_to_subject && position.odba_store } } ::ODBA.cache.create_deferred_indices(true) diff --git a/lib/bbmb/util/server.rb b/lib/bbmb/util/server.rb index 0206620..80c0718 100644 --- a/lib/bbmb/util/server.rb +++ b/lib/bbmb/util/server.rb @@ -6,14 +6,23 @@ require 'bbmb/html/util/known_user' require 'bbmb/html/util/session' require 'bbmb/html/util/validator' require 'bbmb/util/invoicer' +require 'bbmb/util/invoicer' require 'bbmb/util/mail' require 'bbmb/util/updater' require 'bbmb/model/order' # needed to be enable to invoice later require 'bbmb/model/customer' require 'date' require 'sbsm/app' +require 'bbmb/persistence/odba' +require 'bbmb/model/customer' +require 'bbmb/model/quota' +require 'bbmb/model/product' +require 'bbmb/model/promotion' module BBMB + def self.persistence + @@persistence ||= BBMB::Persistence::ODBA + end module Util class RackInterface < SBSM::RackInterface ENABLE_ADMIN = true @@ -25,11 +34,11 @@ module BBMB validator: BBMB::Html::Util::Validator) @auth = auth @app = app - binding.pry super(app: app, session_class: BBMB::Html::Util::Session, unknown_user: Html::Util::KnownUser, validator: validator, + auth: auth, cookie_name: 'virbac.bbmb' ) end @@ -102,6 +111,7 @@ module BBMB Html::Util::KnownUser.new(session) end def logout(session) + require 'pry'; binding.pry BBMB.auth.logout(session) rescue DRb::DRbError, RangeError, NameError end @@ -196,6 +206,7 @@ module BBMB Html::Util::KnownUser.new(session) end def logout(session) + # Here we start when logging in from the home page BBMB.auth.logout(session) rescue DRb::DRbError, RangeError, NameError end -- 2.10.2