scottmotte-merb_auth_slice_multisite 0.6.1 → 0.6.2
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/README.textile +44 -18
- data/VERSION.yml +1 -1
- data/lib/merb_auth_slice_multisite/spectasks.rb +1 -0
- metadata +1 -1
data/README.textile
CHANGED
@@ -1,23 +1,24 @@
|
|
1
1
|
h1. MerbAuthSliceMultisite
|
2
2
|
|
3
|
-
This slice setups multisite capabilities in your merb application with an authentication check.
|
3
|
+
This slice setups multisite capabilities in your merb application with subdomains (i.e. coolcars.yourapp.com) and an authentication check.
|
4
4
|
|
5
|
-
|
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.
|
5
|
+
Noteworthy: This multisite gem validates the usernames by the scope of the subdomain. This means someone can create another subdomain site using the same username and password, but technically it will be a new user in the users table - similar to how blinksale works. This is how I prefer things. It saves the hassle of a has_and_belongs_to_many relationship and the extra signup hoops you have to go through as a user if you want to create multiple accounts - lighthouse is an example of this.
|
8
6
|
|
9
7
|
h2. Instructions for installation:
|
10
8
|
|
11
9
|
1. Add github as a gem source & install
|
12
|
-
<pre><code>
|
13
|
-
gem sources -a http://gems.github.com
|
10
|
+
<pre><code>gem sources -a http://gems.github.com
|
14
11
|
sudo gem install scottmotte-merb_auth_slice_multisite
|
15
12
|
</code></pre>
|
16
13
|
|
17
14
|
2. Setup your application to use the gem.* Add the following to the end of dependencies.rb.
|
18
15
|
<pre><code>dependency "scottmotte-merb_auth_slice_multisite", :require_as => 'merb_auth_slice_multisite'</code></pre>
|
19
16
|
|
20
|
-
3.
|
17
|
+
3. Remove default merb-auth-slice-password in dependencies.rb
|
18
|
+
<pre></code><strike>dependency "merb-auth-slice-password"</strike></code></pre>
|
19
|
+
(merb_auth_slice_multisite is now standalone. it does not depend on merb-auth-slice-password)
|
20
|
+
|
21
|
+
4. Add in mixin. In your user model or in merb/merb-auth/setup.rb add the mixin
|
21
22
|
include Merb::Authentication::Mixins::UserBelongsToSite and then migrate your database.
|
22
23
|
<pre><code>
|
23
24
|
# in model
|
@@ -25,6 +26,7 @@ class User
|
|
25
26
|
include Merb::Authentication::Mixins::UserBelongsToSite
|
26
27
|
end
|
27
28
|
|
29
|
+
|
28
30
|
# or as I prefer in merb/merb-auth/setup.rb
|
29
31
|
Merb::Authentication.user_class.class_eval{
|
30
32
|
include Merb::Authentication::Mixins::SaltedUser
|
@@ -35,19 +37,44 @@ Merb::Authentication.user_class.class_eval{
|
|
35
37
|
|
36
38
|
_Don't forget to migrate your database schema with rake db:autoupgrade or rake db:automigrate_
|
37
39
|
|
40
|
+
5. Setup strategies.rb - make sure it looks like the following. merb_auth_slice_multisite uses its own custom strategy so including the others will mess things up.
|
41
|
+
<pre><code>Merb::Slices::config[:"merb_auth_slice_multisite"][:no_default_strategies] = false</code></pre>
|
42
|
+
|
43
|
+
As an aside, here's what an example router might look like using the slice to support subdomains. See how it checks for subdomain equaling www or nil and routes to the main site. And then everything else goes to the pages controller or users controller that has a subdomain. All of the controller actions then user @current_site.pages.all and @current_site.users.all instead of just User.all & Page.all. Starting to come together in your mind :)
|
44
|
+
|
45
|
+
<pre><code>
|
46
|
+
Merb.logger.info("Compiling routes...")
|
47
|
+
Merb::Router.prepare do
|
48
|
+
|
49
|
+
# Brochure site - equal to site_url from settings.yml and has no first_subdomain else do below
|
50
|
+
match(:first_subdomain => /^(www)*$/) do
|
51
|
+
resources :sites, :collection => { :changed_on_spreedly => :post }
|
52
|
+
resources :static
|
53
|
+
match('/signup').to(:controller => 'sites', :action => 'new')
|
54
|
+
match('/').to(:controller => 'static', :action => 'index')
|
55
|
+
end
|
56
|
+
|
57
|
+
# Subdomain sites
|
58
|
+
resources :pages
|
59
|
+
resources :users, :identify => :login
|
60
|
+
|
61
|
+
match('/').to(:controller => 'pages', :action => 'index')
|
62
|
+
slice(:merb_auth_slice_multisite, :name_prefix => nil, :path_prefix => "") # /login, /logout
|
63
|
+
end
|
64
|
+
</pre></code>
|
65
|
+
|
66
|
+
6. Add the slice's routes to your router
|
67
|
+
<pre><code>slice(:merb_auth_slice_multisite, :name_prefix => nil, :path_prefix => "")</code></pre>
|
38
68
|
|
39
|
-
|
69
|
+
7. Setup @current_site value. I haven't worked out how to make it automatically accessible yet so for now paste the following into your app's application.rb file.
|
40
70
|
<pre><code>
|
41
71
|
before :get_site
|
42
72
|
def get_site
|
43
|
-
# uses @current_site to create pages under
|
73
|
+
# uses @current_site - for example, to create pages under current site do @current_site.pages.new
|
44
74
|
@current_site = Site.first(:subdomain => request.first_subdomain)
|
45
75
|
raise NotFound unless @current_site
|
46
76
|
end
|
47
|
-
</pre
|
48
|
-
|
49
|
-
|
50
|
-
Check out the video. <a href="http://vimeo.com/2844037">How to install merb_auth_slice_multisite in your application.</a>
|
77
|
+
</code></pre>
|
51
78
|
|
52
79
|
|
53
80
|
h2. Additional details:
|
@@ -57,9 +84,10 @@ Schema/Migrations. The mixin adds some fields to your user model. Where needed i
|
|
57
84
|
belongs_to :site
|
58
85
|
property :site_id, Integer
|
59
86
|
validates_is_unique :login, :scope => :site_id
|
87
|
+
validates_is_unique :email, :scope => :site_id
|
60
88
|
</code></pre>
|
61
89
|
|
62
|
-
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:
|
90
|
+
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 which is what I do. For example, if you have pages under a site, you might do something like:
|
63
91
|
<pre><code># site.rb
|
64
92
|
class Site
|
65
93
|
has n, :pages, :order => [:position.asc]
|
@@ -88,7 +116,7 @@ end # Pages
|
|
88
116
|
|
89
117
|
h2. Assumptions
|
90
118
|
|
91
|
-
* works for subdomains (i.e. coolcars.yourapp.com)
|
119
|
+
* works for subdomains (i.e. coolcars.yourapp.com)
|
92
120
|
* merb only
|
93
121
|
* merb-auth-core dependency
|
94
122
|
* merb-auth-more dependency
|
@@ -99,9 +127,7 @@ h2. Assumptions
|
|
99
127
|
|
100
128
|
h2. How does it work?
|
101
129
|
|
102
|
-
When logging in the "user" object found by merb-auth-core will be asked whether the user's site_id matches the
|
103
|
-
|
104
|
-
The current_site is queried by using a model lookup off the request variable.
|
130
|
+
When logging in the "user" object found by merb-auth-core will be asked whether the user's login, password, and site_id match. It matches the site_id against the @current_site.id, and the login/password from the standard form fields.
|
105
131
|
|
106
132
|
|
107
133
|
h2. Rake tasks
|
data/VERSION.yml
CHANGED