devise-jdguyot 1.2.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. data/.gitignore +10 -0
  2. data/CHANGELOG.rdoc +532 -0
  3. data/Gemfile +29 -0
  4. data/Gemfile.lock +152 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +353 -0
  7. data/Rakefile +36 -0
  8. data/TODO +4 -0
  9. data/app/controllers/devise/confirmations_controller.rb +33 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  11. data/app/controllers/devise/passwords_controller.rb +41 -0
  12. data/app/controllers/devise/registrations_controller.rb +110 -0
  13. data/app/controllers/devise/sessions_controller.rb +25 -0
  14. data/app/controllers/devise/unlocks_controller.rb +34 -0
  15. data/app/helpers/devise_helper.rb +19 -0
  16. data/app/mailers/devise/mailer.rb +88 -0
  17. data/app/views/devise/confirmations/new.html.erb +12 -0
  18. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  19. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  20. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  21. data/app/views/devise/passwords/edit.html.erb +16 -0
  22. data/app/views/devise/passwords/new.html.erb +12 -0
  23. data/app/views/devise/registrations/edit.html.erb +25 -0
  24. data/app/views/devise/registrations/new.html.erb +18 -0
  25. data/app/views/devise/sessions/new.html.erb +17 -0
  26. data/app/views/devise/shared/_links.erb +25 -0
  27. data/app/views/devise/unlocks/new.html.erb +12 -0
  28. data/config/locales/en.yml +46 -0
  29. data/devise.gemspec +25 -0
  30. data/lib/devise/controllers/helpers.rb +227 -0
  31. data/lib/devise/controllers/internal_helpers.rb +119 -0
  32. data/lib/devise/controllers/scoped_views.rb +33 -0
  33. data/lib/devise/controllers/url_helpers.rb +39 -0
  34. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  35. data/lib/devise/encryptors/base.rb +20 -0
  36. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  37. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  38. data/lib/devise/encryptors/sha1.rb +25 -0
  39. data/lib/devise/encryptors/sha512.rb +25 -0
  40. data/lib/devise/failure_app.rb +132 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +12 -0
  43. data/lib/devise/hooks/rememberable.rb +48 -0
  44. data/lib/devise/hooks/timeoutable.rb +22 -0
  45. data/lib/devise/hooks/trackable.rb +9 -0
  46. data/lib/devise/mapping.rb +110 -0
  47. data/lib/devise/models/authenticatable.rb +146 -0
  48. data/lib/devise/models/confirmable.rb +160 -0
  49. data/lib/devise/models/database_authenticatable.rb +100 -0
  50. data/lib/devise/models/encryptable.rb +72 -0
  51. data/lib/devise/models/lockable.rb +169 -0
  52. data/lib/devise/models/omniauthable.rb +23 -0
  53. data/lib/devise/models/recoverable.rb +123 -0
  54. data/lib/devise/models/registerable.rb +21 -0
  55. data/lib/devise/models/rememberable.rb +130 -0
  56. data/lib/devise/models/timeoutable.rb +43 -0
  57. data/lib/devise/models/token_authenticatable.rb +72 -0
  58. data/lib/devise/models/trackable.rb +30 -0
  59. data/lib/devise/models/validatable.rb +65 -0
  60. data/lib/devise/models.rb +68 -0
  61. data/lib/devise/modules.rb +30 -0
  62. data/lib/devise/omniauth/config.rb +30 -0
  63. data/lib/devise/omniauth/test_helpers.rb +57 -0
  64. data/lib/devise/omniauth/url_helpers.rb +29 -0
  65. data/lib/devise/omniauth.rb +47 -0
  66. data/lib/devise/orm/active_record.rb +38 -0
  67. data/lib/devise/orm/mongoid.rb +31 -0
  68. data/lib/devise/path_checker.rb +18 -0
  69. data/lib/devise/rails/routes.rb +292 -0
  70. data/lib/devise/rails/warden_compat.rb +125 -0
  71. data/lib/devise/rails.rb +50 -0
  72. data/lib/devise/schema.rb +97 -0
  73. data/lib/devise/strategies/authenticatable.rb +150 -0
  74. data/lib/devise/strategies/base.rb +15 -0
  75. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  76. data/lib/devise/strategies/rememberable.rb +51 -0
  77. data/lib/devise/strategies/token_authenticatable.rb +53 -0
  78. data/lib/devise/test_helpers.rb +100 -0
  79. data/lib/devise/version.rb +3 -0
  80. data/lib/devise.rb +381 -0
  81. data/lib/generators/active_record/devise_generator.rb +28 -0
  82. data/lib/generators/active_record/templates/migration.rb +31 -0
  83. data/lib/generators/devise/devise_generator.rb +17 -0
  84. data/lib/generators/devise/install_generator.rb +24 -0
  85. data/lib/generators/devise/orm_helpers.rb +23 -0
  86. data/lib/generators/devise/views_generator.rb +106 -0
  87. data/lib/generators/mongoid/devise_generator.rb +17 -0
  88. data/lib/generators/templates/README +25 -0
  89. data/lib/generators/templates/devise.rb +186 -0
  90. data/test/controllers/helpers_test.rb +237 -0
  91. data/test/controllers/internal_helpers_test.rb +72 -0
  92. data/test/controllers/url_helpers_test.rb +59 -0
  93. data/test/devise_test.rb +65 -0
  94. data/test/encryptors_test.rb +30 -0
  95. data/test/failure_app_test.rb +187 -0
  96. data/test/generators/active_record_generator_test.rb +24 -0
  97. data/test/generators/install_generator_test.rb +13 -0
  98. data/test/generators/mongoid_generator_test.rb +22 -0
  99. data/test/generators/views_generator_test.rb +35 -0
  100. data/test/indifferent_hash.rb +33 -0
  101. data/test/integration/authenticatable_test.rb +447 -0
  102. data/test/integration/confirmable_test.rb +104 -0
  103. data/test/integration/database_authenticatable_test.rb +60 -0
  104. data/test/integration/http_authenticatable_test.rb +74 -0
  105. data/test/integration/lockable_test.rb +109 -0
  106. data/test/integration/omniauthable_test.rb +107 -0
  107. data/test/integration/recoverable_test.rb +160 -0
  108. data/test/integration/registerable_test.rb +179 -0
  109. data/test/integration/rememberable_test.rb +180 -0
  110. data/test/integration/timeoutable_test.rb +89 -0
  111. data/test/integration/token_authenticatable_test.rb +99 -0
  112. data/test/integration/trackable_test.rb +64 -0
  113. data/test/mailers/confirmation_instructions_test.rb +84 -0
  114. data/test/mailers/reset_password_instructions_test.rb +72 -0
  115. data/test/mailers/unlock_instructions_test.rb +66 -0
  116. data/test/mapping_test.rb +119 -0
  117. data/test/models/confirmable_test.rb +221 -0
  118. data/test/models/database_authenticatable_test.rb +98 -0
  119. data/test/models/encryptable_test.rb +65 -0
  120. data/test/models/lockable_test.rb +204 -0
  121. data/test/models/recoverable_test.rb +190 -0
  122. data/test/models/rememberable_test.rb +279 -0
  123. data/test/models/timeoutable_test.rb +28 -0
  124. data/test/models/token_authenticatable_test.rb +37 -0
  125. data/test/models/trackable_test.rb +5 -0
  126. data/test/models/validatable_test.rb +99 -0
  127. data/test/models_test.rb +84 -0
  128. data/test/omniauth/url_helpers_test.rb +47 -0
  129. data/test/orm/active_record.rb +9 -0
  130. data/test/orm/mongoid.rb +11 -0
  131. data/test/rails_app/Rakefile +10 -0
  132. data/test/rails_app/app/active_record/admin.rb +6 -0
  133. data/test/rails_app/app/active_record/shim.rb +2 -0
  134. data/test/rails_app/app/active_record/user.rb +8 -0
  135. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  136. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  137. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  138. data/test/rails_app/app/controllers/home_controller.rb +16 -0
  139. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  140. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  141. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +7 -0
  142. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  143. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  144. data/test/rails_app/app/mongoid/admin.rb +9 -0
  145. data/test/rails_app/app/mongoid/shim.rb +29 -0
  146. data/test/rails_app/app/mongoid/user.rb +10 -0
  147. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  148. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  149. data/test/rails_app/app/views/home/index.html.erb +1 -0
  150. data/test/rails_app/app/views/home/private.html.erb +1 -0
  151. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  152. data/test/rails_app/app/views/users/index.html.erb +1 -0
  153. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  154. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  155. data/test/rails_app/config/application.rb +40 -0
  156. data/test/rails_app/config/boot.rb +13 -0
  157. data/test/rails_app/config/database.yml +18 -0
  158. data/test/rails_app/config/environment.rb +5 -0
  159. data/test/rails_app/config/environments/development.rb +19 -0
  160. data/test/rails_app/config/environments/production.rb +33 -0
  161. data/test/rails_app/config/environments/test.rb +33 -0
  162. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  163. data/test/rails_app/config/initializers/devise.rb +176 -0
  164. data/test/rails_app/config/initializers/inflections.rb +2 -0
  165. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  166. data/test/rails_app/config/routes.rb +55 -0
  167. data/test/rails_app/config.ru +4 -0
  168. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +31 -0
  169. data/test/rails_app/db/schema.rb +52 -0
  170. data/test/rails_app/lib/shared_admin.rb +9 -0
  171. data/test/rails_app/lib/shared_user.rb +23 -0
  172. data/test/rails_app/public/404.html +26 -0
  173. data/test/rails_app/public/422.html +26 -0
  174. data/test/rails_app/public/500.html +26 -0
  175. data/test/rails_app/public/favicon.ico +0 -0
  176. data/test/rails_app/script/rails +10 -0
  177. data/test/routes_test.rb +179 -0
  178. data/test/support/assertions.rb +24 -0
  179. data/test/support/helpers.rb +60 -0
  180. data/test/support/integration.rb +88 -0
  181. data/test/support/locale/en.yml +4 -0
  182. data/test/support/webrat/integrations/rails.rb +24 -0
  183. data/test/test_helper.rb +29 -0
  184. data/test/test_helpers_test.rb +118 -0
  185. metadata +388 -0
