cantango-masquerade 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +39 -0
  4. data/Gemfile.lock +155 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.mdown +95 -0
  7. data/Rakefile +49 -0
  8. data/VERSION +1 -0
  9. data/cantango-masquerade.gemspec +107 -0
  10. data/lib/cantango/masquerade.rb +14 -0
  11. data/lib/cantango/masquerade/ability.rb +1 -0
  12. data/lib/cantango/masquerade/ability/helper/masquerade.rb +29 -0
  13. data/lib/cantango/masquerade/api.rb +7 -0
  14. data/lib/cantango/masquerade/api/masquerade.rb +9 -0
  15. data/lib/cantango/masquerade/api/masquerade/account.rb +33 -0
  16. data/lib/cantango/masquerade/api/masquerade/user.rb +31 -0
  17. data/lib/cantango/masquerade/macros.rb +7 -0
  18. data/lib/cantango/masquerade/macros/masquerader.rb +7 -0
  19. data/lib/cantango/masquerade/macros/masquerader/account.rb +7 -0
  20. data/lib/cantango/masquerade/macros/masquerader/user.rb +7 -0
  21. data/lib/cantango/masquerade_ext.rb +5 -0
  22. data/lib/cantango/masquerade_ext/macros.rb +11 -0
  23. data/lib/cantango/masquerade_ext/macros/account.rb +13 -0
  24. data/lib/cantango/masquerade_ext/macros/user.rb +13 -0
  25. data/spec/cantango/ability/masquerade/helper_spec.rb +92 -0
  26. data/spec/cantango/api/masquerade/account_spec.rb +62 -0
  27. data/spec/cantango/api/masquerade/user_spec.rb +60 -0
  28. data/spec/cantango/macros/masquerade/account_spec.rb +44 -0
  29. data/spec/cantango/macros/masquerade/user_spec.rb +37 -0
  30. data/spec/cantango/masquerade_ext/macros/admin_account_spec.rb +41 -0
  31. data/spec/cantango/masquerade_ext/macros/admin_user_spec.rb +38 -0
  32. data/spec/cantango/masquerade_spec.rb +7 -0
  33. data/spec/fixtures/models.rb +2 -0
  34. data/spec/fixtures/models/admin.rb +2 -0
  35. data/spec/fixtures/models/admin_account.rb +22 -0
  36. data/spec/fixtures/models/items.rb +8 -0
  37. data/spec/fixtures/models/permission.rb +12 -0
  38. data/spec/fixtures/models/project.rb +2 -0
  39. data/spec/fixtures/models/simple_roles.rb +49 -0
  40. data/spec/fixtures/models/user.rb +52 -0
  41. data/spec/fixtures/models/user_account.rb +7 -0
  42. data/spec/helpers/current_user_accounts.rb +20 -0
  43. data/spec/helpers/current_users.rb +10 -0
  44. data/spec/spec_helper.rb +15 -0
  45. metadata +193 -0
