cantango-roles 0.1.0

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 (75) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +36 -0
  4. data/Gemfile.lock +147 -0
  5. data/LICENSE.txt +20 -0
  6. data/README.mdown +83 -0
  7. data/Rakefile +53 -0
  8. data/VERSION +1 -0
  9. data/lib/cantango/roles.rb +11 -0
  10. data/lib/cantango/roles_ext.rb +9 -0
  11. data/lib/cantango/roles_ext/ability.rb +7 -0
  12. data/lib/cantango/roles_ext/ability/helper.rb +7 -0
  13. data/lib/cantango/roles_ext/ability/helper/role.rb +17 -0
  14. data/lib/cantango/roles_ext/ability/helper/role_group.rb +17 -0
  15. data/lib/cantango/roles_ext/builder.rb +7 -0
  16. data/lib/cantango/roles_ext/builder/permit.rb +7 -0
  17. data/lib/cantango/roles_ext/builder/permit/role.rb +35 -0
  18. data/lib/cantango/roles_ext/builder/permit/role_group.rb +61 -0
  19. data/lib/cantango/roles_ext/configuration.rb +13 -0
  20. data/lib/cantango/roles_ext/configuration/registry/role.rb +34 -0
  21. data/lib/cantango/roles_ext/configuration/role_groups.rb +17 -0
  22. data/lib/cantango/roles_ext/configuration/roles.rb +16 -0
  23. data/lib/cantango/roles_ext/configuration/system.rb +43 -0
  24. data/lib/cantango/roles_ext/engine.rb +7 -0
  25. data/lib/cantango/roles_ext/engine/permits.rb +5 -0
  26. data/lib/cantango/roles_ext/filter.rb +7 -0
  27. data/lib/cantango/roles_ext/filter/role.rb +29 -0
  28. data/lib/cantango/roles_ext/filter/role_group.rb +28 -0
  29. data/lib/cantango/roles_ext/helpers.rb +7 -0
  30. data/lib/cantango/roles_ext/helpers/role.rb +14 -0
  31. data/lib/cantango/roles_ext/helpers/role_group.rb +14 -0
  32. data/lib/cantango/roles_ext/permit.rb +0 -0
  33. data/lib/cantango/roles_ext/permit/helper/role_matcher.rb +13 -0
  34. data/lib/cantango/roles_ext/permit/role.rb +35 -0
  35. data/lib/cantango/roles_ext/permit/role_group.rb +47 -0
  36. data/lib/generators/cantango/base.rb +71 -0
  37. data/lib/generators/cantango/basic.rb +41 -0
  38. data/lib/generators/cantango/license_base.rb +15 -0
  39. data/lib/generators/cantango/permit_generator.rb +58 -0
  40. data/lib/generators/cantango/role_permit/role_permit_generator.rb +39 -0
  41. data/lib/generators/cantango/role_permit/templates/account_permit.erb +4 -0
  42. data/lib/generators/cantango/role_permit/templates/role_group_permit.erb +24 -0
  43. data/lib/generators/cantango/role_permit/templates/role_permit.erb +23 -0
  44. data/lib/generators/cantango/role_permits/role_permits_generator.rb +45 -0
  45. data/spec/cantango/ability/helper/role_group_spec.rb +33 -0
  46. data/spec/cantango/ability/helper/role_spec.rb +33 -0
  47. data/spec/cantango/ability/helper/shared/role_ex.rb +0 -0
  48. data/spec/cantango/ability/helper/shared/role_group_ex.rb +0 -0
  49. data/spec/cantango/builder/role_group_spec.rb +5 -0
  50. data/spec/cantango/builder/role_spec.rb +5 -0
  51. data/spec/cantango/configuration/role_groups_spec.rb +13 -0
  52. data/spec/cantango/configuration/role_registry_spec.rb +9 -0
  53. data/spec/cantango/configuration/roles_spec.rb +11 -0
  54. data/spec/cantango/configuration/shared/registry_ex.rb +40 -0
  55. data/spec/cantango/configuration/shared/role_registry_ex.rb +15 -0
  56. data/spec/cantango/configuration/shared/system_ex.rb +39 -0
  57. data/spec/cantango/configuration/system_spec.rb +9 -0
  58. data/spec/cantango/engine/permits_spec.rb +7 -0
  59. data/spec/cantango/filter/role_group_spec.rb +96 -0
  60. data/spec/cantango/filter/role_spec.rb +96 -0
  61. data/spec/cantango/helpers/role_group_spec.rb +26 -0
  62. data/spec/cantango/helpers/role_spec.rb +26 -0
  63. data/spec/fixtures/models.rb +2 -0
  64. data/spec/fixtures/models/admin.rb +2 -0
  65. data/spec/fixtures/models/admin_account.rb +22 -0
  66. data/spec/fixtures/models/items.rb +8 -0
  67. data/spec/fixtures/models/permission.rb +12 -0
  68. data/spec/fixtures/models/project.rb +2 -0
  69. data/spec/fixtures/models/simple_roles.rb +48 -0
  70. data/spec/fixtures/models/user.rb +52 -0
  71. data/spec/fixtures/models/user_account.rb +7 -0
  72. data/spec/helpers/current_user_accounts.rb +20 -0
  73. data/spec/helpers/current_users.rb +10 -0
  74. data/spec/spec_helper.rb +2 -0
  75. metadata +223 -0
