teamon-merb-auth-slice-activation 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. data/LICENSE +20 -0
  2. data/README.textile +198 -0
  3. data/Rakefile +57 -0
  4. data/TODO +9 -0
  5. data/app/controllers/activations.rb +24 -0
  6. data/app/controllers/application.rb +5 -0
  7. data/app/helpers/application_helper.rb +64 -0
  8. data/app/helpers/mailer_helper.rb +29 -0
  9. data/app/mailers/activation_mailer.rb +18 -0
  10. data/app/mailers/views/activation_mailer/activation.text.erb +1 -0
  11. data/app/mailers/views/activation_mailer/signup.text.erb +7 -0
  12. data/app/views/layout/merb_auth_slice_activation.html.erb +16 -0
  13. data/lib/merb-auth-slice-activation.rb +91 -0
  14. data/lib/merb-auth-slice-activation/merbtasks.rb +112 -0
  15. data/lib/merb-auth-slice-activation/mixins/activated_user.rb +118 -0
  16. data/lib/merb-auth-slice-activation/mixins/activated_user/ar_activated_user.rb +21 -0
  17. data/lib/merb-auth-slice-activation/mixins/activated_user/dm_activated_user.rb +24 -0
  18. data/lib/merb-auth-slice-activation/mixins/activated_user/sq_activated_user.rb +23 -0
  19. data/lib/merb-auth-slice-activation/slicetasks.rb +18 -0
  20. data/lib/merb-auth-slice-activation/spectasks.rb +75 -0
  21. data/public/javascripts/master.js +0 -0
  22. data/public/stylesheets/master.css +2 -0
  23. data/spec/controllers/activations_spec.rb +83 -0
  24. data/spec/mailers/activation_mailer_spec.rb +68 -0
  25. data/spec/mixins/activated_user_spec.rb +122 -0
  26. data/spec/spec_helper.rb +61 -0
  27. data/stubs/app/controllers/activations.rb +8 -0
  28. data/stubs/app/mailers/views/activation_mailer/activation.text.erb +1 -0
  29. data/stubs/app/mailers/views/activation_mailer/signup.text.erb +7 -0
  30. metadata +138 -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,198 @@