@@ -0,0 +1,14 @@
1
+ require 'sugar-high/array'
2
+ require 'sugar-high/blank'
3
+ require 'hashie'
4
+ require 'sweetloader'
5
+
6
+ SweetLoader.mode = :require
7
+
8
+ module CanTango
9
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade'} do
10
+ sweetload :Ability, :Api, :Macros
11
+ end
12
+ end
13
+
14
+ require 'cantango/masquerade_ext'
@@ -0,0 +1 @@
1
+ require 'cantango/masquerade/ability/helper/masquerade'
@@ -0,0 +1,29 @@
1
+ module CanTango::Ability
2
+ module Helper
3
+ module Masquerade
4
+ def masquerading?
5
+ candidate.respond_to?(:masquerading?) && candidate.masquerading?
6
+ end
7
+ alias_method :masquerade?, :masquerading?
8
+
9
+ def masquerade_user?
10
+ candidate.respond_to?(:active_user) && masquerading?
11
+ end
12
+
13
+ def masquerade_account?
14
+ candidate.respond_to?(:active_account) && masquerading?
15
+ end
16
+
17
+ def masquerading_off?
18
+ options[:masquerade] == false
19
+ end
20
+ alias_method :masquerade_off?, :masquerading_off?
21
+
22
+ def subject
23
+ return candidate.active_user if masquerade_user?
24
+ return candidate.active_account if masquerade_account?
25
+ candidate
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,7 @@
1
+ module CanTango
2
+ module Api
3
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade'} do
4
+ sweetload :Masquerade
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ module CanTango
2
+ module Api
3
+ module Masquerade
4
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade'} do
5
+ autoload_modules :Account, :User
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,33 @@
1
+ module CanTango
2
+ module Api
3
+ module Masquerade
4
+ module Account
5
+ attr_reader :masquerading, :active_account
6
+
7
+ def masquerade_as account
8
+ raise "Must be a registered type of account, was: #{account}" unless valid_account? account
9
+ @masquerading = true
10
+ @active_account = account
11
+ end
12
+
13
+ def stop_masquerade
14
+ @active_account, @masquerading = nil, nil
15
+ end
16
+
17
+ def masquerading?
18
+ !@masquerading.nil?
19
+ end
20
+
21
+ private
22
+
23
+ def valid_account? account
24
+ registered_accounts.registered_class?(account.class)
25
+ end
26
+
27
+ def registered_accounts
28
+ CanTango.config.accounts
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ module CanTango::Api::Masquerade
2
+ module User
3
+ attr_reader :masquerading, :active_user
4
+
5
+ def masquerade_as user
6
+ raise "Must be a registered type of user, was: #{user}" unless valid_user? user
7
+ @masquerading = true
8
+ user = user.kind_of?(String) ? ::User.find(user) : user
9
+ @active_user = user
10
+ end
11
+
12
+ def stop_masquerade
13
+ @active_user, @masquerading = nil, nil
14
+ end
15
+
16
+ def masquerading?
17
+ !@masquerading.nil?
18
+ end
19
+ alias_method :masquerade?, :masquerading?
20
+
21
+ private
22
+
23
+ def valid_user? user
24
+ registered_users.registered_class?(user.class)
25
+ end
26
+
27
+ def registered_users
28
+ CanTango.config.users
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ module CanTango
2
+ module Macros
3
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade'} do
4
+ autoload_modules :Masquerader
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module CanTango::Macros
2
+ module Masquerader
3
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade'} do
4
+ autoload_modules :Account, :User
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module CanTango::Macros::Masquerader
2
+ module Account
3
+ def masquerader
4
+ self.send :include, CanTango::Api::Masquerade::Account
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ module CanTango::Macros::Masquerader
2
+ module User
3
+ def masquerader
4
+ self.send :include, CanTango::Api::Masquerade::User
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ module CanTango
2
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade_ext'} do
3
+ sweetload :Macros
4
+ end
5
+ end
@@ -0,0 +1,11 @@
1
+ module CanTango
2
+ module Macros
3
+ sweet_scope :ns => {:CanTango => 'cantango/masquerade_ext'} do
4
+ sweetload :User, :Account
5
+ end
6
+
7
+ def self.extract list
8
+ list.inject({}){|res, v| res[v] = true } if options.kind_of? Array
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module CanTango::Macros
2
+ module Account
3
+ def tango_account options = {}
4
+ self.send :include, CanTango::Api::Account
5
+ options = CanTango::Macros.extract(options) if options.kind_of? Array
6
+
7
+ if defined? CanTango::Macros::Masquerader::Account
8
+ self.send :include, CanTango::Macros::Masquerader::Account
9
+ masquerader if options[:masquerade]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module CanTango::Macros
2
+ module User
3
+ def tango_user options = {}
4
+ self.send :include, CanTango::Api::User
5
+ options = CanTango::Macros.extract(options) if options.kind_of? Array
6
+
7
+ if defined? CanTango::Macros::Masquerader::User
8
+ self.send :include, CanTango::Macros::Masquerader::User
9
+ masquerader if options[:masquerade]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,92 @@
1
+ require 'cantango/config'
2
+ require 'fixtures/models'
3
+ require 'spec_helper'
4
+
5
+ class EditorUser
6
+ include CanTango::Api::Masquerade::User
7
+ end
8
+
9
+ class AdminUser
10
+ include CanTango::Api::Masquerade::User
11
+ end
12
+
13
+ CanTango.config.users do |u|
14
+ u.register :editor, EditorUser
15
+ u.register :admin, AdminUser
16
+ end
17
+
18
+ class MyAbility
19
+ include CanTango::Ability::Helper::Masquerade
20
+
21
+ attr_accessor :candidate, :options
22
+
23
+ def initialize candidate, options = {}
24
+ @candidate = candidate
25
+ @options = options
26
+ options[:masquerade] ||= candidate.masquerade?
27
+ end
28
+ end
29
+
30
+ describe CanTango::Ability::Helper::Masquerade do
31
+ before do
32
+ @editor = EditorUser.new
33
+ @admin = AdminUser.new 'admin'
34
+ @ability = MyAbility.new @admin
35
+ end
36
+
37
+ subject { @ability }
38
+
39
+ context 'NOT masquerading' do
40
+ before do
41
+ @admin.stop_masquerade
42
+ end
43
+
44
+ describe 'masquerading?' do
45
+ specify { subject.masquerading?.should be_false }
46
+ end
47
+
48
+ describe 'masquerade_user?' do
49
+ specify { subject.masquerade_user?.should be_false }
50
+ end
51
+
52
+ describe 'masquerade_account?' do
53
+ specify { subject.masquerade_account?.should be_false }
54
+ end
55
+
56
+ describe 'masquerading_off?' do
57
+ specify { subject.masquerade_off?.should be_true }
58
+ end
59
+
60
+ describe 'subject' do
61
+ specify { subject.send(:subject).should == @admin }
62
+ end
63
+ end
64
+
65
+ context 'IS masquerading' do
66
+ before do
67
+ @admin.masquerade_as @editor
68
+ end
69
+
70
+ specify { @admin.active_user.should == @editor }
71
+
72
+ describe 'masquerading?' do
73
+ specify { subject.masquerading?.should be_true }
74
+ end
75
+
76
+ describe 'masquerading_off?' do
77
+ specify { subject.masquerade_off?.should be_true }
78
+ end
79
+
80
+ describe 'masquerade_user?' do
81
+ specify { subject.masquerade_user?.should be_true }
82
+ end
83
+
84
+ describe 'masquerade_account?' do
85
+ specify { subject.masquerade_account?.should be_false }
86
+ end
87
+
88
+ describe 'subject' do
89
+ specify { subject.send(:subject).should == @editor }
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,62 @@
1
+ require 'cantango/config'
2
+ require 'fixtures/models'
3
+
4
+ require 'spec_helper'
5
+
6
+ class EditorAccount
7
+ include CanTango::Api::Masquerade::Account
8
+ end
9
+
10
+ class AdminAccount
11
+ include CanTango::Api::Masquerade::Account
12
+ end
13
+
14
+ CanTango.config.accounts do |ac|
15
+ ac.register :editor, EditorAccount
16
+ ac.register :admin, AdminAccount
17
+ end
18
+
19
+ describe CanTango::Api::Masquerade::Account do
20
+ before do
21
+ @user = User.new 'mike'
22
+ @editor = EditorAccount.new
23
+ @admin = AdminAccount.new @user
24
+ end
25
+
26
+ subject { @admin }
27
+
28
+ context 'is NOT masquerading' do
29
+ before do
30
+ subject.stop_masquerade
31
+ end
32
+
33
+ specify { subject.masquerading.should be_false }
34
+
35
+ describe 'masquerading?' do
36
+ specify { subject.masquerading?.should be_false }
37
+ end
38
+ end
39
+
40
+ context 'IS masquerading' do
41
+ before do
42
+ subject.masquerade_as @editor
43
+ end
44
+
45
+ specify { subject.masquerading.should be_true }
46
+
47
+ describe 'masquerade_as account' do
48
+ specify { subject.active_account.should == @editor }
49
+ end
50
+
51
+ describe 'stop_masquerade' do
52
+ specify do
53
+ subject.stop_masquerade
54
+ subject.masquerading.should be_false
55
+ end
56
+ end
57
+
58
+ describe 'masquerading?' do
59
+ specify { subject.masquerading?.should be_true }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,60 @@
1
+ require 'cantango/config'
2
+ require 'fixtures/models'
3
+ require 'spec_helper'
4
+
5
+ class EditorUser
6
+ include CanTango::Api::Masquerade::User
7
+ end
8
+
9
+ class AdminUser
10
+ include CanTango::Api::Masquerade::User
11
+ end
12
+
13
+ CanTango.config.users do |u|
14
+ u.register :editor, EditorUser
15
+ u.register :admin, AdminUser
16
+ end
17
+
18
+ describe CanTango::Api::Masquerade::User do
19
+ before do
20
+ @editor = EditorUser.new
21
+ @admin = AdminUser.new 'admin'
22
+ end
23
+
24
+ subject { @admin }
25
+
26
+ context 'is NOT masquerading' do
27
+ before do
28
+ subject.stop_masquerade
29
+ end
30
+
31
+ specify { subject.masquerading.should be_false }
32
+
33
+ describe 'masquerading?' do
34
+ specify { subject.masquerading?.should be_false }
35
+ end
36
+ end
37
+
38
+ context 'IS masquerading' do
39
+ before do
40
+ subject.masquerade_as @editor
41
+ end
42
+
43
+ specify { subject.masquerading.should be_true }
44
+
45
+ describe 'masquerade_as user' do
46
+ specify { subject.active_user.should == @editor }
47
+ end
48
+
49
+ describe 'stop_masquerade' do
50
+ specify do
51
+ subject.stop_masquerade
52
+ subject.masquerading.should be_false
53
+ end
54
+ end
55
+
56
+ describe 'masquerading?' do
57
+ specify { subject.masquerading?.should be_true }
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,44 @@
1
+ require 'cantango/config'
2
+ require 'fixtures/models'
3
+
4
+ require 'spec_helper'
5
+
6
+ class EditorAccount
7
+ extend CanTango::Macros::Masquerader::Account
8
+ end
9
+
10
+ class AdminAccount
11
+ extend CanTango::Macros::Masquerader::Account
12
+ end
13
+
14
+ CanTango.config.accounts do |ac|
15
+ ac.register :editor, EditorAccount
16
+ ac.register :admin, AdminAccount
17
+ end
18
+
19
+ describe CanTango::Macros::Masquerader::Account do
20
+ before do
21
+ @user = User.new 'mike', 'mike@mail.com'
22
+ @admin_ac = AdminAccount.new @user
23
+ @editor_ac = EditorAccount.new @user
24
+ end
25
+
26
+ describe 'masquerader' do
27
+ before do
28
+ AdminAccount.masquerader
29
+ end
30
+
31
+ context 'admin NOT masquerading' do
32
+ specify { @admin_ac.active_account.should == nil }
33
+ specify { @admin_ac.masquerading?.should be_false }
34
+ end
35
+
36
+ context 'admin masquerading as editor account' do
37
+ before do
38
+ @admin_ac.masquerade_as @editor_ac
39
+ end
40
+ specify { @admin_ac.masquerading?.should be_true }
41
+ specify { @admin_ac.active_account.should == @editor_ac }
42
+ end
43
+ end
44
+ end