thoughtbot-clearance 0.2.5 → 0.2.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +8 -16
- data/Rakefile +12 -10
- data/TODO.textile +7 -4
- data/generators/clearance/templates/app/controllers/application.rb +1 -1
- data/generators/clearance/templates/app/controllers/confirmations_controller.rb +1 -1
- data/generators/clearance/templates/app/controllers/passwords_controller.rb +1 -1
- data/generators/clearance/templates/app/controllers/sessions_controller.rb +1 -1
- data/generators/clearance/templates/app/controllers/users_controller.rb +1 -1
- data/generators/clearance/templates/app/models/user.rb +1 -1
- data/generators/clearance/templates/app/models/user_mailer.rb +1 -1
- data/generators/clearance/templates/test/functional/confirmations_controller_test.rb +1 -1
- data/generators/clearance/templates/test/functional/passwords_controller_test.rb +1 -1
- data/generators/clearance/templates/test/functional/sessions_controller_test.rb +1 -1
- data/generators/clearance/templates/test/functional/users_controller_test.rb +1 -1
- data/generators/clearance/templates/test/unit/user_mailer_test.rb +1 -1
- data/generators/clearance/templates/test/unit/user_test.rb +1 -1
- data/lib/clearance.rb +6 -6
- data/lib/clearance/app/controllers/application_controller.rb +58 -54
- data/lib/clearance/app/controllers/confirmations_controller.rb +30 -26
- data/lib/clearance/app/controllers/passwords_controller.rb +48 -44
- data/lib/clearance/app/controllers/sessions_controller.rb +61 -56
- data/lib/clearance/app/controllers/users_controller.rb +32 -28
- data/lib/clearance/app/models/user.rb +58 -56
- data/lib/clearance/app/models/user_mailer.rb +21 -18
- data/lib/clearance/test/test_helper.rb +66 -64
- data/lib/clearance/version.rb +2 -2
- data/test/rails_root/app/controllers/application.rb +1 -1
- data/test/rails_root/app/controllers/confirmations_controller.rb +1 -1
- data/test/rails_root/app/controllers/passwords_controller.rb +1 -1
- data/test/rails_root/app/controllers/sessions_controller.rb +1 -1
- data/test/rails_root/app/controllers/users_controller.rb +1 -1
- data/test/rails_root/app/models/user.rb +1 -1
- data/test/rails_root/app/models/user_mailer.rb +1 -1
- data/test/rails_root/test/functional/confirmations_controller_test.rb +1 -1
- data/test/rails_root/test/functional/passwords_controller_test.rb +1 -1
- data/test/rails_root/test/functional/sessions_controller_test.rb +1 -1
- data/test/rails_root/test/functional/users_controller_test.rb +1 -1
- data/test/rails_root/test/test_helper.rb +1 -1
- data/test/rails_root/test/unit/user_mailer_test.rb +1 -1
- data/test/rails_root/test/unit/user_test.rb +1 -1
- metadata +34 -10
- data/lib/clearance/test/functionals/confirmations_controller_test.rb +0 -81
- data/lib/clearance/test/functionals/passwords_controller_test.rb +0 -188
- data/lib/clearance/test/functionals/sessions_controller_test.rb +0 -91
- data/lib/clearance/test/functionals/users_controller_test.rb +0 -60
- data/lib/clearance/test/units/user_mailer_test.rb +0 -34
- data/lib/clearance/test/units/user_test.rb +0 -203
data/README.textile
CHANGED
@@ -50,15 +50,10 @@ This will create:
|
|
50
50
|
test/unit/user_mailer_test.rb
|
51
51
|
test/unit/user_test.rb
|
52
52
|
|
53
|
-
If you already have some of these files created, the generator will:
|
54
|
-
|
55
|
-
# NOT overwrite your files
|
56
|
-
# print out instructions to include Clearance modules in those files manually
|
57
|
-
|
58
53
|
For example:
|
59
54
|
|
60
55
|
app/models/user.rb already exists. Add this line to it:
|
61
|
-
include Clearance::Models::User
|
56
|
+
include Clearance::App::Models::User
|
62
57
|
|
63
58
|
h2. Tests
|
64
59
|
|
@@ -79,7 +74,7 @@ In test/test_helper.rb:
|
|
79
74
|
class Test::Unit::TestCase
|
80
75
|
self.use_transactional_fixtures = true
|
81
76
|
self.use_instantiated_fixtures = false
|
82
|
-
include Clearance::TestHelper
|
77
|
+
include Clearance::Test::TestHelper
|
83
78
|
end
|
84
79
|
|
85
80
|
h2. Controllers
|
@@ -89,7 +84,7 @@ In app/controllers/application_controller.rb:
|
|
89
84
|
class ApplicationController < ActionController::Base
|
90
85
|
helper :all
|
91
86
|
protect_from_forgery
|
92
|
-
include Clearance::ApplicationController
|
87
|
+
include Clearance::App::Controllers::ApplicationController
|
93
88
|
end
|
94
89
|
|
95
90
|
h2. Migration
|
@@ -112,14 +107,11 @@ h2. Routes
|
|
112
107
|
ActionController::Routing::Routes.draw do |map|
|
113
108
|
|
114
109
|
map.resources :users
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
map.resources :passwords
|
121
|
-
|
122
|
-
map.root :controller => 'sessions', :action => 'new'
|
110
|
+
map.resource :session
|
111
|
+
map.resources :users, :has_one => :password
|
112
|
+
map.resources :users, :has_one => :confirmation
|
113
|
+
map.resources :passwords
|
114
|
+
map.root :controller => 'sessions', :action => 'new'
|
123
115
|
|
124
116
|
end
|
125
117
|
|
data/Rakefile
CHANGED
@@ -1,23 +1,18 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'rake/testtask'
|
3
3
|
require 'date'
|
4
|
-
|
5
|
-
begin
|
6
|
-
require 'jeweler'
|
7
|
-
rescue LoadError
|
8
|
-
end
|
9
|
-
|
4
|
+
|
10
5
|
test_files_pattern = 'test/rails_root/test/{unit,functional,other}/**/*_test.rb'
|
11
6
|
Rake::TestTask.new do |t|
|
12
7
|
t.libs << 'lib'
|
13
8
|
t.pattern = test_files_pattern
|
14
9
|
t.verbose = false
|
15
10
|
end
|
16
|
-
|
11
|
+
|
17
12
|
desc "Run the test suite"
|
18
13
|
task :default => :test
|
19
|
-
|
20
|
-
|
14
|
+
|
15
|
+
spec = Gem::Specification.new do |s|
|
21
16
|
s.name = "clearance"
|
22
17
|
s.summary = "Simple, complete Rails authentication."
|
23
18
|
s.email = "dcroak@thoughtbot.com"
|
@@ -27,6 +22,13 @@ Jeweler.gemspec = Gem::Specification.new do |s|
|
|
27
22
|
s.files = FileList["[A-Z]*", "{generators,lib,test}/**/*"]
|
28
23
|
end
|
29
24
|
|
25
|
+
begin
|
26
|
+
require 'jeweler'
|
27
|
+
Jeweler.gemspec = spec
|
28
|
+
rescue LoadError
|
29
|
+
puts "Jeweler not available. sudo gem install technicalpickles-jeweler --source=http://gems.github.com"
|
30
|
+
end
|
31
|
+
|
30
32
|
namespace :generator do
|
31
33
|
task :templates do
|
32
34
|
app_files = FileList["test/rails_root/app/{controllers,models,views}/**/*"]
|
@@ -52,4 +54,4 @@ namespace :generator do
|
|
52
54
|
end
|
53
55
|
end
|
54
56
|
end
|
55
|
-
end
|
57
|
+
end
|
data/TODO.textile
CHANGED
@@ -1,14 +1,17 @@
|
|
1
|
-
(
|
1
|
+
(highest priority first)
|
2
2
|
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# activation code instead of salt?
|
4
|
+
# users#show doesn't exist, but confirmations create redirects there
|
5
|
+
# sessions controller test is missing remember_me tests
|
5
6
|
# mailers should not hardcode example.com as from address
|
7
|
+
# should_have_form should be pulled into shoulda
|
8
|
+
# generator should print out instructions to include modules existing files
|
6
9
|
# check to make sure attr_accessible doesn't override and w/ attr_protected
|
7
10
|
# move shoulda macros in test_helper to shoulda_macros folder
|
8
11
|
# add shoulda and factory girl dependencies to gemspec
|
9
12
|
# refactor Mailer default_url_options[:host] to something cleaner
|
10
13
|
|
11
|
-
ideas
|
14
|
+
ideas to steal from merb-auth:
|
12
15
|
|
13
16
|
# store current_user on the session, not controller
|
14
17
|
# respond with 401 Unauthorized when request requires authentication
|
data/lib/clearance.rb
CHANGED
@@ -5,11 +5,11 @@ require 'clearance/app/controllers/sessions_controller'
|
|
5
5
|
require 'clearance/app/controllers/users_controller'
|
6
6
|
require 'clearance/app/models/user'
|
7
7
|
require 'clearance/app/models/user_mailer'
|
8
|
-
require 'clearance/test/
|
9
|
-
require 'clearance/test/
|
10
|
-
require 'clearance/test/
|
11
|
-
require 'clearance/test/
|
8
|
+
require 'clearance/test/functional/confirmations_controller_test'
|
9
|
+
require 'clearance/test/functional/sessions_controller_test'
|
10
|
+
require 'clearance/test/functional/users_controller_test'
|
11
|
+
require 'clearance/test/functional/passwords_controller_test'
|
12
12
|
require 'clearance/test/test_helper'
|
13
|
-
require 'clearance/test/
|
14
|
-
require 'clearance/test/
|
13
|
+
require 'clearance/test/unit/user_test'
|
14
|
+
require 'clearance/test/unit/user_mailer_test'
|
15
15
|
require 'clearance/version'
|
@@ -1,73 +1,77 @@
|
|
1
1
|
module Clearance
|
2
|
-
module
|
2
|
+
module App
|
3
|
+
module Controllers
|
4
|
+
module ApplicationController
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
helper_method :current_user
|
9
|
+
helper_method :logged_in?
|
8
10
|
|
9
|
-
|
11
|
+
include InstanceMethods
|
10
12
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
13
|
+
protected
|
14
|
+
include ProtectedInstanceMethods
|
15
|
+
end
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
module InstanceMethods
|
19
|
+
def current_user
|
20
|
+
@current_user ||= (user_from_session || user_from_cookie)
|
21
|
+
end
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
def logged_in?
|
24
|
+
! current_user.nil?
|
25
|
+
end
|
26
|
+
end
|
25
27
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
module ProtectedInstanceMethods
|
29
|
+
def authenticate
|
30
|
+
deny_access unless self.current_user
|
31
|
+
end
|
30
32
|
|
31
|
-
|
32
|
-
|
33
|
-
|
33
|
+
def user_from_session
|
34
|
+
user_model.find_by_id session[:user_id]
|
35
|
+
end
|
34
36
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
37
|
+
def user_from_cookie
|
38
|
+
user = user_model.find_by_remember_token(cookies[:auth_token]) if cookies[:auth_token]
|
39
|
+
user && user.remember_token? ? user : nil
|
40
|
+
end
|
39
41
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
42
|
+
def login(user)
|
43
|
+
create_session_for(user)
|
44
|
+
@current_user = user
|
45
|
+
end
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
def create_session_for(user)
|
48
|
+
session[:user_id] = user.id if user
|
49
|
+
end
|
48
50
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
def redirect_back_or(default)
|
52
|
+
session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default)
|
53
|
+
session[:return_to] = nil
|
54
|
+
end
|
53
55
|
|
54
|
-
|
55
|
-
|
56
|
-
|
56
|
+
def redirect_to_root
|
57
|
+
redirect_to root_url
|
58
|
+
end
|
57
59
|
|
58
|
-
|
59
|
-
|
60
|
-
|
60
|
+
def store_location
|
61
|
+
session[:return_to] = request.request_uri
|
62
|
+
end
|
61
63
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
def deny_access(flash_message = nil, opts = {})
|
65
|
+
opts[:redirect] ||= new_session_url
|
66
|
+
store_location
|
67
|
+
flash[:error] = flash_message if flash_message
|
68
|
+
redirect_to opts[:redirect]
|
69
|
+
end
|
68
70
|
|
69
|
-
|
70
|
-
|
71
|
+
def user_model
|
72
|
+
User
|
73
|
+
end
|
74
|
+
end
|
71
75
|
end
|
72
76
|
end
|
73
77
|
end
|
@@ -1,38 +1,42 @@
|
|
1
1
|
module Clearance
|
2
|
-
module
|
2
|
+
module App
|
3
|
+
module Controllers
|
4
|
+
module ConfirmationsController
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
before_filter :existing_user?, :only => [:new, :create]
|
7
9
|
|
8
|
-
|
10
|
+
include InstanceMethods
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
private
|
13
|
+
include PrivateInstanceMethods
|
14
|
+
end
|
15
|
+
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
17
|
+
module InstanceMethods
|
18
|
+
def new
|
19
|
+
@user = User.find_by_id_and_salt(params[:user_id], params[:salt])
|
20
|
+
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
22
|
+
def create
|
23
|
+
@user = User.find_by_id_and_salt(params[:user_id], params[:salt])
|
24
|
+
@user.confirm!
|
25
|
+
session[:user_id] = @user.id
|
26
|
+
redirect_to user_path(@user)
|
27
|
+
end
|
28
|
+
end
|
27
29
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
30
|
+
module PrivateInstanceMethods
|
31
|
+
def existing_user?
|
32
|
+
user = User.find_by_id_and_salt(params[:user_id], params[:salt])
|
33
|
+
if user.nil?
|
34
|
+
render :nothing => true, :status => :not_found
|
35
|
+
end
|
36
|
+
end
|
33
37
|
end
|
38
|
+
|
34
39
|
end
|
35
40
|
end
|
36
|
-
|
37
41
|
end
|
38
42
|
end
|
@@ -1,57 +1,61 @@
|
|
1
1
|
module Clearance
|
2
|
-
module
|
2
|
+
module App
|
3
|
+
module Controllers
|
4
|
+
module PasswordsController
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
def self.included(base)
|
7
|
+
base.class_eval do
|
8
|
+
before_filter :existing_user?, :only => [:edit, :update]
|
9
|
+
filter_parameter_logging :password, :password_confirmation
|
10
|
+
include InstanceMethods
|
11
|
+
private
|
12
|
+
include PrivateInstanceMethods
|
13
|
+
end
|
14
|
+
end
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
module InstanceMethods
|
17
|
+
def new
|
18
|
+
end
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
20
|
+
def create
|
21
|
+
user = User.find_by_email params[:password][:email]
|
22
|
+
if user.nil?
|
23
|
+
flash.now[:warning] = 'Unknown email'
|
24
|
+
render :action => :new
|
25
|
+
else
|
26
|
+
UserMailer.deliver_change_password user
|
27
|
+
redirect_to new_session_url
|
28
|
+
end
|
29
|
+
end
|
28
30
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
31
|
+
def edit
|
32
|
+
@user = User.find_by_email_and_crypted_password(params[:email],
|
33
|
+
params[:password])
|
34
|
+
end
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
def update
|
37
|
+
@user = User.find_by_email_and_crypted_password(params[:email],
|
38
|
+
params[:password])
|
39
|
+
if @user.update_attributes params[:user]
|
40
|
+
session[:user_id] = @user.id
|
41
|
+
redirect_to @user
|
42
|
+
else
|
43
|
+
render :action => :edit
|
44
|
+
end
|
45
|
+
end
|
42
46
|
end
|
43
|
-
end
|
44
|
-
end
|
45
47
|
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
48
|
+
module PrivateInstanceMethods
|
49
|
+
def existing_user?
|
50
|
+
user = User.find_by_email_and_crypted_password(params[:email],
|
51
|
+
params[:password])
|
52
|
+
if user.nil?
|
53
|
+
render :nothing => true, :status => :not_found
|
54
|
+
end
|
55
|
+
end
|
52
56
|
end
|
57
|
+
|
53
58
|
end
|
54
59
|
end
|
55
|
-
|
56
60
|
end
|
57
61
|
end
|