devise 0.1.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (74) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +220 -0
  3. data/Rakefile +45 -0
  4. data/TODO +37 -0
  5. data/app/controllers/confirmations_controller.rb +32 -0
  6. data/app/controllers/passwords_controller.rb +38 -0
  7. data/app/controllers/sessions_controller.rb +35 -0
  8. data/app/models/notifier.rb +47 -0
  9. data/app/views/confirmations/new.html.erb +16 -0
  10. data/app/views/notifier/confirmation_instructions.html.erb +5 -0
  11. data/app/views/notifier/reset_password_instructions.html.erb +8 -0
  12. data/app/views/passwords/edit.html.erb +20 -0
  13. data/app/views/passwords/new.html.erb +16 -0
  14. data/app/views/sessions/new.html.erb +23 -0
  15. data/config/locales/en.yml +16 -0
  16. data/init.rb +2 -0
  17. data/lib/devise.rb +48 -0
  18. data/lib/devise/active_record.rb +86 -0
  19. data/lib/devise/controllers/filters.rb +109 -0
  20. data/lib/devise/controllers/helpers.rb +91 -0
  21. data/lib/devise/controllers/url_helpers.rb +47 -0
  22. data/lib/devise/hooks/rememberable.rb +24 -0
  23. data/lib/devise/mapping.rb +95 -0
  24. data/lib/devise/migrations.rb +50 -0
  25. data/lib/devise/models/authenticable.rb +98 -0
  26. data/lib/devise/models/confirmable.rb +125 -0
  27. data/lib/devise/models/recoverable.rb +88 -0
  28. data/lib/devise/models/rememberable.rb +71 -0
  29. data/lib/devise/models/validatable.rb +36 -0
  30. data/lib/devise/routes.rb +95 -0
  31. data/lib/devise/strategies/authenticable.rb +45 -0
  32. data/lib/devise/strategies/base.rb +24 -0
  33. data/lib/devise/strategies/rememberable.rb +33 -0
  34. data/lib/devise/version.rb +3 -0
  35. data/lib/devise/warden.rb +64 -0
  36. data/test/active_record_test.rb +96 -0
  37. data/test/controllers/filters_test.rb +97 -0
  38. data/test/controllers/helpers_test.rb +40 -0
  39. data/test/controllers/url_helpers_test.rb +47 -0
  40. data/test/integration/authenticable_test.rb +191 -0
  41. data/test/integration/confirmable_test.rb +60 -0
  42. data/test/integration/recoverable_test.rb +131 -0
  43. data/test/integration/rememberable_test.rb +56 -0
  44. data/test/mailers/confirmation_instructions_test.rb +59 -0
  45. data/test/mailers/reset_password_instructions_test.rb +62 -0
  46. data/test/mapping_test.rb +71 -0
  47. data/test/models/authenticable_test.rb +138 -0
  48. data/test/models/confirmable_test.rb +206 -0
  49. data/test/models/recoverable_test.rb +145 -0
  50. data/test/models/rememberable_test.rb +68 -0
  51. data/test/models/validatable_test.rb +99 -0
  52. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  53. data/test/rails_app/app/controllers/application_controller.rb +10 -0
  54. data/test/rails_app/app/controllers/home_controller.rb +4 -0
  55. data/test/rails_app/app/controllers/users_controller.rb +7 -0
  56. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  57. data/test/rails_app/app/models/account.rb +3 -0
  58. data/test/rails_app/app/models/admin.rb +3 -0
  59. data/test/rails_app/app/models/organizer.rb +3 -0
  60. data/test/rails_app/app/models/user.rb +3 -0
  61. data/test/rails_app/config/boot.rb +110 -0
  62. data/test/rails_app/config/environment.rb +41 -0
  63. data/test/rails_app/config/environments/development.rb +17 -0
  64. data/test/rails_app/config/environments/production.rb +28 -0
  65. data/test/rails_app/config/environments/test.rb +28 -0
  66. data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
  67. data/test/rails_app/config/initializers/session_store.rb +15 -0
  68. data/test/rails_app/config/routes.rb +18 -0
  69. data/test/routes_test.rb +75 -0
  70. data/test/support/assertions_helper.rb +22 -0
  71. data/test/support/integration_tests_helper.rb +66 -0
  72. data/test/support/model_tests_helper.rb +40 -0
  73. data/test/test_helper.rb +39 -0
  74. metadata +136 -0
