roles_generic 0.2.7 → 0.3.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +25 -13
- data/Rakefile +7 -5
- data/VERSION +1 -1
- data/lib/generators/{roles_model → roles_generic}/roles/roles_generator.rb +8 -15
- data/lib/roles_generic/base.rb +40 -2
- data/lib/roles_generic/generic/role.rb +2 -16
- data/lib/roles_generic/generic/role_util.rb +30 -0
- data/lib/roles_generic/generic/user/implementation.rb +108 -6
- data/lib/roles_generic/generic/user/multi_impl.rb +27 -0
- data/lib/roles_generic/generic/user/single_impl.rb +24 -0
- data/lib/roles_generic/generic.rb +1 -1
- data/lib/roles_generic/role.rb +1 -0
- data/lib/roles_generic/rspec/api/completeness.rb +22 -0
- data/lib/roles_generic/rspec/api/full.rb +5 -0
- data/lib/roles_generic/rspec/api/read_api.rb +119 -0
- data/lib/roles_generic/rspec/api/simple_check.rb +11 -0
- data/lib/roles_generic/rspec/api/user_class_api.rb +31 -0
- data/lib/roles_generic/rspec/api/write_api.rb +50 -0
- data/lib/roles_generic/rspec/test_it.rb +2 -0
- data/lib/roles_generic/rspec/user_setup.rb +6 -0
- data/lib/roles_generic/strategy/multi/many_roles.rb +12 -19
- data/lib/roles_generic/strategy/multi/role_strings.rb +15 -12
- data/lib/roles_generic/strategy/multi/roles_mask.rb +20 -16
- data/lib/roles_generic/strategy/multi/roles_string.rb +14 -11
- data/lib/roles_generic/strategy/single/admin_flag.rb +24 -13
- data/lib/roles_generic/strategy/single/one_role.rb +19 -18
- data/lib/roles_generic/strategy/single/role_string.rb +16 -11
- data/lib/roles_generic.rb +1 -0
- data/roles_generic.gemspec +60 -46
- data/spec/generator_spec_helper.rb +1 -1
- data/spec/{generators → roles_generic/generators}/admin_flag_generator_spec.rb +2 -3
- data/spec/{generators → roles_generic/generators}/many_roles_generator_spec.rb +3 -2
- data/spec/{generators → roles_generic/generators}/one_role_generator_spec.rb +3 -2
- data/spec/{generators → roles_generic/generators}/role_string_generator_spec.rb +2 -2
- data/spec/{generators → roles_generic/generators}/role_strings_generator_spec.rb +2 -2
- data/spec/{generators → roles_generic/generators}/roles_mask_generator_spec.rb +2 -2
- data/spec/{generators → roles_generic/generators}/roles_string_generator_spec.rb +2 -2
- data/spec/roles_generic/strategy/multi/many_roles_spec.rb +26 -0
- data/spec/roles_generic/strategy/multi/role_strings_spec.rb +21 -0
- data/spec/roles_generic/strategy/multi/roles_mask_spec.rb +20 -0
- data/spec/roles_generic/strategy/multi/roles_string_spec.rb +21 -0
- data/spec/roles_generic/strategy/single/admin_flag_spec.rb +20 -0
- data/spec/roles_generic/strategy/single/one_role_spec.rb +27 -0
- data/spec/roles_generic/strategy/single/role_string_spec.rb +21 -0
- metadata +72 -50
- data/spec/roles_generic/admin_flag_spec.rb +0 -66
- data/spec/roles_generic/many_roles_spec.rb +0 -64
- data/spec/roles_generic/one_role_spec.rb +0 -71
- data/spec/roles_generic/role_string_spec.rb +0 -65
- data/spec/roles_generic/role_strings_spec.rb +0 -58
- data/spec/roles_generic/roles_mask_spec.rb +0 -57
- data/spec/roles_generic/roles_string_spec.rb +0 -58
data/README.markdown
CHANGED
@@ -1,26 +1,38 @@
|
|
1
|
-
# Generic
|
1
|
+
# Generic Roles
|
2
2
|
|
3
|
-
Generic
|
4
|
-
|
3
|
+
Generic Roles is a common (generic) Roles API and implementation that specific Roles implementations for various ORMs can implement.
|
4
|
+
This library also comes with a Rails 3 generator that can configure an existing user model with a Role strategy of choice.
|
5
|
+
A similar generator should always be created for specific ORM implementations
|
5
6
|
|
6
|
-
I have developed the following *roles* gems for popular
|
7
|
+
I have developed the following *roles* gems for popular Object Mappers that all support the same Roles Generic API.
|
7
8
|
|
8
|
-
|
9
|
-
*
|
10
|
-
*
|
11
|
-
* MongoMapper - [roles_mongo_mapper](http://github.com/kristianmandrup/roles_mongo_mapper))
|
9
|
+
Relational Database (SQL)
|
10
|
+
* Active Record - [roles_active_record](http://github.com/kristianmandrup/roles_active_record)
|
11
|
+
* Data Mapper - [roles_data_mapper](http://github.com/kristianmandrup/roles_data_mapper)
|
12
12
|
|
13
|
-
|
13
|
+
Mongo DB
|
14
|
+
* Mongoid - [roles_mongoid](http://github.com/kristianmandrup/roles_mongoid)
|
15
|
+
* Mongo Mapper - [roles_mongo_mapper](http://github.com/kristianmandrup/roles_mongo_mapper)
|
14
16
|
|
17
|
+
Couch DB
|
18
|
+
* Simply Stored - [roles_simply_stored](http://github.com/kristianmandrup/roles_simply_stored)
|
15
19
|
|
16
|
-
|
17
|
-
|
18
|
-
Roles generic has now been refactored in order to tidy up the code base and make it even more generic, flexible and easy to extend. It should now work! Please let me know if you have any issues ;)
|
20
|
+
Note: Feel free to roll your own ORM extension for your favorite Object Mapper!
|
19
21
|
|
20
22
|
## Install
|
21
23
|
|
22
24
|
<code>gem install roles_generic</code>
|
23
25
|
|
26
|
+
## Roles generator
|
27
|
+
|
28
|
+
A Rails 3 generator is included to update an existing model with a roles strategy and a set of valid roles
|
29
|
+
|
30
|
+
### Usage example
|
31
|
+
|
32
|
+
<code>rails g roles_generic:roles --strategy admin_flag --roles guest admin</code>
|
33
|
+
|
34
|
+
This generator is currently (Oct. 10) experimental. Feel free to provide patches or bug reports ;)
|
35
|
+
|
24
36
|
## Usage
|
25
37
|
|
26
38
|
The library comes with the following role models built-in:
|
@@ -61,7 +73,7 @@ Creates and uses a binary field 'admin_flag', which when true signals that this
|
|
61
73
|
end
|
62
74
|
</pre>
|
63
75
|
|
64
|
-
## Example: Using an
|
76
|
+
## Example: Using an Object Mapper
|
65
77
|
|
66
78
|
Data Mapper with persistent attributes :name and :admin_flag
|
67
79
|
|
data/Rakefile
CHANGED
@@ -7,13 +7,15 @@ begin
|
|
7
7
|
gem.email = "kmandrup@gmail.com"
|
8
8
|
gem.homepage = "http://github.com/kristianmandrup/roles_for_mm"
|
9
9
|
gem.authors = ["Kristian Mandrup"]
|
10
|
-
gem.add_development_dependency "rspec", "
|
11
|
-
gem.add_development_dependency "generator-spec", "
|
10
|
+
gem.add_development_dependency "rspec", ">= 2.0.0"
|
11
|
+
gem.add_development_dependency "generator-spec", ">= 0.7"
|
12
12
|
|
13
13
|
gem.add_dependency "require_all", "~> 1.2.0"
|
14
|
-
gem.add_dependency "activesupport", "
|
15
|
-
gem.add_dependency 'sugar-high', "~> 0.
|
16
|
-
gem.add_dependency 'rails3_artifactor', '~> 0.
|
14
|
+
gem.add_dependency "activesupport", ">= 3.0"
|
15
|
+
gem.add_dependency 'sugar-high', "~> 0.3.0"
|
16
|
+
gem.add_dependency 'rails3_artifactor', '~> 0.3.1'
|
17
|
+
gem.add_dependency 'logging_assist', '>= 0.1.6'
|
18
|
+
|
17
19
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
20
|
end
|
19
21
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.3
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'rails3_artifactor'
|
2
2
|
|
3
|
-
module
|
3
|
+
module RolesGeneric
|
4
4
|
module Generators
|
5
5
|
class RolesGenerator < Rails::Generators::NamedBase
|
6
6
|
include Rails3::Assist::Artifact::Model
|
@@ -12,18 +12,10 @@ module RolesModel
|
|
12
12
|
|
13
13
|
|
14
14
|
class_option :roles, :type => :array, :aliases => "-r", :default => [], :desc => "Valid roles"
|
15
|
-
# TODO: Should detect ORM from file content instead!
|
16
|
-
class_option :orm, :type => :string, :aliases => "-o", :default => nil, :desc => "ORM of model"
|
17
|
-
|
18
15
|
|
19
16
|
# hook_for :orm
|
20
17
|
|
21
|
-
def
|
22
|
-
@source_root ||= File.expand_path("../../templates", __FILE__)
|
23
|
-
end
|
24
|
-
|
25
|
-
def apply_role_strategy
|
26
|
-
self.class.use_orm orm if orm
|
18
|
+
def apply_role_strategy
|
27
19
|
insert_into_model name do
|
28
20
|
insertion_text
|
29
21
|
end
|
@@ -31,10 +23,6 @@ module RolesModel
|
|
31
23
|
|
32
24
|
protected
|
33
25
|
|
34
|
-
def orm
|
35
|
-
@orm ||= options[:orm].to_s.to_sym
|
36
|
-
end
|
37
|
-
|
38
26
|
def default_roles
|
39
27
|
[:admin, :guest]
|
40
28
|
end
|
@@ -51,14 +39,19 @@ module RolesModel
|
|
51
39
|
"strategy :#{strategy}\n"
|
52
40
|
end
|
53
41
|
|
42
|
+
def role_class_statement
|
43
|
+
[:one_role, :many_roles].include?(strategy.to_sym) ? 'role_class :role' : ''
|
44
|
+
end
|
45
|
+
|
54
46
|
def roles_statement
|
55
|
-
roles ? "
|
47
|
+
roles ? "valid_roles_are #{roles.join(',')}" : ''
|
56
48
|
end
|
57
49
|
|
58
50
|
def insertion_text
|
59
51
|
%Q{
|
60
52
|
include Roles::#{orm.to_s.camelize}
|
61
53
|
#{role_strategy_statement}
|
54
|
+
#{role_class_statement}
|
62
55
|
#{roles_statement}
|
63
56
|
}
|
64
57
|
end
|
data/lib/roles_generic/base.rb
CHANGED
@@ -1,11 +1,23 @@
|
|
1
1
|
module Roles
|
2
2
|
module Base
|
3
3
|
attr_accessor :orm_name
|
4
|
+
attr_reader :role_strategy
|
4
5
|
|
5
6
|
def valid_roles_are(*role_list)
|
6
7
|
strategy_class.valid_roles = role_list.to_symbols
|
7
8
|
end
|
8
9
|
|
10
|
+
def valid_role? role
|
11
|
+
strategy_class.valid_roles.include? role.to_sym
|
12
|
+
end
|
13
|
+
|
14
|
+
def valid_roles? *role_names
|
15
|
+
role_names.each do |role|
|
16
|
+
return false if !strategy_class.valid_roles.include? role.to_sym
|
17
|
+
end
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
9
21
|
def valid_roles
|
10
22
|
strategy_class.valid_roles
|
11
23
|
end
|
@@ -14,15 +26,41 @@ module Roles
|
|
14
26
|
strategy_class.valid_roles = Array[*roles].flatten.map { |r| r.to_sym }
|
15
27
|
end
|
16
28
|
|
17
|
-
def
|
29
|
+
def set_role_strategy strategy_name, options=nil
|
18
30
|
include_strategy orm_name, strategy_name, options
|
19
31
|
end
|
32
|
+
|
33
|
+
class RoleStrategyId
|
34
|
+
attr_accessor :name
|
35
|
+
|
36
|
+
def initialize strategy_name
|
37
|
+
@name = strategy_name.to_s.underscore.to_sym
|
38
|
+
end
|
39
|
+
|
40
|
+
def type
|
41
|
+
@type ||= case name
|
42
|
+
when :one_role, :many_roles
|
43
|
+
:complex
|
44
|
+
else
|
45
|
+
return :simple if name
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def multiplicity
|
50
|
+
@multiplicity ||= case name
|
51
|
+
when :many_roles, :role_strings, :roles_mask, :roles_string
|
52
|
+
:multi
|
53
|
+
when :one_role, :admin_flag, :role_string
|
54
|
+
:single
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
20
58
|
|
21
59
|
def include_strategy orm, strategy_name, options=nil
|
22
60
|
begin
|
23
61
|
module_name = "RoleStrategy::#{orm_name.to_s.camelize}::#{strategy_name.to_s.camelize}"
|
24
62
|
constant = module_name.constantize
|
25
|
-
|
63
|
+
@role_strategy = RoleStrategyId.new strategy_name
|
26
64
|
strategy_class_method = %Q{
|
27
65
|
def strategy_class
|
28
66
|
#{constant}
|
@@ -1,24 +1,10 @@
|
|
1
1
|
module Roles::Generic::Role
|
2
|
-
module InstanceMethods
|
2
|
+
module InstanceMethods
|
3
3
|
def role_class
|
4
4
|
self.class.role_class_name
|
5
5
|
end
|
6
|
-
|
7
|
-
def get_roles _roles
|
8
|
-
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
9
|
-
_roles = _roles.flatten.compact
|
10
|
-
_roles = _roles.select{|role| role.kind_of?(role_class) || role.kind_of_label?}
|
11
|
-
_roles.map! do |role|
|
12
|
-
case role
|
13
|
-
when role_class
|
14
|
-
role.name
|
15
|
-
else
|
16
|
-
role.to_s
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
6
|
end
|
21
|
-
|
7
|
+
|
22
8
|
module ClassMethods
|
23
9
|
def role_class_name
|
24
10
|
@role_class_name
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Roles::Generic::RoleUtil
|
2
|
+
# extract role symbols from roles
|
3
|
+
# should handle symbols, strings, arrays and Role instances
|
4
|
+
def extract_roles *roles
|
5
|
+
roles.flatten.map{|role| extract_role role}.compact
|
6
|
+
end
|
7
|
+
|
8
|
+
def extract_role role
|
9
|
+
role = case role
|
10
|
+
when Array
|
11
|
+
role.flat_uniq.first
|
12
|
+
else
|
13
|
+
role
|
14
|
+
end
|
15
|
+
|
16
|
+
if defined?(Role) && role.kind_of?(Role)
|
17
|
+
raise 'Role instances should have a #name method that reflects the role name' if !role.respond_to? :name
|
18
|
+
return role.name.to_s.to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
case role
|
22
|
+
when String, Symbol
|
23
|
+
role.to_sym
|
24
|
+
else
|
25
|
+
nil
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
extend self
|
30
|
+
end
|
@@ -1,29 +1,131 @@
|
|
1
1
|
module Roles::Generic::User
|
2
|
-
module Implementation
|
2
|
+
module Implementation
|
3
|
+
include Roles::Generic::RoleUtil
|
4
|
+
|
3
5
|
def role_attribute
|
4
6
|
strategy_class.roles_attribute_name
|
5
7
|
end
|
6
8
|
|
9
|
+
# set a single role
|
10
|
+
def role= role
|
11
|
+
raise ArgumentError, '#add_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array)
|
12
|
+
self.roles = role
|
13
|
+
end
|
14
|
+
|
15
|
+
# add a single role
|
16
|
+
def add_role role
|
17
|
+
raise ArgumentError, '#add_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array)
|
18
|
+
add_roles role
|
19
|
+
end
|
20
|
+
|
21
|
+
# remove a single role
|
22
|
+
def remove_role role
|
23
|
+
raise ArgumentError, '#remove_role takes a single role String or Symbol as the argument' if !role || role.kind_of?(Array)
|
24
|
+
remove_roles role
|
25
|
+
end
|
26
|
+
|
27
|
+
# should exchange the current role if in list with the first valid role in :with argument
|
28
|
+
def exchange_roles *role_names
|
29
|
+
options = last_option role_names
|
30
|
+
raise ArgumentError, "Must take an options hash as last argument with a :with option signifying which role(s) to replace with" if !options || !options.kind_of?(Hash)
|
31
|
+
remove_roles(role_names.to_symbols)
|
32
|
+
with_roles = options[:with]
|
33
|
+
add_roles(with_roles)
|
34
|
+
end
|
35
|
+
|
36
|
+
def exchange_role role, options = {}
|
37
|
+
raise ArgumentError, '#exchange_role takes a single role String or Symbol as the first argument' if !role || role.kind_of?(Array)
|
38
|
+
raise ArgumentError, '#exchange_role takes a an options hash with a :with option as the last argument' if !options || !options[:with]
|
39
|
+
if options[:with].kind_of?(Array) && self.class.role_strategy.multiplicity == :single
|
40
|
+
raise ArgumentError, '#exchange_role should only take a single role to exchange with for a Role strategy with multiplicity of one' if options[:with].size > 1
|
41
|
+
end
|
42
|
+
exchange_roles role, options
|
43
|
+
end
|
44
|
+
|
7
45
|
# check if a given role has been assigned
|
8
46
|
# if a list of roles: check if ALL of the given roles have been assigned
|
9
|
-
def has_roles?(*
|
10
|
-
|
47
|
+
def has_roles?(*roles_names)
|
48
|
+
compare_roles = extract_roles(roles_names.flat_uniq)
|
49
|
+
(roles_list & compare_roles).not.empty?
|
11
50
|
end
|
12
51
|
|
13
52
|
# check if any (at least ONE) of the given roles have been assigned
|
14
|
-
def has_role?
|
15
|
-
|
53
|
+
def has_role? role_name
|
54
|
+
raise ArgumentError, '#has_role? should take a single role String or Symbol as the argument' if !role_name || role_name.kind_of?(Array)
|
55
|
+
has_roles? role_name
|
16
56
|
end
|
17
57
|
|
18
58
|
def valid_role? role
|
19
59
|
strategy_class.valid_roles.include? role.to_sym
|
20
60
|
end
|
21
61
|
|
62
|
+
def valid_roles? *roles
|
63
|
+
roles.each do |role|
|
64
|
+
return false if !valid_role? role
|
65
|
+
end
|
66
|
+
true
|
67
|
+
end
|
68
|
+
|
69
|
+
def valid_roles
|
70
|
+
strategy_class.valid_roles
|
71
|
+
end
|
72
|
+
|
22
73
|
def admin?
|
23
74
|
is? :admin
|
24
75
|
end
|
76
|
+
|
77
|
+
# assign multiple roles
|
78
|
+
def roles=(*role_names)
|
79
|
+
role_names = role_names.flat_uniq
|
80
|
+
role_names = extract_roles(role_names)
|
81
|
+
return nil if role_names.empty?
|
82
|
+
set_roles(select_valid_roles role_names)
|
83
|
+
end
|
84
|
+
|
85
|
+
# query assigned roles
|
86
|
+
def roles
|
87
|
+
return [] if get_roles.nil?
|
88
|
+
x = [get_roles].flatten.map do |role|
|
89
|
+
role.respond_to?(:to_sym) ? role.to_sym : role
|
90
|
+
end
|
91
|
+
x.first.kind_of?(Set) ? x.first.to_a : x
|
92
|
+
end
|
25
93
|
|
26
94
|
alias_method :has?, :has_role?
|
27
|
-
alias_method :is?,
|
95
|
+
alias_method :is?, :has_roles?
|
96
|
+
|
97
|
+
protected
|
98
|
+
|
99
|
+
def set_role role
|
100
|
+
self.send("#{role_attribute}=", new_role(role))
|
101
|
+
end
|
102
|
+
alias_method :set_roles, :set_role
|
103
|
+
|
104
|
+
def get_role
|
105
|
+
r = self.send(role_attribute)
|
106
|
+
respond_to?(:present_role) ? present_role(r) : r
|
107
|
+
end
|
108
|
+
|
109
|
+
def get_roles
|
110
|
+
r = self.send(role_attribute)
|
111
|
+
respond_to?(:present_roles) ? present_roles(r) : r
|
112
|
+
end
|
113
|
+
|
114
|
+
def set_roles *roles
|
115
|
+
self.send("#{role_attribute}=", new_roles(roles))
|
116
|
+
end
|
117
|
+
|
118
|
+
def roles_diff *roles
|
119
|
+
self.roles_list - extract_roles(roles.flat_uniq)
|
120
|
+
end
|
121
|
+
|
122
|
+
def select_valid_roles *role_names
|
123
|
+
role_names = role_names.flat_uniq.select{|role| valid_role? role }
|
124
|
+
has_role_class? ? role_class.find_roles(role_names).to_a : role_names
|
125
|
+
end
|
126
|
+
|
127
|
+
def has_role_class?
|
128
|
+
self.respond_to?(:role_class)
|
129
|
+
end
|
28
130
|
end
|
29
131
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Roles::Generic::User
|
2
|
+
module Implementation
|
3
|
+
module Multi
|
4
|
+
def add_roles *roles
|
5
|
+
new_roles = select_valid_roles(roles)
|
6
|
+
if !new_roles.empty?
|
7
|
+
self.roles = self.roles + new_roles
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# should remove the current single role (set = nil)
|
12
|
+
# only if it is contained in the list of roles to be removed
|
13
|
+
def remove_roles *role_names
|
14
|
+
role_names = role_names.flat_uniq
|
15
|
+
set_empty_roles and return if roles_diff(role_names).empty?
|
16
|
+
roles_to_remove = select_valid_roles(role_names)
|
17
|
+
self.roles = roles_diff(role_names)
|
18
|
+
true
|
19
|
+
end
|
20
|
+
|
21
|
+
def roles_list
|
22
|
+
my_roles = [roles].flat_uniq
|
23
|
+
my_roles.empty? ? [] : my_roles
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Roles::Generic::User
|
2
|
+
module Implementation
|
3
|
+
module Single
|
4
|
+
# assigns first valid role from list of roles
|
5
|
+
def add_roles *role_names
|
6
|
+
new_roles = select_valid_roles(role_names)
|
7
|
+
self.role = new_roles.first if !new_roles.empty?
|
8
|
+
end
|
9
|
+
|
10
|
+
# should remove the current single role (set = nil)
|
11
|
+
# only if it is contained in the list of roles to be removed
|
12
|
+
def remove_roles *role_names
|
13
|
+
roles = role_names.flat_uniq
|
14
|
+
set_empty_role if roles_diff(roles).empty?
|
15
|
+
true
|
16
|
+
end
|
17
|
+
|
18
|
+
def roles_list
|
19
|
+
raise 'the method #roles should be present' if !respond_to? :roles
|
20
|
+
roles
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/roles_generic/role.rb
CHANGED
@@ -0,0 +1,22 @@
|
|
1
|
+
describe 'Roles Generic API : Completeness test' do
|
2
|
+
before :each do
|
3
|
+
default_user_setup
|
4
|
+
end
|
5
|
+
|
6
|
+
it "should be true that a User that includes Roles::Generic has a complete Roles::Generic interface" do
|
7
|
+
# mutation API
|
8
|
+
[:roles=, :role=, :add_roles, :add_role, :remove_role, :remove_roles, :exchange_roles, :exchange_role].each do |api_method|
|
9
|
+
@admin_user.respond_to?(api_method).should be_true
|
10
|
+
end
|
11
|
+
|
12
|
+
# inspection API
|
13
|
+
[:valid_role?, :valid_roles?, :has_roles?, :has_role?, :has?, :is?, :roles, :roles_list, :admin?].each do |api_method|
|
14
|
+
@admin_user.respond_to?(api_method).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
# class method API
|
18
|
+
[:valid_role?, :valid_roles?, :valid_roles].each do |class_api_method|
|
19
|
+
@admin_user.class.respond_to?(class_api_method).should be_true
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
class Role
|
2
|
+
end
|
3
|
+
|
4
|
+
describe 'Roles Generic API : READ' do
|
5
|
+
before :each do
|
6
|
+
default_user_setup
|
7
|
+
end
|
8
|
+
|
9
|
+
describe '#valid_role?' do
|
10
|
+
it "should be true that the admin user has a valid role of :guest" do
|
11
|
+
@admin_user.valid_role?(:guest).should be_true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#valid_roles' do
|
16
|
+
it "should be true that the admin user has a valid role of :guest" do
|
17
|
+
@admin_user.valid_roles.should include(:guest, :admin)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe '#valid_roles?' do
|
22
|
+
it "should be true that the admin user has a valid role of :guest" do
|
23
|
+
@admin_user.valid_roles?(:guest, :admin).should be_true
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be true that the User class has a valid role of :guest" do
|
27
|
+
User.valid_roles?(:guest, :admin).should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe '#has_role?' do
|
32
|
+
it "should have admin user role to :admin and not to :user" do
|
33
|
+
@admin_user.has_role?(:user).should be_false
|
34
|
+
@admin_user.has_role?(:admin).should be_true
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be true that guest user has role :user and not :admin" do
|
38
|
+
@guest_user.has_role?(:guest).should be_true
|
39
|
+
@guest_user.has_role?(:admin).should be_false
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#has?' do
|
44
|
+
it "should be true that the admin_user has the :admin role" do
|
45
|
+
@admin_user.has?(:admin).should be_true
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should NOT be true that the admin_user has the :admin role" do
|
49
|
+
@guest_user.has?(:admin).should be_false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
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
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should NOT be true that the user has the roles :admin" do
|
59
|
+
@guest_user.has_roles?(:admin).should be_false
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
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
|
119
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
describe 'Roles Generic API : User class' do
|
2
|
+
describe '#in_role' do
|
3
|
+
it "should return first user matching role" do
|
4
|
+
if User.respond_to? :in_role
|
5
|
+
User.in_role(:guest).first.name.should == 'Guest user'
|
6
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#in_roles' do
|
12
|
+
it "should return first user matching role" do
|
13
|
+
if User.respond_to? :in_roles
|
14
|
+
User.in_roles(:guest, :user).first.name.should == 'Guest user'
|
15
|
+
User.in_roles(:admin, :guest).should be_empty
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#valid_role?' do
|
21
|
+
it "should be true that the User class has a valid role of :guest" do
|
22
|
+
User.valid_role?(:guest).should be_true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe '#valid_roles' do
|
27
|
+
it "should be true that the User class has a valid role of :guest" do
|
28
|
+
User.valid_roles.should include(:guest, :admin)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|