public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
user scoping for sites

git-svn-id: http://svn.techno-weenie.net/projects/mephisto/trunk@2094 
567b1171-46fb-0310-a4c9-b4bef9110e78
technoweenie (author)
Wed Sep 06 06:56:30 -0700 2006
commit  adf2dc7dc306f3fa2f306320dce9f0276317e827
tree    d54e78f144358854fe19512b1b079c11421eaf68
parent  993077be52f6ce846bf72136501d836fcbcb679c
...
1
2
3
4
 
5
6
7
...
33
34
35
36
 
37
38
 
39
40
41
42
43
44
45
46
47
48
49
 
50
51
52
53
 
54
55
56
...
1
2
3
 
4
5
6
7
...
33
34
35
 
36
37
 
38
39
40
41
 
42
43
44
45
46
47
 
48
49
50
51
 
52
53
54
55
0
@@ -1,7 +1,7 @@
0
 class Admin::UsersController < Admin::BaseController
0
   MEMBER_ACTIONS = %w(show update).freeze unless const_defined?(:MEMBER_ACTIONS)
0
   before_filter :find_all_users, :only => [:index, :show, :new]
0
- before_filter :find_user, :only => [:show, :update]
0
+ before_filter :find_user, :only => [:show, :update, :enable]
0
   def index
0
     @enabled, @disabled = @users.partition { |u| u.deleted_at.nil? }
0
     @users = @enabled + @disabled
0
@@ -33,24 +33,23 @@ class Admin::UsersController < Admin::BaseController
0
   end
0
 
0
   def destroy
0
- @user = User.find params[:id]
0
+ @user = site.user(params[:id])
0
     @user.destroy
0
- @user = User.find_with_deleted params[:id] # reload
0
+ @user = site.user_with_deleted(params[:id]) # reload
0
   end
0
 
0
   def enable
0
- @user = User.find_with_deleted params[:id]
0
     @user.deleted_at = nil
0
     @user.save!
0
   end
0
   
0
   protected
0
     def find_all_users
0
- @users = User.find_with_deleted :all, :order => 'login'
0
+ @users = site.users_with_deleted
0
     end
0
     
0
     def find_user
0
- @user = User.find_with_deleted params[:id]
0
+ @user = site.user_with_deleted(params[:id])
0
     end
0
     
0
     def authorized?
...
24
25
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
28
29
...
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
0
@@ -24,6 +24,22 @@ class Site < ActiveRecord::Base
0
   validates_format_of :host, :with => /^([a-z0-9]([-a-z0-9]*[a-z0-9])?\.)+((a[cdefgilmnoqrstuwxz]|aero|arpa)|(b[abdefghijmnorstvwyz]|biz)|(c[acdfghiklmnorsuvxyz]|cat|com|coop)|d[ejkmoz]|(e[ceghrstu]|edu)|f[ijkmor]|(g[abdefghilmnpqrstuwy]|gov)|h[kmnrtu]|(i[delmnoqrst]|info|int)|(j[emop]|jobs)|k[eghimnprwyz]|l[abcikrstuvy]|(m[acdghklmnopqrstuvwxyz]|mil|mobi|museum)|(n[acefgilopruz]|name|net)|(om|org)|(p[aefghklmnrstwy]|pro)|qa|r[eouw]|s[abcdeghijklmnortvyz]|(t[cdfghjklmnoprtvwz]|travel)|u[agkmsyz]|v[aceginu]|w[fs]|y[etu]|z[amw])$/
0
   validates_uniqueness_of :host
0
 
0
+ def users
0
+ User.find_all_by_site self
0
+ end
0
+
0
+ def users_with_deleted
0
+ User.find_all_by_site_with_deleted self
0
+ end
0
+
0
+ def user(id)
0
+ User.find_by_site self, id
0
+ end
0
+
0
+ def user_with_deleted(id)
0
+ User.find_by_site_with_deleted self, id
0
+ end
0
+
0
   with_options :order => 'contents.created_at', :class_name => 'Comment' do |comment|
0
     comment.has_many :comments, :conditions => ['contents.approved = ?', true]
0
     comment.has_many :unapproved_comments, :conditions => ['contents.approved = ? or contents.approved is null', false]
...
2
3
4
 
 
 
5
6
7
...
20
21
22
23
24
 
 
25
26
27
28
29
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
32
33
...
2
3
4
5
6
7
8
9
10
...
23
24
25
 
 
26
27
28
29
30
 
 
 
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
0
@@ -2,6 +2,9 @@ require 'digest/sha1'
0
 class UserAuth < ActiveRecord::Base
0
   set_table_name 'users'
0
   self.abstract_class = true
0
+ @@membership_options = {:select => 'distinct users.*, memberships.admin as site_admin', :order => 'users.login',
0
+ :joins => 'left outer join memberships on users.id = memberships.user_id'}
0
+
0
   # Virtual attribute for the unencrypted password