@@ -0,0 +1,20 @@
1
+ Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,220 @@
1
+ == Devise
2
+
3
+ Devise is a flexible authentication solution for Rails based on Warden. It:
4
+
5
+ * Is Rack based;
6
+ * Is a complete MVC solution based on Rails engines;
7
+ * Allows you to have multiple roles (or models/scopes) signed in at the same time;
8
+ * Is based on a modularity concept: use just what you really need.
9
+
10
+ Right now it's composed of five mainly modules:
11
+
12
+ * Authenticable: responsible for encrypting password and validating authenticity of a user while signing in.
13
+ * Confirmable: responsible for verifying whether an account is already confirmed to sign in, and to send emails with confirmation instructions.
14
+ * Recoverable: takes care of reseting the user password and send reset instructions.
15
+ * Rememberable: manages generating and clearing token for remember the user from a saved cookie.
16
+ * Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.
17
+
18
+ There's an example application using Devise at http://github.com/plataformatec/devise_example .
19
+
20
+ == Dependencies
21
+
22
+ Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Please ensure you have it installed in order to use devise (see instalation below).
23
+
24
+ == Installation
25
+
26
+ All gems are on gemcutter, so you need to add gemcutter to your sources if you haven't yet:
27
+
28
+ sudo gem sources -a http://gemcutter.org/
29
+
30
+ Install warden gem if you don't have it installed (requires 0.5.0 or higher):
31
+
32
+ sudo gem install warden
33
+
34
+ Install devise inside your app:
35
+
36
+ sudo gem install devise
37
+
38
+ And you're ready to go.
39
+
40
+ == Basic Usage
41
+
42
+ Devise must be setted up within the model (or models) you want to use, and devise routes must be created inside your routes.rb file.
43
+
44
+ We're assuming here you want a User model. First of all you have to setup a migration with the following fields:
45
+
46
+ create_table :users do
47
+ t.authenticable
48
+ t.confirmable
49
+ t.recoverable
50
+ t.rememberable
51
+ t.timestamps
52
+ end
53
+
54
+ You may also want to add some indexes to improve performance:
55
+
56
+ add_index :your_table, :email
57
+ add_index :your_table, :confirmation_token # for confirmable
58
+ add_index :your_table, :reset_password_token # for recoverable
59
+
60
+ Now let's setup a User model adding the devise line to have your authentication working:
61
+
62
+ class User < ActiveRecord::Base
63
+ devise
64
+ end
65
+
66
+ This line adds devise authenticable automatically for you inside your User class. You could also include the other modules as below:
67
+
68
+ # Same as using only devise, authenticable is activated by default
69
+ devise :authenticable
70
+
71
+ # Include authenticable + confirmable
72
+ devise :confirmable
73
+
74
+ # Include authenticable + recoverable
75
+ devise :recoverable
76
+
77
+ # Include authenticable + rememberable modules
78
+ devise :rememberable
79
+
80
+ # Include authenticable + confirmable + recoverable + rememberable + validatable
81
+ devise :confirmable, :recoverable, :rememberable, :validatable
82
+
83
+ # Same as above, include all of them
84
+ devise :all
85
+
86
+ # Include all except recoverable
87
+ devise :all, :except => :recoverable
88
+
89
+ Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable.
90
+
91
+ In addition to :except, you can provide some options to devise call:
92
+
93
+ * pepper: setup a pepper to generate de encrypted password. By default no pepper is used:
94
+
95
+ devise :all, :pepper => 'my_pepper'
96
+
97
+ * stretches: configure how many times you want the password is reencrypted.
98
+
99
+ devise :all, :stretches => 20
100
+
101
+ The next step after setting up your model is to configure your routes for devise. You do this by opening up your config/routes.rb and adding:
102
+
103
+ map.devise_for :users
104
+
105
+ This is going to look inside you User model and create the needed routes:
106
+
107
+ # Session routes for Authenticable (default)
108
+ new_user_session GET /users/sign_in {:controller=>"sessions", :action=>"new"}
109
+ user_session POST /users/sign_in {:controller=>"sessions", :action=>"create"}
110
+ destroy_user_session GET /users/sign_out {:controller=>"sessions", :action=>"destroy"}
111
+
112
+ # Password routes for Recoverable, if User model has :recoverable configured
113
+ new_user_password GET /users/password/new(.:format) {:controller=>"passwords", :action=>"new"}
114
+ edit_user_password GET /users/password/edit(.:format) {:controller=>"passwords", :action=>"edit"}
115
+ user_password PUT /users/password(.:format) {:controller=>"passwords", :action=>"update"}
116
+ POST /users/password(.:format) {:controller=>"passwords", :action=>"create"}
117
+
118
+ # Confirmation routes for Confirmable, if User model has :confirmable configured
119
+ new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
120
+ user_confirmation GET /users/confirmation(.:format) {:controller=>"confirmations", :action=>"show"}
121
+ POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"}
122
+
123
+ You can run the routes rake task to verify what routes are being created by devise.
124
+ There are also some options available for configuring your routes:
125
+
126
+ * :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name.
127
+
128
+ map.devise_for :users, :class_name => 'Account'
129
+
130
+ * :as => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users:
131
+
132
+ map.devise_for :users, :as => 'accounts'
133
+
134
+ * :singular => setup the name used to create named routes. By default, for a :users key, it is going to be the singularized version, :user. To configure a named route like account_session_path instead of user_session_path just do:
135
+
136
+ map.devise_for :users, :singular => :account
137
+
138
+ * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation.
139
+
140
+ map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
141
+
142
+ And that is it! Devise is gonna create some helpers to use inside your controllers and views. To setup a controller that needs user authentication, just add this before_filter:
143
+
144
+ before_filter :authenticate_user!
145
+
146
+ To verify if a user is signed in, you have the following helper:
147
+
148
+ user_signed_in?
149
+
150
+ And to get the current signed in user this helper is available:
151
+
152
+ current_user
153
+
154
+ You have also access to the session for this scope:
155
+
156
+ user_session
157
+
158
+ After signing in a user, confirming it's account or updating it's password, devise will look for a scoped root path to redirect. Example: For a :user resource, it will use user_root_path if it exists, otherwise default root_path will be used.
159
+
160
+ Devise let's you setup as many roles as you want, so let's say you already have this User model and also want an Admin model with the same authentication stuff, but not confirmation or password recovery. Just follow the same steps:
161
+
162
+ # Create a migration with the required fields
163
+ create_table :admins do |t|
164
+ t.authenticable
165
+ end
166
+
167
+ # Inside your Admin model
168
+ devise :validatable
169
+
170
+ # Inside your routes
171
+ map.devise_for :admin
172
+
173
+ # Inside your protected controller
174
+ before_filter :authenticate_admin!
175
+
176
+ # Inside your controllers and views
177
+ admin_signed_in?
178
+ current_admin
179
+ admin_session
180
+
181
+ == I18n
182
+
183
+ Devise check for flash messages using i18n, so you're able to customize them easily. For example, to change the sign in message you should setup your locale file this way:
184
+
185
+ en:
186
+ devise:
187
+ sessions:
188
+ signed_in: 'Signed in successfully.'
189
+
190
+ You can also create distinct messages based on the resource you've configured:
191
+
192
+ en:
193
+ devise:
194
+ sessions:
195
+ user:
196
+ signed_in: 'Welcome user, you are signed in.'
197
+ admin:
198
+ signed_in: 'Hello admin!'
199
+
200
+ Devise notifier uses the same pattern to create subject messages, but it is not able to know what scope you are, he just know the record (ie user instance) that was sent to it. So you need to customize messages based on the model class name (usually the same as the resource name, if you follow basic conventions):
201
+
202
+ en:
203
+ devise:
204
+ notifier:
205
+ confirmation_instructions: 'Hello everybody!'
206
+ user:
207
+ confirmation_instructions: 'Hello User! Please confirm your email'
208
+ reset_password_instructions: 'Reset instructions'
209
+
210
+ Take a look at our locale file to check all available messages.
211
+
212
+ == TODO
213
+
214
+ Please refer to TODO file.
215
+
216
+ == Bugs and Feedback
217
+
218
+ If you discover any bugs or want to drop a line, feel free to create an issue.
219
+
220
+ MIT License. Copyright 2009 Plataforma Tecnologia. http://blog.plataformatec.com.br
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rake'
4
+ require 'rake/testtask'
5
+ require 'rake/rdoctask'
6
+ require File.join(File.dirname(__FILE__), 'lib', 'devise', 'version')
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test Devise.'
12
+ Rake::TestTask.new(:test) do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
18
+
19
+ desc 'Generate documentation for Devise.'
20
+ Rake::RDocTask.new(:rdoc) do |rdoc|
21
+ rdoc.rdoc_dir = 'rdoc'
22
+ rdoc.title = 'Devise'
23
+ rdoc.options << '--line-numbers' << '--inline-source'
24
+ rdoc.rdoc_files.include('README.rdoc')
25
+ rdoc.rdoc_files.include('lib/**/*.rb')
26
+ end
27
+
28
+ begin
29
+ require 'jeweler'
30
+ Jeweler::Tasks.new do |s|
31
+ s.name = "devise"
32
+ s.version = "0.1.0"
33
+ s.summary = "Flexible authentication solution for Rails with Warden"
34
+ s.email = "contact@plataformatec.com.br"
35
+ s.homepage = "http://github.com/plataformatec/devise"
36
+ s.description = "Flexible authentication solution for Rails with Warden"
37
+ s.authors = ['José Valim', 'Carlos Antônio']
38
+ s.files = FileList["[A-Z]*", "{app,config,lib}/**/*", "init.rb"]
39
+ s.add_dependency("warden", "~> 0.5.0")
40
+ end
41
+
42
+ Jeweler::GemcutterTasks.new
43
+ rescue LoadError
44
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
45
+ end
data/TODO ADDED
@@ -0,0 +1,37 @@
1
+ * Add customizable time frame for remember me
2
+ * Add customizable time frame for confirmation and filters
3
+
4
+ * Create generators
5
+ * Use request_ip in session cookies
6
+ * Session timeout
7
+
8
+ * Devise::BruteForceProtection
9
+ * Devise::MagicColumns
10
+ * Devise::Invitable
11
+
12
+ == Done
13
+
14
+ * Devise::Authenticable
15
+ * Devise::Confirmable
16
+ * Devise::Recoverable
17
+ * Devise::Validatable
18
+ * Devise::Migratable
19
+ * SessionsController
20
+ * PasswordsController
21
+ * ConfirmationsController
22
+ * Create an example app
23
+ * devise :authenticable, :confirmable, :recoverable for ActiveRecord
24
+ * Allow multiple models per controller
25
+ * Add mappings
26
+ * Use sign_in and sign_out in SessionsController
27
+ * Use path_names in routes
28
+ * Store session[:return_to] in session
29
+ * Clear perishable_token in :confirmable and :recoverable
30
+ * Remove perishable token and create attributes for confirmation_token and reset_password_token
31
+ * Add confirmation_sent_at for confirmable
32
+ * Add confirmable filters
33
+ * Sign user in automatically after confirming or changing it's password
34
+
35
+ * Add remember me
36
+ * Mailer subjects namespaced by model
37
+ * Allow stretches and pepper per model
@@ -0,0 +1,32 @@
1
+ class ConfirmationsController < ApplicationController
2
+ before_filter :is_devise_resource?
3
+
4
+ # GET /resource/confirmation/new
5
+ def new
6
+ end
7
+
8
+ # POST /resource/confirmation
9
+ def create
10
+ self.resource = resource_class.send_confirmation_instructions(params[resource_name])
11
+
12
+ if resource.errors.empty?
13
+ set_flash_message :success, :send_instructions
14
+ redirect_to new_session_path(resource_name)
15
+ else
16
+ render :new
17
+ end
18
+ end
19
+
20
+ # GET /resource/confirmation?perishable_token=abcdef
21
+ def show
22
+ self.resource = resource_class.confirm!(:confirmation_token => params[:confirmation_token])
23
+
24
+ if resource.errors.empty?
25
+ sign_in(resource_name, resource)
26
+ set_flash_message :success, :confirmed
27
+ redirect_to home_or_root_path
28
+ else
29
+ render :new
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,38 @@
1
+ class PasswordsController < ApplicationController
2
+ before_filter :is_devise_resource?, :require_no_authentication
3
+
4
+ # GET /resource/password/new
5
+ def new
6
+ end
7
+
8
+ # POST /resource/password
9
+ def create
10
+ self.resource = resource_class.send_reset_password_instructions(params[resource_name])
11
+
12
+ if resource.errors.empty?
13
+ set_flash_message :success, :send_instructions
14
+ redirect_to new_session_path(resource_name)
15
+ else
16
+ render :new
17
+ end
18
+ end
19
+
20
+ # GET /resource/password/edit?perishable_token=abcdef
21
+ def edit
22
+ self.resource = resource_class.new
23
+ resource.reset_password_token = params[:reset_password_token]
24
+ end
25
+
26
+ # PUT /resource/password
27
+ def update
28
+ self.resource = resource_class.reset_password!(params[resource_name])
29
+
30
+ if resource.errors.empty?
31
+ sign_in(resource_name, resource)
32
+ set_flash_message :success, :updated
33
+ redirect_to home_or_root_path
34
+ else
35
+ render :edit
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,35 @@
1
+ class SessionsController < ApplicationController
2
+ before_filter :is_devise_resource?
3
+ before_filter :require_no_authentication, :only => [ :new, :create ]
4
+
5
+ # GET /resource/sign_in
6
+ def new
7
+ unauthenticated! if params[:unauthenticated]
8
+ end
9
+
10
+ # POST /resource/sign_in
11
+ def create
12
+ if authenticate(resource_name)
13
+ set_flash_message :success, :signed_in
14
+ redirect_back_or_to home_or_root_path
15
+ else
16
+ unauthenticated!
17
+ render :new
18
+ end
19
+ end
20
+
21
+ # GET /resource/sign_out
22
+ def destroy
23
+ set_flash_message :success, :signed_out if signed_in?(resource_name)
24
+ sign_out(resource_name)
25
+ redirect_to root_path
26
+ end
27
+
28
+ protected
29
+
30
+ def unauthenticated!
31
+ flash.now[:failure] = I18n.t(:"#{resource_name}.unauthenticated",
32
+ :scope => [:devise, :sessions], :default => :unauthenticated)
33
+ end
34
+
35
+ end
@@ -0,0 +1,47 @@
1
+ class Notifier < ::ActionMailer::Base
2
+ cattr_accessor :sender
3
+
4
+ # Deliver confirmation instructions when the user is created or its email is
5
+ # updated, and also when confirmation is manually requested
6
+ def confirmation_instructions(record)
7
+ setup_mail(record, :confirmation_instructions)
8
+ end
9
+
10
+ # Deliver reset password instructions when manually requested
11
+ def reset_password_instructions(record)
12
+ setup_mail(record, :reset_password_instructions)
13
+ end
14
+
15
+ private
16
+
17
+ # Configure default email options
18
+ def setup_mail(record, key)
19
+ subject translate(record, key)
20
+ from self.class.sender
21
+ recipients record.email
22
+ sent_on Time.now
23
+ content_type 'text/html'
24
+ body underscore_name(record) => record, :resource => record
25
+ end
26
+
27
+ # Setup subject namespaced by model. It means you're able to setup your
28
+ # messages using specific resource scope, or provide a default one.
29
+ # Example (i18n locale file):
30
+ #
31
+ # en:
32
+ # devise:
33
+ # notifier:
34
+ # confirmation_instructions: '...'
35
+ # user:
36
+ # notifier:
37
+ # confirmation_instructions: '...'
38
+ def translate(record, key)
39
+ I18n.t(:"#{underscore_name(record)}.#{key}",
40
+ :scope => [:devise, :notifier],
41
+ :default => key)
42
+ end
43
+
44
+ def underscore_name(record)
45
+ @underscore_name ||= record.class.name.underscore.to_sym
46
+ end
47
+ end