joshuaclayton-clearance 0.6.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/CHANGELOG.textile +135 -0
  2. data/LICENSE +21 -0
  3. data/README.textile +113 -0
  4. data/Rakefile +73 -0
  5. data/TODO.textile +6 -0
  6. data/app/controllers/clearance/confirmations_controller.rb +46 -0
  7. data/app/controllers/clearance/passwords_controller.rb +65 -0
  8. data/app/controllers/clearance/sessions_controller.rb +45 -0
  9. data/app/controllers/clearance/users_controller.rb +29 -0
  10. data/app/models/clearance_mailer.rb +19 -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/routes.rb +19 -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 +103 -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 +20 -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 +28 -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/lib/clearance.rb +5 -0
  38. data/lib/clearance/authentication.rb +96 -0
  39. data/lib/clearance/extensions/errors.rb +4 -0
  40. data/lib/clearance/extensions/rescue.rb +1 -0
  41. data/lib/clearance/user.rb +115 -0
  42. data/rails/init.rb +1 -0
  43. data/shoulda_macros/clearance.rb +261 -0
  44. metadata +129 -0
data/CHANGELOG.textile ADDED
@@ -0,0 +1,135 @@
1
+ h2. 0.6.4 (05/12/2009)
2
+
3
+ * Moved issue tracking to Github from Lighthouse. (Dan Croak)
4
+ * [#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)
5
+ * [#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)
6
+ * [#13] move private methods on sessions controller into Clearance::Authentication module (Dan Croak)
7
+ * [#9] audited flash keys. (Dan Croak)
8
+
9
+ h2. 0.6.3 (04/23/2009)
10
+
11
+ * Scoping ClearanceMailer properly within controllers so it works in production environments. (Nick Quaranto)
12
+
13
+ h2. 0.6.2 (04/22/2009)
14
+
15
+ * Insert Clearance::User into User model if it exists. (Nick Quaranto)
16
+ * World(NavigationHelpers) Cucumber 3.0 style. (Shay Arnett & Mark Cornick)
17
+
18
+ h2. 0.6.1 (04/21/2009)
19
+ * Scope operators are necessary to keep Rails happy. Reverting the original
20
+ revert so they're back in the library now for constants referenced inside of
21
+ the gem. (Nick Quaranto)
22
+
23
+ h2. 0.6.0 (04/21/2009)
24
+
25
+ * Converted Clearance to a Rails engine. (Dan Croak & Joe Ferris)
26
+ * Include Clearance::User in User model in app. (Dan Croak & Joe Ferris)
27
+ * Include Clearance::Authentication in ApplicationController. (Dan Croak & Joe Ferris)
28
+ * Namespace controllers under Clearance. (Dan Croak & Joe Ferris)
29
+ * Routes move to engine, use namespaced controllers but publicly the same. (Dan Croak & Joe Ferris)
30
+ * If you want to override a controller, subclass it like SessionsController <
31
+ Clearance::SessionsController. This gives you access to usual hooks such as
32
+ url_after_create. (Dan Croak & Joe Ferris)
33
+ * Controllers, mailer, model, routes all unit tested inside engine. Use
34
+ script/generate clearance_features to test integration of Clearance with your
35
+ Rails app. No longer including modules in your app's test files. (Dan Croak & Joe Ferris)
36
+ * Moved views to engine. (Joe Ferris)
37
+ * Converted generated test/factories/clearance.rb to use inheritence for
38
+ email_confirmed_user. (Dan Croak)
39
+ * Corrected some spelling errors with methods (Nick Quaranto)
40
+ * Converted "I should see error messages" to use a regex in the features (Nick
41
+ Quaranto)
42
+ * Loading clearance routes after rails routes via some monkeypatching (Nick
43
+ Quaranto)
44
+ * Made the clearance controllers unloadable to stop constant loading errors in
45
+ development mode (Nick Quaranto)
46
+
47
+ h2. 0.5.6 (4/11/2009)
48
+
49
+ * [#57] Step definition changed for "User should see error messages" so
50
+ features won't fail for certain validations. (Nick Quaranto)
51
+
52
+ h2. 0.5.5 (3/23/2009)
53
+
54
+ * Removing duplicate test to get rid of warning. (Nick Quaranto)
55
+
56
+ h2. 0.5.4 (3/21/2009)
57
+
58
+ * When users fail logging in, redirect them instead of rendering. (Matt
59
+ Jankowski)
60
+
61
+ h2. 0.5.3 (3/5/2009)
62
+
63
+ * Clearance now works with (and requires) Shoulda 2.10.0. (Mark Cornick, Joe
64
+ Ferris, Dan Croak)
65
+ * Prefer flat over nested contexts in sessions_controller_test. (Joe Ferris,
66
+ Dan Croak)
67
+
68
+ h2. 0.5.2 (3/2/2009)
69
+
70
+ * Fixed last remaining errors in Rails 2.3 tests. Now fully compatible. (Joe
71
+ Ferris, Dan Croak)
72
+
73
+ h2. 0.5.1 (2/27/2009)
74
+
75
+ * [#46] A user with unconfirmed email who resets password now confirms email.
76
+ (Marcel Görner)
77
+ * Refactored user_from_cookie, user_from_session, User#authenticate to use
78
+ more direct return code instead of ugly, harder to read ternary. (Dan Croak)
79
+ * 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)
80
+ * Altered generator to interact with application_controller.rb instead of
81
+ application.rb in Rails 2.3 apps. (Dan Croak)
82
+ * [#42] Bug fix. Rack-based session change altered how to test remember me
83
+ cookie. (Mihai Anca)
84
+
85
+ h2. 0.5.0 (2/27/2009)
86
+
87
+ * Fixed problem with Cucumber features. (Dan Croak)
88
+ * Fixed mising HTTP fluency use case. (Dan Croak)
89
+ * Refactored User#update_password to take just parameters it needs. (Dan
90
+ Croak)
91
+ * Refactored User unit tests to be more readable. (Dan Croak)
92
+
93
+ h2. 0.4.9 (2/20/2009)
94
+
95
+ * Protect passwords & confirmations actions with forbidden filters. (Dan Croak)
96
+ * Return 403 Forbidden status code in those cases. (Tim Pope)
97
+ * Test 403 Forbidden status code in Cucumber feature. (Dan Croak, Joe Ferris)
98
+ * Raise custom ActionController::Forbidden error internally. (Joe Ferris, Mike Burns, Jason Morrison)
99
+ * Test ActionController::Forbidden error is raised in functional test. (Joe Ferris, Mike Burns, Dan Croak)
100
+ * [#45] Fixed bug that allowed anyone to edit another user's password (Marcel Görner)
101
+ * Required Factory Girl >= 1.2.0. (Dan Croak)
102
+
103
+ h2. 0.4.8 (2/16/2009)
104
+
105
+ * Added support paths for Cucumber. (Ben Mabey)
106
+ * Added documentation for the flash. (Ben Mabey)
107
+ * Generators require "test_helper" instead of File.join. for rr compatibility. (Joe Ferris)
108
+ * Removed interpolated email address from flash message to make i18n easier. (Bence Nagy)
109
+ * Standardized flash messages that refer to email delivery. (Dan Croak)
110
+
111
+ h2. 0.4.7 (2/12/2009)
112
+
113
+ * Removed Clearance::Test::TestHelper so there is one less setup step. (Dan Croak)
114
+ * All test helpers now in shoulda_macros. (Dan Croak)
115
+
116
+ h2. 0.4.6 (2/11/2009)
117
+
118
+ * Made the modules behave like mixins again. (hat-tip Eloy Duran)
119
+ * Created Actions and PrivateMethods modules on controllers for future RDoc reasons. (Dan Croak, Joe Ferris)
120
+
121
+ h2. 0.4.5 (2/9/2009)
122
+
123
+ * [#43] Removed email downcasing because local-part is case sensitive per RFC5321. (Dan Croak)
124
+ * [#42] Removed dependency on Mocha. (Dan Croak)
125
+ * Required Shoulda >= 2.9.1. (Dan Croak)
126
+ * Added password reset feature to clearance_features generator. (Eugene Bolshakov, Dan Croak)
127
+ * Removed unnecessary session[:salt]. (Dan Croak)
128
+ * [#41] Only store location for session[:return_to] for GET requests. (Dan Croak)
129
+ * Audited "sign up" naming convention. "Register" had slipped in a few places. (Dan Croak)
130
+ * 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)
131
+
132
+ h2. 0.4.4 (2/2/2009)
133
+
134
+ * Added a generator for Cucumber features. (Joe Ferris, Dan Croak)
135
+ * 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,113 @@
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. Engine
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
+ config.gem "thoughtbot-clearance",
18
+ :lib => 'clearance',
19
+ :source => 'http://gems.github.com',
20
+ :version => '0.6.4'
21
+
22
+ Vendor the gem:
23
+
24
+ rake gems:install
25
+ rake gems:unpack
26
+
27
+ Make sure the development database exists and run the generator:
28
+
29
+ script/generate clearance
30
+
31
+ A number of files will be created and instructions will be printed.
32
+
33
+ You may already have some of these files. Don't worry. You'll be asked if you want to overwrite them.
34
+
35
+ Run the migration:
36
+
37
+ rake db:migrate
38
+
39
+ h2. Environment
40
+
41
+ Define a HOST constant in your environment files.
42
+ In config/environments/test.rb and config/environments/development.rb it can be:
43
+
44
+ HOST = "localhost"
45
+
46
+ In production.rb it must be the actual host your application is deployed to.
47
+ The constant is used by mailers to generate URLs in emails.
48
+
49
+ In config/environment.rb:
50
+
51
+ DO_NOT_REPLY = "donotreply@example.com"
52
+
53
+ Define root_url to *something* in your config/routes.rb:
54
+
55
+ map.root :controller => 'home'
56
+
57
+ h2. Cucumber Features
58
+
59
+ 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/.
60
+
61
+ In config/environments/test.rb:
62
+
63
+ config.gem 'webrat',
64
+ :version => '= 0.4.4'
65
+ config.gem 'cucumber',
66
+ :version => '= 0.3.0'
67
+ config.gem 'thoughtbot-factory_girl',
68
+ :lib => 'factory_girl',
69
+ :source => "http://gems.github.com",
70
+ :version => '1.2.1'
71
+
72
+ Vendor the gems:
73
+
74
+ rake gems:install RAILS_ENV=test
75
+ rake gems:unpack RAILS_ENV=test
76
+
77
+ Don't vendor nokogiri (due to its native extensions):
78
+
79
+ rm -rf vendor/gems/nokogiri-1.2.3
80
+
81
+ Run the Cucumber generator (if you haven't already) and Clearance's feature generator:
82
+
83
+ script/generate cucumber
84
+ script/generate clearance_features
85
+
86
+ 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:
87
+
88
+ def path_to(page_name)
89
+ case page_name
90
+ ...
91
+ when /the sign up page/i
92
+ new_user_path
93
+ when /the sign in page/i
94
+ new_session_path
95
+ when /the password reset request page/i
96
+ new_password_path
97
+ ...
98
+ end
99
+
100
+ h2. Authors
101
+
102
+ Clearance was extracted out of "Hoptoad":http://hoptoadapp.com. We merged the authentication code from two of thoughtbot's client's Rails apps and have since used it each time we need authentication. The following people have improved the library. Thank you!
103
+
104
+ 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.
105
+
106
+ h2. Questions?
107
+
108
+ Ask the "mailing list":http://groups.google.com/group/thoughtbot-clearance
109
+
110
+ h2. Suggestions, Bugs, Refactoring?
111
+
112
+ Fork away and create a "Github Issue":http://github.com/thoughtbot/clearance/issues. Please don't send pull requests.
113
+
data/Rakefile ADDED
@@ -0,0 +1,73 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'cucumber/rake/task'
4
+
5
+ namespace :test do
6
+ Rake::TestTask.new(:all => ["generator:cleanup",
7
+ "generator:generate"]) do |task|
8
+ task.libs << "lib"
9
+ task.libs << "test"
10
+ task.pattern = "test/**/*_test.rb"
11
+ task.verbose = false
12
+ end
13
+
14
+ Cucumber::Rake::Task.new(:features) do |t|
15
+ t.cucumber_opts = "--format progress"
16
+ t.feature_pattern = "test/rails_root/features/*.feature"
17
+ end
18
+ end
19
+
20
+ generators = %w(clearance clearance_features)
21
+
22
+ namespace :generator do
23
+ desc "Cleans up the test app before running the generator"
24
+ task :cleanup do
25
+ generators.each do |generator|
26
+ FileList["generators/#{generator}/templates/**/*.*"].each do |each|
27
+ file = "test/rails_root/#{each.gsub("generators/#{generator}/templates/",'')}"
28
+ File.delete(file) if File.exists?(file)
29
+ end
30
+ end
31
+
32
+ FileList["test/rails_root/db/**/*"].each do |each|
33
+ FileUtils.rm_rf(each)
34
+ end
35
+ FileUtils.rm_rf("test/rails_root/vendor/plugins/clearance")
36
+ FileUtils.mkdir_p("test/rails_root/vendor/plugins")
37
+ clearance_root = File.expand_path(File.dirname(__FILE__))
38
+ system("ln -s #{clearance_root} test/rails_root/vendor/plugins/clearance")
39
+ end
40
+
41
+ desc "Run the generator on the tests"
42
+ task :generate do
43
+ generators.each do |generator|
44
+ system "cd test/rails_root && ./script/generate #{generator} && rake db:migrate db:test:prepare"
45
+ end
46
+ end
47
+ end
48
+
49
+ desc "Run the test suite"
50
+ task :default => ['test:all', 'test:features']
51
+
52
+ gem_spec = Gem::Specification.new do |gem_spec|
53
+ gem_spec.name = "clearance"
54
+ gem_spec.version = "0.6.4"
55
+ gem_spec.summary = "Rails authentication with email & password."
56
+ gem_spec.email = "support@thoughtbot.com"
57
+ gem_spec.homepage = "http://github.com/thoughtbot/clearance"
58
+ gem_spec.description = "Rails authentication with email & password."
59
+ gem_spec.authors = ["Dan Croak", "Mike Burns", "Jason Morrison",
60
+ "Joe Ferris", "Eugene Bolshakov", "Nick Quaranto",
61
+ "Josh Nichols", "Mike Breen", "Marcel Görner",
62
+ "Bence Nagy", "Ben Mabey", "Eloy Duran",
63
+ "Tim Pope", "Mihai Anca", "Mark Cornick",
64
+ "Shay Arnett"]
65
+ gem_spec.files = FileList["[A-Z]*", "{app,config,generators,lib,shoulda_macros,rails}/**/*"]
66
+ end
67
+
68
+ desc "Generate a gemspec file"
69
+ task :gemspec do
70
+ File.open("#{gem_spec.name}.gemspec", 'w') do |f|
71
+ f.write gem_spec.to_yaml
72
+ end
73
+ end
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,46 @@
1
+ class Clearance::ConfirmationsController < ApplicationController
2
+ unloadable
3
+
4
+ before_filter :forbid_confirmed_user, :only => :new
5
+ before_filter :forbid_missing_token, :only => :new
6
+ before_filter :forbid_non_existent_user, :only => :new
7
+ filter_parameter_logging :token
8
+
9
+ def new
10
+ create
11
+ end
12
+
13
+ def create
14
+ @user = ::User.find_by_id_and_token(params[:user_id], params[:token])
15
+ @user.confirm_email!
16
+
17
+ sign_user_in(@user)
18
+ flash[:success] = "Confirmed email and signed in."
19
+ redirect_to url_after_create
20
+ end
21
+
22
+ private
23
+
24
+ def forbid_confirmed_user
25
+ user = ::User.find_by_id(params[:user_id])
26
+ if user && user.email_confirmed?
27
+ raise ActionController::Forbidden, "confirmed user"
28
+ end
29
+ end
30
+
31
+ def forbid_missing_token
32
+ if params[:token].blank?
33
+ raise ActionController::Forbidden, "missing token"
34
+ end
35
+ end
36
+
37
+ def forbid_non_existent_user
38
+ unless ::User.find_by_id_and_token(params[:user_id], params[:token])
39
+ raise ActionController::Forbidden, "non-existent user"
40
+ end
41
+ end
42
+
43
+ def url_after_create
44
+ root_url
45
+ end
46
+ end
@@ -0,0 +1,65 @@
1
+ class Clearance::PasswordsController < ApplicationController
2
+ unloadable
3
+
4
+ before_filter :forbid_missing_token, :only => [:edit, :update]
5
+ before_filter :forbid_non_existent_user, :only => [:edit, :update]
6
+ filter_parameter_logging :password, :password_confirmation
7
+
8
+ def new
9
+ render :template => 'passwords/new'
10
+ end
11
+
12
+ def create
13
+ if user = ::User.find_by_email(params[:password][:email])
14
+ user.forgot_password!
15
+ ::ClearanceMailer.deliver_change_password user
16
+ flash[:notice] = "You will receive an email within the next few minutes. " <<
17
+ "It contains instructions for changing your password."
18
+ redirect_to url_after_create
19
+ else
20
+ flash.now[:failure] = "Unknown email"
21
+ render :template => 'passwords/new'
22
+ end
23
+ end
24
+
25
+ def edit
26
+ @user = ::User.find_by_id_and_token(params[:user_id], params[:token])
27
+ render :template => 'passwords/edit'
28
+ end
29
+
30
+ def update
31
+ @user = ::User.find_by_id_and_token(params[:user_id], params[:token])
32
+
33
+ if @user.update_password(params[:user][:password],
34
+ params[:user][:password_confirmation])
35
+ @user.confirm_email! unless @user.email_confirmed?
36
+ sign_user_in(@user)
37
+ flash[:success] = "Signed in."
38
+ redirect_to url_after_update
39
+ else
40
+ render :template => 'passwords/edit'
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def forbid_missing_token
47
+ if params[:token].blank?
48
+ raise ActionController::Forbidden, "missing token"
49
+ end
50
+ end
51
+
52
+ def forbid_non_existent_user
53
+ unless ::User.find_by_id_and_token(params[:user_id], params[:token])
54
+ raise ActionController::Forbidden, "non-existent user"
55
+ end
56
+ end
57
+
58
+ def url_after_create
59
+ new_session_url
60
+ end
61
+
62
+ def url_after_update
63
+ root_url
64
+ end
65
+ end