jsmestad-merb-auth-slice-activation 1.0.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 (30) hide show
  1. data/README.textile +205 -0
  2. data/Rakefile +57 -0
  3. data/app/controllers/activations.rb +24 -0
  4. data/app/controllers/application.rb +5 -0
  5. data/app/helpers/application_helper.rb +64 -0
  6. data/app/helpers/mailer_helper.rb +29 -0
  7. data/app/mailers/activation_mailer.rb +18 -0
  8. data/app/mailers/views/activation_mailer/activation.text.erb +1 -0
  9. data/app/mailers/views/activation_mailer/signup.text.erb +7 -0
  10. data/app/views/layout/merb_auth_slice_activation.html.erb +16 -0
  11. data/config/init.rb +84 -0
  12. data/lib/merb-auth-slice-activation.rb +91 -0
  13. data/lib/merb-auth-slice-activation/merbtasks.rb +112 -0
  14. data/lib/merb-auth-slice-activation/mixins/activated_user.rb +118 -0
  15. data/lib/merb-auth-slice-activation/mixins/activated_user/ar_activated_user.rb +21 -0
  16. data/lib/merb-auth-slice-activation/mixins/activated_user/dm_activated_user.rb +24 -0
  17. data/lib/merb-auth-slice-activation/mixins/activated_user/sq_activated_user.rb +23 -0
  18. data/lib/merb-auth-slice-activation/slicetasks.rb +18 -0
  19. data/lib/merb-auth-slice-activation/spectasks.rb +75 -0
  20. data/merb-auth-slice-activation.gemspec +48 -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 +99 -0