@@ -0,0 +1,40 @@
1
+ shared_examples_for 'Registry' do
2
+ describe 'default settings' do
3
+
4
+ #its(:registered) { should be_empty }
5
+
6
+ it 'should register groups' do
7
+ subject.register(:a, :b)
8
+ subject.registered.should include(:a, :b)
9
+ end
10
+
11
+ it 'should set defaults' do
12
+ subject.default = :a, :b
13
+ subject.default.should include(:a, :b)
14
+ end
15
+ end
16
+
17
+ describe 'register' do
18
+ before do
19
+ subject.register :abc, :def
20
+ end
21
+ its(:registered) { should include(:abc, :def) }
22
+ end
23
+
24
+ describe 'append <<' do
25
+ before do
26
+ subject.clean!
27
+ subject.register :abc, :def
28
+ subject << :xyz
29
+ end
30
+ its(:registered) { should include(:abc, :def, :xyz) }
31
+ end
32
+
33
+ describe 'get index []' do
34
+ before do
35
+ subject.clean!
36
+ subject.register :abc, :def
37
+ end
38
+ specify {subject[0].should == :abc }
39
+ end
40
+ end
@@ -0,0 +1,15 @@
1
+ # Fix: from cantango-config
2
+ require 'cantango/configuration/shared/registry_ex'
3
+
4
+ shared_examples_for 'Role Registry' do
5
+ it_should_behave_like "Registry"
6
+
7
+ describe "exclude" do
8
+ before do
9
+ subject.exclude :admin
10
+ end
11
+
12
+ its(:excluded) { should include(:admin) }
13
+ end
14
+ end
15
+
@@ -0,0 +1,39 @@
1
+ require 'cantango/configuration/shared/role_registry_ex'
2
+
3
+ shared_examples_for 'System' do
4
+ it_should_behave_like "Role Registry"
5
+
6
+ describe 'default system - simple_roles' do
7
+ its(:system) { should == :simple_roles }
8
+ end
9
+
10
+ describe 'system=' do
11
+ before do
12
+ subject.system = :my_sys
13
+ end
14
+ its(:system) { should == :my_sys }
15
+ end
16
+
17
+ describe 'system_apis=' do
18
+ let(:my_own_sys) do
19
+ {:my_own_sys => {:list => :listing } }
20
+ end
21
+
22
+ before do
23
+ subject.system_apis = my_own_sys
24
+ subject.system = :my_own_sys
25
+ end
26
+
27
+ specify { subject.system_api[:list].should == :listing }
28
+ specify { subject.system_apis.size.should == 1 }
29
+ end
30
+
31
+ describe 'add_systems' do
32
+ before do
33
+ subject.add_system :my_other_sys => {:list => :listing }
34
+ subject.system = :my_other_sys
35
+ end
36
+ specify { subject.system_api[:list].should == :listing }
37
+ specify { subject.system_apis.size.should > 1 }
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/shared/system_ex'
3
+
4
+ describe CanTango::Configuration::System do
5
+ subject { CanTango.config.roles }
6
+
7
+ it_should_behave_like "System" do
8
+ end
9
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+ require 'cantngo/ability/helper/shared/role_ex'
3
+
4
+ describe CanTango::Engine::Permits do
5
+ # use shared Role helper example
6
+ pending
7
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+
4
+ CanTango.configure do |config|
5
+ config.permission_engine.set :off
6
+ config.permit_engine.set :on
7
+ config.categories.register :blog_items => [Article, Post]
8
+ end
9
+
10
+ class AdminsRoleGroupPermit < CanTango::Permit::RoleGroup
11
+ def initialize ability
12
+ super
13
+ end
14
+
15
+ protected
16
+
17
+ def calc_rules
18
+ can :publish, Post
19
+ can :write, Article
20
+ can :write, category(:blog_items)
21
+ end
22
+ end
23
+
24
+ class EditorsRoleGroupPermit < CanTango::Permit::RoleGroup
25
+ def initialize ability
26
+ super
27
+ end
28
+
29
+ protected
30
+
31
+ def calc_rules
32
+ can :publish, category(:blog_items)
33
+ end
34
+ end
35
+
36
+
37
+ describe CanTango::Filter::RoleGroup do
38
+ describe 'role groups filter - exclude :admins' do
39
+ let (:user) do
40
+ User.new 'stan', 'stan@gmail.com'
41
+ end
42
+
43
+ let (:user_account) do
44
+ ua = UserAccount.new user, :roles => [:user, :admin], :role_groups => [:admins]
45
+ user.account = ua
46
+ end
47
+
48
+ before do
49
+ CanTango.config.role_groups.exclude :admins
50
+ CanTango.config.categories.register :blog_items => [Article, Post]
51
+
52
+ @ability = CanTango::Ability.new user_account
53
+ end
54
+
55
+ after do
56
+ CanTango.config.clear!
57
+ end
58
+
59
+ subject { @ability }
60
+ specify { @ability.should be_allowed_to(:read, Post)}
61
+
62
+ specify { @ability.should_not be_allowed_to(:read, Comment)}
63
+ specify { @ability.should_not be_allowed_to(:write, Article)}
64
+ end
65
+ end
66
+
67
+ describe CanTango::Filter::RoleGroup do
68
+ describe 'role groups filter- only :admins' do
69
+ let (:user) do
70
+ User.new 'stan', 'stan@gmail.com'
71
+ end
72
+
73
+ let (:user_account) do
74
+ ua = UserAccount.new user, :roles => [:user, :admin], :role_groups => [:admins, :editors]
75
+ user.account = ua
76
+ end
77
+
78
+ before do
79
+ CanTango.config.categories.register :blog_items => [Article, Post]
80
+ CanTango.config.role_groups.only :admins
81
+ @ability = CanTango::Ability.new user_account
82
+ end
83
+
84
+ after do
85
+ CanTango.config.clear!
86
+ end
87
+
88
+ subject { @ability }
89
+ specify { @ability.should be_allowed_to(:read, Comment)}
90
+ specify { @ability.should be_allowed_to(:write, Article)}
91
+
92
+ specify { @ability.should be_allowed_to(:publish, Post)}
93
+
94
+ specify { @ability.should_not be_allowed_to(:publish, Article)}
95
+ end
96
+ end
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ require 'fixtures/models'
3
+ require 'cantango/rspec'
4
+
5
+ CanTango.configure do |config|
6
+ config.permission_engine.set :off
7
+ config.permit_engine.set :on
8
+ config.categories.register :blog_items => [Article, Post]
9
+ end
10
+
11
+
12
+ class UserRolePermit < CanTango::Permit::Role
13
+ def initialize ability
14
+ super
15
+ end
16
+
17
+ protected
18
+
19
+ def calculate_rules
20
+ can :read, Comment
21
+ end
22
+ end
23
+
24
+ class AdminRolePermit < CanTango::Permit::Role
25
+ def initialize ability
26
+ super
27
+ end
28
+
29
+ protected
30
+
31
+ def calculate_rules
32
+ can :read, Post
33
+ end
34
+ end
35
+
36
+
37
+ describe CanTango::Filter::Role do
38
+ describe 'roles filter - exclude :user' do
39
+ let (:user) do
40
+ User.new 'stan', 'stan@gmail.com'
41
+ end
42
+
43
+ let (:user_account) do
44
+ ua = UserAccount.new user, :roles => [:user, :admin], :role_groups => [:admins]
45
+ user.account = ua
46
+ end
47
+
48
+ before do
49
+ CanTango.config.roles.exclude :user
50
+ CanTango.config.categories.register :blog_items => [Article, Post]
51
+
52
+ @ability = CanTango::Ability.new user_account
53
+ end
54
+
55
+ after do
56
+ CanTango.config.clear!
57
+ end
58
+
59
+ subject { @ability }
60
+ specify { @ability.should be_allowed_to(:read, Post)}
61
+
62
+ specify { @ability.should_not be_allowed_to(:read, Comment)}
63
+ specify { @ability.should_not be_allowed_to(:write, Article)}
64
+ end
65
+ end
66
+
67
+ describe CanTango::Filter::Role do
68
+ describe 'roles filter - only :user' do
69
+ let (:user) do
70
+ User.new 'stan', 'stan@gmail.com'
71
+ end
72
+
73
+ let (:user_account) do
74
+ ua = UserAccount.new user, :roles => [:user, :admin], :role_groups => [:admins, :editors]
75
+ user.account = ua
76
+ end
77
+
78
+ before do
79
+ CanTango.config.categories.register :blog_items => [Article, Post]
80
+ CanTango.config.roles.only :user
81
+ @ability = CanTango::Ability.new user_account
82
+ end
83
+
84
+ after do
85
+ CanTango.config.clear!
86
+ end
87
+
88
+ subject { @ability }
89
+ specify { @ability.should be_allowed_to(:read, Comment)}
90
+ specify { @ability.should be_allowed_to(:write, Article)}
91
+
92
+ specify { @ability.should be_allowed_to(:publish, Post)}
93
+
94
+ specify { @ability.should_not be_allowed_to(:publish, Article)}
95
+ end
96
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/role_registry_ex'
3
+
4
+ class Subject
5
+ end
6
+
7
+ describe CanTango::Helpers::RoleGroup do
8
+ before do
9
+ CanTango.config.roles.system = :troles
10
+ end
11
+
12
+ subject do
13
+ Subject.new
14
+ end
15
+
16
+ specify do
17
+ subject.role_method(:has).should == :in_role_group?
18
+ end
19
+
20
+ specify do
21
+ subject.role_method(:list).should == :role_group_list
22
+ end
23
+ end
24
+
25
+
26
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'cantango/configuration/role_registry_ex'
3
+
4
+ class Subject
5
+ end
6
+
7
+ describe CanTango::Helpers::Role do
8
+ before do
9
+ CanTango.config.roles.system = :troles
10
+ end
11
+
12
+ subject do
13
+ Subject.new
14
+ end
15
+
16
+ specify do
17
+ subject.role_method(:has).should == :has_role?
18
+ end
19
+
20
+ specify do
21
+ subject.role_method(:list).should == :role_list
22
+ end
23
+ end
24
+
25
+
26
+
@@ -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,48 @@
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
+ attr_accessor :role_groups_list
24
+
25
+ def has_role? role
26
+ roles_list.include? role
27
+ end
28
+
29
+ def has_any_role? roles
30
+ roles.include?(role.to_sym)
31
+ end
32
+
33
+ def roles_list
34
+ roles.map{|r| r.to_sym}
35
+ end
36
+
37
+ def is_in_group? group
38
+ role_groups_list.include? group
39
+ end
40
+ alias_method :in_role_group?, :is_in_group?
41
+
42
+ def role_groups_list
43
+ return role_groups.map(&:to_sym) if respond_to?(:role_groups) && !role_groups.nil?
44
+ @role_groups_list || [] #[:bloggers]
45
+ end
46
+ end
47
+ end
48
+