roles_generic 0.3.5 → 0.3.6

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.
data/README.textile CHANGED
@@ -1,10 +1,12 @@
1
1
  h1. Generic Roles
2
2
 
3
- *Generic Roles* is a generic Roles API implementation that specific ORM Roles implementations can implement.
4
- This way you can easily change ORM and still keep the same underlying API.
3
+ *Generic Roles* (or simply Roles) is a generic Roles API implementation that specific ORM Roles implementations can implement.
4
+ This way you can easily change ORM and still keep using the same underlying API.
5
5
 
6
6
  A Rails 3 generator is included that can configure an existing User model with a generic Role strategy of choice.
7
- Each ORM Roles implementation should have a similar generator for that particular ORM.
7
+ Each ORM Roles implementation should have its own generator suited for that particular ORM.
8
+
9
+ Since Jan. 2011, Roles now includes a Group API that allows ordering roles into groups (see below).
8
10
 
9
11
  h2. Roles ORM implementations
10
12
 
@@ -37,8 +39,6 @@ h2. Role strategy configuration
37
39
 
38
40
  The following demonstrates some examples of role strategy configuration.
39
41
 
40
- _Note_: The DSL has been optimized a bit lately (Dec. 25)
41
-
42
42
  h3. Strategy: admin_flag
43
43
 
44
44
  Example: Default model configuration:
@@ -103,12 +103,49 @@ Embedded Role model (Document stores only):
103
103
 
104
104
  Currently the embedded strategies have only been implemented for Mongoid.
105
105
 
106
- h2. Role API
106
+ See "Roles strategy configuration":https://github.com/kristianmandrup/roles_generic/wiki/Roles-strategy-configuration for more info
107
+
108
+ h2. Roles APIs
107
109
 
108
110
  The full Roles API is described in these Wiki pages:
109
111
 
110
112
  * "Roles-Read-API":https://github.com/kristianmandrup/roles_generic/wiki/Roles-Read-API
111
113
  * "Roles-Write-API":https://github.com/kristianmandrup/roles_generic/wiki/Roles-Write-API
114
+ * "Roles-Group-API":https://github.com/kristianmandrup/roles_generic/wiki/Roles-Group-API
115
+
116
+ h3. Using Roles Groups
117
+
118
+ The Group API allows roles to be grouped.
119
+
120
+ Example:
121
+
122
+ Say you have the _admin_ roles:
123
+
124
+ * Admin
125
+ * Super admin
126
+
127
+ And the _customer_ roles
128
+
129
+ * Individual customer
130
+ * Company customer
131
+
132
+ You might want to group them like this:
133
+
134
+ <pre>
135
+ User.add_role_group :customers => [:individual_customer, :company_customer]
136
+ User.add_role_group :admins => [:admin, :super_admin]
137
+ </pre>
138
+
139
+ Then you can handle any user with regards to his/her role group relationship like this:
140
+
141
+ <pre>
142
+ # do this only for users in the admin role group (user has either :admin or :super_admin role)
143
+ current_user.is_in_group? :admin do
144
+ ...
145
+ end
146
+ </pre>
147
+
148
+ Expect this functionality to soon to be wrapped by Cream helper methods as well ;)
112
149
 
113
150
  h2. Note on Patches/Pull Requests
114
151
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.3.6
@@ -1,6 +1,7 @@
1
1
  module Roles
2
2
  module Base
3
3
  attr_accessor :orm_name
4
+
4
5
  attr_reader :role_strategy
5
6
 
6
7
  def valid_roles_are(*role_list)
@@ -42,9 +42,34 @@ module Roles::Generic::User
42
42
  exchange_roles role, options
43
43
  end
44
44
 
45
- # check if a given role has been assigned
46
- # if a list of roles: check if ALL of the given roles have been assigned
45
+ # is_in_group? :admin
46
+ def is_in_group? group
47
+ raise ArgumentError, 'Group id must be a String or Symbol' if !group.kind_of_label?
48
+ group_roles = self.class.role_groups[group]
49
+ # puts "group_roles: #{group_roles} for group: #{group}"
50
+ # puts "roles_list: #{roles_list}"
51
+ !(group_roles & roles_list).empty?
52
+ end
53
+
54
+ # is_in_groups? :editor, :admin,
55
+ def is_in_groups? *groups
56
+ groups = groups.flat_uniq
57
+ groups.all? {|group| is_in_group? group}
58
+ end
59
+
60
+ def is_in_any_group? *groups
61
+ groups = groups.flat_uniq
62
+ groups.any? {|group| is_in_group? group}
63
+ end
64
+
65
+ # check if all of the roles listed have been assigned to that user
47
66
  def has_roles?(*roles_names)
