Class: BBMB::Html::State::Customer

Inherits:
Global
  • Object
show all
Defined in:
lib/bbmb/html/state/customer.rb

Constant Summary collapse

VIEW =
View::Customer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Global

#direct_arguments, #direct_request?, #initialize, #logout, mandatory, #requested_event, #trigger, #user_input

Constructor Details

This class inherits a constructor from BBMB::Html::State::Global

Instance Attribute Details

#cleartextObject (readonly)

Returns the value of attribute cleartext.



14
15
16
# File 'lib/bbmb/html/state/customer.rb', line 14

def cleartext
  @cleartext
end

Instance Method Details

#_saveObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bbmb/html/state/customer.rb', line 59

def _save
  keys = mandatory + [ :ean13, :title, :drtitle, :lastname, :firstname,
                       :address2, :address3, :plz, :city, :canton,
                       :phone_business, :phone_private, :phone_mobile, :fax ]
  if BBMB.config.mail_confirm_reply_to
    keys.push :order_confirmation
  end
  input = user_input(keys, mandatory)
  update_user(input)
  if(error?)
    @errors.store(:error, create_error(:error, :error, nil))
    @errors.store(:user, create_error(:e_user_unsaved, :error, nil))
  else
    input.each { |key, val|
      writer = "#{key}="
      if(@model.respond_to?(writer) && @model.send(key) != val)
        @model.send(writer, val)
        @model.protect!(key)
      end
    }
    BBMB.persistence.save(@model)
  end
end

#direct_argument_keysObject



20
21
22
# File 'lib/bbmb/html/state/customer.rb', line 20

def direct_argument_keys
  [:customer_id]
end

#direct_eventObject



23
24
25
26
27
# File 'lib/bbmb/html/state/customer.rb', line 23

def direct_event
  unless(error? || @session.event == :change_pass)
    [ :customer, {:customer_id => @model.customer_id} ]
  end
end

#generate_passObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/bbmb/html/state/customer.rb', line 28

def generate_pass
  _save
  if(email = @model.email)
    @cleartext = BBMB::Util::PasswordGenerator.generate(@model)
    passhash = @session.validate(:pass, @cleartext)
    begin
      @session.auth_session.grant(email, 'login',
                        BBMB.config.auth_domain + '.Customer')
      @session.auth_session.set_password(email, passhash)
    rescue Yus::YusError
      @errors.store(:pass, create_error(:e_pass_not_set, :pass, nil))
    end
  else
    error = create_error(:e_email_required, :email, @model.email)
    @errors.store(:email, error)
  end
  self
end

#initObject



17
18
19
# File 'lib/bbmb/html/state/customer.rb', line 17

def init
  @model = Model::Customer.find_by_customer_id(@session.user_input(:customer_id))
end

#mandatoryObject



46
47
48
49
50
51
52
53
# File 'lib/bbmb/html/state/customer.rb', line 46

def mandatory
  mandatory = _mandatory
  if(@session.user_input(:pass) \
     || @session.user_input(:confirm_pass))
    mandatory += [:pass, :confirm_pass]
  end
  mandatory
end

#saveObject Also known as: change_pass



54
55
56
57
# File 'lib/bbmb/html/state/customer.rb', line 54

def save
  _save
  self
end

#show_passObject



82
83
84
85
86
87
88
89
# File 'lib/bbmb/html/state/customer.rb', line 82

def show_pass
  model = OpenStruct.new
  model.cleartext = @cleartext
  model.email = @model.email
  model.address_lines = @model.address_lines
  @cleartext = nil
  ShowPass.new(@session, model)
end

#update_user(input) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/bbmb/html/state/customer.rb', line 90

def update_user(input)
  email = input.delete(:email)
  unless email
    error = create_error(:e_email_required, :email, @model.email)
    return
  end
  @model.email = email
  @model.protect!(:email)
  if(passhash = input.delete(:confirm_pass))
    begin
      @session.auth_session.grant(email, 'login',
                        BBMB.config.auth_domain + '.Customer')
      @session.auth_session.set_password(email, passhash)
    rescue Yus::YusError => e
      @errors.store(:pass, create_error(:e_pass_not_set, :email, email))
    end
  end
rescue Yus::YusError => e
  @errors.store(:email, create_error(:e_duplicate_email, :email, email))
end