qrush-clearance 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGELOG.textile +176 -0
  2. data/LICENSE +21 -0
  3. data/README.textile +123 -0
  4. data/Rakefile +103 -0
  5. data/TODO.textile +6 -0
  6. data/app/controllers/clearance/confirmations_controller.rb +72 -0
  7. data/app/controllers/clearance/passwords_controller.rb +80 -0
  8. data/app/controllers/clearance/sessions_controller.rb +66 -0
  9. data/app/controllers/clearance/users_controller.rb +34 -0
  10. data/app/models/clearance_mailer.rb +23 -0
  11. data/app/views/clearance_mailer/change_password.html.erb +7 -0
  12. data/app/views/clearance_mailer/confirmation.html.erb +2 -0
  13. data/app/views/passwords/edit.html.erb +23 -0
  14. data/app/views/passwords/new.html.erb +15 -0
  15. data/app/views/sessions/new.html.erb +28 -0
  16. data/app/views/users/_form.html.erb +13 -0
  17. data/app/views/users/new.html.erb +6 -0
  18. data/config/clearance_routes.rb +30 -0
  19. data/generators/clearance/USAGE +1 -0
  20. data/generators/clearance/clearance_generator.rb +41 -0
  21. data/generators/clearance/lib/insert_commands.rb +33 -0
  22. data/generators/clearance/lib/rake_commands.rb +22 -0
  23. data/generators/clearance/templates/README +22 -0
  24. data/generators/clearance/templates/factories.rb +13 -0
  25. data/generators/clearance/templates/migrations/create_users.rb +21 -0
  26. data/generators/clearance/templates/migrations/update_users.rb +41 -0
  27. data/generators/clearance/templates/user.rb +3 -0
  28. data/generators/clearance_features/USAGE +1 -0
  29. data/generators/clearance_features/clearance_features_generator.rb +20 -0
  30. data/generators/clearance_features/templates/features/password_reset.feature +33 -0
  31. data/generators/clearance_features/templates/features/sign_in.feature +42 -0
  32. data/generators/clearance_features/templates/features/sign_out.feature +23 -0
  33. data/generators/clearance_features/templates/features/sign_up.feature +45 -0
  34. data/generators/clearance_features/templates/features/step_definitions/clearance_steps.rb +110 -0
  35. data/generators/clearance_features/templates/features/step_definitions/factory_girl_steps.rb +5 -0
  36. data/generators/clearance_features/templates/features/support/paths.rb +22 -0
  37. data/generators/clearance_views/USAGE +0 -0
  38. data/generators/clearance_views/clearance_views_generator.rb +27 -0
  39. data/generators/clearance_views/templates/formtastic/passwords/edit.html.erb +21 -0
  40. data/generators/clearance_views/templates/formtastic/passwords/new.html.erb +15 -0
  41. data/generators/clearance_views/templates/formtastic/sessions/new.html.erb +22 -0
  42. data/generators/clearance_views/templates/formtastic/users/_inputs.html.erb +6 -0
  43. data/generators/clearance_views/templates/formtastic/users/new.html.erb +10 -0
  44. data/lib/clearance.rb +6 -0
  45. data/lib/clearance/authentication.rb +102 -0
  46. data/lib/clearance/extensions/errors.rb +6 -0
  47. data/lib/clearance/extensions/rescue.rb +3 -0
  48. data/lib/clearance/extensions/routes.rb +14 -0
  49. data/lib/clearance/user.rb +143 -0
  50. data/rails/init.rb +1 -0
  51. data/shoulda_macros/clearance.rb +268 -0
  52. metadata +145 -0
