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,37 @@
1
+ require 'cantango/config'
2
+ require 'fixtures/models'
3
+
4
+ require 'spec_helper'
5
+
6
+ class User
7
+ extend CanTango::Macros::Masquerader::User
8
+ end
9
+
10
+ class Admin
11
+ extend CanTango::Macros::Masquerader::User
12
+ end
13
+
14
+ CanTango.config.users do |u|
15
+ u.register :user, User
16
+ u.register :admin, Admin
17
+ end
18
+
19
+ describe CanTango::Macros::Masquerader::User do
20
+ before do
21
+ @mike = User.new 'mike', 'mike@mail.com'
22
+ @admin = Admin.new 'admin', 'admin@mail.com'
23
+ end
24
+
25
+ describe 'masquerader' do
26
+ before do
27
+ Admin.masquerader
28
+ end
29
+
30
+ context 'admin masquerading as mike' do
31
+ before do
32
+ @admin.masquerade_as @mike
33
+ end
34
+ specify { @admin.active_user.should == @mike }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,41 @@
1
+ require 'active_record/spec_helper'
2
+ require 'active_record/scenarios/user_accounts/helpers'
3
+
4
+ require_all File.dirname(__FILE__) + "/../shared/"
5
+
6
+ describe 'Scenario: masquerading for admin account' do
7
+ include UserFactory
8
+
9
+ let(:user_profile) { @u ||= User.create(:name => 'kris', :role => 'user') }
10
+ let(:admin_account) { create_user_account user_profile, :admin }
11
+
12
+ context "Masquerading as user" do
13
+ before(:each) do
14
+ admin_account.masquerade_as user_profile
15
+ end
16
+
17
+ it_should_behave_like "User role" do
18
+ let(:current_admin) { admin_account }
19
+ let(:user) { current_user_ability(:admin) }
20
+ let(:own_article) { Article.create!(:user_id => user_profile.id) }
21
+ let(:own_post) { Post.create(:user_id => user_profile.id) }
22
+ let(:own_comment) { Comment.create(:user_id => user_profile.id) }
23
+ end
24
+ end
25
+
26
+ context "Masquerading as editor" do
27
+ before(:each) do
28
+ @user = User.create!(:role => "editor")
29
+ @admin = Admin.create!(:name => "kris")
30
+ @admin.masquerade_as @user
31
+ end
32
+
33
+ it_should_behave_like "Editor role" do
34
+ let(:current_user) { @admin }
35
+ let(:user) { current_user_ability(:user) }
36
+ let(:own_article) { Article.create!(:user_id => @user.id) }
37
+ let(:own_post) { Post.create(:user_id => @user.id) }
38
+ let(:own_comment) { Comment.create(:user_id => @user.id) }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ require 'active_record/spec_helper'
2
+
3
+ require_all File.dirname(__FILE__) + "/../shared/"
4
+
5
+ describe 'Scenario: masquerading for admin user' do
6
+
7
+ context "Masquerading as user" do
8
+ before(:each) do
9
+ @user = User.create!(:role => "user")
10
+ @admin = Admin.create!(:name => "kris")
11
+ @admin.masquerade_as @user
12
+ end
13
+
14
+ it_should_behave_like "User role" do
15
+ let(:current_user) { @admin }
16
+ let(:user) { current_user_ability(:user) }
17
+ let(:own_article) { Article.create!(:user_id => @user.id) }
18
+ let(:own_post) { Post.create(:user_id => @user.id) }
19
+ let(:own_comment) { Comment.create(:user_id => @user.id) }
20
+ end
21
+ end
22
+
23
+ context "Masquerading as editor" do
24
+ before(:each) do
25
+ @user = User.create!(:role => "editor")
26
+ @admin = Admin.create!(:name => "kris")
27
+ @admin.masquerade_as @user
28
+ end
29
+
30
+ it_should_behave_like "Editor role" do
31
+ let(:current_user) { @admin }
32
+ let(:user) { current_user_ability(:user) }
33
+ let(:own_article) { Article.create!(:user_id => @user.id) }
34
+ let(:own_post) { Post.create(:user_id => @user.id) }
35
+ let(:own_comment) { Comment.create(:user_id => @user.id) }
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Cantango::Masquerade' do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ require 'require_all'
2
+ require_all File.dirname(__FILE__) + '/models'
@@ -0,0 +1,2 @@
1
+ class Admin < User
2
+ end
@@ -0,0 +1,22 @@
1
+ class AdminAccount
2
+ attr_accessor :user, :roles, :role_groups
3
+
4
+ def initialize user, options = {}
5
+ @user = user
6
+ @roles = options[:roles]
7
+ @role_groups = options[:role_groups]
8
+ end
9
+
10
+ def has_role? name
11
+ true
12
+ end
13
+
14
+ def roles_list
15
+ roles
16
+ end
17
+
18
+ def role_groups_list
19
+ role_groups
20
+ end
21
+ end
22
+
@@ -0,0 +1,8 @@
1
+ class Post
2
+ end
3
+
4
+ class Article
5
+ end
6
+
7
+ class Comment
8
+ end
@@ -0,0 +1,12 @@
1
+ class Permission
2
+ attr_accessor :thing, :thing_type, :action, :user
3
+
4
+ def initialize user, action, thing
5
+ @user, @action, @thing = [user, action, thing]
6
+ @thing_type = @thing.class.to_s
7
+ end
8
+
9
+ def thing_id
10
+ thing.id
11
+ end
12
+ end
@@ -0,0 +1,2 @@
1
+ class Project #< ActiveRecord::Base
2
+ end
@@ -0,0 +1,49 @@
1
+ module SimpleRoles
2
+ def self.included(base)
3
+ base.send :include, InstanceMethods
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def is_role_in_group?(role, group)
9
+ raise "No group #{group} defined in User model" if !role_groups.has_key?(group)
10
+ role_groups[group].include?(role)
11
+ end
12
+
13
+ def role_groups
14
+ {:bloggers => [:editor]}
15
+ end
16
+
17
+ def roles
18
+ [:guest, :user, :admin, :editor]
19
+ end
20
+ end
21
+
22
+ module InstanceMethods
23
+
24
+ attr_accessor :role_groups_list
25
+
26
+ def has_role? role
27
+ roles_list.include? role
28
+ end
29
+
30
+ def has_any_role? roles
31
+ roles.include?(role.to_sym)
32
+ end
33
+
34
+ def roles_list
35
+ role.to_s.scan(/\w+/).map{|r| r.to_sym}
36
+ end
37
+
38
+ def is_in_group? group
39
+ role_groups_list.include? group
40
+ end
41
+ alias_method :in_role_group?, :is_in_group?
42
+
43
+ def role_groups_list
44
+ return role_groups.scan(/\w+/).map(&:to_sym) if respond_to?(:role_groups) && !role_groups.nil?
45
+ @role_groups_list || [] #[:bloggers]
46
+ end
47
+ end
48
+ end
49
+
@@ -0,0 +1,52 @@
1
+ require 'fixtures/models/simple_roles'
2
+
3
+ class User
4
+ attr_accessor :name, :account, :email
5
+
6
+ # tango_user # see macros
7
+
8
+ include ::SimpleRoles
9
+
10
+ def initialize name, email = nil, options = {}
11
+ @name = name
12
+ @email = email
13
+ set_option_vars options
14
+ end
15
+
16
+ def set_option_vars options = {}
17
+ options.each_pair do |name, value|
18
+ var = :"@#{name}"
19
+ self.instance_variable_set(var, value)
20
+ end
21
+ end
22
+
23
+ def email
24
+ @email ||= 'default@gmail.com'
25
+ end
26
+
27
+ def role
28
+ @role || ''
29
+ end
30
+
31
+ # This hash should be recalculated anytime the permissions collection changes
32
+ #
33
+
34
+ # after_update :recalculate_permissions_hash
35
+
36
+ def permissions_hash
37
+ @permissions_hash = permissions.hash
38
+ end
39
+
40
+ def permissions
41
+ @permissions ||= []
42
+ end
43
+
44
+ # allows implementation specific to ORM, fx using #all on some datastores such as Mongoid etc.
45
+ alias_method :all_permissions, :permissions
46
+
47
+ protected
48
+
49
+ def recalculate_permissions_hash
50
+ @permissions_hash = nil if self.permissions_changed?
51
+ end
52
+ end
@@ -0,0 +1,7 @@
1
+ class UserAccount
2
+ attr_accessor :user
3
+
4
+ def initialize user, options = {}
5
+ @user = user
6
+ end
7
+ end
@@ -0,0 +1,20 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'current_users'
4
+
5
+ module CurrentUserAccounts
6
+ include ::CurrentUsers
7
+
8
+ def self.included(base)
9
+ base.extend ::CurrentUsers
10
+ end
11
+
12
+ def current_user_account
13
+ ::UserAccount.new(current_user, :roles => [:user])
14
+ end
15
+
16
+ def current_admin_account
17
+ ::AdminAccount.new(current_admin, :roles => [:admin])
18
+ end
19
+ end
20
+
@@ -0,0 +1,10 @@
1
+ module CurrentUsers
2
+ def current_user
3
+ @current_user ||= ::User.new 'stan', 'stan@mail.ru', :role => 'user'
4
+ end
5
+
6
+ def current_admin
7
+ @current_admin ||= ::User.new 'admin', 'admin@mail.ru', :role => 'admin'
8
+ end
9
+ end
10
+
@@ -0,0 +1,15 @@
1
+ require 'rspec'
2
+
3
+ require 'cantango/core'
4
+ require 'cantango/configuration'
5
+ # require 'cantango/api'
6
+
7
+ require 'cantango/masquerade'
8
+
9
+ # Requires supporting files with custom matchers and macros, etc,
10
+ # in ./support/ and its subdirectories.
11
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
12
+
13
+ RSpec.configure do |config|
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,193 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cantango-masquerade
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kristian Mandrup
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70205428633780 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.1'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70205428633780
25
+ - !ruby/object:Gem::Dependency
26
+ name: sugar-high
27
+ requirement: &70205428632360 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.6.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70205428632360
36
+ - !ruby/object:Gem::Dependency
37
+ name: sweetloader
38
+ requirement: &70205428630840 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: 0.1.6
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70205428630840
47
+ - !ruby/object:Gem::Dependency
48
+ name: hashie
49
+ requirement: &70205428628440 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.2'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70205428628440
58
+ - !ruby/object:Gem::Dependency
59
+ name: cantango-core
60
+ requirement: &70205428627420 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: 0.1.9.1
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70205428627420
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: &70205428626420 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: 1.1.rc
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70205428626420
80
+ - !ruby/object:Gem::Dependency
81
+ name: jeweler
82
+ requirement: &70205428625380 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: 1.6.4
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *70205428625380
91
+ - !ruby/object:Gem::Dependency
92
+ name: rcov
93
+ requirement: &70205428623420 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70205428623420
102
+ - !ruby/object:Gem::Dependency
103
+ name: rspec
104
+ requirement: &70205428622440 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: 2.6.0
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: *70205428622440
113
+ description: Masquerade extensions and macros for CanTango
114
+ email: kmandrup@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files:
118
+ - LICENSE.txt
119
+ - README.mdown
120
+ files:
121
+ - .document
122
+ - .rspec
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.mdown
127
+ - Rakefile
128
+ - VERSION
129
+ - cantango-masquerade.gemspec
130
+ - lib/cantango/masquerade.rb
131
+ - lib/cantango/masquerade/ability.rb
132
+ - lib/cantango/masquerade/ability/helper/masquerade.rb
133
+ - lib/cantango/masquerade/api.rb
134
+ - lib/cantango/masquerade/api/masquerade.rb
135
+ - lib/cantango/masquerade/api/masquerade/account.rb
136
+ - lib/cantango/masquerade/api/masquerade/user.rb
137
+ - lib/cantango/masquerade/macros.rb
138
+ - lib/cantango/masquerade/macros/masquerader.rb
139
+ - lib/cantango/masquerade/macros/masquerader/account.rb
140
+ - lib/cantango/masquerade/macros/masquerader/user.rb
141
+ - lib/cantango/masquerade_ext.rb
142
+ - lib/cantango/masquerade_ext/macros.rb
143
+ - lib/cantango/masquerade_ext/macros/account.rb
144
+ - lib/cantango/masquerade_ext/macros/user.rb
145
+ - spec/cantango/ability/masquerade/helper_spec.rb
146
+ - spec/cantango/api/masquerade/account_spec.rb
147
+ - spec/cantango/api/masquerade/user_spec.rb
148
+ - spec/cantango/macros/masquerade/account_spec.rb
149
+ - spec/cantango/macros/masquerade/user_spec.rb
150
+ - spec/cantango/masquerade_ext/macros/admin_account_spec.rb
151
+ - spec/cantango/masquerade_ext/macros/admin_user_spec.rb
152
+ - spec/cantango/masquerade_spec.rb
153
+ - spec/fixtures/models.rb
154
+ - spec/fixtures/models/admin.rb
155
+ - spec/fixtures/models/admin_account.rb
156
+ - spec/fixtures/models/items.rb
157
+ - spec/fixtures/models/permission.rb
158
+ - spec/fixtures/models/project.rb
159
+ - spec/fixtures/models/simple_roles.rb
160
+ - spec/fixtures/models/user.rb
161
+ - spec/fixtures/models/user_account.rb
162
+ - spec/helpers/current_user_accounts.rb
163
+ - spec/helpers/current_users.rb
164
+ - spec/spec_helper.rb
165
+ homepage: http://github.com/kristianmandrup/cantango-masquerade
166
+ licenses:
167
+ - MIT
168
+ post_install_message:
169
+ rdoc_options: []
170
+ require_paths:
171
+ - lib
172
+ required_ruby_version: !ruby/object:Gem::Requirement
173
+ none: false
174
+ requirements:
175
+ - - ! '>='
176
+ - !ruby/object:Gem::Version
177
+ version: '0'
178
+ segments:
179
+ - 0
180
+ hash: 725092924563287622
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ none: false
183
+ requirements:
184
+ - - ! '>='
185
+ - !ruby/object:Gem::Version
186
+ version: '0'
187
+ requirements: []
188
+ rubyforge_project:
189
+ rubygems_version: 1.8.6
190
+ signing_key:
191
+ specification_version: 3
192
+ summary: Masquerade extension for CanTango
193
+ test_files: []