scottmotte-merb_auth_slice_multisite 0.3.2 → 0.6.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.
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 3
4
- :patch: 2
3
+ :minor: 6
4
+ :patch: 0
@@ -0,0 +1,33 @@
1
+ # the mixin to provide the exceptions controller action for Unauthenticated
2
+ module MerbAuthSliceMultisite::ExceptionsMixin
3
+ def unauthenticated
4
+ provides :xml, :js, :json, :yaml
5
+
6
+ case content_type
7
+ when :html
8
+ render
9
+ else
10
+ basic_authentication.request!
11
+ ""
12
+ end
13
+ end # unauthenticated
14
+ end
15
+
16
+ Merb::Authentication.customize_default do
17
+
18
+ Exceptions.class_eval do
19
+ include Merb::Slices::Support # Required to provide slice_url
20
+
21
+ # # This stuff allows us to provide a default view
22
+ the_view_path = File.expand_path(File.dirname(__FILE__) / ".." / "views")
23
+ self._template_roots ||= []
24
+ self._template_roots << [the_view_path, :_template_location]
25
+ self._template_roots << [Merb.dir_for(:view), :_template_location]
26
+
27
+ include MerbAuthSliceMultisite::ExceptionsMixin
28
+
29
+ show_action :unauthenticated
30
+
31
+ end# Exceptions.class_eval
32
+
33
+ end # Customize default
@@ -0,0 +1,55 @@
1
+ class MerbAuthSliceMultisite::Sessions < MerbAuthSliceMultisite::Application
2
+
3
+ before :_maintain_auth_session_before, :exclude => [:destroy] # Need to hang onto the redirection during the session.abandon!
4
+ before :_abandon_session, :only => [:update, :destroy]
5
+ before :_maintain_auth_session_after, :exclude => [:destroy] # Need to hang onto the redirection during the session.abandon!
6
+ before :ensure_authenticated, :only => [:update]
7
+
8
+ # redirect from an after filter for max flexibility
9
+ # We can then put it into a slice and ppl can easily
10
+ # customize the action
11
+ after :redirect_after_login, :only => :update, :if => lambda{ !(300..399).include?(status) }
12
+ after :redirect_after_logout, :only => :destroy
13
+
14
+ def update
15
+ "Add an after filter to do stuff after login"
16
+ end
17
+
18
+ def destroy
19
+ "Add an after filter to do stuff after logout"
20
+ end
21
+
22
+
23
+ private
24
+ # @overwritable
25
+ def redirect_after_login
26
+ message[:notice] = "Authenticated Successfully"
27
+ redirect_back_or "/", :message => message, :ignore => [slice_url(:login), slice_url(:logout)]
28
+ end
29
+
30
+ # @overwritable
31
+ def redirect_after_logout
32
+ message[:notice] = "Logged Out"
33
+ redirect "/", :message => message
34
+ end
35
+
36
+ # @private
37
+ def _maintain_auth_session_before
38
+ @_maintain_auth_session = {}
39
+ Merb::Authentication.maintain_session_keys.each do |k|
40
+ @_maintain_auth_session[k] = session[k]
41
+ end
42
+ end
43
+
44
+ # @private
45
+ def _maintain_auth_session_after
46
+ @_maintain_auth_session.each do |k,v|
47
+ session[k] = v
48
+ end
49
+ end
50
+
51
+ # @private
52
+ def _abandon_session
53
+ session.abandon!
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ <h3>Login</h3>
2
+
3
+ <div>
4
+
5
+ <%= error_messages_for session.authentication %>
6
+ <% @login_param = Merb::Authentication::Strategies::Multisite::Base.login_param %>
7
+ <% @password_param = Merb::Authentication::Strategies::Multisite::Base.password_param %>
8
+ <% @site_id_param = Merb::Authentication::Strategies::Multisite::Base.site_id_param %>
9
+ <%
10
+ # make @current_site value. application.rb does not get call
11
+ # because the authentication is protected at the rack level - which is better,
12
+ # but it means I have to add the following duplicate line of code as far as I know.
13
+ @current_site = Site.first(:subdomain => request.first_subdomain)
14
+ %>
15
+
16
+ <form action="<%= slice_url(:merb_auth_slice_multisite, :perform_login) %>" method="POST" accept-charset="utf-8">
17
+ <input type="hidden" name="<%= @site_id_param.to_s %>" value="<%= @current_site.id %>" id="<%= @site_id_param.to_s %>">
18
+ <input type="hidden" name="_method" value="PUT" />
19
+ <div class="formRow">
20
+ <label><%= @login_param.to_s.capitalize %>: <input type="text" name="<%= @login_param.to_s %>" value="" id="<%= @login_param.to_s %>"></label>
21
+ </div> <!-- close: formRow -->
22
+ <div class="formRow">
23
+ <label><%= @password_param.to_s.capitalize %>:<input type="password" name="<%= @password_param.to_s %>" value="" id="<%= @password_param.to_s %>"></label>
24
+ </div> <!-- close: formRow -->
25
+ <div class="formRow">
26
+ <input type="submit" name="Submit" value="Log In" id="Submit">
27
+ </div> <!-- close: formRow -->
28
+ </form>
29
+ </div>
@@ -1,6 +1,6 @@
1
1
  # dependencies are generated using a strict version, don't forget to edit the dependency versions when upgrading.