data/CHANGELOG.textile ADDED
@@ -0,0 +1,176 @@
1
+ h2. 0.7.0 (08/04/2009)
2
+
3
+ * Redirect signed in user who clicks confirmation link again. (Dan Croak)
4
+ * Redirect signed out user who clicks confirmation link again. (Dan Croak)
5
+ * Added signed_out? convenience method for controllers, helpers, views. (Dan
6
+ Croak)
7
+ * Added clearance_views generator. By default, creates formtastic views which
8
+ pass all tests and features. (Dan Croak)
9
+
10
+ h2. 0.6.9 (07/04/2009)
11
+
12
+ * Added timestamps to create users migration. (Dan Croak)
13
+ * Ready for Ruby 1.9. (Jason Morrison, Nick Quaranto)
14
+
15
+ h2. 0.6.8 (06/24/2009)
16
+
17
+ * Added defined? checks for various Rails constants such as ActionController
18
+ for easier unit testing of Clearance extensions... particularly ActiveRecord
19
+ extensions... particularly strong_password. (Dan Croak)
20
+
21
+ h2. 0.6.7 (06/13/2009)
22
+
23
+ * [#30] Added sign_up, sign_in, sign_out named routes. (Dan Croak)
24
+ * [#22] Minimizing Reek smell: Duplication in redirect_back_or. (Dan Croak)
25
+ * Deprecated sign_user_in. Told developers to use sign_in instead. (Dan
26
+ Croak)
27
+ * [#16] flash_success_after_create, flash_notice_after_create, flash_failure_after_create, flash_sucess_after_update, flash_success_after_destroy, etc. (Dan Croak)
28
+ * [#17] bug. added #create to forbidden before_filters on confirmations controller. (Dan Croak)
29
+ * [#24] should_be_signed_in_as shouldn't look in the session. (Dan Croak)
30
+ * README improvements. (Dan Croak)
31
+ * Move routes loading to separate file. (Joshua Clayton)
32
+
33
+ h2. 0.6.6 (05/18/2009)
34
+
35
+ * [#14] replaced class_eval in Clearance::User with modules. This was needed
36
+ in a thoughtbot client app so we could write our own validations. (Dan Croak)
37
+
38
+ h2. 0.6.5 (05/17/2009)
39
+
40
+ * [#6] Make Clearance i18n aware. (Timur Vafin, Marcel Goerner, Eugene Bolshakov, Dan Croak)
41
+
42
+ h2. 0.6.4 (05/12/2009)
43
+
44
+ * Moved issue tracking to Github from Lighthouse. (Dan Croak)
45
+ * [#7] asking higher-level questions of controllers in webrat steps, such as signed_in? instead of what's in the session. same for accessors. (Dan Croak)
46
+ * [#11] replacing sign_in_as & sign_out shoulda macros with a stubbing (requires no dependency) approach. this will avoid dealing with the internals of current_user, such as session & cookies. added sign_in macro which signs in an email confirmed user from clearance's factories. (Dan Croak)
47
+ * [#13] move private methods on sessions controller into Clearance::Authentication module (Dan Croak)
48
+ * [#9] audited flash keys. (Dan Croak)
49
+
50
+ h2. 0.6.3 (04/23/2009)
51
+
52
+ * Scoping ClearanceMailer properly within controllers so it works in production environments. (Nick Quaranto)
53
+
54
+ h2. 0.6.2 (04/22/2009)
55
+
56
+ * Insert Clearance::User into User model if it exists. (Nick Quaranto)
57
+ * World(NavigationHelpers) Cucumber 3.0 style. (Shay Arnett & Mark Cornick)
58
+
59
+ h2. 0.6.1 (04/21/2009)
60
+ * Scope operators are necessary to keep Rails happy. Reverting the original
61
+ revert so they're back in the library now for constants referenced inside of
62
+ the gem. (Nick Quaranto)
63
+
64
+ h2. 0.6.0 (04/21/2009)
65
+
66
+ * Converted Clearance to a Rails engine. (Dan Croak & Joe Ferris)
67
+ * Include Clearance::User in User model in app. (Dan Croak & Joe Ferris)
68
+ * Include Clearance::Authentication in ApplicationController. (Dan Croak & Joe Ferris)
69
+ * Namespace controllers under Clearance. (Dan Croak & Joe Ferris)
70
+ * Routes move to engine, use namespaced controllers but publicly the same. (Dan Croak & Joe Ferris)
71
+ * If you want to override a controller, subclass it like SessionsController <
72
+ Clearance::SessionsController. This gives you access to usual hooks such as
73
+ url_after_create. (Dan Croak & Joe Ferris)
74
+ * Controllers, mailer, model, routes all unit tested inside engine. Use
75
+ script/generate clearance_features to test integration of Clearance with your
76
+ Rails app. No longer including modules in your app's test files. (Dan Croak & Joe Ferris)
77
+ * Moved views to engine. (Joe Ferris)
78
+ * Converted generated test/factories/clearance.rb to use inheritence for
79
+ email_confirmed_user. (Dan Croak)
80
+ * Corrected some spelling errors with methods (Nick Quaranto)
81
+ * Converted "I should see error messages" to use a regex in the features (Nick
82
+ Quaranto)
83
+ * Loading clearance routes after rails routes via some monkeypatching (Nick
84
+ Quaranto)
85
+ * Made the clearance controllers unloadable to stop constant loading errors in
86
+ development mode (Nick Quaranto)
87
+
88
+ h2. 0.5.6 (4/11/2009)
89
+
90
+ * [#57] Step definition changed for "User should see error messages" so
91
+ features won't fail for certain validations. (Nick Quaranto)
92
+
93
+ h2. 0.5.5 (3/23/2009)
94
+
95
+ * Removing duplicate test to get rid of warning. (Nick Quaranto)
96
+
97
+ h2. 0.5.4 (3/21/2009)
98
+
99
+ * When users fail logging in, redirect them instead of rendering. (Matt
100
+ Jankowski)
101
+
102
+ h2. 0.5.3 (3/5/2009)
103
+
104
+ * Clearance now works with (and requires) Shoulda 2.10.0. (Mark Cornick, Joe
105
+ Ferris, Dan Croak)
106
+ * Prefer flat over nested contexts in sessions_controller_test. (Joe Ferris,
107
+ Dan Croak)
108
+
109
+ h2. 0.5.2 (3/2/2009)
110
+
111
+ * Fixed last remaining errors in Rails 2.3 tests. Now fully compatible. (Joe
112
+ Ferris, Dan Croak)
113
+
114
+ h2. 0.5.1 (2/27/2009)
115
+
116
+ * [#46] A user with unconfirmed email who resets password now confirms email.
117
+ (Marcel Görner)
118
+ * Refactored user_from_cookie, user_from_session, User#authenticate to use
119
+ more direct return code instead of ugly, harder to read ternary. (Dan Croak)
120
+ * Switch order of cookies and sessions to take advantage of Rails 2.3's "Rack-based lazy-loaded sessions":http://is.gd/i23E. (Dan Croak)
121
+ * Altered generator to interact with application_controller.rb instead of
122
+ application.rb in Rails 2.3 apps. (Dan Croak)
123
+ * [#42] Bug fix. Rack-based session change altered how to test remember me
124
+ cookie. (Mihai Anca)
125
+
126
+ h2. 0.5.0 (2/27/2009)
127
+
128
+ * Fixed problem with Cucumber features. (Dan Croak)
129
+ * Fixed mising HTTP fluency use case. (Dan Croak)
130
+ * Refactored User#update_password to take just parameters it needs. (Dan
131
+ Croak)
132
+ * Refactored User unit tests to be more readable. (Dan Croak)
133
+
134
+ h2. 0.4.9 (2/20/2009)
135
+
136
+ * Protect passwords & confirmations actions with forbidden filters. (Dan Croak)
137
+ * Return 403 Forbidden status code in those cases. (Tim Pope)
138
+ * Test 403 Forbidden status code in Cucumber feature. (Dan Croak, Joe Ferris)
139
+ * Raise custom ActionController::Forbidden error internally. (Joe Ferris, Mike Burns, Jason Morrison)
140
+ * Test ActionController::Forbidden error is raised in functional test. (Joe Ferris, Mike Burns, Dan Croak)
141
+ * [#45] Fixed bug that allowed anyone to edit another user's password (Marcel Görner)
142
+ * Required Factory Girl >= 1.2.0. (Dan Croak)
143
+
144
+ h2. 0.4.8 (2/16/2009)
145
+
146
+ * Added support paths for Cucumber. (Ben Mabey)
147
+ * Added documentation for the flash. (Ben Mabey)
148
+ * Generators require "test_helper" instead of File.join. for rr compatibility. (Joe Ferris)
149
+ * Removed interpolated email address from flash message to make i18n easier. (Bence Nagy)
150
+ * Standardized flash messages that refer to email delivery. (Dan Croak)
151
+
152
+ h2. 0.4.7 (2/12/2009)
153
+
154
+ * Removed Clearance::Test::TestHelper so there is one less setup step. (Dan Croak)
155
+ * All test helpers now in shoulda_macros. (Dan Croak)
156
+
157
+ h2. 0.4.6 (2/11/2009)
158
+
159
+ * Made the modules behave like mixins again. (hat-tip Eloy Duran)
160
+ * Created Actions and PrivateMethods modules on controllers for future RDoc reasons. (Dan Croak, Joe Ferris)
161
+
162
+ h2. 0.4.5 (2/9/2009)
163
+
164
+ * [#43] Removed email downcasing because local-part is case sensitive per RFC5321. (Dan Croak)
165
+ * [#42] Removed dependency on Mocha. (Dan Croak)
166
+ * Required Shoulda >= 2.9.1. (Dan Croak)
167
+ * Added password reset feature to clearance_features generator. (Eugene Bolshakov, Dan Croak)
168
+ * Removed unnecessary session[:salt]. (Dan Croak)
169
+ * [#41] Only store location for session[:return_to] for GET requests. (Dan Croak)
170
+ * Audited "sign up" naming convention. "Register" had slipped in a few places. (Dan Croak)
171
+ * Switched to SHA1 encryption. Cypher doesn't matter much for email confirmation, password reset. Better to have shorter hashes in the emails for clients who line break on 72 chars. (Dan Croak)
172
+
173
+ h2. 0.4.4 (2/2/2009)
174
+
175
+ * Added a generator for Cucumber features. (Joe Ferris, Dan Croak)
176
+ * Standarized naming for "Sign up," "Sign in," and "Sign out". (Dan Croak)
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2008 thoughtbot, inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,123 @@
1
+ h1. Clearance
2
+
3
+ Rails authentication with email & password.
4
+
5
+ "We have clearance, Clarence.":http://www.youtube.com/v/mNRXJEE3Nz8
6
+
7
+ h2. Wiki
8
+
9
+ Most information regarding Clearance is on the "Github Wiki":http://wiki.github.com/thoughtbot/clearance.
10
+
11
+ h2. Installation
12
+
13
+ Clearance is a Rails engine. It works with versions of Rails greater than 2.3.
14
+
15
+ In config/environment.rb:
16
+
17
+ <pre>
18
+ config.gem "thoughtbot-clearance",
19
+ :lib => 'clearance',
20
+ :source => 'http://gems.github.com',
21
+ :version => '0.7.0'
22
+ </pre>
23
+
24
+ Vendor the gem:
25
+
26
+ <pre>
27
+ rake gems:install
28
+ rake gems:unpack
29
+ </pre>
30
+
31
+ Make sure the development database exists and run the generator:
32
+
33
+ @script/generate clearance@
34
+
35
+ A number of files will be created and instructions will be printed.
36
+
37
+ You may already have some of these files. Don't worry. You'll be asked if you want to overwrite them.
38
+
39
+ Run the migration:
40
+
41
+ @rake db:migrate@
42
+
43
+ Define a HOST constant in your environment files.
44
+ In config/environments/test.rb and config/environments/development.rb it can be:
45
+
46
+ @HOST = "localhost"@
47
+
48
+ In production.rb it must be the actual host your application is deployed to.
49
+ The constant is used by mailers to generate URLs in emails.
50
+
51
+ In config/environment.rb:
52
+
53
+ @DO_NOT_REPLY = "donotreply@example.com"@
54
+
55
+ Define root_url to *something* in your config/routes.rb:
56
+
57
+ @map.root :controller => 'home'@
58
+
59
+ h2. Cucumber Features
60
+
61
+ As your app evolves, you want to know that authentication still works. Clearance's opinion is that you should test its integration with your app using "Cucumber":http://cukes.info/.
62
+
63
+ In config/environments/test.rb:
64
+
65
+ <pre>
66
+ config.gem 'webrat',
67
+ :version => '>= 0.4.4'
68
+ config.gem 'cucumber',
69
+ :version => '>= 0.3.0'
70
+ config.gem 'thoughtbot-factory_girl',
71
+ :lib => 'factory_girl',
72
+ :source => "http://gems.github.com",
73
+ :version => '1.2.1'
74
+ </pre>
75
+
76
+ Vendor the gems:
77
+
78
+ <pre>
79
+ rake gems:install RAILS_ENV=test
80
+ rake gems:unpack RAILS_ENV=test
81
+ </pre>
82
+
83
+ We don't vendor nokogiri due to its native extensions, so install it normally on your machine:
84
+
85
+ @sudo gem install nokogiri@
86
+
87
+ Run the Cucumber generator (if you haven't already) and Clearance's feature generator:
88
+
89
+ <pre>
90
+ script/generate cucumber
91
+ script/generate clearance_features
92
+ </pre>
93
+
94
+ All of the files generated should be new with the exception of the features/support/paths.rb file. If you have not modified your paths.rb then you will be okay to replace it with this one. If you need to keep your paths.rb file then add these locations in your paths.rb manually:
95
+
96
+ <pre>
97
+ def path_to(page_name)
98
+ case page_name
99
+ ...
100
+ when /the sign up page/i
101
+ new_user_path
102
+ when /the sign in page/i
103
+ new_session_path
104
+ when /the password reset request page/i
105
+ new_password_path
106
+ ...
107
+ end
108
+ </pre>
109
+
110
+ h2. Authors
111
+
112
+ Clearance was extracted out of "Hoptoad":http://hoptoadapp.com. We merged the authentication code from two of thoughtbot's clients' Rails apps and have since used it each time we need authentication. The following people have improved the library. Thank you!
113
+
114
+ Dan Croak, Mike Burns, Jason Morrison, Joe Ferris, Eugene Bolshakov, Nick Quaranto, Josh Nichols, Mike Breen, Marcel Görner, Bence Nagy, Ben Mabey, Eloy Duran, Tim Pope, Mihai Anca, Mark Cornick, Shay Arnett, Joshua Clayton & Mustafa Ekim.
115
+
116
+ h2. Questions?
117
+
118
+ Ask the "mailing list":http://groups.google.com/group/thoughtbot-clearance
119
+
120
+ h2. Suggestions, Bugs, Refactoring?
121
+
122
+ Fork away and create a "Github Issue":http://github.com/thoughtbot/clearance/issues. Please don't send pull requests.
123
+
data/Rakefile ADDED
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'cucumber/rake/task'
6
+
7
+ namespace :test do
8
+ Rake::TestTask.new(:basic => ["generator:cleanup",
9
+ "generator:clearance",
10
+ "generator:clearance_features"]) do |task|
11
+ task.libs << "lib"
12
+ task.libs << "test"
13
+ task.pattern = "test/**/*_test.rb"
14
+ task.verbose = false
15
+ end
16
+
17
+ Rake::TestTask.new(:views => ["generator:clearance_views"]) do |task|
18
+ task.libs << "lib"
19
+ task.libs << "test"
20
+ task.pattern = "test/**/*_test.rb"
21
+ task.verbose = false
22
+ end
23
+
24
+ Cucumber::Rake::Task.new(:features) do |t|
25
+ t.cucumber_opts = "--format progress"
26
+ t.feature_pattern = "test/rails_root/features/*.feature"
27
+ end
28
+
29
+ Cucumber::Rake::Task.new(:features_for_views) do |t|
30
+ t.cucumber_opts = "--format progress"
31
+ t.feature_pattern = "test/rails_root/features/*.feature"
32
+ end
33
+ end
34
+
35
+ generators = %w(clearance clearance_features clearance_views)
36
+
37
+ namespace :generator do
38
+ desc "Cleans up the test app before running the generator"
39
+ task :cleanup do
40
+ generators.each do |generator|
41
+ FileList["generators/#{generator}/templates/**/*.*"].each do |each|
42
+ file = "test/rails_root/#{each.gsub("generators/#{generator}/templates/",'')}"
43
+ File.delete(file) if File.exists?(file)
44
+ end
45
+ end
46
+
47
+ FileList["test/rails_root/db/**/*"].each do |each|
48
+ FileUtils.rm_rf(each)
49
+ end
50
+
51
+ FileUtils.rm_rf("test/rails_root/vendor/plugins/clearance")
52
+ FileUtils.mkdir_p("test/rails_root/vendor/plugins")
53
+ clearance_root = File.expand_path(File.dirname(__FILE__))
54
+ system("ln -s #{clearance_root} test/rails_root/vendor/plugins/clearance")
55
+
56
+ FileUtils.rm_rf("test/rails_root/app/views/passwords")
57
+ FileUtils.rm_rf("test/rails_root/app/views/sessions")
58
+ FileUtils.rm_rf("test/rails_root/app/views/users")
59
+ end
60
+
61
+ desc "Run the clearance generator"
62
+ task :clearance do
63
+ system "cd test/rails_root && ./script/generate clearance && rake db:migrate db:test:prepare"
64
+ end
65
+
66
+ desc "Run the clearance features generator"
67
+ task :clearance_features do
68
+ system "cd test/rails_root && ./script/generate clearance_features"
69
+ end
70
+
71
+ desc "Run the clearance views generator"
72
+ task :clearance_views do
73
+ system "cd test/rails_root && ./script/generate clearance_views"
74
+ end
75
+ end
76
+
77
+ desc "Run the test suite"
78
+ task :default => ['test:basic', 'test:features',
79
+ 'test:views', 'test:features_for_views']
80
+
81
+ gem_spec = Gem::Specification.new do |gem_spec|
82
+ gem_spec.name = "clearance"
83
+ gem_spec.version = "0.7.0"
84
+ gem_spec.summary = "Rails authentication with email & password."
85
+ gem_spec.email = "support@thoughtbot.com"
86
+ gem_spec.homepage = "http://github.com/thoughtbot/clearance"
87
+ gem_spec.description = "Rails authentication with email & password."
88
+ gem_spec.authors = ["Dan Croak", "Mike Burns", "Jason Morrison",
89
+ "Joe Ferris", "Eugene Bolshakov", "Nick Quaranto",
90
+ "Josh Nichols", "Mike Breen", "Marcel Görner",
91
+ "Bence Nagy", "Ben Mabey", "Eloy Duran",
92
+ "Tim Pope", "Mihai Anca", "Mark Cornick",
93
+ "Shay Arnett"]
94
+ gem_spec.files = FileList["[A-Z]*", "{app,config,generators,lib,shoulda_macros,rails}/**/*"]
95
+ end
96
+
97
+ desc "Generate a gemspec file"
98
+ task :gemspec do
99
+ File.open("#{gem_spec.name}.gemspec", 'w') do |f|
100
+ f.write gem_spec.to_yaml
101
+ end
102
+ end
103
+
data/TODO.textile ADDED
@@ -0,0 +1,6 @@
1
+ h1. To-do
2
+
3
+ * Make insertion of Clearance::User into User model automatic from the generator.
4
+ * Change generated README to include instruction about running the migration.
5
+ * DO_NOT_REPLY, HOST refactoring.
6
+
@@ -0,0 +1,72 @@
1
+ class Clearance::ConfirmationsController < ApplicationController
2
+
3
+ before_filter :redirect_signed_in_confirmed_user, :only => [:new, :create]
4
+ before_filter :redirect_signed_out_confirmed_user, :only => [:new, :create]
5
+ before_filter :forbid_missing_token, :only => [:new, :create]
6
+ before_filter :forbid_non_existent_user, :only => [:new, :create]
7
+
8
+ filter_parameter_logging :token
9
+
10
+ def new
11
+ create
12
+ end
13
+
14
+ def create
15
+ @user = ::User.find_by_id_and_token(params[:user_id], params[:token])
16
+ @user.confirm_email!
17
+
18
+ sign_in(@user)
19
+ flash_success_after_create
20
+ redirect_to(url_after_create)
21
+ end
22
+
23
+ private
24
+
25
+ def redirect_signed_in_confirmed_user
26
+ user = ::User.find_by_id(params[:user_id])
27
+ if user && user.email_confirmed? && current_user == user
28
+ flash_success_after_create
29
+ redirect_to(url_after_create)
30
+ end
31
+ end
32
+
33
+ def redirect_signed_out_confirmed_user
34
+ user = ::User.find_by_id(params[:user_id])
35
+ if user && user.email_confirmed? && signed_out?
36
+ flash_already_confirmed
37
+ redirect_to(url_already_confirmed)
38
+ end
39
+ end
40
+
41
+ def forbid_missing_token
42
+ if params[:token].blank?
43
+ raise ActionController::Forbidden, "missing token"
44
+ end
45
+ end
46
+
47
+ def forbid_non_existent_user
48
+ unless ::User.find_by_id_and_token(params[:user_id], params[:token])
49
+ raise ActionController::Forbidden, "non-existent user"
50
+ end
51
+ end
52
+
53
+ def flash_success_after_create
54
+ flash[:success] = translate(:confirmed_email,
55
+ :scope => [:clearance, :controllers, :confirmations],
56
+ :default => "Confirmed email and signed in.")
57
+ end
58
+
59
+ def flash_already_confirmed
60
+ flash[:success] = translate(:already_confirmed_email,
61
+ :scope => [:clearance, :controllers, :confirmations],
62
+ :default => "Already confirmed email. Please sign in.")
63
+ end
64
+
65
+ def url_after_create
66
+ root_url
67
+ end
68
+
69
+ def url_already_confirmed
70
+ sign_in_url
71
+ end
72
+ end