adva_rbac 0.0.1

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.
Files changed (50) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/NOTES +98 -0
  6. data/README.md +29 -0
  7. data/Rakefile +2 -0
  8. data/adva_rbac.gemspec +17 -0
  9. data/app/controllers/roles_controller.rb +28 -0
  10. data/app/helpers/roles_helper.rb +64 -0
  11. data/app/views/admin/sections/settings/_permissions.html.erb +16 -0
  12. data/app/views/roles/index.js.erb +3 -0
  13. data/config/initializers/base_controller.rb +4 -0
  14. data/config/initializers/rbac.rb +60 -0
  15. data/config/initializers/user.rb +80 -0
  16. data/db/migrate/20080402000006_create_role_tables.rb +13 -0
  17. data/db/migrate/20090720132900_migrate_roles_table_to_new_rbac.rb +15 -0
  18. data/lib/action_controller/guards_permissions.rb +77 -0
  19. data/lib/adva_rbac.rb +18 -0
  20. data/lib/adva_rbac/version.rb +3 -0
  21. data/lib/permission_map.rb +70 -0
  22. data/lib/rbac.rb +26 -0
  23. data/lib/rbac/acts_as_role_context.rb +44 -0
  24. data/lib/rbac/acts_as_role_subject.rb +65 -0
  25. data/lib/rbac/context.rb +85 -0
  26. data/lib/rbac/role.rb +10 -0
  27. data/lib/rbac/role_type.rb +73 -0
  28. data/lib/rbac/role_type/active_record.rb +47 -0
  29. data/lib/rbac/role_type/static.rb +144 -0
  30. data/lib/rbac/subject.rb +52 -0
  31. data/test/functional/roles_controller_test.rb +21 -0
  32. data/test/integration/user_rbac_test.rb +34 -0
  33. data/test/rbac/all.rb +3 -0
  34. data/test/rbac/database.rb +155 -0
  35. data/test/rbac/database.yml +3 -0
  36. data/test/rbac/implementation/active_record_test.rb +17 -0
  37. data/test/rbac/implementation/static_test.rb +14 -0
  38. data/test/rbac/static.rb +25 -0
  39. data/test/rbac/test_helper.rb +62 -0
  40. data/test/rbac/tests/acts_as_role_context.rb +37 -0
  41. data/test/rbac/tests/context.rb +35 -0
  42. data/test/rbac/tests/group.rb +40 -0
  43. data/test/rbac/tests/has_role.rb +126 -0
  44. data/test/rbac/tests/role_type.rb +110 -0
  45. data/test/test_helper.rb +1 -0
  46. data/test/unit/helpers/roles_helper_test.rb +69 -0
  47. data/test/unit/models/rbac_context_test.rb +37 -0
  48. data/test/unit/models/rbac_user_test.rb +100 -0
  49. data/test/unit/models/role_test.rb +185 -0
  50. metadata +110 -0