2
- merb_gems_version = "1.0.10"
3
- dm_gems_version = "0.9.10"
2
+ merb_gems_version = "1.0.11"
3
+ dm_gems_version = "0.9.11"
4
4
  do_gems_version = "0.9.11"
5
5
 
6
6
  # For more information about each component, please read http://wiki.merbivore.com/faqs/merb_components
@@ -0,0 +1,88 @@
1
+ require 'merb-auth-more/strategies/abstract_password'
2
+ # This strategy uses a login, password, and site_id parameter.
3
+ #
4
+ # Overwrite the :password_param, :login_param, and :site_id_param
5
+ # to return the name of the field (on the form) that you're using the
6
+ # login with. These can be strings or symbols
7
+ #
8
+ # == Required
9
+ #
10
+ # === Methods
11
+ # <User>.authenticate(login_param, password_param)
12
+ #
13
+ class Merb::Authentication
14
+ module Strategies
15
+ module Multisite
16
+
17
+ # add site_id to base params.
18
+ # http://github.com/wycats/merb/blob/784ac7d71780d1a7cfb9152ba4cb0e
19
+ # 18a990ab7a/merb-auth/merb-auth-more/lib/merb-auth-more/
20
+ # strategies/abstract_password.rb
21
+ class Base < Merb::Authentication::Strategy
22
+ abstract!
23
+
24
+ # Overwrite this method to customize the field
25
+ def self.password_param
26
+ (Merb::Plugins.config[:"merb-auth"][:password_param] || :password).to_s.to_sym
27
+ end
28
+
29
+ # Overwrite this method to customize the field
30
+ def self.login_param
31
+ (Merb::Plugins.config[:"merb-auth"][:login_param] || :login).to_s.to_sym
32
+ end
33
+
34
+ # http://scottmotte.com/archives/194.html
35
+ def self.site_id_param
36
+ (Merb::Plugins.config[:"merb-auth"][:site_id_param] || :site_id).to_s.to_sym
37
+ end
38
+
39
+ def password_param
40
+ @password_param ||= Base.password_param
41
+ end
42
+
43
+ def login_param
44
+ @login_param ||= Base.login_param
45
+ end
46
+
47
+ # http://scottmotte.com/archives/194.html
48
+ def site_id_param
49
+ @site_id_param ||= Base.site_id_param
50
+ end
51
+ end
52
+
53
+ # custom strategy taking into account site_id and authenticating with it
54
+ # the authenticate method is already well defined so for now I made a muck of
55
+ # if/else statements
56
+ class Form < Base
57
+
58
+ def run!
59
+
60
+ if (login = request.params[login_param]) && (password = request.params[password_param]) && (site_id = request.params[site_id_param])
61
+ # see if user exists for the site_id
62
+ user = user_class.first(login_param => login, site_id_param => site_id)
63
+ if user
64
+ # user_class.get(:login)
65
+ user = user_class.authenticate(login, password)
66
+ if !user
67
+ errors = request.session.authentication.errors
68
+ errors.clear!
69
+ errors.add(login_param, strategy_error_message)
70
+ end
71
+ user
72
+ else
73
+ errors = request.session.authentication.errors
74
+ errors.clear!
75
+ errors.add(login_param, strategy_error_message)
76
+ end
77
+ user
78
+ end
79
+ end # run!
80
+
81
+ def strategy_error_message
82
+ "#{login_param.to_s.capitalize} or #{password_param.to_s.capitalize} were incorrect"
83
+ end
84
+
85
+ end # Form
86
+ end # Multisite Password
87
+ end # Strategies
88
+ end # Authentication
@@ -22,17 +22,17 @@ namespace :slices do
22
22
  end
23
23
  end
24
24
 
25
- # desc "Copy stub files to host application"
26
- # task :stubs do
27
- # puts "Copying stubs for MerbAuthSliceMultisite - resolves any collisions"
28
- # copied, preserved = MerbAuthSliceMultisite.mirror_stubs!
29
- # puts "- no files to copy" if copied.empty? && preserved.empty?
30
- # copied.each { |f| puts "- copied #{f}" }
31
- # preserved.each { |f| puts "! preserved override as #{f}" }
32
- # end
25
+ desc "Copy stub files to host application"
26
+ task :stubs do
27
+ puts "Copying stubs for MerbAuthSliceMultisite - resolves any collisions"
28
+ copied, preserved = MerbAuthSliceMultisite.mirror_stubs!
29
+ puts "- no files to copy" if copied.empty? && preserved.empty?
30
+ copied.each { |f| puts "- copied #{f}" }
31
+ preserved.each { |f| puts "! preserved override as #{f}" }
32
+ end
33
33
 
