-
Notifications
You must be signed in to change notification settings - Fork 92
/
user.rb
188 lines (153 loc) · 6.09 KB
/
user.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
module Hobo
module Controller
module User
class << self
def included(base)
base.class_eval do
extend ClassMethods
class << self
alias_method_chain :available_auto_actions, :user_actions
alias_method_chain :def_auto_actions, :user_actions
end
skip_before_filter :login_required, :only => [:login, :signup, :forgot_password, :reset_password, :do_reset_password,
:accept_invitation, :do_accept_invitation]
include_taglib "rapid_user_pages", :plugin => "hobo"
alias_method_chain :hobo_update, :account_flash
end
end
end
module ClassMethods
def available_auto_actions_with_user_actions
available_auto_actions_without_user_actions +
[:login, :logout, :forgot_password, :reset_password, :account]
end
def def_auto_actions_with_user_actions
def_auto_actions_without_user_actions
class_eval do
def login; hobo_login; end if include_action?(:login)
def logout; hobo_logout; end if include_action?(:logout)
def signup; hobo_signup; end if include_action?(:signup)
def do_signup; hobo_do_signup end if include_action?(:do_signup)
def forgot_password; hobo_forgot_password; end if include_action?(:forgot_password)
def do_reset_password; hobo_do_reset_password; end if include_action?(:do_reset_password)
show_action :account if include_action?(:account)
end
end
end
private
def hobo_login(options={})
if logged_in?
respond_to do |wants|
wants.html { redirect_to home_page }
wants.js { hobo_ajax_response }
end
return
end
login_attr = model.human_attribute_name(model.login_attribute)
options.reverse_merge!(:success_notice => ht(:"#{model.to_s.underscore}.messages.login.success", :default=>["You have logged in."]),
:failure_notice => ht(:"#{model.to_s.underscore}.messages.login.error", :login=>login_attr, :default=>["You did not provide a valid #{login_attr} and password."]))
if request.post?
user = model.authenticate(params[:login], params[:password])
if user.nil?
flash[:error] = options[:failure_notice]
hobo_ajax_response if request.xhr? && !performed?
else
old_user = current_user
self.current_user = user
yield if block_given?
if !user.account_active?
# account not activate - cancel this login
self.current_user = old_user
unless performed?
respond_to do |wants|
wants.html {render :action => :account_disabled}
wants.js {hobo_ajax_response}
end
end
else
if params[:remember_me].present?
current_user.remember_me
create_auth_cookie
end
flash[:notice] ||= options[:success_notice]
unless performed?
respond_to do |wants|
wants.html {redirect_back_or_default(options[:redirect_to] || home_page) }
wants.js {hobo_ajax_response}
end
end
end
end
end
end
def hobo_signup(&b)
if logged_in?
redirect_back_or_default(home_page)
else
creator_page_action(:signup, &b)
end
end
def hobo_do_signup(&b)
do_creator_action(:signup) do
if valid?
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.signup.success", :default=>["Thanks for signing up!"])
end
response_block(&b) or if valid?
self.current_user = this if this.account_active?
respond_to do |wants|
wants.html { redirect_back_or_default(home_page) }
wants.js { hobo_ajax_response }
end
end
end
end
def hobo_logout(options={})
options = options.reverse_merge(:notice => ht(:"#{model.to_s.underscore}.messages.logout", :default=>["You have logged out."]),
:redirect_to => base_url)
logout_current_user
yield if block_given?
flash[:notice] ||= options[:notice]
redirect_back_or_default(options[:redirect_to]) unless performed?
end
def hobo_forgot_password
if request.post?
user = model.find_by_email_address(params[:email_address])
if user && (!block_given? || yield(user))
user.lifecycle.request_password_reset!(:nobody)
end
respond_to do |wants|
wants.html { render :forgot_password_email_sent }
wants.js { hobo_ajax_response}
end
end
end
def hobo_do_reset_password(&b)
do_transition_action :reset_password do
response_block(&b) or if valid?
self.current_user = this
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.reset_password", :default=>["Your password has been reset"])
respond_to do |wants|
wants.html { redirect_to(home_page) }
wants.js { hobo_ajax_response }
end
end
end
end
def hobo_update_with_account_flash(*args)
hobo_update_without_account_flash(*args) do
flash[:notice] = ht(:"#{model.to_s.underscore}.messages.update.success", :default=>["Changes to your account were saved"]) if valid? && @this == current_user
yield if block_given?
end
end
private
def logout_current_user
if logged_in?
current_user.forget_me
cookies.delete :auth_token
reset_session
self.current_user = nil
end
end
end
end
end