1
+ h3. MerbAuthSliceActivation
2
+
3
+ h3. Installation
4
+
5
+
6
+ file: app/models/user.rb
7
+
8
+ Include Merb::Authentication::Mixins::ActivatedUser into your user model
9
+ <pre><code>
10
+ class User
11
+ include DataMapper::Resource
12
+ include Merb::Authentication::Mixins::ActivatedUser
13
+
14
+ ...
15
+ end
16
+ </code></pre>
17
+
18
+
19
+ file: config/init.rb
20
+
21
+ # add the slice as a regular dependency @dependency 'merb-auth-slice-activation'@
22
+ # if needed, configure which slices to load and in which order @Merb::Plugins.config[:merb_slices] = { :queue => ["MerbAuthSliceActivation", ...] }@
23
+ # optionally configure the plugins in a before_app_loads callback
24
+
25
+ <pre><code>
26
+ Merb::BootLoader.before_app_loads do
27
+ Merb::Slices::config[:merb_auth_slice_activation][:from_email] = 'no-reply@example.com'
28
+ Merb::Slices::config[:merb_auth_slice_activation][:activation_host] = 'localhost'
29
+ end
30
+ </code></pre>
31
+
32
+ file: config/router.rb
33
+
34
+ @slice(:merb_auth_slice_activation, :name_prefix => nil, :path_prefix => "")@
35
+
36
+ **Remember to migrate database!**
37
+
38
+ ------------------------------------------------------------------------------
39
+
40
+ h3. Other less important stuff ;)
41
+
42
+ This slice provides a check to make sure that a user is active on login. It also provides
43
+ activation on the "user" object via an activation action (slice_url :activate). When loggin in,
44
+ the "user" object found by merb-auth-core will be asked
45
+
46
+ Do you respond_to?(:active?):
47
+ # If yes, check if active and return the user if they are
48
+ # If no, return the user (i.e. no activation check)
49
+
50
+ This slice adds a mixin that you should include in your user model to priovide the active? method.
51
+ The mixin will automatically select the correct sub mixin for all supported orms.
52
+
53
+ <pre><code>
54
+ class User
55
+ include DataMapper::Resource
56
+ include Merb::Authentication::Mixins::ActivatedUser
57
+
58
+ property :id, Serial
59
+ end
60
+ </code></pre>
61
+
62
+ The mixin provides a number of methods. The most common are:
63
+
64
+ <pre><code>
65
+ @user.activate! # activates (and saves) the user
66
+ @user.activated? # Returns the "active" status of the user
67
+ @user.active? # Alias for activated?
68
+ </pre></code>
69
+
70
+ h3. Migration Requirements
71
+
72
+ The mixin requires some fields to be in-place on your model. Where needed include these in your migrations.
73
+ <pre><code>
74
+ :activated_at, DateTime
75
+ :activation_code, String
76
+ </code></pre>
77
+
78
+ h3. Mailers
79
+
80
+ The slice contains 2 mailing actions that are setup as callback hooks on the model. When the model is created
81
+ a "signup" email is sent with the link to follow to activate the account. Also an activation acknowledgment
82
+ email.
83
+
84
+ h3. Configuration Options
85
+
86
+ These options may be declared in your @init.rb@ or @environment/*.rb@ files
87
+
88
+ Use the standard slice configuration hash to set these up @Merb::Slices::config[:'merb-auth-slice-activation']@
89
+
90
+
91
+ h4. Required
92
+
93
+ :from_email # The email account to send the email from
94
+ :activation_host # The host to go to for activation. This is used to construct the
95
+ # activation link. Symbol, String or Procs are available.
96
+ # Procs will have the @user@ object passed in
97
+
98
+ h4. Optional
99
+
100
+ :welcome_subject # The subject of the email to send after activation (Welcome)
101
+ :activation_subject # The subject of the email sent to ask for activation
102
+
103
+ h3. Customizing the emails
104
+
105
+ To customize your emails, rake the slices stubs
106
+
107
+ <pre><code>
108
+ $ rake slices:merb-auth-slice-activation:stubs
109
+ </code></pre>
110
+
111
+ This will create stubs of the views in @slices/merb-auth-slice-activation/app/mailers/views/@
112
+
113
+ To create HTML emails just add an html template like @signup.html.erb@
114
+
115
+ h3. Customize the Redirect after activation
116
+
117
+ rake the slices stubs as above. There is an @activations.rb@ controller in the
118
+ @slices/merb-auth-slice-activation/app/controllers@ directory. You can overwrite the stubbed
119
+ method in there to have it change it's redirection behavior.
120
+
121
+
122
+ # Rake tasks to package/install the gem - edit this to modify the manifest.
123
+ # The slice application: controllers, models, helpers, views.
124
+ # The default layout, as specified in @Merb::Slices::config[:'merb-auth-slice-activation'][:layout]@ change this to :application to use the app's layout.
125
+ # Standard rake tasks available to your application.
126
+ # Your custom application rake tasks.
127
+ # The main slice file - contains all slice setup @logic/config@.
128
+ # Public assets you (optionally) install using @rake slices:merb-auth-slice-activation:install@
129
+ # Specs for basis slice behaviour - you usually adapt these for your slice.
130
+ # 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.
131
+
132
+
133
+ To see all available tasks for MerbAuthSliceActivation run:
134
+
135
+ <pre><code>
136
+ $ rake -T slices:merb-auth-slice-activation
137
+ </code></pre>
138
+
139
+ ------------------------------------------------------------------------------
140
+
141
+ You can put your application-level overrides in:
142
+
143
+ host-app/slices/merb-auth-slice-activation/app - controllers, models, views ...
144
+
145
+ Templates are located in this order:
146
+
147
+ 1. host-app/slices/merb-auth-slice-activation/app/views/*
148
+ 2. gems/merb-auth-slice-activation/app/views/*
149
+ 3. host-app/app/views/*
150
+
151
+ You can use the host application's layout by configuring the
152
+ merb-auth-slice-activation slice in a before_app_loads block:
153
+
154
+ Merb::Slices.config[:merb-auth-slice-activation] = { :layout => :application }
155
+
156
+ By default :merb-auth-slice-activation is used. If you need to override
157
+ stylesheets or javascripts, just specify your own files in your layout
158
+ instead/in addition to the ones supplied (if any) in
159
+ host-app/public/slices/merb-auth-slice-activation.
160
+
161
+ In any case don't edit those files directly as they may be clobbered any time
162
+ rake merb-auth-slice-activation:install is run.
163
+
164
+ ------------------------------------------------------------------------------
165
+
166
+ About Slices
167
+ ============
168
+
169
+ Merb-Slices is a Merb plugin for using and creating application 'slices' which
170
+ help you modularize your application. Usually these are reuseable extractions
171
+ from your main app. In effect, a Slice is just like a regular Merb MVC
172
+ application, both in functionality as well as in structure.
173
+
174
+ When you generate a Slice stub structure, a module is setup to serve as a
175
+ namespace for your controller, models, helpers etc. This ensures maximum
176
+ encapsulation. You could say a Slice is a mixture between a Merb plugin (a
177
+ Gem) and a Merb application, reaping the benefits of both.
178
+
179
+ A host application can 'mount' a Slice inside the router, which means you have
180
+ full over control how it integrates. By default a Slice's routes are prefixed
181
+ by its name (a router :namespace), but you can easily provide your own prefix
182
+ or leave it out, mounting it at the root of your url-schema. You can even
183
+ mount a Slice multiple times and give extra parameters to customize an
184
+ instance's behaviour.
185
+
186
+ A Slice's Application controller uses controller_for_slice to setup slice
187
+ specific behaviour, which mainly affects cascaded view handling. Additionaly,
188
+ this method is available to any kind of controller, so it can be used for
189
+ Merb Mailer too for example.
190
+
191
+ There are many ways which let you customize a Slice's functionality and
192
+ appearance without ever touching the Gem-level code itself. It's not only easy
193
+ to add template/layout overrides, you can also add/modify controllers, models
194
+ and other runtime code from within the host application.
195
+
196
+ To create your own Slice run this (somewhere outside of your merb app):
197
+
198
+ $ merb-gen slice <your-lowercase-slice-name>
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-activation"
9
+ AUTHOR = "Daniel Neighman, Christian Kebekus"
10
+ EMAIL = "has.sox@gmail.com"
11
+ HOMEPAGE = "http://merbivore.com/"
12
+ SUMMARY = "Merb Slice that adds basic activation functionality to merb-auth."
13
+ GEM_VERSION = "1.0"
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')
28
+ s.add_dependency('merb-auth-core', ">= 1.0")
29
+ s.add_dependency('merb-auth-more', ">= 1.0")
30
+ s.add_dependency('merb-mailer', ">= 1.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,9 @@
1
+ TODO:
2
+
3
+ - Figure out why the mailer views are not copies during mailer freeze.
4
+
5
+ Remove anything that you don't need:
6
+
7
+ - app/views/layout/merb-auth-slice-activation.html.erb
8
+ - public/* any public files
9
+ - stubs/* any stub files
@@ -0,0 +1,24 @@
1
+ class MerbAuthSliceActivation::Activations < MerbAuthSliceActivation::Application
2
+
3
+ after :redirect_after_activation
4
+
5
+ # Activate a user from the submitted activation code.
6
+ #
7
+ # ==== Returns
8
+ # String:: Empty string.
9
+ def activate
10
+ session.user = Merb::Authentication.user_class.find_with_activation_code(params[:activation_code])
11
+ raise NotFound if session.user.nil?
12
+ if session.authenticated? && !session.user.activated?
13
+ session.user.activate!
14
+ end
15
+ ""
16
+ end
17
+
18
+ private
19
+
20
+ def redirect_after_activation
21
+ redirect "/", :message => {:notice => "Activation Successful"}
22
+ end
23
+
24
+ end # MerbAuthSliceActivation::Activations
@@ -0,0 +1,5 @@
1
+ class MerbAuthSliceActivation::Application < Merb::Controller
2
+
3
+ controller_for_slice
4
+
5
+ end
@@ -0,0 +1,64 @@
1
+ module Merb
2
+ module MerbAuthSliceActivation
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
+ ::MerbAuthSliceActivation.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
+ ::MerbAuthSliceActivation.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
+ ::MerbAuthSliceActivation.slice_path_for(type, *segments)
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,29 @@
1
+ module Merb
2
+ module MerbAuthSliceActivation
3
+ module MailerHelper
4
+
5
+ # Does it's best to
6
+ def activation_url(user)
7
+ @activation_host ||= MaSA[:activation_host] || MaSA[:default_activation_host]
8
+ @activation_protocol ||= MaSA[:activation_protocol] || "http"
9
+
10
+ if base_controller # Rendering from a web controller
11
+ @activation_host ||= base_controller.request.host
12
+ @activation_protocol ||= "http"
13
+ end
14
+
15
+ @activation_host ||= case @activation_host
16
+ when Proc
17
+ @activation_host.call(user)
18
+ when String
19
+ @activation_host
20
+ end
21
+
22
+ raise "There is no host set for the activation email. Set Merb::Slices::config[:merb_auth_slice_activation][:activation_host]" unless @activation_host
23
+
24
+ "#{@activation_protocol}://#{@activation_host}#{slice_url(:activate, :activation_code => user.activation_code)}"
25
+ end
26
+
27
+ end # MailerHelper
28
+ end # MerbAuthSliceActivation
29
+ end # Merb
@@ -0,0 +1,18 @@
1
+ class MerbAuthSliceActivation::ActivationMailer < Merb::MailController
2
+ include Merb::MerbAuthSliceActivation::MailerHelper
3
+
4
+ controller_for_slice MerbAuthSliceActivation, :templates_for => :mailer, :path => "views"
5
+
6
+ def signup
7
+ @user = params[:user]
8
+ Merb.logger.info "Sending Signup to #{@user.email} with code #{@user.activation_code}"
9
+ render_mail :layout => nil
10
+ end
11
+
12
+ def activation
13
+ @user = params[:user]
14
+ Merb.logger.info "Sending Activation email to #{@user.email}"
15
+ render_mail :layout => nil
16
+ end
17
+
18
+ end
@@ -0,0 +1 @@
1
+ Your email has been authenticated <%= @user.email %>
@@ -0,0 +1,7 @@
1
+ Your account has been created.
2
+
3
+ Username: <%= @user.login %>
4
+
5
+ Visit this url to activate your account:
6
+
7
+ <%= activation_url(@user) %>