scottmotte-merb_auth_slice_multisite 0.1.1 → 0.2.6
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/LICENSE +2 -2
- data/README.textile +115 -0
- data/VERSION.yml +2 -2
- data/bin/autospec +31 -0
- data/bin/autotest +31 -0
- data/bin/common.rb +68 -0
- data/bin/edit_json.rb +31 -0
- data/bin/erubis +31 -0
- data/bin/merb +31 -0
- data/bin/multigem +31 -0
- data/bin/multiruby +31 -0
- data/bin/multiruby_setup +31 -0
- data/bin/parse_tree_abc +31 -0
- data/bin/parse_tree_audit +31 -0
- data/bin/parse_tree_deps +31 -0
- data/bin/parse_tree_show +31 -0
- data/bin/r2r_show +31 -0
- data/bin/rackup +31 -0
- data/bin/rake +31 -0
- data/bin/rake2thor +31 -0
- data/bin/slice +31 -0
- data/bin/spec +31 -0
- data/bin/thor +31 -0
- data/bin/unit_diff +31 -0
- data/bin/zentest +31 -0
- data/lib/merb_auth_slice_multisite.rb +41 -10
- data/lib/merb_auth_slice_multisite/mixins/user_belongs_to_site.rb +63 -0
- data/lib/merb_auth_slice_multisite/mixins/user_belongs_to_site/dm_user_belongs_to_site.rb +28 -0
- data/spec/mixins/user_belongs_to_site_spec.rb +56 -0
- data/spec/models/site_spec.rb +83 -0
- data/spec/spec_helper.rb +46 -1
- metadata +56 -8
- data/README.rdoc +0 -7
- data/spec/requests/main_spec.rb +0 -30
- data/test/merb_auth_slice_multisite_test.rb +0 -7
- data/test/test_helper.rb +0 -10
@@ -0,0 +1,63 @@
|
|
1
|
+
module Merb
|
2
|
+
class Authentication
|
3
|
+
module Mixins
|
4
|
+
# This mixin provides basic user authentication by a site_id.
|
5
|
+
#
|
6
|
+
# Added properties:
|
7
|
+
# :site_id, Integer
|
8
|
+
#
|
9
|
+
# Added Relationships
|
10
|
+
# belongs_to :site
|
11
|
+
#
|
12
|
+
# Added Validations
|
13
|
+
# validates_present :site_id
|
14
|
+
# validates_is_unique :login, :scope => :site_id
|
15
|
+
# validates_is_unique :email, :scope => :site_id
|
16
|
+
#
|
17
|
+
# To use it simply require it and include it into your user class.
|
18
|
+
#
|
19
|
+
# class User
|
20
|
+
# include Authentication::Mixins::UserBelongsToSite
|
21
|
+
#
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
#
|
25
|
+
# OR I RECOMMEND recommend that you put it in your application under merb/merb-auth/setup.rb so it looks like this:
|
26
|
+
# # require 'merb-auth-more/mixins/salted_user'
|
27
|
+
# # Merb::Authentication.user_class.class_eval{
|
28
|
+
# # include Merb::Authentication::Mixins::SaltedUser
|
29
|
+
# # include Merb::Authentication::Mixins::UserBelongsToSite
|
30
|
+
# # }
|
31
|
+
#
|
32
|
+
module UserBelongsToSite
|
33
|
+
def self.included(base)
|
34
|
+
base.class_eval do
|
35
|
+
include Merb::Authentication::Mixins::UserBelongsToSite::InstanceMethods
|
36
|
+
extend Merb::Authentication::Mixins::UserBelongsToSite::ClassMethods
|
37
|
+
|
38
|
+
path = File.expand_path(File.dirname(__FILE__)) / "user_belongs_to_site"
|
39
|
+
if defined?(DataMapper) && DataMapper::Resource > self
|
40
|
+
require path / "dm_user_belongs_to_site"
|
41
|
+
extend(Merb::Authentication::Mixins::UserBelongsToSite::DMClassMethods)
|
42
|
+
elsif defined?(ActiveRecord) && ancestors.include?(ActiveRecord::Base)
|
43
|
+
require path / "ar_user_belongs_to_site"
|
44
|
+
extend(Merb::Authentication::Mixins::UserBelongsToSite::ARClassMethods)
|
45
|
+
elsif defined?(Sequel) && ancestors.include?(Sequel::Model)
|
46
|
+
require path / "sq_user_belongs_to_site"
|
47
|
+
extend(Merb::Authentication::Mixins::UserBelongsToSite::SQClassMethods)
|
48
|
+
end
|
49
|
+
|
50
|
+
end # base.class_eval
|
51
|
+
end # self.included
|
52
|
+
|
53
|
+
module ClassMethods
|
54
|
+
#
|
55
|
+
end # ClassMethods
|
56
|
+
|
57
|
+
module InstanceMethods
|
58
|
+
#
|
59
|
+
end # InstanceMethods
|
60
|
+
end # UserBelongsToSite
|
61
|
+
end # Mixins
|
62
|
+
end # Authentication
|
63
|
+
end # Merb
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Merb
|
2
|
+
class Authentication
|
3
|
+
module Mixins
|
4
|
+
module UserBelongsToSite
|
5
|
+
module DMClassMethods
|
6
|
+
def self.extended(base)
|
7
|
+
base.class_eval do
|
8
|
+
# Schema
|
9
|
+
property :site_id, Integer
|
10
|
+
# Validations
|
11
|
+
validates_present :site_id
|
12
|
+
validates_is_unique :login, :scope => :site_id
|
13
|
+
validates_is_unique :email, :scope => :site_id
|
14
|
+
# Relationships/Associations
|
15
|
+
belongs_to :site
|
16
|
+
end # base.class_eval
|
17
|
+
end # self.extended
|
18
|
+
end # DMClassMethods
|
19
|
+
end # UserBelongsToSite
|
20
|
+
end # Mixins
|
21
|
+
end # Authentication
|
22
|
+
|
23
|
+
class Request
|
24
|
+
def first_subdomain
|
25
|
+
subdomains.first #subdomains is a request variable array so we take the first array entry
|
26
|
+
end #first_subdomain
|
27
|
+
end #Request
|
28
|
+
end #Merb
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe "User Belongs To Site" do
|
4
|
+
before(:all) do
|
5
|
+
Merb::Router.prepare { add_slice(:merb_auth_slice_multisite)}
|
6
|
+
end
|
7
|
+
|
8
|
+
after(:all) do
|
9
|
+
Merb::Router.reset!
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "UserBelongsToSite Mixin" do
|
13
|
+
|
14
|
+
before(:each) do
|
15
|
+
User.all.destroy!
|
16
|
+
@user = User.new(valid_user_attributes)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should add the 'site_id' property to the user model" do
|
20
|
+
@user.should respond_to(:site_id)
|
21
|
+
@user.should respond_to(:site_id=)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be invalid when site_id is nil" do
|
25
|
+
@user.site_id = nil
|
26
|
+
@user.save
|
27
|
+
@user.should_not be_valid
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should have a unique login and email" do
|
31
|
+
@user.save
|
32
|
+
@user.should be_valid
|
33
|
+
|
34
|
+
# not unique
|
35
|
+
@user2 = User.new(valid_user_attributes)
|
36
|
+
@user2.should_not be_valid
|
37
|
+
|
38
|
+
# unique email but duplicate login should fail
|
39
|
+
@user2 = User.new(valid_user_attributes)
|
40
|
+
@user2.email = "different@example.org"
|
41
|
+
@user2.should_not be_valid
|
42
|
+
|
43
|
+
# unique login but duplicate email should fail
|
44
|
+
@user2 = User.new(valid_user_attributes)
|
45
|
+
@user2.login = "different"
|
46
|
+
@user2.should_not be_valid
|
47
|
+
|
48
|
+
# unique login & unique email should be valid
|
49
|
+
@user2 = User.new(valid_user_attributes)
|
50
|
+
@user2.login = "unique"
|
51
|
+
@user2.email = "unique@example.org"
|
52
|
+
@user2.should be_valid
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
|
2
|
+
|
3
|
+
describe Site do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
Site.all.destroy!
|
7
|
+
@site = Site.new(valid_site_attributes)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should be valid when new" do
|
11
|
+
@site.should be_valid
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should be invalid when domain is not unique" do
|
15
|
+
@site.save
|
16
|
+
@site.should be_valid
|
17
|
+
|
18
|
+
@site2 = Site.new(valid_site_attributes)
|
19
|
+
@site2.subdomain = "differentsubdomain"
|
20
|
+
@site2.save
|
21
|
+
@site2.should_not be_valid
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should be invalid when subdomain is not unique" do
|
25
|
+
@site.save
|
26
|
+
@site.should be_valid
|
27
|
+
|
28
|
+
@site2 = Site.new(valid_site_attributes)
|
29
|
+
@site2.domain = "http://differenturl.com"
|
30
|
+
@site2.save
|
31
|
+
@site2.should_not be_valid
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should not allow use of reserved subdomains" do
|
35
|
+
Site::ReservedSubdomains.each do |reserved|
|
36
|
+
@site.subdomain = reserved
|
37
|
+
@site.save
|
38
|
+
@site.should_not be_valid
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be invalid if missing a domain extension" do
|
43
|
+
@site.save
|
44
|
+
@site.domain.should match(/(\.[a-z]{2,4})$/)
|
45
|
+
|
46
|
+
@site2 = Site.new(valid_site_attributes)
|
47
|
+
@site2.domain = "secondmemorial.com"
|
48
|
+
@site2.save
|
49
|
+
@site2.domain.should match(/(\.[a-z]{2,4})$/)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should only allow text and numbers as a subdomain and should not allow any odd characters or whitespace" do
|
53
|
+
@site.save
|
54
|
+
@site.subdomain.should match(/^[a-zA-Z0-9\-]*?$/)
|
55
|
+
|
56
|
+
@site2 = Site.new(valid_site_attributes)
|
57
|
+
@site2.subdomain = "strange-but-legal"
|
58
|
+
@site2.save
|
59
|
+
@site2.subdomain.should match(/^[a-zA-Z0-9\-]*?$/)
|
60
|
+
|
61
|
+
@site3 = Site.new(valid_site_attributes)
|
62
|
+
@site3.subdomain = "i!!egal@ch@racters"
|
63
|
+
@site3.save
|
64
|
+
@site3.subdomain.should_not match(/^[a-zA-Z0-9\-]*?$/)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should clean http:// and www from the domain" do
|
68
|
+
@site.save
|
69
|
+
@site.domain.should_not include("http://")
|
70
|
+
@site.domain.should_not include("www.")
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should respond to users" do
|
74
|
+
@site.save
|
75
|
+
@site.should respond_to(:users)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should have a full created_at field after save" do
|
79
|
+
@site.save
|
80
|
+
@site.created_at.should_not be_nil
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -2,6 +2,8 @@ require 'rubygems'
|
|
2
2
|
require 'merb-core'
|
3
3
|
require 'merb-slices'
|
4
4
|
require 'spec'
|
5
|
+
require 'dm-core'
|
6
|
+
require 'dm-validations'
|
5
7
|
|
6
8
|
# Add merb_auth_slice_multisite.rb to the search path
|
7
9
|
Merb::Plugins.config[:merb_slices][:auto_register] = true
|
@@ -43,6 +45,10 @@ Spec::Runner.configure do |config|
|
|
43
45
|
config.include(Merb::Test::RouteHelper)
|
44
46
|
config.include(Merb::Test::ControllerHelper)
|
45
47
|
config.include(Merb::Test::SliceHelper)
|
48
|
+
|
49
|
+
config.before(:all) do
|
50
|
+
DataMapper.auto_migrate! if Merb.orm == :datamapper
|
51
|
+
end
|
46
52
|
end
|
47
53
|
|
48
54
|
# You can add your own helpers here
|
@@ -55,4 +61,43 @@ Merb::Test.add_helpers do
|
|
55
61
|
def dismount_slice
|
56
62
|
Merb::Router.reset! if standalone?
|
57
63
|
end
|
58
|
-
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
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.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- scottmotte
|
@@ -15,26 +15,74 @@ dependencies: []
|
|
15
15
|
|
16
16
|
description:
|
17
17
|
email: scott@scottmotte.com
|
18
|
-
executables:
|
19
|
-
|
18
|
+
executables:
|
19
|
+
- autospec
|
20
|
+
- autotest
|
21
|
+
- common.rb
|
22
|
+
- edit_json.rb
|
23
|
+
- erubis
|
24
|
+
- merb
|
25
|
+
- multigem
|
26
|
+
- multiruby
|
27
|
+
- multiruby_setup
|
28
|
+
- parse_tree_abc
|
29
|
+
- parse_tree_audit
|
30
|
+
- parse_tree_deps
|
31
|
+
- parse_tree_show
|
32
|
+
- r2r_show
|
33
|
+
- rackup
|
34
|
+
- rake
|
35
|
+
- rake2thor
|
36
|
+
- slice
|
37
|
+
- spec
|
38
|
+
- thor
|
39
|
+
- unit_diff
|
40
|
+
- zentest
|
20
41
|
extensions: []
|
21
42
|
|
22
43
|
extra_rdoc_files:
|
23
|
-
- README.
|
44
|
+
- README.textile
|
24
45
|
- LICENSE
|
25
46
|
files:
|
26
|
-
- README.
|
47
|
+
- README.textile
|
27
48
|
- VERSION.yml
|
49
|
+
- bin/autospec
|
50
|
+
- bin/autotest
|
51
|
+
- bin/common.rb
|
52
|
+
- bin/edit_json.rb
|
53
|
+
- bin/erubis
|
54
|
+
- bin/merb
|
55
|
+
- bin/multigem
|
56
|
+
- bin/multiruby
|
57
|
+
- bin/multiruby_setup
|
58
|
+
- bin/parse_tree_abc
|
59
|
+
- bin/parse_tree_audit
|
60
|
+
- bin/parse_tree_deps
|
61
|
+
- bin/parse_tree_show
|
62
|
+
- bin/r2r_show
|
63
|
+
- bin/rackup
|
64
|
+
- bin/rake
|
65
|
+
- bin/rake2thor
|
66
|
+
- bin/slice
|
67
|
+
- bin/spec
|
68
|
+
- bin/thor
|
69
|
+
- bin/unit_diff
|
70
|
+
- bin/zentest
|
28
71
|
- lib/merb_auth_slice_multisite
|
29
72
|
- lib/merb_auth_slice_multisite/merbtasks.rb
|
73
|
+
- lib/merb_auth_slice_multisite/mixins
|
74
|
+
- lib/merb_auth_slice_multisite/mixins/user_belongs_to_site
|
75
|
+
- lib/merb_auth_slice_multisite/mixins/user_belongs_to_site/dm_user_belongs_to_site.rb
|
76
|
+
- lib/merb_auth_slice_multisite/mixins/user_belongs_to_site.rb
|
30
77
|
- lib/merb_auth_slice_multisite/slicetasks.rb
|
31
78
|
- lib/merb_auth_slice_multisite/spectasks.rb
|
32
79
|
- lib/merb_auth_slice_multisite.rb
|
33
|
-
- test/merb_auth_slice_multisite_test.rb
|
34
|
-
- test/test_helper.rb
|
35
80
|
- spec/merb_auth_slice_multisite_spec.rb
|
81
|
+
- spec/mixins
|
82
|
+
- spec/mixins/user_belongs_to_site_spec.rb
|
83
|
+
- spec/models
|
84
|
+
- spec/models/site_spec.rb
|
36
85
|
- spec/requests
|
37
|
-
- spec/requests/main_spec.rb
|
38
86
|
- spec/spec_helper.rb
|
39
87
|
- LICENSE
|
40
88
|
has_rdoc: true
|
data/README.rdoc
DELETED
data/spec/requests/main_spec.rb
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), '..', 'spec_helper.rb')
|
2
|
-
|
3
|
-
describe "/merb_auth_slice_multisite/" do
|
4
|
-
|
5
|
-
before(:all) do
|
6
|
-
mount_slice
|
7
|
-
end
|
8
|
-
|
9
|
-
describe "GET /" do
|
10
|
-
|
11
|
-
before(:each) do
|
12
|
-
@response = request("/merb_auth_slice_multisite/")
|
13
|
-
end
|
14
|
-
|
15
|
-
it "should be successful" do
|
16
|
-
@response.status.should be_successful
|
17
|
-
end
|
18
|
-
|
19
|
-
# This is just an example of what you can do
|
20
|
-
# You can also use the other webrat methods to click links,
|
21
|
-
# fill up forms etc...
|
22
|
-
it "should render the default slice layout" do
|
23
|
-
@response.should have_tag(:h1, :content => "MerbAuthSliceMultisite Slice")
|
24
|
-
@response.should have_selector("div#container div#main")
|
25
|
-
@response.should have_xpath("//div[@id='container']/div[@id='main']")
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
data/test/test_helper.rb
DELETED