67
+ compare_roles = extract_roles(roles_names.flat_uniq)
68
+ (compare_roles - roles_list).empty?
69
+ end
70
+
71
+ # check if any of the roles listed have been assigned to that user
72
+ def has_any_role?(*roles_names)
48
73
  compare_roles = extract_roles(roles_names.flat_uniq)
49
74
  (roles_list & compare_roles).not.empty?
50
75
  end
@@ -17,8 +17,6 @@ module Roles::Generic
17
17
  roles_attribute options[:attribute]
18
18
  return
19
19
  end
20
-
21
- puts "default: #{default_options? options}, att: #{default_role_attribute}"
22
20
  roles_attribute default_role_attribute if default_options? options
23
21
  end
24
22
 
@@ -5,6 +5,7 @@ module Roles
5
5
  def self.included(base)
6
6
  base.extend Roles::Base
7
7
  base.extend ClassMethods
8
+
8
9
  base.orm_name = :generic
9
10
  end
10
11
 
@@ -24,6 +25,11 @@ module Roles
24
25
  :embed_many_roles => "attr_accessor :many_roles",
25
26
  :embed_one_role => "attr_accessor :one_role"
26
27
  }
28
+
29
+ attr_accessor :role_groups
30
+ def role_groups
31
+ @role_groups ||= {}
32
+ end
27
33
 
28
34
  def strategy name, options = {}
29
35
  strategy_name = name.to_sym
@@ -44,6 +50,29 @@ module Roles
44
50
 
45
51
  set_role_strategy name, options
46
52
  end
53
+
54
+ def add_role_groups(groups_hash)
55
+ raise ArgumentError, "Role group must be passed a Hash where the key is the group name" if !groups_hash.kind_of? Hash
56
+ groups_hash.each_pair do |key, list|
57
+ add_role_group(key => list)
58
+ end
59
+ end
60
+
61
+ # role_group :admin => :admin, :super_admin
62
+ def add_role_group(group_hash)
63
+ raise ArgumentError, '#add_role_group must be passed a Hash where the key is the group name' if !group_hash.kind_of? Hash
64
+ raise ArgumentError, '#add_role_group must be a Hash with a single key value pair' if group_hash.size > 1
65
+
66
+ # first key/value pair?
67
+ group = group_hash.keys.first # see sugar_high
68
+
69
+ raise ArgumentError, "Role group identifier must be a String or Symbol " if !group.kind_of_label?
70
+ role_list = group_hash.values.first
71
+ hash = {group.to_sym => [role_list].flat_uniq.to_symbols}
72
+
73
+ role_groups.merge!(hash)
74
+ end
75
+
47
76
 
48
77
  private
49
78
 
@@ -1,13 +1,4 @@
1
- module Roles::Base
2
- def valid_roles_are(*role_list)
3
- strategy_class.valid_roles = role_list.to_symbols
4
- if role_class_name
5
- role_list.each do |name|
6
- role_class_name.new(name)
7
- end
8
- end
9
- end
10
- end
1
+ require 'roles_generic/roles_base'
11
2
 
12
3
  class Role
13
4
  attr_accessor :name
@@ -0,0 +1,11 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ if role_class_name
5
+ role_list.each do |name|
6
+ role_class_name.new(name)
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -1,5 +1,6 @@
1
1
  require "roles_generic/rspec/user_setup"
2
- # require "roles_generic/rspec/api/write_api"
2
+ require "roles_generic/rspec/api/write_api"
3
3
  require "roles_generic/rspec/api/read_api"
