roles_data_mapper 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +6 -6
- data/VERSION +1 -1
- data/lib/roles_data_mapper/base.rb +3 -3
- data/lib/roles_data_mapper/strategy/multi/many_roles.rb +51 -25
- data/lib/roles_data_mapper/strategy/multi/roles_mask.rb +70 -23
- data/lib/roles_data_mapper/strategy/multi.rb +54 -0
- data/lib/roles_data_mapper/strategy/shared.rb +26 -0
- data/lib/roles_data_mapper/strategy/single/admin_flag.rb +55 -22
- data/lib/roles_data_mapper/strategy/single/one_role.rb +34 -23
- data/lib/roles_data_mapper/strategy/single/role_string.rb +19 -18
- data/lib/roles_data_mapper/strategy/single.rb +34 -0
- data/roles_data_mapper.gemspec +35 -30
- data/spec/roles_data_mapper/strategy/api_examples.rb +212 -0
- data/spec/roles_data_mapper/strategy/multi/many_roles_spec.rb +32 -0
- data/spec/roles_data_mapper/strategy/{roles_mask_spec.rb → multi/roles_mask_spec.rb} +13 -7
- data/spec/roles_data_mapper/strategy/{admin_flag_spec.rb → single/admin_flag_spec.rb} +11 -7
- data/spec/roles_data_mapper/strategy/single/one_role_spec.rb +27 -0
- data/spec/roles_data_mapper/strategy/single/role_string_spec.rb +49 -0
- data/spec/roles_data_mapper/strategy/user_setup.rb +4 -4
- metadata +35 -31
- data/spec/roles_data_mapper/strategy/many_roles_spec.rb +0 -23
- data/spec/roles_data_mapper/strategy/one_role_spec.rb +0 -23
- data/spec/roles_data_mapper/strategy/role_string_spec.rb +0 -22
data/Rakefile
CHANGED
@@ -7,19 +7,19 @@ begin
|
|
7
7
|
gem.email = "kmandrup@gmail.com"
|
8
8
|
gem.homepage = "http://github.com/kristianmandrup/roles_for_dm"
|
9
9
|
gem.authors = ["Kristian Mandrup"]
|
10
|
-
gem.add_development_dependency "rspec", ">= 2.0.
|
10
|
+
gem.add_development_dependency "rspec", ">= 2.0.1"
|
11
11
|
|
12
12
|
gem.add_dependency "dm-core", "~> 1.0"
|
13
13
|
gem.add_dependency "dm-types", "~> 1.0"
|
14
14
|
gem.add_dependency "dm-migrations", "~> 1.0"
|
15
15
|
|
16
|
-
gem.add_dependency "activesupport", "
|
16
|
+
gem.add_dependency "activesupport", ">= 3.0.0"
|
17
17
|
gem.add_dependency "require_all", '~> 1.2.0'
|
18
|
-
gem.add_dependency "sugar-high", '
|
19
|
-
gem.add_dependency "roles_generic", '
|
18
|
+
gem.add_dependency "sugar-high", '>= 0.3.0'
|
19
|
+
gem.add_dependency "roles_generic", '>= 0.3.1'
|
20
20
|
|
21
|
-
gem.add_dependency 'rails3_artifactor', '>= 0.
|
22
|
-
gem.add_dependency 'logging_assist', '>= 0.1.
|
21
|
+
gem.add_dependency 'rails3_artifactor', '>= 0.3.1'
|
22
|
+
gem.add_dependency 'logging_assist', '>= 0.1.6'
|
23
23
|
|
24
24
|
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
25
25
|
end
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
@@ -15,8 +15,8 @@ module Roles
|
|
15
15
|
module ClassMethods
|
16
16
|
|
17
17
|
MAP = {
|
18
|
-
:admin_flag => "property :admin_flag, ::DataMapper::Property::Flag[:admin, :
|
19
|
-
:roles_mask => "property :roles_mask, Integer, :default =>
|
18
|
+
:admin_flag => "property :admin_flag, ::DataMapper::Property::Flag[:admin, :guest]",
|
19
|
+
:roles_mask => "property :roles_mask, Integer, :default => 0",
|
20
20
|
:role_string => "property :role_string, String",
|
21
21
|
:roles_string => "property :roles_string, String"
|
22
22
|
}
|
@@ -25,7 +25,7 @@ module Roles
|
|
25
25
|
if options == :default && MAP[name]
|
26
26
|
instance_eval MAP[name]
|
27
27
|
end
|
28
|
-
|
28
|
+
set_role_strategy name, options
|
29
29
|
end
|
30
30
|
end
|
31
31
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/multi'
|
2
|
+
|
1
3
|
class Role
|
2
4
|
def self.named role_names
|
3
5
|
where(:name.in => role_names.flatten)
|
@@ -41,43 +43,67 @@ module RoleStrategy::DataMapper
|
|
41
43
|
all("user_roles.role_id" => role.id)
|
42
44
|
end
|
43
45
|
|
44
|
-
def
|
46
|
+
def in_any_role(*roles)
|
45
47
|
role_ids = Role.all(:name.in => roles.flatten).map{|role| role.id}
|
46
48
|
all("user_roles.role_id" => role_ids)
|
47
49
|
end
|
48
50
|
end
|
49
51
|
|
50
|
-
module Implementation
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
self.send("#{role_attribute}=", role_relations)
|
62
|
-
self.save
|
52
|
+
module Implementation
|
53
|
+
include Roles::DataMapper::Strategy::Multi
|
54
|
+
|
55
|
+
# assign multiple roles
|
56
|
+
def roles=(*role_names)
|
57
|
+
role_names = role_names.flat_uniq
|
58
|
+
role_names = extract_roles(role_names)
|
59
|
+
return nil if role_names.empty?
|
60
|
+
valids = role_class.find_roles(role_names).to_a
|
61
|
+
vrs = select_valid_roles role_names
|
62
|
+
set_roles(vrs)
|
63
63
|
end
|
64
64
|
|
65
|
-
def
|
66
|
-
|
67
|
-
role_relations = role_class.find_roles(*roles)
|
68
|
-
role_relations.each do |rel|
|
69
|
-
self.send(role_attribute) << rel
|
70
|
-
end
|
65
|
+
def new_roles *role_names
|
66
|
+
role_class.find_roles(extract_roles role_names)
|
71
67
|
end
|
72
68
|
|
73
|
-
|
74
|
-
|
75
|
-
self.send(role_attribute)
|
69
|
+
def present_roles roles_names
|
70
|
+
roles_names.to_a.map{|role| role.name.to_s.to_sym}
|
76
71
|
end
|
77
72
|
|
78
|
-
def
|
79
|
-
|
73
|
+
def set_empty_roles
|
74
|
+
self.send("#{role_attribute}=", [])
|
80
75
|
end
|
76
|
+
#
|
77
|
+
# def role_attribute
|
78
|
+
# strategy_class.roles_attribute_name
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# # assign roles
|
82
|
+
# def roles=(*_roles)
|
83
|
+
# _roles = get_roles(_roles)
|
84
|
+
# return nil if roles.none?
|
85
|
+
#
|
86
|
+
# role_relations = role_class.find_roles(_roles)
|
87
|
+
# self.send("#{role_attribute}=", role_relations)
|
88
|
+
# self.save
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# def add_roles(*roles)
|
92
|
+
# raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
93
|
+
# role_relations = role_class.find_roles(*roles)
|
94
|
+
# role_relations.each do |rel|
|
95
|
+
# self.send(role_attribute) << rel
|
96
|
+
# end
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# # query assigned roles
|
100
|
+
# def roles
|
101
|
+
# self.send(role_attribute)
|
102
|
+
# end
|
103
|
+
#
|
104
|
+
# def roles_list
|
105
|
+
# [roles].flatten.map{|r| r.name }.compact.to_symbols
|
106
|
+
# end
|
81
107
|
end
|
82
108
|
|
83
109
|
extend Roles::Generic::User::Configuration
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/multi'
|
2
|
+
|
1
3
|
module RoleStrategy::DataMapper
|
2
4
|
module RolesMask
|
3
5
|
def self.default_role_attribute
|
@@ -20,21 +22,27 @@ module RoleStrategy::DataMapper
|
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
|
-
def
|
25
|
+
def in_any_role(*roles)
|
24
26
|
all.select do |user|
|
25
27
|
roles.flatten.any? do |role|
|
26
28
|
mask = calc_index(role.to_s)
|
27
|
-
|
29
|
+
if mask
|
30
|
+
(user.send(role_attribute) & mask) > 0
|
31
|
+
end
|
28
32
|
end
|
29
33
|
end
|
30
34
|
end
|
31
35
|
|
32
36
|
def calc_index(r)
|
33
|
-
|
37
|
+
idx = strategy_class.valid_roles.index(r.to_sym)
|
38
|
+
return nil if !idx
|
39
|
+
2**idx
|
34
40
|
end
|
35
41
|
end
|
36
42
|
|
37
|
-
module Implementation
|
43
|
+
module Implementation
|
44
|
+
include Roles::DataMapper::Strategy::Multi
|
45
|
+
|
38
46
|
class Roles < ::Set # :nodoc:
|
39
47
|
attr_reader :model_instance
|
40
48
|
|
@@ -49,31 +57,70 @@ module RoleStrategy::DataMapper
|
|
49
57
|
end
|
50
58
|
end
|
51
59
|
|
52
|
-
|
53
|
-
|
54
|
-
|
60
|
+
protected
|
61
|
+
|
62
|
+
def calc_index(r)
|
63
|
+
self.class.calc_index(r)
|
64
|
+
end
|
55
65
|
|
56
|
-
|
57
|
-
|
58
|
-
self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
|
66
|
+
def get_roles
|
67
|
+
strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })
|
59
68
|
end
|
60
|
-
alias_method :role=, :roles=
|
61
69
|
|
62
|
-
|
63
|
-
|
64
|
-
|
70
|
+
def new_roles *role_names
|
71
|
+
role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
|
72
|
+
role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
|
65
73
|
end
|
66
74
|
|
67
|
-
def
|
68
|
-
|
69
|
-
end
|
70
|
-
|
71
|
-
protected
|
72
|
-
|
73
|
-
def calc_index(r)
|
74
|
-
2**strategy_class.valid_roles.index(r)
|
75
|
+
def set_empty_roles
|
76
|
+
self.send("#{role_attribute}=", 0)
|
75
77
|
end
|
76
|
-
|
78
|
+
|
79
|
+
def present_roles *role_names
|
80
|
+
role_names.to_a.to_symbols
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# module Implementation
|
85
|
+
# class Roles < ::Set # :nodoc:
|
86
|
+
# attr_reader :model_instance
|
87
|
+
#
|
88
|
+
# def initialize(sender, *roles)
|
89
|
+
# super(*roles)
|
90
|
+
# @model_instance = sender
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# def <<(role)
|
94
|
+
# model_instance.roles = super.to_a
|
95
|
+
# self
|
96
|
+
# end
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# def role_attribute
|
100
|
+
# strategy_class.roles_attribute_name
|
101
|
+
# end
|
102
|
+
#
|
103
|
+
# # assign roles
|
104
|
+
# def roles=(*roles)
|
105
|
+
# self.send("#{role_attribute}=", (roles.flatten.map { |r| r.to_sym } & strategy_class.valid_roles).map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue })
|
106
|
+
# end
|
107
|
+
# alias_method :role=, :roles=
|
108
|
+
#
|
109
|
+
# # query assigned roles
|
110
|
+
# def roles
|
111
|
+
# strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# def roles_list
|
115
|
+
# roles.to_a
|
116
|
+
# end
|
117
|
+
#
|
118
|
+
# protected
|
119
|
+
#
|
120
|
+
# def calc_index(r)
|
121
|
+
# 2**strategy_class.valid_roles.index(r)
|
122
|
+
# end
|
123
|
+
# end
|
77
124
|
|
78
125
|
extend Roles::Generic::User::Configuration
|
79
126
|
configure
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/shared'
|
2
|
+
|
3
|
+
module Roles::DataMapper
|
4
|
+
module Strategy
|
5
|
+
module Multi
|
6
|
+
# assign multiple roles
|
7
|
+
def roles=(*role_names)
|
8
|
+
extracted_roles = extract_roles(role_names)
|
9
|
+
return nil if extracted_roles.empty?
|
10
|
+
set_roles(select_valid_roles extracted_roles)
|
11
|
+
end
|
12
|
+
|
13
|
+
def add_roles *roles
|
14
|
+
new_roles = select_valid_roles(roles)
|
15
|
+
if !new_roles.empty?
|
16
|
+
self.roles = self.roles + new_roles
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# should remove the current single role (set = nil)
|
21
|
+
# only if it is contained in the list of roles to be removed
|
22
|
+
def remove_roles *role_names
|
23
|
+
role_names = role_names.flat_uniq
|
24
|
+
set_empty_roles and return if roles_diff(role_names).empty?
|
25
|
+
roles_to_remove = select_valid_roles(role_names)
|
26
|
+
set_roles roles_diff(role_names)
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
# query assigned roles
|
31
|
+
def roles
|
32
|
+
get_roles.map do |role|
|
33
|
+
role.respond_to?(:sym) ? role.to_sym : role
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def roles_list
|
38
|
+
my_roles = [roles].flat_uniq
|
39
|
+
return [] if my_roles.empty?
|
40
|
+
has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
|
41
|
+
end
|
42
|
+
|
43
|
+
protected
|
44
|
+
|
45
|
+
def set_roles *roles
|
46
|
+
self.send("#{role_attribute}=", new_roles(roles))
|
47
|
+
end
|
48
|
+
|
49
|
+
def get_roles
|
50
|
+
self.send(role_attribute)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Roles::DataMapper
|
2
|
+
module Strategy
|
3
|
+
module Shared
|
4
|
+
def set_role role
|
5
|
+
vr = new_role(role)
|
6
|
+
self.send("#{role_attribute}=", vr)
|
7
|
+
# update_attributes(role_attribute => vr)
|
8
|
+
end
|
9
|
+
alias_method :set_roles, :set_role
|
10
|
+
|
11
|
+
def get_role
|
12
|
+
r = self.send(role_attribute)
|
13
|
+
# respond_to?(:present_role) ? present_role(r) : r
|
14
|
+
end
|
15
|
+
|
16
|
+
def get_roles
|
17
|
+
r = self.send(role_attribute)
|
18
|
+
end
|
19
|
+
|
20
|
+
def select_valid_roles *roles
|
21
|
+
roles.flat_uniq.select{|role| valid_role? role }
|
22
|
+
has_role_class? ? role_class.find_roles(roles).to_a.flat_uniq : roles.flat_uniq
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
require 'roles_data_mapper/strategy/single'
|
2
2
|
|
3
3
|
module RoleStrategy::DataMapper
|
4
4
|
module AdminFlag
|
@@ -15,40 +15,73 @@ module RoleStrategy::DataMapper
|
|
15
15
|
strategy_class.roles_attribute_name.to_sym
|
16
16
|
end
|
17
17
|
|
18
|
-
def in_role(role_name)
|
18
|
+
def in_role(role_name)
|
19
|
+
puts "in role: #{role_name}"
|
19
20
|
case role_name.downcase.to_sym
|
20
21
|
when :admin
|
21
22
|
all(role_attribute => :admin)
|
22
23
|
else
|
23
|
-
all(role_attribute => :
|
24
|
+
all(role_attribute => :guest)
|
24
25
|
end
|
25
26
|
end
|
27
|
+
|
28
|
+
def in_any_role(*role_names)
|
29
|
+
role_names = role_names.flatten
|
30
|
+
all(role_attribute => role_names)
|
31
|
+
end
|
26
32
|
end
|
27
33
|
|
28
34
|
module Implementation
|
29
|
-
|
30
|
-
strategy_class.roles_attribute_name
|
31
|
-
end
|
32
|
-
|
33
|
-
# assign roles
|
34
|
-
def roles=(*new_roles)
|
35
|
-
first_role = new_roles.flatten.first
|
36
|
-
if valid_role?(first_role)
|
37
|
-
value = first_role.admin? ? :admin : :default
|
38
|
-
self.send("#{role_attribute}=", value)
|
39
|
-
else
|
40
|
-
raise ArgumentError, "The role #{first_role} is not a valid role"
|
41
|
-
end
|
42
|
-
end
|
35
|
+
include Roles::DataMapper::Strategy::Single
|
43
36
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
[role]
|
37
|
+
def has_roles?(*roles_names)
|
38
|
+
compare_roles = extract_roles(roles_names.flat_uniq)
|
39
|
+
(roles_list & compare_roles).not.empty?
|
48
40
|
end
|
49
|
-
alias_method :roles_list, :roles
|
50
41
|
|
42
|
+
def new_role role
|
43
|
+
role = role.kind_of?(Array) ? role.flatten.first : role
|
44
|
+
role.to_sym
|
45
|
+
# role.admin?
|
46
|
+
end
|
47
|
+
|
48
|
+
def get_role
|
49
|
+
self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
50
|
+
end
|
51
|
+
|
52
|
+
def present_roles *roles
|
53
|
+
roles = roles.flat_uniq
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_empty_role
|
57
|
+
self.send("#{role_attribute}=", :guest)
|
58
|
+
end
|
51
59
|
end # Implementation
|
60
|
+
#
|
61
|
+
# module Implementation
|
62
|
+
# def role_attribute
|
63
|
+
# strategy_class.roles_attribute_name
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# # assign roles
|
67
|
+
# def roles=(*new_roles)
|
68
|
+
# first_role = new_roles.flatten.first
|
69
|
+
# if valid_role?(first_role)
|
70
|
+
# value = first_role.admin? ? :admin : :default
|
71
|
+
# self.send("#{role_attribute}=", value)
|
72
|
+
# else
|
73
|
+
# raise ArgumentError, "The role #{first_role} is not a valid role"
|
74
|
+
# end
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# # query assigned roles
|
78
|
+
# def roles
|
79
|
+
# role = self.send(role_attribute).include?(:admin) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
80
|
+
# [role]
|
81
|
+
# end
|
82
|
+
# alias_method :roles_list, :roles
|
83
|
+
#
|
84
|
+
# end # Implementation
|
52
85
|
|
53
86
|
extend Roles::Generic::User::Configuration
|
54
87
|
configure :num => :single
|
@@ -1,9 +1,12 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/single'
|
2
|
+
|
1
3
|
class Role
|
2
4
|
def self.named role_names
|
3
5
|
where(:name.in => role_names.flatten)
|
4
6
|
end
|
5
7
|
|
6
|
-
belongs_to :user, :required => false
|
8
|
+
# belongs_to :user, :required => false
|
9
|
+
# has n, :users, :child_key => [ :one_role ]
|
7
10
|
end
|
8
11
|
|
9
12
|
module RoleStrategy::DataMapper
|
@@ -15,43 +18,51 @@ module RoleStrategy::DataMapper
|
|
15
18
|
def self.included base
|
16
19
|
base.extend Roles::Generic::Role::ClassMethods
|
17
20
|
base.extend ClassMethods
|
18
|
-
base.instance_eval "has 1, :one_role, 'Role'" # , :parent_key => [role_id], :child_key => [:id]
|
21
|
+
# base.instance_eval "has 1, :one_role, 'Role'" # , :parent_key => [role_id], :child_key => [:id]
|
22
|
+
base.instance_eval "belongs_to :one_role, :class_name => 'Role'" # :foreign_key => :role_id
|
19
23
|
end
|
20
24
|
|
21
25
|
module ClassMethods
|
22
|
-
def role_attribute
|
23
|
-
|
24
|
-
end
|
26
|
+
# def role_attribute
|
27
|
+
# strategy_class.roles_attribute_name.to_sym
|
28
|
+
# end
|
25
29
|
|
26
30
|
def in_role(role_name)
|
27
31
|
role = Role.all(:name => role_name).first
|
28
|
-
|
32
|
+
# puts "in role: #{role.inspect}, id: #{role.id}"
|
33
|
+
all(:one_role_id => role.id)
|
29
34
|
end
|
30
35
|
|
31
|
-
def
|
36
|
+
def in_any_role(*roles)
|
32
37
|
role_ids = Role.all(:name.in => roles.flatten).map{|role| role.id}
|
33
|
-
all(:
|
38
|
+
all(:one_role_id => role_ids)
|
34
39
|
end
|
35
40
|
end
|
36
41
|
|
37
|
-
module Implementation
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
+
module Implementation
|
43
|
+
include Roles::DataMapper::Strategy::Single
|
44
|
+
|
45
|
+
def new_role role
|
46
|
+
role_class.find_role(extract_role role)
|
47
|
+
end
|
42
48
|
|
43
|
-
|
44
|
-
|
45
|
-
save
|
49
|
+
def new_roles *roles
|
50
|
+
new_role roles.flatten.first
|
46
51
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
52
|
+
|
53
|
+
def remove_roles *role_names
|
54
|
+
roles = role_names.flat_uniq
|
55
|
+
set_empty_role if roles_diff(roles).empty?
|
56
|
+
true
|
57
|
+
end
|
58
|
+
|
59
|
+
def present_roles *roles
|
60
|
+
roles.map{|role| extract_role role}
|
61
|
+
end
|
62
|
+
|
63
|
+
def set_empty_role
|
64
|
+
self.send("#{role_attribute}=", nil)
|
53
65
|
end
|
54
|
-
alias_method :roles_list, :roles
|
55
66
|
end
|
56
67
|
|
57
68
|
extend Roles::Generic::User::Configuration
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/single'
|
2
|
+
|
1
3
|
module RoleStrategy::DataMapper
|
2
4
|
module RoleString
|
3
5
|
def self.default_role_attribute
|
@@ -18,29 +20,28 @@ module RoleStrategy::DataMapper
|
|
18
20
|
end
|
19
21
|
end
|
20
22
|
|
21
|
-
|
22
23
|
module Implementation
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
24
|
+
include Roles::DataMapper::Strategy::Single
|
25
|
+
|
26
|
+
def new_role role
|
27
|
+
role = role.kind_of?(Array) ? role.first : role
|
28
|
+
role.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def new_roles *roles
|
32
|
+
new_role roles.flatten.first
|
33
|
+
end
|
31
34
|
|
32
|
-
def role
|
33
|
-
if
|
34
|
-
|
35
|
-
end
|
35
|
+
def present_role role
|
36
|
+
return role if role.kind_of? Array
|
37
|
+
role.split(',').map(&:to_sym)
|
36
38
|
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
role = self.send(role_attribute)
|
41
|
-
[role.to_sym]
|
40
|
+
def set_empty_role
|
41
|
+
self.send("#{role_attribute}=", "")
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
|
+
alias_method :present_roles, :present_role
|
44
45
|
end
|
45
46
|
|
46
47
|
extend Roles::Generic::User::Configuration
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'roles_data_mapper/strategy/shared'
|
2
|
+
|
3
|
+
class Symbol
|
4
|
+
def admin?
|
5
|
+
self == :admin
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
module Roles::DataMapper
|
10
|
+
module Strategy
|
11
|
+
module Single
|
12
|
+
include Shared
|
13
|
+
# assigns first valid role from list of roles
|
14
|
+
def add_roles *role_names
|
15
|
+
new_roles = select_valid_roles(role_names)
|
16
|
+
new_role = new_roles.first if !new_roles.empty?
|
17
|
+
set_role new_role
|
18
|
+
end
|
19
|
+
|
20
|
+
# should remove the current single role (set = nil)
|
21
|
+
# only if it is contained in the list of roles to be removed
|
22
|
+
def remove_roles *role_names
|
23
|
+
roles = role_names.flat_uniq
|
24
|
+
set_empty_role if roles_diff(roles).empty?
|
25
|
+
true
|
26
|
+
end
|
27
|
+
|
28
|
+
def roles_list
|
29
|
+
raise 'the method #roles should be present' if !respond_to? :roles
|
30
|
+
present_roles(roles)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|