merb-auth-slice-password-reset 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. data/.gitignore +15 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +219 -0
  4. data/Rakefile +41 -0
  5. data/TODO +2 -0
  6. data/app/controllers/application.rb +5 -0
  7. data/app/controllers/passwords.rb +49 -0
  8. data/app/helpers/application_helper.rb +64 -0
  9. data/app/helpers/mailer_helper.rb +28 -0
  10. data/app/mailers/password_reset_mailer.rb +13 -0
  11. data/app/mailers/views/password_reset_mailer/password_reset.text.erb +3 -0
  12. data/app/views/layout/merb_auth_slice_password_reset.html.erb +16 -0
  13. data/app/views/passwords/forgot_password.html.erb +7 -0
  14. data/app/views/passwords/reset.html.erb +15 -0
  15. data/config/init.rb +80 -0
  16. data/lib/merb-auth-slice-password-reset.rb +82 -0
  17. data/lib/merb-auth-slice-password-reset/merbtasks.rb +112 -0
  18. data/lib/merb-auth-slice-password-reset/mixins/senile_user.rb +81 -0
  19. data/lib/merb-auth-slice-password-reset/mixins/senile_user/ar_senile_user.rb +19 -0
  20. data/lib/merb-auth-slice-password-reset/mixins/senile_user/dm_senile_user.rb +22 -0
  21. data/lib/merb-auth-slice-password-reset/mixins/senile_user/mm_senile_user.rb +22 -0
  22. data/lib/merb-auth-slice-password-reset/mixins/senile_user/sq_senile_user.rb +20 -0
  23. data/lib/merb-auth-slice-password-reset/slicetasks.rb +18 -0
  24. data/lib/merb-auth-slice-password-reset/spectasks.rb +75 -0
  25. data/public/javascripts/master.js +0 -0
  26. data/public/stylesheets/master.css +2 -0
  27. data/spec/mailers/password_reset_mailer_spec.rb +50 -0
  28. data/spec/mixins/senile_user_spec.rb +111 -0
  29. data/spec/requests/passwords_spec.rb +85 -0
  30. data/spec/spec_helper.rb +61 -0
  31. data/stubs/app/controllers/passwords.rb +13 -0
  32. data/stubs/app/mailers/views/password_reset_mailer/new_password.html.erb +3 -0
  33. data/stubs/app/mailers/views/password_reset_mailer/password_reset.text.erb +5 -0
  34. data/stubs/app/views/passwords/forgot_password.html.erb +7 -0
  35. metadata +169 -0