data/Gemfile.lock ADDED
@@ -0,0 +1,152 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ devise (1.2.rc)
5
+ bcrypt-ruby (~> 2.1.2)
6
+ orm_adapter (~> 0.0.3)
7
+ warden (~> 1.0.3)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ abstract (1.0.0)
13
+ actionmailer (3.0.3)
14
+ actionpack (= 3.0.3)
15
+ mail (~> 2.2.9)
16
+ actionpack (3.0.3)
17
+ activemodel (= 3.0.3)
18
+ activesupport (= 3.0.3)
19
+ builder (~> 2.1.2)
20
+ erubis (~> 2.6.6)
21
+ i18n (~> 0.4)
22
+ rack (~> 1.2.1)
23
+ rack-mount (~> 0.6.13)
24
+ rack-test (~> 0.5.6)
25
+ tzinfo (~> 0.3.23)
26
+ activemodel (3.0.3)
27
+ activesupport (= 3.0.3)
28
+ builder (~> 2.1.2)
29
+ i18n (~> 0.4)
30
+ activerecord (3.0.3)
31
+ activemodel (= 3.0.3)
32
+ activesupport (= 3.0.3)
33
+ arel (~> 2.0.2)
34
+ tzinfo (~> 0.3.23)
35
+ activerecord-jdbc-adapter (1.0.2-java)
36
+ activerecord-jdbcsqlite3-adapter (1.0.2-java)
37
+ activerecord-jdbc-adapter (= 1.0.2)
38
+ jdbc-sqlite3 (~> 3.6.0)
39
+ activeresource (3.0.3)
40
+ activemodel (= 3.0.3)
41
+ activesupport (= 3.0.3)
42
+ activesupport (3.0.3)
43
+ addressable (2.2.2)
44
+ arel (2.0.4)
45
+ bcrypt-ruby (2.1.2)
46
+ bson (1.1.2)
47
+ bson_ext (1.1.2)
48
+ builder (2.1.2)
49
+ erubis (2.6.6)
50
+ abstract (>= 1.0.0)
51
+ faraday (0.5.2)
52
+ addressable (~> 2.2.2)
53
+ multipart-post (~> 1.0.1)
54
+ rack (>= 1.1.0, < 2)
55
+ i18n (0.4.2)
56
+ jdbc-sqlite3 (3.6.14.2.056-java)
57
+ mail (2.2.10)
58
+ activesupport (>= 2.3.6)
59
+ i18n (~> 0.4.1)
60
+ mime-types (~> 1.16)
61
+ treetop (~> 1.4.8)
62
+ mime-types (1.16)
63
+ mocha (0.9.9)
64
+ rake
65
+ mongo (1.1.2)
66
+ bson (>= 1.1.1)
67
+ mongoid (2.0.0.beta.20)
68
+ activemodel (~> 3.0)
69
+ mongo (~> 1.1)
70
+ tzinfo (~> 0.3.22)
71
+ will_paginate (~> 3.0.pre)
72
+ multi_json (0.0.5)
73
+ multipart-post (1.0.1)
74
+ nokogiri (1.4.3.1)
75
+ nokogiri (1.4.3.1-java)
76
+ weakling (>= 0.0.3)
77
+ oa-core (0.1.6)
78
+ rack (~> 1.1)
79
+ oa-oauth (0.1.6)
80
+ multi_json (~> 0.0.2)
81
+ nokogiri (~> 1.4.2)
82
+ oa-core (= 0.1.6)
83
+ oauth (~> 0.4.0)
84
+ oauth2 (~> 0.1.0)
85
+ oa-openid (0.1.6)
86
+ oa-core (= 0.1.6)
87
+ rack-openid (~> 1.2.0)
88
+ ruby-openid-apps-discovery
89
+ oauth (0.4.4)
90
+ oauth2 (0.1.0)
91
+ faraday (~> 0.5.0)
92
+ multi_json (~> 0.0.4)
93
+ orm_adapter (0.0.3)
94
+ polyglot (0.3.1)
95
+ rack (1.2.1)
96
+ rack-mount (0.6.13)
97
+ rack (>= 1.0.0)
98
+ rack-openid (1.2.0)
99
+ rack (>= 1.1.0)
100
+ ruby-openid (>= 2.1.8)
101
+ rack-test (0.5.6)
102
+ rack (>= 1.0)
103
+ rails (3.0.3)
104
+ actionmailer (= 3.0.3)
105
+ actionpack (= 3.0.3)
106
+ activerecord (= 3.0.3)
107
+ activeresource (= 3.0.3)
108
+ activesupport (= 3.0.3)
109
+ bundler (~> 1.0)
110
+ railties (= 3.0.3)
111
+ railties (3.0.3)
112
+ actionpack (= 3.0.3)
113
+ activesupport (= 3.0.3)
114
+ rake (>= 0.8.7)
115
+ thor (~> 0.14.4)
116
+ rake (0.8.7)
117
+ ruby-openid (2.1.8)
118
+ ruby-openid-apps-discovery (1.2.0)
119
+ ruby-openid (>= 2.1.7)
120
+ sqlite3-ruby (1.3.2)
121
+ thor (0.14.6)
122
+ treetop (1.4.9)
123
+ polyglot (>= 0.3.1)
124
+ tzinfo (0.3.23)
125
+ warden (1.0.3)
126
+ rack (>= 1.0.0)
127
+ weakling (0.0.4-java)
128
+ webrat (0.7.2)
129
+ nokogiri (>= 1.2.0)
130
+ rack (>= 1.0)
131
+ rack-test (>= 0.5.3)
132
+ will_paginate (3.0.pre2)
133
+
134
+ PLATFORMS
135
+ java
136
+ ruby
137
+
138
+ DEPENDENCIES
139
+ activerecord-jdbcsqlite3-adapter
140
+ bcrypt-ruby (~> 2.1.2)
141
+ bson_ext (= 1.1.2)
142
+ devise!
143
+ mocha
144
+ mongo (= 1.1.2)
145
+ mongoid (= 2.0.0.beta.20)
146
+ oa-oauth
147
+ oa-openid
148
+ orm_adapter (~> 0.0.3)
149
+ rails (~> 3.0.0)
150
+ sqlite3-ruby
151
+ warden (~> 1.0.3)
152
+ webrat (= 0.7.2)
data/MIT-LICENSE ADDED
@@ -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.
data/README.rdoc ADDED
@@ -0,0 +1,353 @@
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
+ It's composed of 12 modules:
11
+
12
+ * Database Authenticatable: encrypts and stores a password in the database to validate the authenticity of an user while signing in. The authentication can be done both through POST requests or HTTP Basic Authentication.
13
+ * Token Authenticatable: signs in a user based on an authentication token (also known as "single access token"). The token can be given both through query string or HTTP Basic Authentication.
14
+ * Omniauthable: adds Omniauth (github.com/intridea/omniauth) support;
15
+ * Confirmable: sends emails with confirmation instructions and verifies whether an account is already confirmed during sign in.
16
+ * Recoverable: resets the user password and sends reset instructions.
17
+ * Registerable: handles signing up users through a registration process, also allowing them to edit and destroy their account.
18
+ * Rememberable: manages generating and clearing a token for remembering the user from a saved cookie.
19
+ * Trackable: tracks sign in count, timestamps and IP address.
20
+ * Timeoutable: expires sessions that have no activity in a specified period of time.
21
+ * Validatable: provides validations of email and password. It's optional and can be customized, so you're able to define your own validations.
22
+ * Lockable: locks an account after a specified number of failed sign-in attempts. Can unlock via email or after a specified time period.
23
+ * Encryptable: adds support of other authentication mechanisms besides the built-in Bcrypt (the default).
24
+
25
+ == Information
26
+
27
+ === The Devise wiki
28
+
29
+ The Devise Wiki has lots of additional information about Devise including many "how-to" articles and answers to the most frequently asked questions. Please browse the Wiki after finishing this README:
30
+
31
+ http://wiki.github.com/plataformatec/devise
32
+
33
+ === Bug reports
34
+
35
+ If you discover a problem with Devise, we would like to know about it. However, we ask that you please review these guidelines before submitting a bug report:
36
+
37
+ http://github.com/plataformatec/devise/wiki/Bug-reports
38
+
39
+ If you found a security bug, do *NOT* use the GitHub issue tracker. Send email or a private GitHub message to the maintainers listed at the bottom of the README.
40
+
41
+ === Mailing list
42
+
43
+ If you have any questions, comments, or concerns, please use the Google Group instead of the GitHub issue tracker:
44
+
45
+ http://groups.google.com/group/plataformatec-devise
46
+
47
+ === RDocs
48
+
49
+ You can view the Devise documentation in RDoc format here:
50
+
51
+ http://rubydoc.info/github/plataformatec/devise/master/frames
52
+
53
+ If you need to use Devise with Rails 2.3, you can always run `gem server` from the command line after you install the gem to access the old documentation.
54
+
55
+ === Example applications
56
+
57
+ There are a few example applications available on GitHub that demonstrate various features of Devise with different versions of Rails. You can view them here:
58
+
59
+ http://github.com/plataformatec/devise/wiki/Example-Applications
60
+
61
+ === Extensions
62
+
63
+ Our community has created a number of extensions that add functionality above and beyond what is included with Devise. You can view a list of available extensions and add your own here:
64
+
65
+ http://github.com/plataformatec/devise/wiki/Extensions
66
+
67
+ === Contributing
68
+
69
+ We hope that you will consider contributing to Devise. Please read this short overview for some information about how to get started:
70
+
71
+ http://github.com/plataformatec/devise/wiki/Contributing
72
+
73
+ You will usually want to write tests for your changes. To run the test suite, `cd` into Devise's top-level directory and run `bundle install` and `rake`. For the tests to pass, you will need to have a MongoDB server (version 1.6 or newer) running on your system.
74
+
75
+ == Installation
76
+
77
+ You can use the latest Rails 3 gem with the latest Devise gem:
78
+
79
+ gem install devise
80
+
81
+ After you install Devise and add it to your Gemfile, you need to run the generator:
82
+
83
+ rails generate devise:install
84
+
85
+ The generator will install an initializer which describes ALL Devise's configuration options and you MUST take a look at it. When you are done, you are ready to add Devise to any of your models using the generator:
86
+
87
+ rails generate devise MODEL
88
+
89
+ Replace MODEL by the class name you want to add devise, like User, Admin, etc. This will create a model (if one does not exist) and configure it with default Devise modules. The generator will also create a migration file (if your ORM support them) and configure your routes. Continue reading this file to understand exactly what the generator produces and how to use it.
90
+
91
+ Support for Rails 2.3.x can be found by installing Devise 1.0.x from the v1.0 branch.
92
+
93
+ == Getting started
94
+
95
+ This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration.
96
+
97
+ Devise must be set up within the model (or models) you want to use. Devise routes must be created inside your config/routes.rb file.
98
+
99
+ We're assuming here you want a User model with some Devise modules, as outlined below:
100
+
101
+ class User < ActiveRecord::Base
102
+ devise :database_authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
103
+ end
104
+
105
+ After you choose which modules to use, you need to set up your migrations. Luckily, Devise has some helpers to save you from this boring work:
106
+
107
+ create_table :users do |t|
108
+ t.database_authenticatable
109
+ t.confirmable
110
+ t.recoverable
111
+ t.rememberable
112
+ t.trackable
113
+ t.timestamps
114
+ end
115
+
116
+ Devise doesn't use _attr_accessible_ or _attr_protected_ inside its modules, so be sure to define attributes as accessible or protected in your model.
117
+
118
+ Configure your routes after setting up your model. Open your config/routes.rb file and add:
119
+
120
+ devise_for :users
121
+
122
+ This will use your User model to create a set of needed routes (you can see them by running `rake routes`). If you invoked the devise generator, you noticed that this is exactly what the generator produces for us: model, routes and migrations.
123
+
124
+ Don't forget to run rake db:migrate and you are ready to go! But don't stop reading here, we still have a lot to tell you.
125
+
126
+ === Controller filters and helpers
127
+
128
+ Devise will create some helpers to use inside your controllers and views. To set up a controller with user authentication, just add this before_filter:
129
+
130
+ before_filter :authenticate_user!
131
+
132
+ To verify if a user is signed in, use the following helper:
133
+
134
+ user_signed_in?
135
+
136
+ For the current signed-in user, this helper is available:
137
+
138
+ current_user
139
+
140
+ You can access the session for this scope:
141
+
142
+ user_session
143
+
144
+ After signing in a user, confirming the account or updating the 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. This means that you need to set the root inside your routes:
145
+
146
+ root :to => "home"
147
+
148
+ You can also overwrite after_sign_in_path_for and after_sign_out_path_for to customize your redirect hooks.
149
+
150
+ Finally, you need to set up default url options for the mailer in each environment. Here is the configuration for config/environments/development.rb:
151
+
152
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
153
+
154
+ Notice that if your devise model is not called "user" but "member", then the helpers you should use are:
155
+
156
+ before_filter :authenticate_member!
157
+
158
+ member_signed_in?
159
+
160
+ current_member
161
+
162
+ member_session
163
+
164
+ === Configuring Models
165
+
166
+ The devise method in your models also accepts some options to configure its modules. For example, you can choose which encryptor to use in database_authenticatable:
167
+
168
+ devise :database_authenticatable, :confirmable, :recoverable, :stretches => 20
169
+
170
+ Besides :stretches, you can define :pepper, :encryptor, :confirm_within, :remember_for, :timeout_in, :unlock_in and other values. For details, see the initializer file that was created when you invoked the "devise:install" generator described above.
171
+
172
+ === Configuring multiple models
173
+
174
+ Devise allows you to set up as many roles as you want. For example, you may have a User model and also want an Admin model with just authentication, trackable, lockable and timeoutable features and no confirmation or password-recovery features. Just follow these steps:
175
+
176
+ # Create a migration with the required fields
177
+ create_table :admins do |t|
178
+ t.database_authenticatable
179
+ t.lockable
180
+ t.trackable
181
+ t.timestamps
182
+ end
183
+
184
+ # Inside your Admin model
185
+ devise :database_authenticatable, :trackable, :timeoutable, :lockable
186
+
187
+ # Inside your routes
188
+ devise_for :admins
189
+
190
+ # Inside your protected controller
191
+ before_filter :authenticate_admin!
192
+
193
+ # Inside your controllers and views
194
+ admin_signed_in?
195
+ current_admin
196
+ admin_session
197
+
198
+ === Configuring views
199
+
200
+ We built Devise to help you quickly develop an application that uses authentication. However, we don't want to be in your way when you need to customize it.
201
+
202
+ Since Devise is an engine, all its views are packaged inside the gem. These views will help you get started, but after sometime you may want to change them. If this is the case, you just need to invoke the following generator, and it will copy all views to your application:
203
+
204
+ rails generate devise:views
205
+
206
+ Devise currently supports generating views for the following template engines (use the `-e` flag for the devise:views generator):
207
+
208
+ * Erb
209
+ * Haml (http://github.com/nex3/haml)
210
+ * Slim (http://github.com/stonean/slim)
211
+
212
+ Note: If you are generating Haml or Slim templates, you will need to have a few dependencies such as `hpricot` (for both Haml and Slim) and `haml2slim` (for Slim) installed.
213
+
214
+ If you have more than one role in your application (such as "User" and "Admin"), you will notice that Devise uses the same views for all roles. Fortunately, Devise offers an easy way to customize views. All you need to do is set "config.scoped_views = true" inside "config/initializers/devise.rb".
215
+
216
+ After doing so, you will be able to have views based on the role like "users/sessions/new" and "admins/sessions/new". If no view is found within the scope, Devise will use the default view at "devise/sessions/new". You can also use the generator to generate scoped views:
217
+
218
+ rails generate devise:views users
219
+
220
+ === Configuring controllers
221
+
222
+ If the customization at the views level is not enough, you can customize each controller by following these steps:
223
+
224
+ 1) Create your custom controller, for example a Admins::SessionsController:
225
+
226
+ class Admins::SessionsController < Devise::SessionsController
227
+ end
228
+
229
+ 2) Tell the router to use this controller:
230
+
231
+ devise_for :admins, :controllers => { :sessions => "admins/sessions" }
232
+
233
+ 3) And since we changed the controller, it won't use the "devise/sessions" views, so remember to copy "devise/sessions" to "admin/sessions".
234
+
235
+ Remember that Devise uses flash messages to let users know if sign in was successful or failed. Devise expects your application to call "flash[:notice]" and "flash[:alert]" as appropriate.
236
+
237
+ === Configuring routes
238
+
239
+ Devise also ships with default routes. If you need to customize them, you should probably be able to do it through the devise_for method. It accepts several options like :class_name, :path_prefix and so on, including the possibility to change path names for I18n:
240
+
241
+ devise_for :users, :path => "usuarios", :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification', :unlock => 'unblock', :registration => 'register', :sign_up => 'cmon_let_me_in' }
242
+
243
+ Be sure to check devise_for documentation for details.
244
+
245
+ If you have the need for more deep customization, for instance to also allow "/sign_in" besides "/users/sign_in", all you need to do is to create your routes normally and wrap them in a +devise_scope+ block in the router:
246
+
247
+ devise_scope :user do
248
+ get "sign_in", :to => "devise/sessions#new"
249
+ end
250
+
251
+ This way you tell devise to use the scope :user when "/sign_in" is accessed. Notice +devise_scope+ is also aliased as +as+ and you can also give a block to +devise_for+, resulting in the same behavior:
252
+
253
+ devise_for :users do
254
+ get "sign_in", :to => "devise/sessions#new"
255
+ end
256
+
257
+ Feel free to choose the one you prefer!
258
+
259
+ === I18n
260
+
261
+ Devise uses flash messages with I18n with the flash keys :success and :failure. To customize your app, you can set up your locale file:
262
+
263
+ en:
264
+ devise:
265
+ sessions:
266
+ signed_in: 'Signed in successfully.'
267
+
268
+ You can also create distinct messages based on the resource you've configured using the singular name given in routes:
269
+
270
+ en:
271
+ devise:
272
+ sessions:
273
+ user:
274
+ signed_in: 'Welcome user, you are signed in.'
275
+ admin:
276
+ signed_in: 'Hello admin!'
277
+
278
+ The Devise mailer uses a similar pattern to create subject messages:
279
+
280
+ en:
281
+ devise:
282
+ mailer:
283
+ confirmation_instructions:
284
+ subject: 'Hello everybody!'
285
+ user_subject: 'Hello User! Please confirm your email'
286
+ reset_password_instructions:
287
+ subject: 'Reset instructions'
288
+
289
+ Take a look at our locale file to check all available messages. You may also be interested in one of the many translations that are available on our wiki:
290
+
291
+ http://github.com/plataformatec/devise/wiki/I18n
292
+
293
+ === Test helpers
294
+
295
+ Devise includes some tests helpers for functional specs. To use them, you just need to include Devise::TestHelpers in your test class and use the sign_in and sign_out method. Such methods have the same signature as in controllers:
296
+
297
+ sign_in :user, @user # sign_in(scope, resource)
298
+ sign_in @user # sign_in(resource)
299
+
300
+ sign_out :user # sign_out(scope)
301
+ sign_out @user # sign_out(resource)
302
+
303
+ You can include the Devise Test Helpers in all of your tests by adding the following to the bottom of your test/test_helper.rb file:
304
+
305
+ class ActionController::TestCase
306
+ include Devise::TestHelpers
307
+ end
308
+
309
+ If you're using RSpec and want the helpers automatically included within all +describe+ blocks, add a file called spec/support/devise.rb with the following contents:
310
+
311
+ RSpec.configure do |config|
312
+ config.include Devise::TestHelpers, :type => :controller
313
+ end
314
+
315
+ Do not use such helpers for integration tests such as Cucumber or Webrat. Instead, fill in the form or explicitly set the user in session. For more tips, check the wiki (http://wiki.github.com/plataformatec/devise).
316
+
317
+ === OAuth2
318
+
319
+ Devise comes with OAuth support out of the box if you're using Devise from the git repository (for now). You can read more about OAuth2 support in the wiki:
320
+
321
+ * http://github.com/plataformatec/devise/wiki/OAuth2:-Overview
322
+ * http://github.com/plataformatec/devise/wiki/OAuth2:-Testing
323
+
324
+ === Other ORMs
325
+
326
+ Devise supports ActiveRecord (default) and Mongoid. To choose other ORM, you just need to require it in the initializer file.
327
+
328
+ === Migrating from other solutions
329
+
330
+ Devise implements encryption strategies for Clearance, Authlogic and Restful-Authentication. To make use of these strategies, you need set the desired encryptor in the encryptor initializer config option and add :encryptable to your model. You might also need to rename your encrypted password and salt columns to match Devise's fields (encrypted_password and password_salt).
331
+
332
+ == Additional information
333
+
334
+ === Warden
335
+
336
+ Devise is based on Warden, which is a general Rack authentication framework created by Daniel Neighman. We encourage you to read more about Warden here:
337
+
338
+ http://github.com/hassox/warden
339
+
340
+ === Contributors
341
+
342
+ We have a long list of valued contributors. Check them all at:
343
+
344
+ http://github.com/plataformatec/devise/contributors
345
+
346
+ === Maintainers
347
+
348
+ * José Valim (http://github.com/josevalim)
349
+ * Carlos Antônio da Silva (http://github.com/carlosantoniodasilva)
350
+
351
+ == License
352
+
353
+ MIT License. Copyright 2010 Plataforma Tecnologia. http://blog.plataformatec.com.br
data/Rakefile ADDED
@@ -0,0 +1,36 @@
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 tests for all ORMs.'
9
+ task :default => :pre_commit
10
+
11
+ desc 'Run Devise tests for all ORMs.'
12
+ task :pre_commit do
13
+ Dir[File.join(File.dirname(__FILE__), 'test', 'orm', '*.rb')].each do |file|
14
+ orm = File.basename(file).split(".").first
15
+ # "Some day, my son, rake's inner wisdom will reveal itself. Until then,
16
+ # take this `system` -- may its brute force protect you well."
17
+ exit 1 unless system "rake test DEVISE_ORM=#{orm}"
18
+ end
19
+ end
20
+
21
+ desc 'Run Devise unit tests.'
22
+ Rake::TestTask.new(:test) do |t|
23
+ t.libs << 'lib'
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = true
27
+ end
28
+
29
+ desc 'Generate documentation for Devise.'
30
+ Rake::RDocTask.new(:rdoc) do |rdoc|
31
+ rdoc.rdoc_dir = 'rdoc'
32
+ rdoc.title = 'Devise'
33
+ rdoc.options << '--line-numbers' << '--inline-source'
34
+ rdoc.rdoc_files.include('README.rdoc')
35
+ rdoc.rdoc_files.include('lib/**/*.rb')
36
+ end
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ * Move integration tests to Capybara
2
+ * Better ORM integration
3
+ * Add support to automatically refresh the access token for OAuth
4
+ * Add test to generators using the new Rails::Generators::TestCase
@@ -0,0 +1,33 @@
1
+ class Devise::ConfirmationsController < ApplicationController
2
+ include Devise::Controllers::InternalHelpers
3
+
4
+ # GET /resource/confirmation/new
5
+ def new
6
+ build_resource({})
7
+ render_with_scope :new
8
+ end
9
+
10
+ # POST /resource/confirmation
11
+ def create
12
+ self.resource = resource_class.send_confirmation_instructions(params[resource_name])
13
+
14
+ if resource.errors.empty?
15
+ set_flash_message :notice, :send_instructions
16
+ redirect_to new_session_path(resource_name)
17
+ else
18
+ render_with_scope :new
19
+ end
20
+ end
21
+
22
+ # GET /resource/confirmation?confirmation_token=abcdef
23
+ def show
24
+ self.resource = resource_class.confirm_by_token(params[:confirmation_token])
25
+
26
+ if resource.errors.empty?
27
+ set_flash_message :notice, :confirmed
28
+ sign_in_and_redirect(resource_name, resource)
29
+ else
30
+ render_with_scope :new
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,26 @@
1
+ class Devise::OmniauthCallbacksController < ApplicationController
2
+ include Devise::Controllers::InternalHelpers
3
+
4
+ def failure
5
+ set_flash_message :alert, :failure, :kind => failed_strategy.name.to_s.humanize, :reason => failure_message
6
+ redirect_to after_omniauth_failure_path_for(resource_name)
7
+ end
8
+
9
+ protected
10
+
11
+ def failed_strategy
12
+ env["omniauth.failed_strategy"]
13
+ end
14
+
15
+ def failure_message
16
+ exception = env["omniauth.error"]
17
+ error = exception.error_reason if exception.respond_to?(:error_reason)
18
+ error ||= exception.error if exception.respond_to?(:error)
19
+ error ||= env["omniauth.failure_key"]
20
+ error.to_s.humanize if error
21
+ end
22
+
23
+ def after_omniauth_failure_path_for(scope)
24
+ new_session_path(scope)
25
+ end
26
+ end
@@ -0,0 +1,41 @@
1
+ class Devise::PasswordsController < ApplicationController
2
+ prepend_before_filter :require_no_authentication
3
+ include Devise::Controllers::InternalHelpers
4
+
5
+ # GET /resource/password/new
6
+ def new
7
+ build_resource({})
8
+ render_with_scope :new
9
+ end
10
+
11
+ # POST /resource/password
12
+ def create
13
+ self.resource = resource_class.send_reset_password_instructions(params[resource_name])
14
+
15
+ if resource.errors.empty?
16
+ set_flash_message :notice, :send_instructions
17
+ redirect_to new_session_path(resource_name)
18
+ else
19
+ render_with_scope :new
20
+ end
21
+ end
22
+
23
+ # GET /resource/password/edit?reset_password_token=abcdef
24
+ def edit
25
+ self.resource = resource_class.new
26
+ resource.reset_password_token = params[:reset_password_token]
27
+ render_with_scope :edit
28
+ end
29
+
30
+ # PUT /resource/password
31
+ def update
32
+ self.resource = resource_class.reset_password_by_token(params[resource_name])
33
+
34
+ if resource.errors.empty?
35
+ set_flash_message :notice, :updated
36
+ sign_in_and_redirect(resource_name, resource)
37
+ else
38
+ render_with_scope :edit
39
+ end
40
+ end
41
+ end