0
   attr_accessor :password
0
 
0
@@ -20,14 +23,26 @@ class UserAuth < ActiveRecord::Base
0
 
0
   # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
0
   def self.authenticate_for(site, login, password)
0
- u = find(:first, :select => 'users.*, memberships.admin as site_admin', :joins => 'left outer join memberships on users.id = memberships.user_id',
0
- :conditions => ['users.login = ? and (memberships.site_id = ? or users.admin = ?)', login, site.id, true])
0
+ u = find(:first, @@membership_options.merge(
0
+ :conditions => ['users.login = ? and (memberships.site_id = ? or users.admin = ?)', login, site.id, true]))
0
     u && u.authenticated?(password) ? u : nil
0
   end
0
 
0
- def self.find_for_site(site, id)
0
- find(:first, :select => 'users.*, memberships.admin as site_admin', :joins => 'left outer join memberships on users.id = memberships.user_id',
0
- :conditions => ['users.id = ? and (memberships.site_id = ? or users.admin = ?)', id, site.id, true])
0
+ def self.find_by_site(site, id)
0
+ with_deleted_scope { find_by_site_with_deleted(site, id) }
0
+ end
0
+
0
+ def self.find_by_site_with_deleted(site, id)
0
+ find_with_deleted(:first, @@membership_options.merge(
0
+ :conditions => ['users.id = ? and (memberships.site_id = ? or users.admin = ?)', id, site.id, true]))
0
+ end
0
+
0
+ def self.find_all_by_site(site)
0
+ with_deleted_scope { find_all_by_site_with_deleted(site) }
0
+ end
0
+
0
+ def self.find_all_by_site_with_deleted(site)
0
+ find_with_deleted(:all, @@membership_options.merge(:conditions => ['memberships.site_id = ? or users.admin = ?', site.id, true])).uniq
0
   end
0
 
0
   # Encrypts some data with the salt.
...
1
2
3
4
 
5
6
7
...
1
2
3
 
4
5
6
7
0
@@ -1,7 +1,7 @@
0
 module AuthenticatedSystem
0
   protected
0
     def logged_in?
0
- (@current_user ||= session[:user] ? User.find_for_site(site, session[:user]) : :false).is_a?(User)
0
+ (@current_user ||= session[:user] ? User.find_by_site(site, session[:user]) : :false).is_a?(User)
0
     end
0
 
0
     def current_user
...
15
16
17
 
 
 
 
 
 
 
 
 
 
18
...
15
16
17
18
19
20
21
22
23
24
25
26
27
28
0
@@ -15,4 +15,14 @@ class MembershipTest < Test::Unit::TestCase
0
     assert_models_equal [users(:arthur), users(:quentin)], sites(:first).admins
0
     assert_models_equal [users(:quentin)], sites(:hostess).admins
0
   end
0
+
0
+ def test_should_find_all_site_users
0
+ assert_models_equal [users(:arthur), users(:quentin)], User.find_all_by_site(sites(:first))
0
+ assert_models_equal [users(:arthur), users(:quentin)], sites(:first).users
0
+ end
0
+
0
+ def test_should_find_all_site_users_with_deleted
0
+ assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)], User.find_all_by_site_with_deleted(sites(:first))
0
+ assert_models_equal [User.find_with_deleted(3), users(:arthur), users(:quentin)], sites(:first).users_with_deleted
0
+ end
0
 end
...
45
46
47
48
 
49
50
51
...
60
61
62
63
64
 
 
65
66
67
...
45
46
47
 
48
49
50
51
...
60
61
62
 
 
63
64
65
66
67
0
@@ -45,7 +45,7 @@ class UserTest < Test::Unit::TestCase
0
 
0
   def test_should_find_admin_by_id
0
     [:first, :hostess, :garden].each do |s|
0
- user = User.find_for_site(sites(s), users(:quentin).id)
0
+ user = User.find_by_site(sites(s), users(:quentin).id)
0
       assert_equal users(:quentin), user, "Unable to login to site: #{s}"
0
     end
0
   end
0
@@ -60,8 +60,8 @@ class UserTest < Test::Unit::TestCase
0
   end
0
   
0
   def test_should_find_member_by_site
0
- first_member = User.find_for_site(sites(:first), users(:arthur).id)
0
- hostess_member = User.find_for_site(sites(:hostess), users(:arthur).id)
0
+ first_member = User.find_by_site(sites(:first), users(:arthur).id)
0
+ hostess_member = User.find_by_site(sites(:hostess), users(:arthur).id)
0
     assert_equal users(:arthur), first_member
0
     assert_equal users(:arthur), hostess_member
0
     assert first_member.site_admin?

Comments

    No one has commented yet.