@@ -0,0 +1 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../../adva_cms/test/test_helper')
@@ -0,0 +1,69 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../test_helper")
2
+
3
+ class RolesHelperTest < ActionView::TestCase
4
+ include RolesHelper
5
+
6
+ def setup
7
+ super
8
+ @article = Article.first
9
+
10
+ @superuser_role = Role.new(:name => 'superuser')
11
+ @admin_role = Role.new(:name => 'admin', :context => Site.first)
12
+ @moderator_role = Role.new(:name => 'moderator', :context => Section.first)
13
+ @author_role = Role.new(:name => 'author', :context => @article)
14
+ @user_role = Role.new(:name => 'user')
15
+ @anonymous_role = Role.new(:name => 'anonymous')
16
+ end
17
+
18
+ # role_to_default_css_class
19
+ test "#role_to_default_css_class returns the role's name if no context is given" do
20
+ role_to_default_css_class(@user_role).should == 'user'
21
+ end
22
+
23
+ test "#role_to_default_css_class returns the role's context and name if context is given" do
24
+ role_to_default_css_class(@moderator_role).should =~ /section-[\d]+-moderator/
25
+ end
26
+
27
+ # role_to_css_class
28
+ test "#role_to_css_class returns 'anonymous' for an anonymous role" do
29
+ role_to_css_class(@anonymous_role).should == 'anonymous'
30
+ end
31
+
32
+ test "#role_to_css_class returns 'user' for a user role" do
33
+ role_to_css_class(@user_role).should == 'user'
34
+ end
35
+
36
+ test "#role_to_css_class returns 'user-1 content-1-author' for an author role when the author is a user" do
37
+ role_to_css_class(@author_role).should =~ /user-[\d]+ content-[\d]+-author/
38
+ end
39
+
40
+ test "#role_to_css_class returns 'anonymous-1 content-1-author' for an author role when the author is an anonymous" do
41
+ @article.author_type = 'Anonymous'
42
+ role_to_css_class(@author_role).should =~ /anonymous-[\d]+ content-[\d]+-author/
43
+ end
44
+
45
+ test "#role_to_css_class returns 'section-1-moderator' for a user role" do
46
+ role_to_css_class(@moderator_role).should =~ /section-[\d]+-moderator/
47
+ end
48
+
49
+ test "#role_to_css_class returns 'site-1-admin' for a admin role" do
50
+ role_to_css_class(@admin_role).should =~ /site-[\d]+-admin/
51
+ end
52
+
53
+ test "#role_to_css_class returns 'superuser' for a superuser role" do
54
+ role_to_css_class(@superuser_role).should == 'superuser'
55
+ end
56
+
57
+ # authorizing_css_classes
58
+ test "#quoted_role_names turns the given roles to css classes that allow a user to see an element" do
59
+ quoted_role_names([@superuser_role]).should == 'superuser'
60
+ end
61
+
62
+ test "#quoted_role_names given the option :quote it encloses the classes in single quotes" do
63
+ quoted_role_names([@superuser_role], {:quote => true}).should == "'superuser'"
64
+ end
65
+
66
+ test "#quoted_role_names given the option :separator it joins the classes using it" do
67
+ quoted_role_names([@superuser_role, @superuser_role], {:separator => ','}).should == "superuser,superuser"
68
+ end
69
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../test_helper")
2
+
3
+ class RbacContextTest < ActiveSupport::TestCase
4
+ def setup
5
+ super
6
+ @account = Account.find_by_name('an account')
7
+ @site = Site.find_by_name('site with pages')
8
+ end
9
+
10
+ define_method "test: roles have a reference to an ancestor context" do
11
+ superuser = Rbac::Role.find_by_name('superuser')
12
+ admin = Rbac::Role.find_by_name('admin')
13
+ @moderator = User.find_by_first_name('a moderator')
14
+ moderator = Rbac::Role.find_by_user_id(@moderator.id)
15
+
16
+ assert_equal nil, superuser.ancestor_context
17
+ assert_equal nil, admin.ancestor_context
18
+ assert_equal @site, moderator.ancestor_context
19
+ end
20
+
21
+ define_method "test: an account has members" do
22
+ assert @account.members.empty?
23
+ assert !@site.members.empty?
24
+ end
25
+
26
+ # define_method "test: any user is a user of an account and a site" do
27
+ # user = User.find_by_name('a user')
28
+ #
29
+ # assert @site.users.include?(user)
30
+ # assert @account.users.include?(user)
31
+ # end
32
+ #
33
+ # define_method "test: an account has members" do
34
+ # @account.resources << @site
35
+ # assert !@account.members.empty?
36
+ # end
37
+ end
@@ -0,0 +1,100 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../test_helper")
2
+
3
+ class RbacUserTest < ActiveSupport::TestCase
4
+ def setup
5
+ super
6
+ @user = User.find_by_first_name('a user')
7
+ @role_attributes = [
8
+ { "name" => "superuser", "selected" => "1" },
9
+ { "name" => "admin", "context_id" => Site.first.id, "context_type" => "Site", "selected" => "1" }
10
+ ]
11
+ end
12
+
13
+ # the roles association
14
+ # FIXME implement ...
15
+
16
+ # stub_scenario :user_having_several_roles
17
+ # test 'roles.by_site returns all superuser, site and section roles for the given user' do
18
+ # roles = @user.roles.by_site(@site)
19
+ # roles.map(&:type).should == ['Rbac::Role::Superuser', 'Rbac::Role::Admin', 'Rbac::Role::Moderator']
20
+ # end
21
+ #
22
+ # test 'roles.by_context returns all roles by_site for the given object' do
23
+ # roles = @user.roles.by_context(@site)
24
+ # roles.map(&:type).should == ['Rbac::Role::Superuser', 'Rbac::Role::Admin', 'Rbac::Role::Moderator']
25
+ # end
26
+ #
27
+ # test 'roles.by_context adds the implicit roles for the given object if it has any' do
28
+ # @topic.stub!(:implicit_roles).and_return [@comment_author_role]
29
+ # roles = @user.roles.by_context(@topic)
30
+ # roles.map(&:type).should == ['Rbac::Role::Superuser', 'Rbac::Role::Admin', 'Rbac::Role::Moderator', 'Rbac::Role::Author']
31
+ # end
32
+ #
33
+
34
+ test 'makes the new user a superuser' do
35
+ user = User.create_superuser(@valid_user_params)
36
+ user.has_role?(:superuser).should be_true
37
+ end
38
+
39
+ test "User.by_role_and_context finds all superusers" do
40
+ users = User.by_role_and_context(:superuser, nil)
41
+ users.size.should == 1
42
+ end
43
+
44
+ test "User.by_role_and_context finds all admins of a given site" do
45
+ site = Site.find_by_name('site with blog')
46
+ users = User.by_role_and_context(:admin, site)
47
+ users.size.should == 1
48
+ end
49
+
50
+ test "role_matches_attributes?" do
51
+ role = Role.new(:name => 'superuser')
52
+
53
+ attributes = { :name => 'superuser' }
54
+ assert_equal true, User.role_matches_attributes?(attributes, role)
55
+
56
+ attributes = { :name => 'god' }
57
+ assert_equal false, User.role_matches_attributes?(attributes, role)
58
+
59
+ role = Role.new(:name => 'admin', :context_type => 'Site', :context_id => '1')
60
+ attributes = { :name => 'admin', :context_type => 'Site', :context_id => '1' }
61
+ assert_equal true, User.role_matches_attributes?(attributes, role)
62
+
63
+ attributes = { :name => 'admin', :context_type => 'Site', :context_id => '2' }
64
+ assert_equal false, User.role_matches_attributes?(attributes, role)
65
+ end
66
+
67
+ test "selected roles" do
68
+ fields = ['name', 'context_type', 'context_id']
69
+ expected = [Role.new(@role_attributes[0].slice(*fields)), Role.new(@role_attributes[1].slice(*fields))]
70
+ assert_equal expected.map { |r| r.attributes.slice(*fields) }, @user.selected_roles(@role_attributes).map { |r| r.attributes.slice(*fields) }
71
+
72
+ @user.roles.build(@role_attributes[0].slice(*fields))
73
+ expected = [Role.new(@role_attributes[1].slice(*fields))]
74
+ assert_equal expected.map { |r| r.attributes.slice(*fields) }, @user.selected_roles(@role_attributes).map { |r| r.attributes.slice(*fields) }
75
+ end
76
+
77
+ test "unselected_roles" do
78
+ fields = ['name', 'context_type', 'context_id']
79
+ @role_attributes[0]['selected'] = '0'
80
+ @user.roles.build(@role_attributes[0].slice(*fields))
81
+ expected = [Role.new(@role_attributes[0].slice(*fields))]
82
+ assert_equal expected.map { |r| r.attributes.slice(*fields) }, @user.unselected_roles(@role_attributes).map { |r| r.attributes.slice(*fields) }
83
+ end
84
+
85
+ test "creates new roles from the given attributes" do
86
+ @user.roles.should be_empty
87
+
88
+ @user.roles_attributes = @role_attributes
89
+ @user.roles(true).size.should == 2
90
+ end
91
+
92
+ test "ignores parameters that do not have the :selected flag set" do
93
+ @user.roles.should be_empty
94
+
95
+ @role_attributes[0]['selected'] = '0'
96
+ @user.roles_attributes = @role_attributes
97
+
98
+ @user.roles(true).size.should == 1
99
+ end
100
+ end
@@ -0,0 +1,185 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../../test_helper")
2
+
3
+ class RoleTest < ActiveSupport::TestCase
4
+ def setup
5
+ super
6
+
7
+ @superuser = User.find_by_first_name('a superuser')
8
+ @admin = User.find_by_first_name('an admin')
9
+ @moderator = User.find_by_first_name('a moderator')
10
+ @another_moderator = User.find_by_first_name('another moderator')
11
+ @another_author = User.find_by_first_name('a author')
12
+ @user = User.find_by_first_name('a user')
13
+ @anonymous = User.anonymous
14
+ @designer = User.find_by_first_name('a designer')
15
+
16
+ @section = @moderator.roles.detect { |r| r.context.is_a?(Section) }.context
17
+ @site = @section.site
18
+ @content = @section.articles.first
19
+ @author = @content.author
20
+
21
+ @another_site = Site.find_by_name 'another site'
22
+ end
23
+
24
+ test "a superuser has the global role :superuser" do
25
+ @superuser.has_global_role?(:superuser).should be_true
26
+ @superuser.has_global_role?(:superuser, @site).should be_true
27
+ @superuser.has_global_role?(:superuser, @another_site).should be_true
28
+ end
29
+
30
+ test "a admin for 'site' has the global role :admin on 'site'" do
31
+ @admin.has_global_role?(:admin, @site).should be_true
32
+ end
33
+
34
+ test "a moderator for 'site' has the global role :moderator on 'site'" do
35
+ @another_moderator.has_global_role?(:moderator, @site).should be_true
36
+ end
37
+
38
+ test "a author for 'site' has the global role :author on 'site'" do
39
+ @another_author.has_global_role?(:author, @site).should be_true
40
+ end
41
+
42
+ test "a designer for 'site' has the global role :designer on 'site'" do
43
+ @designer.has_global_role?(:designer, @site).should be_true
44
+ end
45
+
46
+ test "a admin for 'site' does not have the global role :admin on 'another site'" do
47
+ @admin.has_global_role?(:admin, @another_site).should be_false
48
+ end
49
+
50
+ test "a moderator for a page of 'site' does not have the global role :moderator on 'site'" do
51
+ @moderator.has_global_role?(:moderator, @site).should be_false
52
+ end
53
+
54
+ test "a superuser has permissions for the admin areas of all sites" do
55
+ @superuser.has_permission_for_admin_area?(@site).should be_true
56
+ @superuser.has_permission_for_admin_area?(@another_site).should be_true
57
+ end
58
+
59
+ test "a admin, moderator, author and designer for 'site' have permission for the admin area of 'site'" do
60
+ @admin.has_permission_for_admin_area?(@site).should be_true
61
+ @another_moderator.has_permission_for_admin_area?(@site).should be_true
62
+ @another_author.has_permission_for_admin_area?(@site).should be_true
63
+ @designer.has_permission_for_admin_area?(@site).should be_true
64
+ end
65
+
66
+ test "a admin, moderator, author and designer for 'site' do not have permissions for the admin area of 'another_site'" do
67
+ @admin.has_permission_for_admin_area?(@another_site).should be_false
68
+ @another_moderator.has_permission_for_admin_area?(@another_site).should be_false
69
+ @author.has_permission_for_admin_area?(@another_site).should be_false
70
+ @designer.has_permission_for_admin_area?(@another_site).should be_false
71
+ end
72
+
73
+ test "a moderator for a page of a 'site' does not have permission for the admin area of 'site'" do
74
+ @moderator.has_permission_for_admin_area?(@site).should be_false
75
+ end
76
+
77
+ # has_role? (with a user)
78
+ test "a user has the role :user" do
79
+ @user.has_role?(:user).should be_true
80
+ end
81
+
82
+ test "a user does not have the role :moderator" do
83
+ @user.has_role?(:moderator, @section).should be_false
84
+ end
85
+
86
+ test "a user does not have the role :admin" do
87
+ @user.has_role?(:admin, @site).should be_false
88
+ end
89
+
90
+ test "a user does not have the role :superuser" do
91
+ @user.has_role?(:superuser).should be_false
92
+ end
93
+
94
+ # has_role? (with a content author)
95
+ test "a content author has the role :user" do
96
+ @author.has_role?(:user).should be_true
97
+ end
98
+
99
+ test 'a content author has the role :author for that content' do
100
+ @author.has_role?(:author, @content).should be_true
101
+ end
102
+
103
+ test "a content author does not have the role :moderator" do
104
+ @author.has_role?(:moderator, @section).should be_false
105
+ end
106
+
107
+ test "a content author does not have the role :admin" do
108
+ @author.has_role?(:admin, @site).should be_false
109
+ end
110
+
111
+ test "a content author does not have the role :superuser" do
112
+ @author.has_role?(:superuser).should be_false
113
+ end
114
+
115
+ # has_role? (with a section moderator)
116
+ test "a section moderator has the role :user" do
117
+ @moderator.has_role?(:user).should be_true
118
+ end
119
+
120
+ test "a section moderator has the role :author for another user's content" do
121
+ @moderator.has_role?(:author, @content).should be_true
122
+ end
123
+
124
+ test "a section moderator has the role :moderator for that section" do
125
+ @moderator.has_role?(:moderator, @section).should be_true
126
+ end
127
+
128
+ test "a section moderator does not have the role :admin" do
129
+ @moderator.has_role?(:admin, @site).should be_false
130
+ end
131
+
132
+ test "a section moderator does not have the role :superuser" do
133
+ @moderator.has_role?(:superuser).should be_false
134
+ end
135
+
136
+ # has_role? (with a site admin)
137
+ test "a site admin has the role :user" do
138
+ @admin.has_role?(:user).should be_true
139
+ end
140
+
141
+ test "a site admin has the role :author for another user's content" do
142
+ @admin.has_role?(:author, @content).should be_true
143
+ end
144
+
145
+ test "a site admin has the role :moderator for sections belonging to that site" do
146
+ @admin.has_role?(:moderator, @section).should be_true
147
+ end
148
+
149
+ test "a site admin has the role :admin for that site" do
150
+ @admin.has_role?(:admin, @site).should be_true
151
+ end
152
+
153
+ test "a site admin does not have role :admin for another site" do
154
+ @admin.has_role?(:admin, @another_site).should be_false
155
+ end
156
+
157
+ test "a site admin does not have role :admin for a non-existent site" do
158
+ @admin.has_role?(:admin, nil).should be_false
159
+ end
160
+
161
+ test "a site admin does not have the role :superuser" do
162
+ @admin.has_role?(:superuser).should be_false
163
+ end
164
+
165
+ # has_role? (with a superuser)
166
+ test "a superuser has the role :user" do
167
+ @superuser.has_role?(:user).should be_true
168
+ end
169
+
170
+ test "a superuser has the role :author for another user's content" do
171
+ @superuser.has_role?(:author, @content).should be_true
172
+ end
173
+
174
+ test "a superuser has the role :moderator for sections belonging to that site" do
175
+ @superuser.has_role?(:moderator, @section).should be_true
176
+ end
177
+
178
+ test "a superuser has the role :site for that site" do
179
+ @superuser.has_role?(:admin, @site).should be_true
180
+ end
181
+
182
+ test "a superuser has the role :superuser" do
183
+ @superuser.has_role?(:superuser).should be_true
184
+ end
185
+ end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: adva_rbac
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Micah Geisel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Adva RBAC
14
+ email:
15
+ - micah@botandrose.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - LICENSE
23
+ - NOTES
24
+ - README.md
25
+ - Rakefile
26
+ - adva_rbac.gemspec
27
+ - app/controllers/roles_controller.rb
28
+ - app/helpers/roles_helper.rb
29
+ - app/views/admin/sections/settings/_permissions.html.erb
30
+ - app/views/roles/index.js.erb
31
+ - config/initializers/base_controller.rb
32
+ - config/initializers/rbac.rb
33
+ - config/initializers/user.rb
34
+ - db/migrate/20080402000006_create_role_tables.rb
35
+ - db/migrate/20090720132900_migrate_roles_table_to_new_rbac.rb
36
+ - lib/action_controller/guards_permissions.rb
37
+ - lib/adva_rbac.rb
38
+ - lib/adva_rbac/version.rb
39
+ - lib/permission_map.rb
40
+ - lib/rbac.rb
41
+ - lib/rbac/acts_as_role_context.rb
42
+ - lib/rbac/acts_as_role_subject.rb
43
+ - lib/rbac/context.rb
44
+ - lib/rbac/role.rb
45
+ - lib/rbac/role_type.rb
46
+ - lib/rbac/role_type/active_record.rb
47
+ - lib/rbac/role_type/static.rb
48
+ - lib/rbac/subject.rb
49
+ - test/functional/roles_controller_test.rb
50
+ - test/integration/user_rbac_test.rb
51
+ - test/rbac/all.rb
52
+ - test/rbac/database.rb
53
+ - test/rbac/database.yml
54
+ - test/rbac/implementation/active_record_test.rb
55
+ - test/rbac/implementation/static_test.rb
56
+ - test/rbac/static.rb
57
+ - test/rbac/test_helper.rb
58
+ - test/rbac/tests/acts_as_role_context.rb
59
+ - test/rbac/tests/context.rb
60
+ - test/rbac/tests/group.rb
61
+ - test/rbac/tests/has_role.rb
62
+ - test/rbac/tests/role_type.rb
63
+ - test/test_helper.rb
64
+ - test/unit/helpers/roles_helper_test.rb
65
+ - test/unit/models/rbac_context_test.rb
66
+ - test/unit/models/rbac_user_test.rb
67
+ - test/unit/models/role_test.rb
68
+ homepage: ''
69
+ licenses: []
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.4.6
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: Engine for role-based authorization in Adva CMS
91
+ test_files:
92
+ - test/functional/roles_controller_test.rb
93
+ - test/integration/user_rbac_test.rb
94
+ - test/rbac/all.rb
95
+ - test/rbac/database.rb
96
+ - test/rbac/database.yml
97
+ - test/rbac/implementation/active_record_test.rb
98
+ - test/rbac/implementation/static_test.rb
99
+ - test/rbac/static.rb
100
+ - test/rbac/test_helper.rb
101
+ - test/rbac/tests/acts_as_role_context.rb
102
+ - test/rbac/tests/context.rb
103
+ - test/rbac/tests/group.rb
104
+ - test/rbac/tests/has_role.rb
105
+ - test/rbac/tests/role_type.rb
106
+ - test/test_helper.rb
107
+ - test/unit/helpers/roles_helper_test.rb
108
+ - test/unit/models/rbac_context_test.rb
109
+ - test/unit/models/rbac_user_test.rb
110
+ - test/unit/models/role_test.rb