@@ -0,0 +1,15 @@
1
+ *.gemspec
2
+ pkg
3
+ pkg/*
4
+ */pkg/*
5
+ *.log
6
+ log
7
+ !log*.rb
8
+ */log
9
+ log/*
10
+ */log/*
11
+ coverage
12
+ */coverage
13
+ bundle
14
+ *.DS_Store
15
+ *.tmproj
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Daniel Neighman, Christian Kebekus
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,219 @@
1
+ h3. Fork Info
2
+
3
+ The behavior of this fork is different from its parent. Instead of auto-generating a password
4
+ on the reset action of the Passwords controller a form is sent that asks the user to create a new
5
+ password. The default is for a :password and :password_confirmation field to be sent, but you
6
+ could override that in your own app.
7
+
8
+ h3. MerbAuthSlicePasswordReset
9
+
10
+ This slice provides a way for users to reset their forgotten password. Passwords are mailed to them and they can then use the form provided to reset them.
11
+
12
+ <pre><code>
13
+ class User
14
+ include DataMapper::Resource
15
+ include Merb::Authentication::Mixins::SenileUser
16
+
17
+ property :id, Serial
18
+ end
19
+ </code></pre>
20
+
21
+ The mixin provides a number of methods. The most common are:
22
+
23
+ <pre><code>
24
+ @user.activate # activates (and saves) the user
25
+ @user.activated? # Returns the "active" status of the user
26
+ @user.active? # Alias for activated?
27
+ </pre></code>
28
+
29
+ h3. Migration Requirements
30
+
31
+ The mixin requires some fields to be in-place on your model. Where needed include these in your migrations.
32
+
33
+ <pre><code>
34
+ :password_reset_code, String
35
+ </code></pre>
36
+
37
+ h3. Mailers
38
+
39
+ The slice contains 2 mailing actions that are setup as callback hooks on the model. When the model is created
40
+ a "signup" email is sent with the link to follow to activate the account. Also an activation acknowledgment
41
+ email.
42
+
43
+ h3. Configuration Options
44
+
45
+ These options may be declared in your @init.rb@ or @environment/*.rb@ files
46
+
47
+ Use the standard slice configuration hash to set these up @Merb::Slices::config[:merb_auth_slice_password_reset]@
48
+
49
+
50
+ h4. Required
51
+
52
+ :from_email # The email account to send the email from
53
+ :password_reset_host # The host to go to for activation. This is used to construct the
54
+ # activation link. Symbol, String or Procs are available.
55
+ # Procs will have the @user@ object passed in
56
+
57
+ h4. Optional
58
+
59
+ :password_reset_subject # The subject of the email to send after activation (Welcome)
60
+
61
+ h3. Customizing the emails
62
+
63
+ To customize your emails, rake the slices stubs
64
+
65
+ <pre><code>
66
+ $ rake slices:merb_auth_slice_password_reset:stubs
67
+ </code></pre>
68
+
69
+ This will create stubs of the views in @slices/merb_auth_slice_password_reset/app/mailers/views/@
70
+
71
+ To create HTML emails just add an html template like @signup.html.erb@
72
+
73
+ h3. Customize the Redirect after activation
74
+
75
+ rake the slices stubs as above. There is an @activations.rb@ controller in the
76
+ @slices/merb_auth_slice_password_reset/app/controllers@ directory. You can overwrite the stubbed
77
+ method in there to have it change it's redirection behavior.
78
+
79
+
80
+ # Rake tasks to package/install the gem - edit this to modify the manifest.
81
+ # The slice application: controllers, models, helpers, views.
82
+ # The default layout, as specified in @Merb::Slices::config[:merb_auth_slice_password_reset][:layout]@ change this to :application to use the app's layout.
83
+ # Standard rake tasks available to your application.
84
+ # Your custom application rake tasks.
85
+ # The main slice file - contains all slice setup @logic/config@.
86
+ # Public assets you (optionally) install using @rake slices:merb_auth_slice_password_reset:install@
87
+ # Specs for basis slice behaviour - you usually adapt these for your slice.
88
+ # Stubs of classes/views/files for the end-user to override - usually these mimic the files in @app/@ and/or @public/@; use @rake slices:merb_auth_slice_password_reset:stubs@ to get started with the override stubs. Also, @rake slices:merb_auth_slice_password_reset:patch@ will copy over views to override in addition to the files found in /stubs.
89
+
90
+
91
+ To see all available tasks for MerbAuthSliceActivation run:
92
+
93
+ <pre><code>
94
+ $ rake -T slices:merb_auth_slice_password_reset
95
+ </code></pre>
96
+
97
+
98
+ h3. Instructions for installation:
99
+
100
+ h4. @Gemfile@
101
+
102
+ Add the slice as a regular dependency
103
+
104
+ <pre><code>
105
+ gem "merb-auth-slice-password-reset"
106
+ </code></pre>
107
+
108
+ h4. @config/init.rb@
109
+
110
+ If needed, configure which slices to load and in which order
111
+
112
+ <pre><code>
113
+ Merb::Plugins.config[:merb_slices] = { :queue => ["MerbAuthSlicePasswordReset", ...] }
114
+ </code></pre>
115
+
116
+ Optionally configure the plugins in a before_app_loads callback
117
+
118
+ <pre><code>
119
+ Merb::BootLoader.before_app_loads do
120
+
121
+ Merb::Slices::config[:merb_auth_slice_password_reset][:option] = value
122
+
123
+ end
124
+ </code></pre>
125
+
126
+ h4. @config/router.rb@
127
+
128
+ Example: /merb_auth_slice_password_reset/:controller/:action/:id
129
+
130
+ <pre><code>
131
+ add_slice(:MerbAuthSlicePasswordReset)
132
+ </code></pre>
133
+
134
+ Example: /foo/:controller/:action/:id
135
+
136
+ <pre><code>
137
+ add_slice(:MerbAuthSlicePasswordReset, 'foo') # same as :path => 'foo'
138
+ </code></pre>
139
+
140
+ Example: /:lang/:controller/:action/:id
141
+
142
+ <pre><code>
143
+ add_slice(:MerbAuthSlicePasswordReset, :path => ':lang')
144
+ </code></pre>
145
+
146
+ Example: /:controller/:action/:id
147
+
148
+ <pre><code>
149
+ slice(:MerbAuthSlicePasswordReset)
150
+ </code></pre>
151
+
152
+ Normally you should also run the following rake task:
153
+
154
+ <pre><code>
155
+ $ rake slices:merb_auth_slice_password_reset:install
156
+ </code></pre>
157
+
158
+ h4. Overrides
159
+
160
+ You can put your application-level overrides in:
161
+
162
+ host-app/slices/merb-auth-slice-password-reset/app - controllers, models, views ...
163
+
164
+ Templates are located in this order:
165
+
166
+ # host-app/slices/merb-auth-slice-password-reset/app/views/*
167
+ # gems/merb-auth-slice-password-reset/app/views/*
168
+ # host-app/app/views/*
169
+
170
+ You can use the host application's layout by configuring the
171
+ merb-auth-slice-password-reset slice in a before_app_loads block:
172
+
173
+ <pre><code>
174
+ Merb::Slices.config[:merb_auth_slice_password_reset] = { :layout => :application }
175
+ </code></pre>
176
+
177
+ By default :merb_auth_slice_password_reset is used. If you need to override
178
+ stylesheets or javascripts, just specify your own files in your layout
179
+ instead/in addition to the ones supplied (if any) in
180
+ host-app/public/slices/merb-auth-slice-password-reset.
181
+
182
+ In any case don't edit those files directly as they may be clobbered any time
183
+ rake merb_auth_slice_password_reset:install is run.
184
+
185
+
186
+ h3. About Slices
187
+
188
+ Merb-Slices is a Merb plugin for using and creating application 'slices' which
189
+ help you modularize your application. Usually these are reuseable extractions
190
+ from your main app. In effect, a Slice is just like a regular Merb MVC
191
+ application, both in functionality as well as in structure.
192
+
193
+ When you generate a Slice stub structure, a module is setup to serve as a
194
+ namespace for your controller, models, helpers etc. This ensures maximum
195
+ encapsulation. You could say a Slice is a mixture between a Merb plugin (a
196
+ Gem) and a Merb application, reaping the benefits of both.
197
+
198
+ A host application can 'mount' a Slice inside the router, which means you have
199
+ full over control how it integrates. By default a Slice's routes are prefixed
200
+ by its name (a router :namespace), but you can easily provide your own prefix
201
+ or leave it out, mounting it at the root of your url-schema. You can even
202
+ mount a Slice multiple times and give extra parameters to customize an
203
+ instance's behaviour.
204
+
205
+ A Slice's Application controller uses controller_for_slice to setup slice
206
+ specific behaviour, which mainly affects cascaded view handling. Additionaly,
207
+ this method is available to any kind of controller, so it can be used for
208
+ Merb Mailer too for example.
209
+
210
+ There are many ways which let you customize a Slice's functionality and
211
+ appearance without ever touching the Gem-level code itself. It's not only easy
212
+ to add template/layout overrides, you can also add/modify controllers, models
213
+ and other runtime code from within the host application.
214
+
215
+ To create your own Slice run this (somewhere outside of your merb app):
216
+
217
+ <pre><code>
218
+ $ merb-gen slice <your-lowercase-slice-name>
219
+ </code></pre>
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ # Assume a typical dev checkout to fetch the current merb-core version
5
+ require 'merb-core/version'
6
+
7
+ begin
8
+
9
+ require 'jeweler'
10
+
11
+ Jeweler::Tasks.new do |gemspec|
12
+
13
+ gemspec.version = '1.1.0'
14
+
15
+ gemspec.name = "merb-auth-slice-password-reset"
16
+ gemspec.description = "Merb Slice for password-reset functionality"
17
+ gemspec.summary = "Merb Slice that adds basic password-reset functionality to merb-auth-based merb applications."
18
+
19
+ gemspec.authors = [ "Daniel Neighman", "Christian Kebekus" ]
20
+ gemspec.email = "has.sox@gmail.com"
21
+ gemspec.homepage = "http://merbivore.com/"
22
+
23
+ # Runtime dependencies
24
+ gemspec.add_dependency 'merb-slices', '~> 1.1.0'
25
+ gemspec.add_dependency 'merb-auth-core', '~> 1.1.0'
26
+ gemspec.add_dependency 'merb-auth-more', '~> 1.1.0'
27
+ gemspec.add_dependency 'merb-mailer', '~> 1.1.0'
28
+
29
+ end
30
+
31
+ Jeweler::GemcutterTasks.new
32
+
33
+ rescue LoadError
34
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
35
+ end
36
+
37
+
38
+ require 'spec/rake/spectask'
39
+ require 'merb-core/test/tasks/spectasks'
40
+ desc 'Default: run spec examples'
41
+ task :default => 'spec'
data/TODO ADDED
@@ -0,0 +1,2 @@
1
+ TODO:
2
+
@@ -0,0 +1,5 @@
1
+ class MerbAuthSlicePasswordReset::Application < Merb::Controller
2
+
3
+ controller_for_slice
4
+
5
+ end
@@ -0,0 +1,49 @@
1
+ class MerbAuthSlicePasswordReset::Passwords < MerbAuthSlicePasswordReset::Application
2
+
3
+ def forgot_password
4
+ @login_param_name = Merb::Authentication::Strategies::Basic::Base.login_param
5
+ render
6
+ end
7
+
8
+ def send_confirmation
9
+ @login_param_name = Merb::Authentication::Strategies::Basic::Base.login_param
10
+ @user = Merb::Authentication.user_class.find_with_login_param(@login_param_name, params[@login_param_name])
11
+ if @user
12
+ @user.send_password_reset_notification
13
+ redirect_after_sending_confirmation
14
+ else
15
+ message[:error] = "User with #{@login_param_name} \"%s\" not found".t(params[@login_param_name].freeze)
16
+ render :forgot_password
17
+ end
18
+ end
19
+
20
+ def reset
21
+ @user = Merb::Authentication.user_class.find_with_password_reset_code(params[:password_reset_code])
22
+ raise NotFound if @user.nil?
23
+ render
24
+ end
25
+
26
+ def reset_check
27
+ @user = Merb::Authentication.user_class.find_with_password_reset_code(params[:password_reset_code])
28
+ # FIXME: This only works for DataMapper right now. I assume that the ActiveORM abstraction
29
+ # will have a method that works for all ORMs.
30
+ raise NotFound if @user.nil?
31
+ if @user.update(params[:user])
32
+ redirect_after_password_reset
33
+ else
34
+ message[:error] = @user.errors.map { |e| e.to_s }.join(" and ") if @user.errors
35
+ render :reset
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def redirect_after_password_reset
42
+ redirect "/", :message => {:notice => "Your password has been changed".t}
43
+ end
44
+
45
+ def redirect_after_sending_confirmation
46
+ redirect "/", :message => {:notice => "Password reset confirmation sent".t}
47
+ end
48
+
49
+ end # MerbAuthSlicePasswordReset::Passwords
@@ -0,0 +1,64 @@
1
+ module Merb
2
+ module MerbAuthSlicePasswordReset
3
+ module ApplicationHelper
4
+
5
+ # @param *segments<Array[#to_s]> Path segments to append.
6
+ #
7
+ # @return <String>
8
+ # A path relative to the public directory, with added segments.
9
+ def image_path(*segments)
10
+ public_path_for(:image, *segments)
11
+ end
12
+
13
+ # @param *segments<Array[#to_s]> Path segments to append.
14
+ #
15
+ # @return <String>
16
+ # A path relative to the public directory, with added segments.
17
+ def javascript_path(*segments)
18
+ public_path_for(:javascript, *segments)
19
+ end
20
+
21
+ # @param *segments<Array[#to_s]> Path segments to append.
22
+ #
23
+ # @return <String>
24
+ # A path relative to the public directory, with added segments.
25
+ def stylesheet_path(*segments)
26
+ public_path_for(:stylesheet, *segments)
27
+ end
28
+
29
+ # Construct a path relative to the public directory
30
+ #
31
+ # @param <Symbol> The type of component.
32
+ # @param *segments<Array[#to_s]> Path segments to append.
33
+ #
34
+ # @return <String>
35
+ # A path relative to the public directory, with added segments.
36
+ def public_path_for(type, *segments)
37
+ ::MerbAuthSlicePasswordReset.public_path_for(type, *segments)
38
+ end
39
+
40
+ # Construct an app-level path.
41
+ #
42
+ # @param <Symbol> The type of component.
43
+ # @param *segments<Array[#to_s]> Path segments to append.
44
+ #
45
+ # @return <String>
46
+ # A path within the host application, with added segments.
47
+ def app_path_for(type, *segments)
48
+ ::MerbAuthSlicePasswordReset.app_path_for(type, *segments)
49
+ end
50
+
51
+ # Construct a slice-level path.
52
+ #
53
+ # @param <Symbol> The type of component.
54
+ # @param *segments<Array[#to_s]> Path segments to append.
55
+ #
56
+ # @return <String>
57
+ # A path within the slice source (Gem), with added segments.
58
+ def slice_path_for(type, *segments)
59
+ ::MerbAuthSlicePasswordReset.slice_path_for(type, *segments)
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,28 @@
1
+ module Merb
2
+ module MerbAuthSlicePasswordReset
3
+ module MailerHelper
4
+
5
+ def password_reset_url(user)
6
+ @password_reset_host ||= MaSPR[:password_reset_host] || MaSPR[:default_password_reset_host]
7
+ @password_reset_protocol ||= MaSPR[:password_reset_protocol] || "http"
8
+
9
+ if base_controller # Rendering from a web controller
10
+ @password_reset_host ||= base_controller.request.host
11
+ @password_reset_protocol ||= "http"
12
+ end
13
+
14
+ @password_reset_host ||= case @password_reset_host
15
+ when Proc
16
+ @password_reset_host.call(user)
17
+ when String
18
+ @password_reset_host
19
+ end
20
+
21
+ raise "There is no host set for the password-reset email. Set Merb::Slices::config[:merb_auth_slice_password_reset][:password_reset_host]" unless @password_reset_host
22
+
23
+ "#{@password_reset_protocol}://#{@password_reset_host}#{slice_url(:reset_password, :password_reset_code => user.password_reset_code)}"
24
+ end
25
+
26
+ end # MailerHelper
27
+ end # MerbAuthSlicePasswordReset
28
+ end # Merb