4
- # require "roles_generic/rspec/api/user_class_api"
5
- # require "roles_generic/rspec/api/completeness"
4
+ require "roles_generic/rspec/api/group_api"
5
+ require "roles_generic/rspec/api/user_class_api"
6
+ require "roles_generic/rspec/api/completeness"
@@ -0,0 +1,51 @@
1
+ class Role
2
+ end
3
+
4
+ describe 'Roles Generic API : GROUP' do
5
+ before :each do
6
+ default_user_setup
7
+ end
8
+
9
+ describe '#add_group' do
10
+ it "should add a group with multiple roles" do
11
+ User.add_role_group(:guests => [:user, :guest]).should be_true
12
+ User.role_groups[:guests].should include(:user)
13
+ end
14
+
15
+ it "should not add a group using hash of multiple groups" do
16
+ lambda { User.add_role_group(:guests => [:user, :guest], :editors => :editor) }.should raise_error
17
+ end
18
+ end
19
+
20
+ describe '#add_groups' do
21
+ it "should add a group with multiple roles" do
22
+ User.add_role_groups :guests => [:user, :guest], :editors => :editor
23
+ User.role_groups[:guests].should include(:user)
24
+ User.role_groups[:editors].should include(:editor)
25
+ end
26
+ end
27
+
28
+ describe '#is_in_group' do
29
+ it "should be true that the admin user has a valid role of :guest" do
30
+ User.add_role_groups :guests => [:user, :guest], :editors => :editor
31
+ @admin_user.is_in_group?(:editors).should be_false
32
+ @normal_user.is_in_group?(:guests).should be_true
33
+ end
34
+ end
35
+
36
+ describe '#is_in_groups' do
37
+ it "should be true that the admin user has a valid role of :guest" do
38
+ User.add_role_groups :guests => [:user, :guest], :users => :user
39
+ @admin_user.is_in_groups?(:guests).should be_false
40
+ @normal_user.is_in_groups?(:guests, :users).should be_true
41
+ end
42
+ end
43
+
44
+ describe '#is_in_any_group' do
45
+ it "should be true that the admin user has a valid role of :guest" do
46
+ User.add_role_groups :guests => [:user, :guest], :editors => :editor
47
+ @admin_user.is_in_any_group?(:guests).should be_false
48
+ @normal_user.is_in_any_group?(:guests, :editors).should be_true
49
+ end
50
+ end
51
+ end
@@ -51,69 +51,87 @@ describe 'Roles Generic API : READ' do
51
51
  end
52
52
 
53
53
  describe '#has_roles?' do
54
- it "should be true that the admin_user has the roles :admin" do
55
- @admin_user.has_roles?(:admin).should be_true
54
+ it "should be true that the normal user has the roles :guest and :user" do
55
+ @normal_user.has_roles?(:guest, :user).should be_true
56
+ end
57
+
58
+ it "should NOT be true that the normal user has the roles :guest and :admin" do
59
+ @normal_user.has_roles?(:guest, :admin).should be_false
56
60
  end
57
61
 
58
62
  it "should NOT be true that the user has the roles :admin" do
59
63
  @guest_user.has_roles?(:admin).should be_false
60
64
  end
61
65
  end
66
+
67
+ describe '#has_roles?' do
68
+ it "should be true that the normal user has the roles :guest and :user" do
69
+ @normal_user.has_any_role?(:guest, :user).should be_true
70
+ end
71
+
72
+ it "should be true that the normal user has one of the roles :guest or :admin" do
73
+ @normal_user.has_any_role?(:guest, :admin).should be_true
74
+ end
75
+
76
+ it "should NOT be true that the user has the roles :admin" do
77
+ @guest_user.has_any_role?(:admin).should be_false
78
+ end
79
+ end
80
+
81
+ describe '#roles_list' do
82
+ it "should be true that the first role of admin_user is the :admin role" do
83
+ @admin_user.roles_list.should include(:admin)
84
+ end
62
85
 
