roles_for_mongoid 0.1.1 → 0.2.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.
- data/.rspec +1 -0
- data/README.markdown +14 -109
- data/Rakefile +5 -1
- data/VERSION +1 -1
- data/lib/generators/mongoid/roles/roles_generator.rb +48 -25
- data/lib/roles_mongoid.rb +9 -0
- data/lib/roles_mongoid/base.rb +35 -0
- data/lib/roles_mongoid/namespaces.rb +12 -0
- data/lib/roles_mongoid/role.rb +28 -0
- data/lib/roles_mongoid/strategy.rb +15 -0
- data/lib/roles_mongoid/strategy/multi/many_roles.rb +65 -0
- data/lib/roles_mongoid/strategy/multi/role_strings.rb +63 -0
- data/lib/roles_mongoid/strategy/multi/roles_mask.rb +81 -0
- data/lib/roles_mongoid/strategy/single/admin_flag.rb +53 -0
- data/lib/roles_mongoid/strategy/single/one_role.rb +56 -0
- data/lib/roles_mongoid/strategy/single/role_string.rb +51 -0
- data/roles_for_mongoid.gemspec +43 -24
- data/sandbox/roles_assign.rb +54 -0
- data/sandbox/single_role.rb +68 -0
- data/spec/roles_mongoid/admin_flag_spec.rb +74 -0
- data/spec/roles_mongoid/many_roles_spec.rb +74 -0
- data/spec/roles_mongoid/one_role_spec.rb +74 -0
- data/spec/roles_mongoid/role_string_spec.rb +73 -0
- data/spec/roles_mongoid/role_strings_spec.rb +73 -0
- data/spec/roles_mongoid/roles_mask_spec.rb +73 -0
- data/spec/spec_helper.rb +17 -7
- metadata +94 -25
- data/lib/roles_for_mongoid.rb +0 -1
- data/lib/roles_for_mongoid/role.rb +0 -4
- data/lib/roles_for_mongoid/strategies/admin_flag.rb +0 -17
- data/lib/roles_for_mongoid/strategies/inline_role.rb +0 -6
- data/lib/roles_for_mongoid/strategies/inline_roles.rb +0 -13
- data/lib/roles_for_mongoid/strategies/role_relations.rb +0 -34
- data/lib/roles_for_mongoid/strategies/roles_mask.rb +0 -52
- data/lib/roles_for_mongoid/user.rb +0 -12
- data/spec/roles_for_mongoid/user_admin_field_spec.rb +0 -24
- data/spec/roles_for_mongoid/user_inline_role_spec.rb +0 -23
- data/spec/roles_for_mongoid/user_inline_roles_spec.rb +0 -27
- data/spec/roles_for_mongoid/user_role_relations_spec.rb +0 -37
- data/spec/roles_for_mongoid/user_roles_mask_spec.rb +0 -29
- data/spec/spec.opts +0 -1
@@ -0,0 +1,81 @@
|
|
1
|
+
module RoleStrategy::Mongoid
|
2
|
+
module RolesMask
|
3
|
+
def self.default_role_attribute
|
4
|
+
:roles_mask
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def role_attribute
|
13
|
+
strategy_class.roles_attribute_name.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_role(role)
|
17
|
+
mask = calc_index(role.to_s)
|
18
|
+
all.select do |user|
|
19
|
+
(user.send(role_attribute) & mask) > 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def in_roles(*roles)
|
24
|
+
all.select do |user|
|
25
|
+
roles.flatten.any? do |role|
|
26
|
+
mask = calc_index(role.to_s)
|
27
|
+
(user.send(role_attribute) & mask) > 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def calc_index(r)
|
33
|
+
2**strategy_class.valid_roles.index(r.to_sym)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Implementation
|
38
|
+
class Roles < ::Set # :nodoc:
|
39
|
+
attr_reader :model_instance
|
40
|
+
|
41
|
+
def initialize(sender, *roles)
|
42
|
+
super(*roles)
|
43
|
+
@model_instance = sender
|
44
|
+
end
|
45
|
+
|
46
|
+
def <<(role)
|
47
|
+
model_instance.roles = super.to_a
|
48
|
+
self
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def role_attribute
|
53
|
+
strategy_class.roles_attribute_name
|
54
|
+
end
|
55
|
+
|
56
|
+
# assign roles
|
57
|
+
def roles=(*roles)
|
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 })
|
59
|
+
end
|
60
|
+
alias_method :role=, :roles=
|
61
|
+
|
62
|
+
# query assigned roles
|
63
|
+
def roles
|
64
|
+
strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
|
65
|
+
end
|
66
|
+
|
67
|
+
def roles_list
|
68
|
+
roles.to_a
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def calc_index(r)
|
74
|
+
2**strategy_class.valid_roles.index(r)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
extend Roles::Generic::User::Configuration
|
79
|
+
configure
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module RoleStrategy::Mongoid
|
2
|
+
module AdminFlag
|
3
|
+
def self.default_role_attribute
|
4
|
+
:admin_flag
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def role_attribute
|
13
|
+
strategy_class.roles_attribute_name.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_role(role_name)
|
17
|
+
case role_name.downcase.to_sym
|
18
|
+
when :admin
|
19
|
+
where(role_attribute => true)
|
20
|
+
else
|
21
|
+
where(role_attribute => false)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Implementation
|
27
|
+
def role_attribute
|
28
|
+
strategy_class.roles_attribute_name
|
29
|
+
end
|
30
|
+
|
31
|
+
# assign roles
|
32
|
+
def roles=(*new_roles)
|
33
|
+
first_role = new_roles.flatten.first
|
34
|
+
if valid_role?(first_role)
|
35
|
+
self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
|
36
|
+
else
|
37
|
+
raise ArgumentError, "The role #{first_role} is not a valid role"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# query assigned roles
|
42
|
+
def roles
|
43
|
+
role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
44
|
+
[role]
|
45
|
+
end
|
46
|
+
alias_method :roles_list, :roles
|
47
|
+
|
48
|
+
end # Implementation
|
49
|
+
|
50
|
+
extend Roles::Generic::User::Configuration
|
51
|
+
configure :num => :single
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
module RoleStrategy::Mongoid
|
2
|
+
module OneRole
|
3
|
+
def self.default_role_attribute
|
4
|
+
:one_role
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend Roles::Generic::Role::ClassMethods
|
9
|
+
base.extend ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def role_attribute
|
14
|
+
strategy_class.roles_attribute_name.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def in_role(role_name)
|
18
|
+
in_roles(role_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def in_roles(*role_names)
|
22
|
+
begin
|
23
|
+
role_ids = Role.where(:name.in => role_names.to_strings).to_a.map(&:user_id)
|
24
|
+
where(:_id.in => role_ids)
|
25
|
+
rescue
|
26
|
+
return []
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
module Implementation
|
32
|
+
# assign roles
|
33
|
+
def roles=(*roles)
|
34
|
+
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
35
|
+
first_role = roles.flatten.first
|
36
|
+
role_relation = role_class.find_role(first_role)
|
37
|
+
self.send("#{role_attribute}=", role_relation)
|
38
|
+
one_role.save
|
39
|
+
end
|
40
|
+
alias_method :role=, :roles=
|
41
|
+
|
42
|
+
# query assigned roles
|
43
|
+
def roles
|
44
|
+
role = self.send(role_attribute).name.to_sym
|
45
|
+
[role]
|
46
|
+
end
|
47
|
+
|
48
|
+
def roles_list
|
49
|
+
self.roles.to_a
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
extend Roles::Generic::User::Configuration
|
54
|
+
configure :num => :single, :type => :role_class
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RoleStrategy::Mongoid
|
2
|
+
module RoleString
|
3
|
+
def self.default_role_attribute
|
4
|
+
:role_string
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def role_attribute
|
13
|
+
strategy_class.roles_attribute_name.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_role(role_name)
|
17
|
+
where(role_attribute => role_name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
module Implementation
|
23
|
+
def role_attribute
|
24
|
+
strategy_class.roles_attribute_name
|
25
|
+
end
|
26
|
+
|
27
|
+
# assign roles
|
28
|
+
def roles=(*roles)
|
29
|
+
self.role = roles.select_labels.first.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def role= role_name
|
33
|
+
if role_name.kind_of_label? && valid_role?(role_name)
|
34
|
+
self.send("#{role_attribute}=", role_name.to_s)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# query assigned roles
|
39
|
+
def roles
|
40
|
+
role = self.send(role_attribute)
|
41
|
+
[role.to_sym]
|
42
|
+
end
|
43
|
+
alias_method :roles_list, :roles
|
44
|
+
end
|
45
|
+
|
46
|
+
extend Roles::Generic::User::Configuration
|
47
|
+
configure :num => :single
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
data/roles_for_mongoid.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{roles_for_mongoid}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
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
|
+
s.date = %q{2010-09-06}
|
13
13
|
s.description = %q{Facilitates adding Roles to your Mongoid user models}
|
14
14
|
s.email = %q{kmandrup@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -19,27 +19,33 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
".rspec",
|
22
23
|
"LICENSE",
|
23
24
|
"README.markdown",
|
24
25
|
"Rakefile",
|
25
26
|
"VERSION",
|
26
27
|
"lib/generators/mongoid/roles/roles_generator.rb",
|
27
|
-
"lib/
|
28
|
-
"lib/
|
29
|
-
"lib/
|
30
|
-
"lib/
|
31
|
-
"lib/
|
32
|
-
"lib/
|
33
|
-
"lib/
|
34
|
-
"lib/
|
28
|
+
"lib/roles_mongoid.rb",
|
29
|
+
"lib/roles_mongoid/base.rb",
|
30
|
+
"lib/roles_mongoid/namespaces.rb",
|
31
|
+
"lib/roles_mongoid/role.rb",
|
32
|
+
"lib/roles_mongoid/strategy.rb",
|
33
|
+
"lib/roles_mongoid/strategy/multi/many_roles.rb",
|
34
|
+
"lib/roles_mongoid/strategy/multi/role_strings.rb",
|
35
|
+
"lib/roles_mongoid/strategy/multi/roles_mask.rb",
|
36
|
+
"lib/roles_mongoid/strategy/single/admin_flag.rb",
|
37
|
+
"lib/roles_mongoid/strategy/single/one_role.rb",
|
38
|
+
"lib/roles_mongoid/strategy/single/role_string.rb",
|
35
39
|
"roles_for_mongoid.gemspec",
|
40
|
+
"sandbox/roles_assign.rb",
|
41
|
+
"sandbox/single_role.rb",
|
36
42
|
"sandbox/test.rb",
|
37
|
-
"spec/
|
38
|
-
"spec/
|
39
|
-
"spec/
|
40
|
-
"spec/
|
41
|
-
"spec/
|
42
|
-
"spec/
|
43
|
+
"spec/roles_mongoid/admin_flag_spec.rb",
|
44
|
+
"spec/roles_mongoid/many_roles_spec.rb",
|
45
|
+
"spec/roles_mongoid/one_role_spec.rb",
|
46
|
+
"spec/roles_mongoid/role_string_spec.rb",
|
47
|
+
"spec/roles_mongoid/role_strings_spec.rb",
|
48
|
+
"spec/roles_mongoid/roles_mask_spec.rb",
|
43
49
|
"spec/spec_helper.rb"
|
44
50
|
]
|
45
51
|
s.homepage = %q{http://github.com/kristianmandrup/roles_for_mm}
|
@@ -48,11 +54,12 @@ Gem::Specification.new do |s|
|
|
48
54
|
s.rubygems_version = %q{1.3.7}
|
49
55
|
s.summary = %q{Facilitates adding Roles to your Mongoid user models}
|
50
56
|
s.test_files = [
|
51
|
-
"spec/
|
52
|
-
"spec/
|
53
|
-
"spec/
|
54
|
-
"spec/
|
55
|
-
"spec/
|
57
|
+
"spec/roles_mongoid/admin_flag_spec.rb",
|
58
|
+
"spec/roles_mongoid/many_roles_spec.rb",
|
59
|
+
"spec/roles_mongoid/one_role_spec.rb",
|
60
|
+
"spec/roles_mongoid/role_string_spec.rb",
|
61
|
+
"spec/roles_mongoid/role_strings_spec.rb",
|
62
|
+
"spec/roles_mongoid/roles_mask_spec.rb",
|
56
63
|
"spec/spec_helper.rb"
|
57
64
|
]
|
58
65
|
|
@@ -61,12 +68,24 @@ Gem::Specification.new do |s|
|
|
61
68
|
s.specification_version = 3
|
62
69
|
|
63
70
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
64
|
-
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.
|
71
|
+
s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
72
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.0.beta.17"])
|
73
|
+
s.add_runtime_dependency(%q<require_all>, ["~> 1.1.0"])
|
74
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
75
|
+
s.add_runtime_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
65
76
|
else
|
66
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.
|
77
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
78
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.17"])
|
79
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
80
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
81
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
67
82
|
end
|
68
83
|
else
|
69
|
-
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.
|
84
|
+
s.add_dependency(%q<rspec>, [">= 2.0.0.beta.19"])
|
85
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.17"])
|
86
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
87
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.3"])
|
88
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.2"])
|
70
89
|
end
|
71
90
|
end
|
72
91
|
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
Mongoid.configure do |config|
|
4
|
+
config.master = Mongo::Connection.new.db('testing')
|
5
|
+
end
|
6
|
+
|
7
|
+
Mongoid.database.collections.each do |coll|
|
8
|
+
coll.remove
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
class Role
|
13
|
+
include Mongoid::Document
|
14
|
+
field :name, :type => String
|
15
|
+
|
16
|
+
class << self
|
17
|
+
def find_roles(*role_names)
|
18
|
+
where(:name.in => role_names.flatten).to_a #.map(&:_id)
|
19
|
+
end
|
20
|
+
alias_method :find_role, :find_roles
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Blip
|
25
|
+
def self.included(base)
|
26
|
+
base.extend ClassMethods
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def do_it
|
31
|
+
instance_eval "references_many :many_roles, :stored_as => :array, :class_name => 'Role'"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class User
|
37
|
+
include Mongoid::Document
|
38
|
+
include Blip
|
39
|
+
field :name, :type => String
|
40
|
+
|
41
|
+
do_it
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
Role.create :name => 'guest'
|
46
|
+
Role.create :name => 'admin'
|
47
|
+
|
48
|
+
user = User.create :name => 'Kristian'
|
49
|
+
|
50
|
+
user.many_roles << Role.find_role(:guest)
|
51
|
+
user.many_roles << Role.find_role(:admin)
|
52
|
+
|
53
|
+
puts user.many_roles.inspect
|
54
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
|
3
|
+
Mongoid.configure do |config|
|
4
|
+
config.master = Mongo::Connection.new.db('testing')
|
5
|
+
end
|
6
|
+
|
7
|
+
Mongoid.database.collections.each do |coll|
|
8
|
+
coll.remove
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
class Role
|
13
|
+
include Mongoid::Document
|
14
|
+
field :name, :type => String
|
15
|
+
referenced_in :user
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def find_roles(*role_names)
|
19
|
+
where(:name.in => role_names.flatten).to_a #.map(&:_id)
|
20
|
+
end
|
21
|
+
alias_method :find_role, :find_roles
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module Blip
|
26
|
+
def self.included(base)
|
27
|
+
base.extend ClassMethods
|
28
|
+
end
|
29
|
+
|
30
|
+
module ClassMethods
|
31
|
+
def do_it
|
32
|
+
instance_eval "references_one :role" # :class_name => 'Role'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class User
|
38
|
+
include Mongoid::Document
|
39
|
+
include Blip
|
40
|
+
field :name, :type => String
|
41
|
+
references_one :role
|
42
|
+
# do_it
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
user = User.create
|
47
|
+
role = Role.create
|
48
|
+
|
49
|
+
puts "role: #{role.inspect}"
|
50
|
+
user.role = role
|
51
|
+
|
52
|
+
user.save
|
53
|
+
|
54
|
+
# Role.create :name => 'guest'
|
55
|
+
# Role.create :name => 'admin'
|
56
|
+
#
|
57
|
+
# user = User.create :name => 'Kristian'
|
58
|
+
#
|
59
|
+
# puts user.inspect
|
60
|
+
#
|
61
|
+
# user.role.create :name => :guest
|
62
|
+
# user.save
|
63
|
+
|
64
|
+
# user.role = Role.find_role(:guest).first
|
65
|
+
# user.save
|
66
|
+
|
67
|
+
puts user.role.inspect
|
68
|
+
|