34
- # desc "Copy stub files and views to host application"
35
- # task :patch => [ "stubs", "freeze:views" ]
34
+ desc "Copy stub files and views to host application"
35
+ task :patch => [ "stubs", "freeze:views" ]
36
36
 
37
37
  desc "Copy public assets to host application"
38
38
  task :copy_assets do
@@ -52,11 +52,11 @@ namespace :slices do
52
52
 
53
53
  namespace :freeze do
54
54
 
55
- # desc "Freezes MerbAuthSliceMultisite by installing the gem into application/gems"
56
- # task :gem do
57
- # ENV["GEM"] ||= "merb_auth_slice_multisite"
58
- # Rake::Task['slices:install_as_gem'].invoke
59
- # end
55
+ desc "Freezes MerbAuthSliceMultisite by installing the gem into application/gems"
56
+ task :gem do
57
+ ENV["GEM"] ||= "merb_auth_slice_multisite"
58
+ Rake::Task['slices:install_as_gem'].invoke
59
+ end
60
60
 
61
61
  desc "Freezes MerbAuthSliceMultisite by copying all files from merb_auth_slice_multisite/app to your application"
62
62
  task :app do
@@ -9,8 +9,8 @@ module Merb
9
9
  property :site_id, Integer
10
10
  # Validations
11
11
  validates_present :site_id
12
- validates_is_unique :login
13
- validates_is_unique :email
12
+ validates_is_unique :login, :scope => :site_id
13
+ validates_is_unique :email, :scope => :site_id
14
14
  # Relationships/Associations
15
15
  belongs_to :site
16
16
  end # base.class_eval
@@ -3,18 +3,16 @@ namespace :slices do
3
3
 
4
4
  # add your own merb_auth_slice_multisite tasks here
5
5
 
6
- # # Uncomment the following lines and edit the pre defined tasks
7
- #
8
- # # implement this to test for structural/code dependencies
9
- # # like certain directories or availability of other files
10
- # desc "Test for any dependencies"
11
- # task :preflight do
12
- # end
13
- #
14
- # # implement this to perform any database related setup steps
15
- # desc "Migrate the database"
16
- # task :migrate do
17
- # end
6
+ # implement this to test for structural/code dependencies
7
+ # like certain directories or availability of other files
8
+ desc "Test for any dependencies"
9
+ task :preflight do
10
+ end
11
+
12
+ # implement this to perform any database related setup steps
13
+ desc "Migrate the database"
14
+ task :migrate do
15
+ end
18
16
 
19
17
  end
20
18
  end
@@ -12,6 +12,13 @@ if defined?(Merb::Plugins)
12
12
  # Register the Slice for the current host application
13
13
  Merb::Slices::register(__FILE__)
14
14
 
15
+ # Register the custom strategy so that this slice may utilize it
16
+ # from http://github.com/wycats/merb/blob/784ac7d71780d1a7cfb9152ba4cb0
17
+ # e18a990ab7a/merb-auth/merb-auth-more/lib/merb-auth-more.rb
18
+ basic_path = File.expand_path(File.dirname(__FILE__)) / "merb-auth-more" / "strategies" / "multisite"
19
+
20
+ Merb::Authentication.register(:multisite_password_form, basic_path / "multisite_password_form.rb")
21
+
15
22
  # Slice configuration - set this in a before_app_loads callback.
16
23
  # By default a Slice uses its own layout, so you can swicht to
17
24
  # the main application layout or no layout at all if needed.
@@ -36,22 +43,28 @@ if defined?(Merb::Plugins)
36
43
 
37
44
  # Initialization hook - runs before AfterAppLoads BootLoader
38
45
  def self.init
46
+ require 'merb-auth-more/mixins/redirect_back'
47
+ unless MerbAuthSliceMultisite[:no_default_strategies]
48
+ ::Merb::Authentication.activate!(:default_password_form)
49
+ end
50
+
39
51
 
