scottmotte-merb-auth-slice-password-reset 0.9.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +219 -0
  3. data/Rakefile +57 -0
  4. data/TODO +2 -0
  5. data/app/controllers/application.rb +5 -0
  6. data/app/controllers/passwords.rb +37 -0
  7. data/app/helpers/application_helper.rb +64 -0
  8. data/app/helpers/mailer_helper.rb +28 -0
  9. data/app/mailers/password_reset_mailer.rb +19 -0
  10. data/app/mailers/views/password_reset_mailer/new_password.html.erb +3 -0
  11. data/app/mailers/views/password_reset_mailer/password_reset.text.erb +5 -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/lib/merb-auth-slice-password-reset.rb +85 -0
  15. data/lib/merb-auth-slice-password-reset/merbtasks.rb +112 -0
  16. data/lib/merb-auth-slice-password-reset/mixins/senile_user.rb +93 -0
  17. data/lib/merb-auth-slice-password-reset/mixins/senile_user/ar_senile_user.rb +19 -0
  18. data/lib/merb-auth-slice-password-reset/mixins/senile_user/dm_senile_user.rb +22 -0
  19. data/lib/merb-auth-slice-password-reset/mixins/senile_user/mm_senile_user.rb +22 -0
  20. data/lib/merb-auth-slice-password-reset/mixins/senile_user/sq_senile_user.rb +20 -0
  21. data/lib/merb-auth-slice-password-reset/slicetasks.rb +18 -0
  22. data/lib/merb-auth-slice-password-reset/spectasks.rb +75 -0
  23. data/public/javascripts/master.js +0 -0
  24. data/public/stylesheets/master.css +2 -0
  25. data/spec/mailers/password_reset_mailer_spec.rb +49 -0
  26. data/spec/mixins/senile_user_spec.rb +111 -0
  27. data/spec/spec_helper.rb +61 -0
  28. data/stubs/app/controllers/passwords.rb +13 -0
  29. data/stubs/app/mailers/views/password_reset_mailer/new_password.html.erb +3 -0
  30. data/stubs/app/mailers/views/password_reset_mailer/password_reset.text.erb +5 -0
  31. data/stubs/app/views/passwords/forgot_password.html.erb +7 -0
  32. metadata +146 -0
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.
data/README.textile ADDED
@@ -0,0 +1,219 @@
1
+ h3. MerbAuthSliceActivation
2
+
3
+ This slice provides a check to make sure that a user is active on login. It also provides
4
+ activation on the "user" object via an activation action (slice_url :activate). When loggin in,
5
+ the "user" object found by merb-auth-core will be asked
6
+
7
+ Do you respond_to?(:active?):
8
+ # If yes, check if active and return the user if they are
9
+ # If no, return the user (i.e. no activation check)
10
+
11
+ This slice adds a mixin that you should include in your user model to priovide the active? method.
12
+ The mixin will automatically select the correct sub mixin for all supported orms.
13
+
14
+ <pre><code>
15
+ class User
16
+ include DataMapper::Resource
17
+ include Merb::Authentication::Mixins::SenileUser
18
+
19
+ property :id, Serial
20
+ end
21
+ </code></pre>
22
+
23
+ The mixin provides a number of methods. The most common are:
24
+
25
+ <pre><code>
26
+ @user.activate # activates (and saves) the user
27
+ @user.activated? # Returns the "active" status of the user
28
+ @user.active? # Alias for activated?
29
+ </pre></code>
30
+
31
+ h3. Migration Requirements
32
+
33
+ The mixin requires some fields to be in-place on your model. Where needed include these in your migrations.
34
+
35
+ <pre><code>
36
+ :password_reset_code, String
37
+ </code></pre>
38
+
39
+ h3. Mailers
40
+
41
+ The slice contains 2 mailing actions that are setup as callback hooks on the model. When the model is created
42
+ a "signup" email is sent with the link to follow to activate the account. Also an activation acknowledgment
43
+ email.
44
+
45
+ h3. Configuration Options
46
+
47
+ These options may be declared in your @init.rb@ or @environment/*.rb@ files
48
+
49
+ Use the standard slice configuration hash to set these up @Merb::Slices::config[:'merb-auth-slice-activation']@
50
+
51
+
52
+ h4. Required
53
+
54
+ :from_email # The email account to send the email from
55
+ :activation_host # The host to go to for activation. This is used to construct the
56
+ # activation link. Symbol, String or Procs are available.
57
+ # Procs will have the @user@ object passed in
58
+
59
+ h4. Optional
60
+
61
+ :welcome_subject # The subject of the email to send after activation (Welcome)
62
+
63
+ h3. Customizing the emails
64
+
65
+ To customize your emails, rake the slices stubs
66
+
67
+ <pre><code>
68
+ $ rake slices:merb-auth-slice-activation:stubs
69
+ </code></pre>
70
+
71
+ This will create stubs of the views in @slices/merb-auth-slice-activation/app/mailers/views/@
72
+
73
+ To create HTML emails just add an html template like @signup.html.erb@
74
+
75
+ h3. Customize the Redirect after activation
76
+
77
+ rake the slices stubs as above. There is an @activations.rb@ controller in the
78
+ @slices/merb-auth-slice-activation/app/controllers@ directory. You can overwrite the stubbed
79
+ method in there to have it change it's redirection behavior.
80
+
81
+
82
+ # Rake tasks to package/install the gem - edit this to modify the manifest.
83
+ # The slice application: controllers, models, helpers, views.
84
+ # The default layout, as specified in @Merb::Slices::config[:'merb-auth-slice-activation'][:layout]@ change this to :application to use the app's layout.
85
+ # Standard rake tasks available to your application.
86
+ # Your custom application rake tasks.
87
+ # The main slice file - contains all slice setup @logic/config@.
88
+ # Public assets you (optionally) install using @rake slices:merb-auth-slice-activation:install@
89
+ # Specs for basis slice behaviour - you usually adapt these for your slice.
90
+ # 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-activation:stubs@ to get started with the override stubs. Also, @rake slices:merb-auth-slice-activation:patch@ will copy over views to override in addition to the files found in /stubs.
91
+
92
+
93
+ To see all available tasks for MerbAuthSliceActivation run:
94
+
95
+ <pre><code>
96
+ $ rake -T slices:merb_auth_slice_password_reset
97
+ </code></pre>
98
+
99
+
100
+ h3. Instructions for installation:
101
+
102
+ h4. @config/init.rb@
103
+
104
+ Add the slice as a regular dependency
105
+
106
+ <pre><code>
107
+ dependency 'merb-auth-slice-password-reset'
108
+ </code></pre>
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>
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake/gempackagetask'
3
+ require 'spec/rake/spectask'
4
+ require 'merb-core'
5
+ require 'merb-core/tasks/merb'
6
+ require 'merb-core/test/tasks/spectasks'
7
+
8
+ GEM_NAME = "merb-auth-slice-password-reset"
9
+ AUTHOR = "Daniel Neighman, Christian Kebekus"
10
+ EMAIL = "has.sox@gmail.com"
11
+ HOMEPAGE = "http://merbivore.com/"
12
+ SUMMARY = "Merb Slice that adds basic password-reset functionality to merb-auth-based merb applications."
13
+ GEM_VERSION = "0.9.14"
14
+
15
+ spec = Gem::Specification.new do |s|
16
+ s.rubyforge_project = 'merb'
17
+ s.name = GEM_NAME
18
+ s.version = GEM_VERSION
19
+ s.platform = Gem::Platform::RUBY
20
+ s.has_rdoc = true
21
+ s.extra_rdoc_files = ["README.textile", "LICENSE", 'TODO']
22
+ s.summary = SUMMARY
23
+ s.description = s.summary
24
+ s.author = AUTHOR
25
+ s.email = EMAIL
26
+ s.homepage = HOMEPAGE
27
+ s.add_dependency('merb-slices', '>= 1.0.0')
28
+ s.add_dependency('merb-auth-core', ">= 1.0.0")
29
+ s.add_dependency('merb-auth-more', ">= 1.0.0")
30
+ s.add_dependency('merb-mailer', ">= 1.0.0")
31
+ s.require_path = 'lib'
32
+ s.files = %w(LICENSE README.textile Rakefile TODO) + Dir.glob("{lib,spec,app,public,stubs}/**/*")
33
+ end
34
+
35
+ Rake::GemPackageTask.new(spec) do |pkg|
36
+ pkg.gem_spec = spec
37
+ end
38
+
39
+ desc "Install the gem"
40
+ task :install do
41
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
42
+ end
43
+
44
+ desc "Uninstall the gem"
45
+ task :uninstall do
46
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
47
+ end
48
+
49
+ desc "Create a gemspec file"
50
+ task :gemspec do
51
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
52
+ file.puts spec.to_ruby
53
+ end
54
+ end
55
+
56
+ desc 'Default: run spec examples'
57
+ 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,37 @@
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
+ session.user = Merb::Authentication.user_class.find_with_password_reset_code(params[:password_reset_code])
22
+ raise NotFound if session.user.nil?
23
+ session.user.reset_password!
24
+ redirect_after_password_reset
25
+ end
26
+
27
+ private
28
+
29
+ def redirect_after_password_reset
30
+ redirect "/", :message => {:notice => "New password sent".t}
31
+ end
32
+
33
+ def redirect_after_sending_confirmation
34
+ redirect "/", :message => {:notice => "Password reset confirmation sent".t}
35
+ end
36
+
37
+ 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
@@ -0,0 +1,19 @@
1
+ class MerbAuthSlicePasswordReset::PasswordResetMailer < Merb::MailController
2
+
3
+ include Merb::MerbAuthSlicePasswordReset::MailerHelper
4
+
5
+ controller_for_slice MerbAuthSlicePasswordReset, :templates_for => :mailer, :path => "views"
6
+
7
+ def password_reset
8
+ @user = params[:user]
9
+ Merb.logger.info "Sending Password Reset to #{@user.email} with code #{@user.password_reset_code}"
10
+ render_mail :layout => nil
11
+ end
12
+
13
+ def new_password
14
+ @user = params[:user]
15
+ Merb.logger.info "Sending New Password to #{@user.email} with password #{@user.password}"
16
+ render_mail :layout => nil
17
+ end
18
+
19
+ end