-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthlogic.rb
418 lines (360 loc) · 11.6 KB
/
authlogic.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
gem 'authlogic', :source => 'http://gems.github.com'
gem 'collectiveidea-delayed_job', :lib => 'delayed_job', :source => 'http://gems.github.com'
file 'app/models/user_session.rb', <<-CODE
class UserSession < Authlogic::Session::Base
generalize_credentials_error_messages true
end
CODE
file "db/migrate/#{Time.now.utc.strftime("%Y%m%d%H%M%S")}_create_users.rb", <<-CODE
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :email, :null => false
t.string :crypted_password, :null => false
t.string :password_salt, :null => false
t.string :persistence_token, :null => false
t.string :single_access_token, :null => false
t.string :perishable_token, :null => false
t.integer :login_count, :null => false, :default => 0
t.integer :failed_login_count, :null => false, :default => 0
t.datetime :last_request_at
t.datetime :current_login_at
t.datetime :last_login_at
t.string :current_login_ip
t.string :last_login_ip
t.timestamps
end
end
def self.down
drop_table :users
end
end
CODE
file 'app/controllers/user_sessions_controller.rb', <<-CODE
class UserSessionsController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => :destroy
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:success] = "Login successful!"
redirect_back_or_default account_path
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:success] = "Logout successful!"
redirect_back_or_default login_path
end
end
CODE
file 'app/controllers/users_controller.rb', <<-CODE
class UsersController < InheritedResources::Base
defaults :singleton => true
before_filter :require_user, :except => [:new, :create]
before_filter :require_no_user, :only => [:new, :create]
def create
@user = User.new(params[:user])
@user.email = params[:user][:email]
if @user.save
flash[:success] = 'Your account has been created!'
Notifier.send_later :deliver_welcome, @user
redirect_to(account_path)
else
render :action => "new"
end
end
def update
update! do |success, failure|
success.html { redirect_to account_path }
end
end
def destroy
destroy! do |success, failure|
success.html { redirect_to login_path }
end
end
private
def resource
@user ||= current_user
end
end
CODE
gsub_file 'app/controllers/application_controller.rb', /^ private$/, <<-CODE
helper_method :current_user_session, :current_user
filter_parameter_logging :password, :password_confirmation
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to login_path
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to account_path
return false
end
end
def store_location
session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
CODE
file 'app/models/user.rb', <<-CODE
class User < ActiveRecord::Base
acts_as_authentic
attr_accessible :password, :password_confirmation
def deliver_password_reset_instructions!
reset_perishable_token!
Notifier.deliver_password_reset_instructions(self)
end
handle_asynchronously :deliver_password_reset_instructions!
end
CODE
file 'app/models/user_session.rb', <<-CODE
class UserSession < Authlogic::Session::Base
generalize_credentials_error_messages true
end
CODE
file 'app/views/user_sessions/new.haml', <<-CODE
%h1 Login
- form_for @user_session, :url => {:action => :create} do |f|
= f.error_messages
= f.label :email
%br
= f.text_field :email
%br
= f.label :password
%br
= f.password_field :password
%br
%br
= f.check_box :remember_me
= f.label :remember_me
%br
%br
= f.submit "Login"
%br
%br
= link_to "Forgot Password", password_resets_path
CODE
file 'app/views/users/_form.haml', <<-CODE
= form.label :password
%br
= form.password_field :password
%br
= form.label :password_confirmation
%br
= form.password_field :password_confirmation
%br
CODE
file 'app/views/users/edit.haml', <<-CODE
%h1 Editing Account
- form_for(@user, :url => account_path) do |f|
= f.error_messages
= render :partial => "form", :object => f
= f.submit "Save Changes"
.links
= link_to 'View Account', account_path
= link_to 'Delete Account', account_path, :confirm => 'Are you sure?', :method => :delete
CODE
file 'app/views/users/new.haml', <<-CODE
%h1 Create Account
- form_for(@user, :url => account_path) do |f|
= f.error_messages
= f.label :email
%br
= f.text_field :email
%br
= render :partial => "form", :object => f
= f.submit "Create Account"
CODE
file 'app/views/users/show.haml', <<-CODE
%h1 Account Details
- content_tag_for :div, @user, :class => 'show' do
.field Email:
.value=h @user.email
.links
= link_to 'Edit Account', edit_account_path
= link_to 'Delete Account', account_path, :confirm => 'Are you sure?', :method => :delete
CODE
file 'app/views/shared/_header.haml', <<-CODE
= link_to 'Home', '/'
%span{:style => 'float: right;'}
- if current_user
=h "You are logged in as \#{current_user.email}."
|
= link_to 'user settings', edit_account_path
|
= link_to 'logout', logout_path
- else
= link_to 'login', login_path
|
= link_to 'register', register_path
CODE
file 'config/routes.rb', <<-CODE
ActionController::Routing::Routes.draw do |map|
map.login '/', :controller => "user_sessions", :action => "new", :conditions => {:method => :get}
map.logout 'logout', :controller => "user_sessions", :action => "destroy"
map.register 'register', :controller => "users", :action => "new"
map.resources :password_resets, :as => 'reset_password', :only => [:index, :create, :show, :update]
map.resource :login, :controller => "user_sessions", :only => [:new, :create, :destroy]
map.resource :account, :controller => "users"
end
CODE
file 'script/delayed_job', <<-CODE
#!/usr/bin/env ruby
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
require 'delayed/command'
Delayed::Command.new(ARGV).daemonize
CODE
require 'fileutils'
FileUtils.chmod 0755, 'script/delayed_job'
file "db/migrate/#{(Time.now.utc+1).strftime("%Y%m%d%H%M%S")}_create_delayed_jobs.rb", <<-CODE
class CreateDelayedJobs < ActiveRecord::Migration
def self.up
create_table :delayed_jobs, :force => true do |table|
table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
table.text :handler # YAML-encoded string of the object that will do work
table.text :last_error # reason for last failure (See Note below)
table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future.
table.datetime :locked_at # Set when a client is working on this object
table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
table.string :locked_by # Who is working on this object (if locked)
table.timestamps
end
end
def self.down
drop_table :delayed_jobs
end
end
CODE
file 'app/models/notifier.rb', <<-CODE
class Notifier < ActionMailer::Base
SYSTEM_ACCOUNT = %("#{APPLICATION.titlecase}" <#{SYSTEM_SENDER_EMAIL}>)
def welcome(user)
subject 'Welcome to #{APPLICATION.titlecase}'
recipients user.email
from SYSTEM_ACCOUNT
sent_on Time.zone.now
content_type "multipart/alternative"
part :content_type => "text/plain", :body => render_message("welcome.text.plain", {})
part :content_type => "text/html", :body => render_message("welcome.text.html", {})
end
def password_reset_instructions(user)
subject "Password Reset Instructions"
from SYSTEM_ACCOUNT
recipients user.email
sent_on Time.zone.now
body :url => password_reset_url(user.perishable_token)
end
end
CODE
file 'app/views/notifier/password_reset_instructions.erb', <<-CODE
A request to reset your password has been made. If you did not make this request, simply ignore this email. If you did make this request just click the link below:
<%= @url %>
If the above URL does not work try copying and pasting it into your browser. If you continue to have problems, please feel free to contact us.
CODE
file 'app/views/notifier/welcome.text.html.erb', <<-CODE
Welcome to #{APPLICATION.titlecase}!<br>
<br>
Login with your e-mail address and password at:<br>
<%= link_to login_url, login_url %>
CODE
file 'app/views/notifier/welcome.text.plain.erb', <<-CODE
Welcome to #{APPLICATION.titlecase}!
Login with your e-mail address and password at:
<%= login_url %>
CODE
file 'app/controllers/password_resets_controller.rb', <<-CODE
class PasswordResetsController < ApplicationController
before_filter :load_user_using_perishable_token, :only => [:show, :update]
before_filter :require_no_user
def create
@user = User.find_by_email(params[:email])
if @user
@user.deliver_password_reset_instructions!
flash[:notice] = "Instructions to reset your password have been emailed to you. Please check your email."
redirect_to login_path
else
flash[:notice] = "No user was found with that email address."
render :action => :index
end
end
def update
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][:password_confirmation]
if @user.save
flash[:notice] = "Password successfully updated."
redirect_to account_url
else
render :controller => :users, :action => :show
end
end
private
def load_user_using_perishable_token
@user = User.find_using_perishable_token(params[:id])
unless @user
flash[:notice] = "We're sorry, but we could not locate your account." +
"If you are having issues try copying and pasting the URL " +
"from your email into your browser or restarting the " +
"reset password process."
redirect_to login_path
end
end
end
CODE
file 'app/views/password_resets/index.haml', <<-CODE
%h1 Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
%br
%br
- form_tag password_resets_path do
%label Email:
%br
= text_field_tag "email"
%br
%br
= submit_tag "Reset my password"
%br
%br
= link_to "Back to Login", login_path
CODE
file 'app/views/password_resets/show.haml', <<-CODE
%h1 Forgot Password: Change My Password
- form_for @user, :url => password_reset_path, :method => :put do |f|
= f.error_messages
= f.label :password
%br
= f.password_field :password
%br
%br
= f.label :password_confirmation
%br
= f.password_field :password_confirmation
%br
%br
= f.submit "Update my password and log me in"
CODE