40
- # Actually check if the user belongs to the site
41
- ::Merb::Authentication.after_authentication do |user, request, params|
42
- # clean this up somehow
43
- if request.first_subdomain != nil
44
- current_site = Site.first(:subdomain => request.first_subdomain)
45
- if user.site_id != current_site.id
46
- errors = request.session.authentication.errors
47
- errors.clear!
48
- errors.add("Label", "User does not belong to this site.")
49
- nil
50
- else
51
- user
52
- end
53
- end
54
- end
52
+ # from 0.3.2 version of merb_auth_slice_multisite
53
+ # # Actually check if the user belongs to the site
54
+ # ::Merb::Authentication.after_authentication do |user, request, params|
55
+ # # clean this up somehow
56
+ # if request.first_subdomain != nil
57
+ # current_site = Site.first(:subdomain => request.first_subdomain)
58
+ # if user.site_id != current_site.id
59
+ # errors = request.session.authentication.errors
60
+ # errors.clear!
61
+ # errors.add("Label", "User does not belong to this site.")
62
+ # nil
63
+ # else
64
+ # user
65
+ # end
66
+ # end
67
+ # end
55
68
 
56
69
  end
57
70
 
@@ -73,12 +86,10 @@ if defined?(Merb::Plugins)
73
86
  # @note prefix your named routes with :merb_auth_slice_multisite_
74
87
  # to avoid potential conflicts with global named routes.
75
88
  def self.setup_router(scope)
76
- # # example of a named route
77
- # scope.match('/index(.:format)').to(:controller => 'main', :action => 'index').name(:index)
78
- # # the slice is mounted at /merb_auth_slice_multisite - note that it comes before default_routes
79
- # scope.match('/').to(:controller => 'main', :action => 'index').name(:home)
80
- # # enable slice-level default routes by default
81
- # scope.default_routes
89
+ # example of a named route
90
+ scope.match("/login", :method => :get ).to(:controller => "/exceptions", :action => "unauthenticated").name(:login)
91
+ scope.match("/login", :method => :put ).to(:controller => "sessions", :action => "update" ).name(:perform_login)
92
+ scope.match("/logout" ).to(:controller => "sessions", :action => "destroy" ).name(:logout)
82
93
  end
83
94
 
84
95
  end
@@ -0,0 +1,19 @@
1
+ class MerbAuthSliceMultisite::Sessions < MerbAuthSliceMultisite::Application
2
+
3
+ after :redirect_after_login, :only => :update, :if => lambda{ !(300..399).include?(status) }
4
+ after :redirect_after_logout, :only => :destroy
5
+
6
+ private
7
+ # @overwritable
8
+ def redirect_after_login
9
+ message[:notice] = "Authenticated Successfully"
10
+ redirect_back_or "/", :message => message, :ignore => [slice_url(:login), slice_url(:logout)]
11
+ end
12
+
13
+ # @overwritable
14
+ def redirect_after_logout
15
+ message[:notice] = "Logged Out"
16
+ redirect "/", :message => message
17
+ end
18
+
19
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scottmotte-merb_auth_slice_multisite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - scottmotte
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-04-08 00:00:00 -07:00
12
+ date: 2009-04-29 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -24,6 +24,10 @@ extra_rdoc_files: []
24
24
  files:
25
25
  - README.textile
26
26
  - VERSION.yml
27
+ - lib/merb-auth-more
28
+ - lib/merb-auth-more/strategies
29
+ - lib/merb-auth-more/strategies/multisite
30
+ - lib/merb-auth-more/strategies/multisite/multisite_password_form.rb
27
31
  - lib/merb_auth_slice_multisite
28
32
  - lib/merb_auth_slice_multisite/merbtasks.rb
29
33
  - lib/merb_auth_slice_multisite/mixins
@@ -41,11 +45,15 @@ files:
41
45
  - spec/spec_helper.rb
42
46
  - app/controllers
43
47
  - app/controllers/application.rb
48
+ - app/controllers/exceptions.rb
49
+ - app/controllers/sessions.rb
44
50
  - app/helpers
45
51
  - app/helpers/application_helper.rb
46
52
  - app/models
47
53
  - app/models/site.rb
48
54
  - app/views
55
+ - app/views/exceptions
56
+ - app/views/exceptions/unauthenticated.html.erb
49
57
  - app/views/layout
50
58
  - app/views/layout/merb_auth_slice_multisite.html.erb
51
59
  - config/database.yml
@@ -58,7 +66,7 @@ files:
58
66
  - public/stylesheets/master.css
59
67
  - stubs/app
60
68
  - stubs/app/controllers
61
- - stubs/app/controllers/application.rb
69
+ - stubs/app/controllers/sessions.rb
62
70
  has_rdoc: true
63
71
  homepage: http://github.com/scottmotte/merb_auth_slice_multisite
64
72
  post_install_message:
@@ -84,7 +92,7 @@ requirements: []
84
92
  rubyforge_project:
85
93
  rubygems_version: 1.2.0
86
94
  signing_key:
87
- specification_version: 2
95
+ specification_version: 3
88
96
  summary: add multisite/subdomain functionality to your merb app on top of merb-auth
89
97
  test_files: []
90
98
 
@@ -1,2 +0,0 @@
1
- class MerbAuthSliceMultisite::Application < Merb::Controller
2
- end