63
- # describe '#roles_list' do
64
- # it "should be true that the first role of admin_user is the :admin role" do
65
- # @admin_user.roles_list.should include(:admin)
66
- # end
67
- #
68
- # it "should be true that the first role of admin_user is the :user role" do
69
- # case @normal_user.class.role_strategy.multiplicity
70
- # when :single
71
- # if @normal_user.class.role_strategy.name == :admin_flag
72
- # @normal_user.roles_list.should include(:guest)
73
- # else
74
- # @normal_user.roles_list.should include(:user)
75
- # end
76
- # when :multi
77
- # @normal_user.roles_list.should include(:user, :guest)
78
- # end
79
- # end
80
- # end
81
- #
82
- # describe '#roles' do
83
- # it "should be true that the roles of admin_user is an array with the role :admin" do
84
- # roles = @admin_user.roles
85
- # if roles.kind_of? Role
86
- # roles.name.to_sym.should == :admin
87
- # elsif roles.kind_of? Array
88
- # if @normal_user.class.role_strategy.type == :complex
89
- # # roles.first.name.to_sym.should == :admin
90
- # roles.first.to_sym.should == :admin
91
- # else
92
- # roles.first.to_sym.should == :admin
93
- # end
94
- # else
95
- # roles.to_sym.should == :admin
96
- # end
97
- # end
98
- # end
99
- #
100
- # describe '#admin?' do
101
- # it "should be true that admin_user is in the :admin role" do
102
- # @admin_user.admin?.should be_true
103
- # end
104
- #
105
- # it "should NOT be true that the user is in the :admin role" do
106
- # @guest_user.admin?.should be_false
107
- # end
108
- # end
109
- #
110
- # describe '#is?' do
111
- # it "should be true that admin_user is in the :admin role" do
112
- # @admin_user.is?(:admin).should be_true
113
- # end
114
- #
115
- # it "should NOT be true that the user is in the :admin role" do
116
- # @guest_user.is?(:admin).should be_false
117
- # end
118
- # end
86
+ it "should be true that the first role of admin_user is the :user role" do
87
+ case @normal_user.class.role_strategy.multiplicity
88
+ when :single
89
+ if @normal_user.class.role_strategy.name == :admin_flag
90
+ @normal_user.roles_list.should include(:guest)
91
+ else
92
+ @normal_user.roles_list.should include(:user)
93
+ end
94
+ when :multi
95
+ @normal_user.roles_list.should include(:user, :guest)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe '#roles' do
101
+ it "should be true that the roles of admin_user is an array with the role :admin" do
102
+ roles = @admin_user.roles
103
+ if roles.kind_of? Role
104
+ roles.name.to_sym.should == :admin
105
+ elsif roles.kind_of? Array
106
+ if @normal_user.class.role_strategy.type == :complex
107
+ # roles.first.name.to_sym.should == :admin
108
+ roles.first.to_sym.should == :admin
109
+ else
110
+ roles.first.to_sym.should == :admin
111
+ end
112
+ else
113
+ roles.to_sym.should == :admin
114
+ end
115
+ end
116
+ end
117
+
118
+ describe '#admin?' do
119
+ it "should be true that admin_user is in the :admin role" do
120
+ @admin_user.admin?.should be_true
121
+ end
122
+
123
+ it "should NOT be true that the user is in the :admin role" do
124
+ @guest_user.admin?.should be_false
125
+ end
126
+ end
127
+
128
+ describe '#is?' do
129
+ it "should be true that admin_user is in the :admin role" do
130
+ @admin_user.is?(:admin).should be_true
131
+ end
132
+
133
+ it "should NOT be true that the user is in the :admin role" do
134
+ @guest_user.is?(:admin).should be_false
135
+ end
136
+ end
119
137
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{roles_generic}
8
- s.version = "0.3.5"
8
+ s.version = "0.3.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kristian Mandrup"]
12
- s.date = %q{2010-12-26}
12
+ s.date = %q{2011-01-06}
13
13
  s.description = %q{Generic role strategies sharing the same API. Easy to insert in any model}
14
14
  s.email = %q{kmandrup@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -38,8 +38,10 @@ Gem::Specification.new do |s|
38
38
  "lib/roles_generic/generic/user/single_impl.rb",
39
39
  "lib/roles_generic/namespaces.rb",
40
40
  "lib/roles_generic/role.rb",
41
+ "lib/roles_generic/roles_base.rb",
41
42
  "lib/roles_generic/rspec/api/completeness.rb",
42
43
  "lib/roles_generic/rspec/api/full.rb",
44
+ "lib/roles_generic/rspec/api/group_api.rb",
43
45
  "lib/roles_generic/rspec/api/read_api.rb",
44
46
  "lib/roles_generic/rspec/api/simple_check.rb",
45
47
  "lib/roles_generic/rspec/api/user_class_api.rb",
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 5
9
- version: 0.3.5
8
+ - 6
9
+ version: 0.3.6
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-26 00:00:00 +01:00
17
+ date: 2011-01-06 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -153,8 +153,10 @@ files:
153
153
  - lib/roles_generic/generic/user/single_impl.rb
154
154
  - lib/roles_generic/namespaces.rb
155
155
  - lib/roles_generic/role.rb
156
+ - lib/roles_generic/roles_base.rb
156
157
  - lib/roles_generic/rspec/api/completeness.rb
157
158
  - lib/roles_generic/rspec/api/full.rb
159
+ - lib/roles_generic/rspec/api/group_api.rb
158
160
  - lib/roles_generic/rspec/api/read_api.rb
159
161
  - lib/roles_generic/rspec/api/simple_check.rb
160
162
  - lib/roles_generic/rspec/api/user_class_api.rb