scottmotte-merb-auth-core 1.0.10
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.textile +115 -0
- data/Rakefile +62 -0
- data/spec/spec_helper.rb +103 -0
- metadata +103 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Scott Motte
|
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,115 @@
|
|
1
|
+
h1. MerbAuthSliceMultisite
|
2
|
+
|
3
|
+
This slice setups multisite capabilities in your merb application with an authentication check.
|
4
|
+
|
5
|
+
It works for subdomains (i.e. coolcars.yourapp.com) or for domains routed to your application via your server (i.e. coolcars.com).
|
6
|
+
|
7
|
+
It has a check to make a user to the site he's trying to login to. If the user does not match than authentication fails.
|
8
|
+
|
9
|
+
h2. Instructions for installation:
|
10
|
+
|
11
|
+
1. Add github as a gem source & install
|
12
|
+
<pre><code>
|
13
|
+
gem sources -a http://gems.github.com
|
14
|
+
sudo gem install scottmotte-merb_auth_slice_multisite
|
15
|
+
</code></pre>
|
16
|
+
|
17
|
+
2. Setup your application to use the gem.* Add the following to the end of dependencies.rb.
|
18
|
+
<pre><code>dependency "scottmotte-merb_auth_slice_multisite", :require_as => 'merb_auth_slice_multisite'</code></pre>
|
19
|
+
|
20
|
+
3. Add in mixin. In your user model or in merb/merb-auth/setup.rb add the mixin
|
21
|
+
include Merb::Authentication::Mixins::UserBelongsToSite and then migrate your database.
|
22
|
+
<pre><code>
|
23
|
+
# in model
|
24
|
+
class User
|
25
|
+
include Merb::Authentication::Mixins::UserBelongsToSite
|
26
|
+
end
|
27
|
+
|
28
|
+
# or as I prefer in merb/merb-auth/setup.rb
|
29
|
+
Merb::Authentication.user_class.class_eval{
|
30
|
+
include Merb::Authentication::Mixins::SaltedUser
|
31
|
+
include Merb::Authentication::Mixins::ActivatedUser
|
32
|
+
include Merb::Authentication::Mixins::UserBelongsToSite # <-- this one
|
33
|
+
}
|
34
|
+
</code></pre>
|
35
|
+
|
36
|
+
_Don't forget to migrate your database schema with rake db:autoupgrade or rake db:automigrate_
|
37
|
+
|
38
|
+
|
39
|
+
4. Setup @current_site value. I haven't worked out how to make it automatically accessible yet so for now I just paste the following into my app's application.rb file.
|
40
|
+
<pre><code>
|
41
|
+
before :get_site
|
42
|
+
def get_site
|
43
|
+
# uses @current_site to create pages under appropriate site like @current_site.pages.new
|
44
|
+
# if there is a subdomain then fetch from subdomain, otherwise fetch from domain
|
45
|
+
if request.first_subdomain != nil
|
46
|
+
@current_site = Site.first(:subdomain => request.first_subdomain)
|
47
|
+
else
|
48
|
+
@current_site = Site.first(:domain => request.domain)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
</pre></code>
|
52
|
+
|
53
|
+
|
54
|
+
Check out the video. <a href="http://scottmotte.github.com/merb_auth_slice_multisite/">How to install merb_auth_slice_multisite in your application.</a>
|
55
|
+
|
56
|
+
|
57
|
+
h2. Additional details:
|
58
|
+
|
59
|
+
Schema/Migrations. The mixin requires some fields to be in-place in your user model. Where needed include these in your migrations.
|
60
|
+
<pre><code># Relationships/Associations
|
61
|
+
belongs_to :site
|
62
|
+
property :site_id, Integer
|
63
|
+
validates_is_unique :login, :scope => :site_id
|
64
|
+
</code></pre>
|
65
|
+
|
66
|
+
Site model. You're probably wondering where the heck is the site model. It's in the slice. You can override it by running one of the rake tasks or you can create your own site.rb model and add additional fields. For example, if you have pages under a site, you might do something like:
|
67
|
+
<pre><code># site.rb
|
68
|
+
class Site
|
69
|
+
has n, :pages, :order => [:position.asc]
|
70
|
+
end
|
71
|
+
</code></pre>
|
72
|
+
|
73
|
+
@current_site. You can use @current_site in your controllers like so:
|
74
|
+
<pre><code>
|
75
|
+
class Pages < Application
|
76
|
+
# provides :xml, :yaml, :js
|
77
|
+
|
78
|
+
def index
|
79
|
+
@pages = @current_site.pages.all
|
80
|
+
display @pages
|
81
|
+
end
|
82
|
+
|
83
|
+
def show(id)
|
84
|
+
@page = @current_site.pages.get(id)
|
85
|
+
raise NotFound unless @page
|
86
|
+
display @page
|
87
|
+
end
|
88
|
+
|
89
|
+
end # Pages
|
90
|
+
</pre></code>
|
91
|
+
|
92
|
+
|
93
|
+
h2. Assumptions
|
94
|
+
|
95
|
+
* works for subdomains (i.e. coolcars.yourapp.com) or for domains routed to your application via your server (i.e. coolcars.com). see lines 40-55 in lib/merb_auth_slice_multisite.rb and lib/merb_auth_slice_multisite/mixins/user_belongs_to_site/dm_user_belongs_to_site.rb for details
|
96
|
+
* merb only
|
97
|
+
* merb-auth-core dependency
|
98
|
+
* merb-auth-more dependency
|
99
|
+
* *only supports datamapper so far* (help me extend it! fork the project and request me to pull)
|
100
|
+
* A site has n (has_many) users. A user belongs_to a site.
|
101
|
+
* You can have multiple users with the same username and password as long as they each belong_to a different site. For example, there can be an admin user with the credentials { :login => 'admin', :email => 'admin@example.org', :site_id => 1} and an admin user with the credentials { :login => 'admin', :email => 'admin@example.org', :site_id => 66}. As long as the site_id is different then it is ok. This allows more freedom when your users want to setup multiple sites.
|
102
|
+
|
103
|
+
|
104
|
+
h2. How does it work?
|
105
|
+
|
106
|
+
When logging in the "user" object found by merb-auth-core will be asked whether the user's site_id matches the id of the current_site. It user merb-auth's after_authentication method to do this.
|
107
|
+
|
108
|
+
The current_site is queried by using a model lookup off the request variable.
|
109
|
+
|
110
|
+
|
111
|
+
h2. Rake tasks
|
112
|
+
|
113
|
+
To see all available tasks for MerbAuthSliceMultisite run:
|
114
|
+
|
115
|
+
rake -T slices:merb_auth_slice_multisite
|
data/Rakefile
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/gempackagetask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
require 'merb-core'
|
6
|
+
require 'merb-core/tasks/merb'
|
7
|
+
require 'merb-core/test/tasks/spectasks'
|
8
|
+
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'jeweler'
|
12
|
+
Jeweler::Tasks.new do |gem|
|
13
|
+
gem.name = "merb_auth_slice_multisite"
|
14
|
+
gem.summary = %Q{add multisite/subdomain functionality to your merb app on top of merb-auth}
|
15
|
+
gem.email = "scott@scottmotte.com"
|
16
|
+
gem.homepage = "http://github.com/scottmotte/merb_auth_slice_multisite"
|
17
|
+
gem.authors = ["scottmotte"]
|
18
|
+
|
19
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
20
|
+
end
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'rake/testtask'
|
26
|
+
Rake::TestTask.new(:test) do |test|
|
27
|
+
test.libs << 'lib' << 'test'
|
28
|
+
test.pattern = 'test/**/*_test.rb'
|
29
|
+
test.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
require 'rcov/rcovtask'
|
34
|
+
Rcov::RcovTask.new do |test|
|
35
|
+
test.libs << 'test'
|
36
|
+
test.pattern = 'test/**/*_test.rb'
|
37
|
+
test.verbose = true
|
38
|
+
end
|
39
|
+
rescue LoadError
|
40
|
+
task :rcov do
|
41
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
if File.exist?('VERSION.yml')
|
48
|
+
config = YAML.load(File.read('VERSION.yml'))
|
49
|
+
version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
|
50
|
+
else
|
51
|
+
version = ""
|
52
|
+
end
|
53
|
+
|
54
|
+
rdoc.rdoc_dir = 'rdoc'
|
55
|
+
rdoc.title = "merb_auth_slice_multisite #{version}"
|
56
|
+
rdoc.rdoc_files.include('README*')
|
57
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
58
|
+
end
|
59
|
+
|
60
|
+
|
61
|
+
desc 'Default: run spec examples'
|
62
|
+
task :default => 'spec'
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'merb-core'
|
3
|
+
require 'merb-slices'
|
4
|
+
require 'spec'
|
5
|
+
require 'dm-core'
|
6
|
+
require 'dm-validations'
|
7
|
+
|
8
|
+
# Add merb_auth_slice_multisite.rb to the search path
|
9
|
+
Merb::Plugins.config[:merb_slices][:auto_register] = true
|
10
|
+
Merb::Plugins.config[:merb_slices][:search_path] = File.join(File.dirname(__FILE__), '..', 'lib', 'merb_auth_slice_multisite.rb')
|
11
|
+
|
12
|
+
# Require merb_auth_slice_multisite.rb explicitly so any dependencies are loaded
|
13
|
+
require Merb::Plugins.config[:merb_slices][:search_path]
|
14
|
+
|
15
|
+
# Using Merb.root below makes sure that the correct root is set for
|
16
|
+
# - testing standalone, without being installed as a gem and no host application
|
17
|
+
# - testing from within the host application; its root will be used
|
18
|
+
Merb.start_environment(
|
19
|
+
:testing => true,
|
20
|
+
:adapter => 'runner',
|
21
|
+
:environment => ENV['MERB_ENV'] || 'test',
|
22
|
+
:session_store => 'memory'
|
23
|
+
)
|
24
|
+
|
25
|
+
module Merb
|
26
|
+
module Test
|
27
|
+
module SliceHelper
|
28
|
+
|
29
|
+
# The absolute path to the current slice
|
30
|
+
def current_slice_root
|
31
|
+
@current_slice_root ||= File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
32
|
+
end
|
33
|
+
|
34
|
+
# Whether the specs are being run from a host application or standalone
|
35
|
+
def standalone?
|
36
|
+
Merb.root == ::MerbAuthSliceMultisite.root
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
Spec::Runner.configure do |config|
|
44
|
+
config.include(Merb::Test::ViewHelper)
|
45
|
+
config.include(Merb::Test::RouteHelper)
|
46
|
+
config.include(Merb::Test::ControllerHelper)
|
47
|
+
config.include(Merb::Test::SliceHelper)
|
48
|
+
|
49
|
+
config.before(:all) do
|
50
|
+
DataMapper.auto_migrate! if Merb.orm == :datamapper
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# You can add your own helpers here
|
55
|
+
#
|
56
|
+
Merb::Test.add_helpers do
|
57
|
+
def mount_slice
|
58
|
+
Merb::Router.prepare { add_slice(:MerbAuthSliceMultisite, "merb_auth_slice_multisite") } if standalone?
|
59
|
+
end
|
60
|
+
|
61
|
+
def dismount_slice
|
62
|
+
Merb::Router.reset! if standalone?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# =============================================================================
|
67
|
+
# SITE STUFF
|
68
|
+
# =============================================================================
|
69
|
+
def valid_site_attributes(options = {})
|
70
|
+
{
|
71
|
+
:domain => 'http://www.example.org',
|
72
|
+
:subdomain => 'example',
|
73
|
+
:id => 1
|
74
|
+
}.merge(options)
|
75
|
+
end
|
76
|
+
|
77
|
+
def valid_second_site_attributes(options = {})
|
78
|
+
{
|
79
|
+
:domain => 'http://www.example2.org',
|
80
|
+
:subdomain => 'example2',
|
81
|
+
:active => true,
|
82
|
+
:id => 2
|
83
|
+
}.merge(options)
|
84
|
+
end
|
85
|
+
|
86
|
+
def valid_third_site_attributes(options = {})
|
87
|
+
{
|
88
|
+
:domain => 'http://www.example3.org',
|
89
|
+
:subdomain => 'example3',
|
90
|
+
:active => true,
|
91
|
+
:id => 3
|
92
|
+
}.merge(options)
|
93
|
+
end
|
94
|
+
|
95
|
+
# =============================================================================
|
96
|
+
# USER STUFF - see init.rb for where this gets spoofed
|
97
|
+
# =============================================================================
|
98
|
+
def valid_user_attributes(options = {})
|
99
|
+
{ :login => 'fred',
|
100
|
+
:email => 'fred@example.com',
|
101
|
+
:site_id => 1
|
102
|
+
}.merge(options)
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scottmotte-merb-auth-core
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.10
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam French, Daniel Neighman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-03-18 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: merb-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.10
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: extlib
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: An Authentication framework for Merb
|
36
|
+
email: has.sox@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README.textile
|
43
|
+
- LICENSE
|
44
|
+
- TODO
|
45
|
+
files:
|
46
|
+
- LICENSE
|
47
|
+
- README.textile
|
48
|
+
- Rakefile
|
49
|
+
- TODO
|
50
|
+
- lib/merb-auth-core
|
51
|
+
- lib/merb-auth-core/authenticated_helper.rb
|
52
|
+
- lib/merb-auth-core/authentication.rb
|
53
|
+
- lib/merb-auth-core/bootloader.rb
|
54
|
+
- lib/merb-auth-core/callbacks.rb
|
55
|
+
- lib/merb-auth-core/customizations.rb
|
56
|
+
- lib/merb-auth-core/errors.rb
|
57
|
+
- lib/merb-auth-core/merbtasks.rb
|
58
|
+
- lib/merb-auth-core/responses.rb
|
59
|
+
- lib/merb-auth-core/router_helper.rb
|
60
|
+
- lib/merb-auth-core/session_mixin.rb
|
61
|
+
- lib/merb-auth-core/strategy.rb
|
62
|
+
- lib/merb-auth-core.rb
|
63
|
+
- spec/helpers
|
64
|
+
- spec/helpers/authentication_helper_spec.rb
|
65
|
+
- spec/merb-auth-core
|
66
|
+
- spec/merb-auth-core/activation_fixture.rb
|
67
|
+
- spec/merb-auth-core/authentication_spec.rb
|
68
|
+
- spec/merb-auth-core/callbacks_spec.rb
|
69
|
+
- spec/merb-auth-core/customizations_spec.rb
|
70
|
+
- spec/merb-auth-core/errors_spec.rb
|
71
|
+
- spec/merb-auth-core/failed_login_spec.rb
|
72
|
+
- spec/merb-auth-core/merb-auth-core_spec.rb
|
73
|
+
- spec/merb-auth-core/router_helper_spec.rb
|
74
|
+
- spec/merb-auth-core/strategy_spec.rb
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://merbivore.com/
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: "0"
|
88
|
+
version:
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: "0"
|
94
|
+
version:
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project: merb
|
98
|
+
rubygems_version: 1.2.0
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: An Authentication framework for Merb
|
102
|
+
test_files: []
|
103
|
+
|