@@ -0,0 +1,205 @@
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::ActivatedUser
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
+ <pre><code>
35
+ :activated_at, DateTime
36
+ :activation_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
+ :activation_subject # The subject of the email sent to ask for activation
63
+
64
+ h3. Customizing the emails
65
+
66
+ To customize your emails, rake the slices stubs
67
+
68
+ <pre><code>
69
+ $ rake slices:merb-auth-slice-activation:stubs
70
+ </code></pre>
71
+
72
+ This will create stubs of the views in @slices/merb-auth-slice-activation/app/mailers/views/@
73
+
74
+ To create HTML emails just add an html template like @signup.html.erb@
75
+
76
+ h3. Customize the Redirect after activation
77
+
78
+ rake the slices stubs as above. There is an @activations.rb@ controller in the
79
+ @slices/merb-auth-slice-activation/app/controllers@ directory. You can overwrite the stubbed
80
+ method in there to have it change it's redirection behavior.
81
+
82
+
83
+ # Rake tasks to package/install the gem - edit this to modify the manifest.
84
+ # The slice application: controllers, models, helpers, views.
85
+ # The default layout, as specified in @Merb::Slices::config[:'merb-auth-slice-activation'][:layout]@ change this to :application to use the app's layout.
86
+ # Standard rake tasks available to your application.
87
+ # Your custom application rake tasks.
88
+ # The main slice file - contains all slice setup @logic/config@.
89
+ # Public assets you (optionally) install using @rake slices:merb-auth-slice-activation:install@
90
+ # Specs for basis slice behaviour - you usually adapt these for your slice.
91
+ # 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.
92
+
93
+
94
+ To see all available tasks for MerbAuthSliceActivation run:
95
+
96
+ <pre><code>
97
+ $ rake -T slices:merb-auth-slice-activation
98
+ </code></pre>
99
+
100
+
101
+
102
+ ------------------------------------------------------------------------------
103
+
104
+ Instructions for installation:
105
+
106
+ file: config/init.rb
107
+
108
+ # add the slice as a regular dependency
109
+
110
+ dependency 'merb-auth-slice-activation'
111
+
112
+ # if needed, configure which slices to load and in which order
113
+
114
+ Merb::Plugins.config[:merb_slices] = { :queue => ["MerbAuthSliceActivation", ...] }
115
+
116
+ # optionally configure the plugins in a before_app_loads callback
117
+
118
+ Merb::BootLoader.before_app_loads do
119
+
120
+ Merb::Slices::config[:merb-auth-slice-activation][:option] = value
121
+
122
+ end
123
+
124
+ file: config/router.rb
125
+
126
+ # example: /merb-auth-slice-activation/:controller/:action/:id
127
+
128
+ add_slice(:MerbAuthSliceActivation)
129
+
130
+ # example: /foo/:controller/:action/:id
131
+
132
+ add_slice(:MerbAuthSliceActivation, 'foo') # same as :path => 'foo'
133
+
134
+ # example: /:lang/:controller/:action/:id
135
+
136
+ add_slice(:MerbAuthSliceActivation, :path => ':lang')
137
+
138
+ # example: /:controller/:action/:id
139
+
140
+ slice(:MerbAuthSliceActivation)
141
+
142
+ Normally you should also run the following rake task:
143
+
144
+ rake slices:merb-auth-slice-activation:install
145
+
146
+ ------------------------------------------------------------------------------
147
+
148
+ You can put your application-level overrides in:
149
+
150
+ host-app/slices/merb-auth-slice-activation/app - controllers, models, views ...
151
+
152
+ Templates are located in this order:
153
+
154
+ 1. host-app/slices/merb-auth-slice-activation/app/views/*
155
+ 2. gems/merb-auth-slice-activation/app/views/*
156
+ 3. host-app/app/views/*
157
+
158
+ You can use the host application's layout by configuring the
159
+ merb-auth-slice-activation slice in a before_app_loads block:
160
+
161
+ Merb::Slices.config[:merb-auth-slice-activation] = { :layout => :application }
162
+
163
+ By default :merb-auth-slice-activation is used. If you need to override
164
+ stylesheets or javascripts, just specify your own files in your layout
165
+ instead/in addition to the ones supplied (if any) in
166
+ host-app/public/slices/merb-auth-slice-activation.
167
+
168
+ In any case don't edit those files directly as they may be clobbered any time
169
+ rake merb-auth-slice-activation:install is run.
170
+
171
+ ------------------------------------------------------------------------------
172
+
173
+ About Slices
174
+ ============
175
+
176
+ Merb-Slices is a Merb plugin for using and creating application 'slices' which
177
+ help you modularize your application. Usually these are reuseable extractions
178
+ from your main app. In effect, a Slice is just like a regular Merb MVC
179
+ application, both in functionality as well as in structure.
180
+
181
+ When you generate a Slice stub structure, a module is setup to serve as a
182
+ namespace for your controller, models, helpers etc. This ensures maximum
183
+ encapsulation. You could say a Slice is a mixture between a Merb plugin (a
184
+ Gem) and a Merb application, reaping the benefits of both.
185
+
186
+ A host application can 'mount' a Slice inside the router, which means you have
187
+ full over control how it integrates. By default a Slice's routes are prefixed
188
+ by its name (a router :namespace), but you can easily provide your own prefix
189
+ or leave it out, mounting it at the root of your url-schema. You can even
190
+ mount a Slice multiple times and give extra parameters to customize an
191
+ instance's behaviour.
192
+
193
+ A Slice's Application controller uses controller_for_slice to setup slice
194
+ specific behaviour, which mainly affects cascaded view handling. Additionaly,
195
+ this method is available to any kind of controller, so it can be used for
196
+ Merb Mailer too for example.
197
+
198
+ There are many ways which let you customize a Slice's functionality and
199
+ appearance without ever touching the Gem-level code itself. It's not only easy
200
+ to add template/layout overrides, you can also add/modify controllers, models
201
+ and other runtime code from within the host application.
202
+
203
+ To create your own Slice run this (somewhere outside of your merb app):
204
+
205
+ $ merb-gen slice <your-lowercase-slice-name>
@@ -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'
@@ -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) %>
@@ -0,0 +1,16 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
3
+ <head>
4
+ <meta http-equiv="content-type" content="text/html; charset=utf-8" />
5
+ <title>Fresh MerbAuthSliceActivation Slice</title>
6
+ <link href="<%= public_path_for :stylesheet, 'master.css' %>" type="text/css" charset="utf-8" rel="stylesheet" media="all" />
7
+ <script src="<%= public_path_for :javascript, 'master.js' %>" type="text/javascript" charset="utf-8"></script>
8
+ </head>
9
+ <!-- you can override this layout at slices/merb-auth-slice-activation/app/views/layout/merb-auth-slice-activation.html.erb -->
10
+ <body class="merb-auth-slice-activation-slice">
11
+ <div id="container">
12
+ <h1>MerbAuthSliceActivation Slice</h1>
13
+ <div id="main"><%= catch_content :for_layout %></div>
14
+ </div>
15
+ </body>
16
+ </html>
@@ -0,0 +1,84 @@
1
+ #
2
+ # ==== Standalone MerbAuthSliceActivation configuration
3
+ #
4
+ # This configuration/environment file is only loaded by bin/slice, which can be
5
+ # used during development of the slice. It has no effect on this slice being
6
+ # loaded in a host application. To run your slice in standalone mode, just
7
+ # run 'slice' from its directory. The 'slice' command is very similar to
8
+ # the 'merb' command, and takes all the same options, including -i to drop
9
+ # into an irb session for example.
10
+ #
11
+ # The usual Merb configuration directives and init.rb setup methods apply,
12
+ # including use_orm and before_app_loads/after_app_loads.
13
+ #
14
+ # If you need need different configurations for different environments you can
15
+ # even create the specific environment file in config/environments/ just like
16
+ # in a regular Merb application.
17
+ #
18
+ # In fact, a slice is no different from a normal # Merb application - it only
19
+ # differs by the fact that seamlessly integrates into a so called 'host'
20
+ # application, which in turn can override or finetune the slice implementation
21
+ # code and views.
22
+ #
23
+ # require 'merb-auth-core'
24
+ # require File.join(File.expand_path(File.dirname(__FILE__)), "..", "lib", "merb-auth-slice-activation", "mixins", "activated_user")
25
+
26
+ require(File.join(File.expand_path(File.dirname(__FILE__)),"..","lib","merb-auth-slice-activation"))
27
+
28
+ # Setup the required configuration for the slice
29
+ Merb::Slices::config[:merb_auth_slice_activation][:from_email] = "homer@example.com"
30
+ Merb::Slices::config[:merb_auth_slice_activation][:activation_host] = "example.com"
31
+
32
+
33
+ Merb::BootLoader.before_app_loads do
34
+
35
+ DataMapper.setup(:default, "sqlite3::memory:")
36
+ class User
37
+ include DataMapper::Resource
38
+ include Merb::Authentication::Mixins::ActivatedUser
39
+
40
+ property :id, Serial
41
+ property :email, String
42
+ property :login, String
43
+
44
+ end
45
+
46
+ class Merb::Authentication
47
+ def self.user_class
48
+ ::User
49
+ end
50
+
51
+ def store_user(user)
52
+ return nil if user.nil?
53
+ user.login
54
+ end
55
+ def fetch_user(user_id)
56
+ User.first(:login => login)
57
+ end
58
+ end
59
+
60
+ end
61
+
62
+ # puts Merb::Authentication.respond_to?(:user_class)
63
+ # Merb::Authentication.user_class = User
64
+
65
+ Merb::Config.use do |c|
66
+
67
+ # Sets up a custom session id key which is used for the session persistence
68
+ # cookie name. If not specified, defaults to '_session_id'.
69
+ # c[:session_id_key] = '_session_id'
70
+
71
+ # The session_secret_key is only required for the cookie session store.
72
+ c[:session_secret_key] = '72c5184af853f00f83d84adb562c56f3f1248d89'
73
+
74
+ # There are various options here, by default Merb comes with 'cookie',
75
+ # 'memory', 'memcache' or 'container'.
76
+ # You can of course use your favorite ORM instead:
77
+ # 'datamapper', 'sequel' or 'activerecord'.
78
+ c[:session_store] = 'memory'
79
+
80
+ # When running a slice standalone, you're usually developing it,
81
+ # so enable template reloading by default.
82
+ c[:reload_templates] = true
83
+
84
+ end