mongoid_roles_zhd 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/.bundle/config +2 -0
  2. data/.document +5 -0
  3. data/.rspec +1 -0
  4. data/Gemfile +17 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +99 -0
  7. data/Rakefile +26 -0
  8. data/VERSION +1 -0
  9. data/lib/generators/mongoid/roles/core_ext.rb +11 -0
  10. data/lib/generators/mongoid/roles/roles_generator.rb +173 -0
  11. data/lib/generators/mongoid/roles/templates/role.rb +8 -0
  12. data/lib/mongoid_roles.rb +10 -0
  13. data/lib/mongoid_roles/base.rb +91 -0
  14. data/lib/mongoid_roles/base_role.rb +27 -0
  15. data/lib/mongoid_roles/embedded_role.rb +24 -0
  16. data/lib/mongoid_roles/namespaces.rb +12 -0
  17. data/lib/mongoid_roles/role.rb +10 -0
  18. data/lib/mongoid_roles/strategy.rb +15 -0
  19. data/lib/mongoid_roles/strategy/multi.rb +59 -0
  20. data/lib/mongoid_roles/strategy/multi/embed_many_roles.rb +124 -0
  21. data/lib/mongoid_roles/strategy/multi/many_roles.rb +83 -0
  22. data/lib/mongoid_roles/strategy/multi/role_strings.rb +54 -0
  23. data/lib/mongoid_roles/strategy/multi/roles_mask.rb +130 -0
  24. data/lib/mongoid_roles/strategy/shared.rb +29 -0
  25. data/lib/mongoid_roles/strategy/single.rb +34 -0
  26. data/lib/mongoid_roles/strategy/single/admin_flag.rb +55 -0
  27. data/lib/mongoid_roles/strategy/single/embed_one_role.rb +83 -0
  28. data/lib/mongoid_roles/strategy/single/one_role.rb +66 -0
  29. data/lib/mongoid_roles/strategy/single/role_string.rb +61 -0
  30. data/mongoid_roles.gemspec +125 -0
  31. data/sandbox/roles_assign.rb +54 -0
  32. data/sandbox/single_role.rb +82 -0
  33. data/sandbox/test.rb +47 -0
  34. data/sandbox/test_query.rb +49 -0
  35. data/spec/generator_spec_helper.rb +12 -0
  36. data/spec/mongoid_roles/generators/roles_generator_spec.rb +67 -0
  37. data/spec/mongoid_roles/strategy/api_examples.rb +200 -0
  38. data/spec/mongoid_roles/strategy/multi/embed_many_roles_spec.rb +15 -0
  39. data/spec/mongoid_roles/strategy/multi/many_roles_old_spec.rb +22 -0
  40. data/spec/mongoid_roles/strategy/multi/many_roles_spec.rb +19 -0
  41. data/spec/mongoid_roles/strategy/multi/role_strings_spec.rb +16 -0
  42. data/spec/mongoid_roles/strategy/multi/roles_mask_spec.rb +20 -0
  43. data/spec/mongoid_roles/strategy/single/admin_flag_spec.rb +18 -0
  44. data/spec/mongoid_roles/strategy/single/embed_one_role_spec.rb +18 -0
  45. data/spec/mongoid_roles/strategy/single/one_role_spec.rb +18 -0
  46. data/spec/mongoid_roles/strategy/single/role_string_spec.rb +18 -0
  47. data/spec/mongoid_roles/strategy/user_setup.rb +13 -0
  48. data/spec/spec_helper.rb +15 -0
  49. data/tmp/rails/app/models/user.rb +8 -0
  50. data/tmp/rails/config/routes.rb +2 -0
  51. metadata +256 -0
@@ -0,0 +1,27 @@
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
+ begin
7
+ role_class_name.create(:name => name.to_s)
8
+ rescue
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ module RoleClass
16
+ module ClassMethods
17
+ def find_roles(*role_names)
18
+ where(:name.in => role_names.flatten).to_a
19
+ end
20
+
21
+ def find_role role_name
22
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
23
+ res = find_roles(role_name)
24
+ res ? res.first : res
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Roles::Base
2
+ def valid_roles_are(*role_list)
3
+ strategy_class.valid_roles = role_list.to_symbols
4
+ end
5
+ end
6
+
7
+ class Role
8
+ include Mongoid::Document
9
+ field :name, :type => String
10
+
11
+ validates_uniqueness_of :name
12
+
13
+ class << self
14
+ def find_roles(*role_names)
15
+ where(:name.in => role_names.flatten).to_a
16
+ end
17
+
18
+ def find_role role_name
19
+ raise ArgumentError, "#find_role takes a single role name as argument, not: #{role_name.inspect}" if !role_name.kind_of_label?
20
+ res = find_roles(role_name)
21
+ res ? res.first : res
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ require 'sugar-high/module'
2
+
3
+ module Roles
4
+ modules :mongoid do
5
+ nested_modules :user, :role
6
+ end
7
+ modules :base, :strategy
8
+ end
9
+
10
+ module RoleStrategy
11
+ modules :mongoid
12
+ end
@@ -0,0 +1,10 @@
1
+ require 'mongoid_roles/base_role'
2
+
3
+ class Role
4
+ include Mongoid::Document
5
+ field :name, :type => String
6
+
7
+ validates_uniqueness_of :name
8
+
9
+ extend RoleClass::ClassMethods
10
+ end
@@ -0,0 +1,15 @@
1
+ require 'sugar-high/file'
2
+ require 'sugar-high/array'
3
+
4
+ module Roles::Strategy
5
+ class << self
6
+ def role_dir
7
+ File.dirname(__FILE__)
8
+ end
9
+
10
+ def gem_name
11
+ :mongoid_roles
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,59 @@
1
+ require 'mongoid_roles/strategy/shared'
2
+
3
+ module Mongoid::Roles
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
+ diff = roles_diff(role_names)
27
+ diff = role_class.find_roles(diff).to_a if self.class.role_strategy.type == :complex
28
+ set_roles(diff)
29
+ true
30
+ end
31
+
32
+ # query assigned roles
33
+ def roles
34
+ get_roles.to_a.map do |role|
35
+ role.respond_to?(:sym) ? role.to_sym : role
36
+ end
37
+ end
38
+
39
+ def roles_list
40
+ my_roles = [roles].flat_uniq
41
+ return [] if my_roles.empty?
42
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
43
+ end
44
+
45
+ protected
46
+
47
+ def set_roles *roles
48
+ roles = roles.flat_uniq
49
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
50
+ # self.send("#{role_attribute}").send(:<<, roles)
51
+ self.send("#{role_attribute}=", roles)
52
+ end
53
+
54
+ def get_roles
55
+ self.send(role_attribute)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,124 @@
1
+ require 'mongoid_roles/strategy/multi'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module EmbedManyRoles
5
+ def self.default_role_attribute
6
+ :many_roles
7
+ end
8
+
9
+ def self.included base
10
+ base.extend Roles::Generic::Role::ClassMethods
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def role_attribute
16
+ "#{strategy_class.roles_attribute_name.to_s.singularize}_ids".to_sym
17
+ end
18
+
19
+ def in_role(role_name)
20
+ in_any_role(role_name)
21
+ end
22
+
23
+ def in_any_role(*role_names)
24
+ begin
25
+ any_in("many_roles.name" => role_names)
26
+ rescue
27
+ return []
28
+ end
29
+ end
30
+ end
31
+
32
+ module Implementation
33
+ include Mongoid::Roles::Strategy::Multi
34
+
35
+ def set_role role
36
+ raise ArgumentError, "#set_role only takes a single role as argument, not #{role}" if role.kind_of?(Array)
37
+ self.send("#{role_attribute}=", nil)
38
+ self.send("create_#{role_attribute}").create(:name => role)
39
+ end
40
+
41
+ def set_roles *roles
42
+ roles = roles.flat_uniq
43
+ self.send("#{role_attribute}=", nil)
44
+ roles.each do |role|
45
+ self.send("#{role_attribute}").create(:name => role)
46
+ end
47
+ end
48
+
49
+ def roles_diff *roles
50
+ self.roles_list - extract_roles(roles.flat_uniq)
51
+ end
52
+
53
+ def remove_roles *role_names
54
+ role_names = role_names.flat_uniq
55
+ set_empty_roles and return if roles_diff(role_names).empty?
56
+ roles_to_remove = select_valid_roles(role_names)
57
+
58
+ diff = roles_diff(role_names)
59
+ set_roles(diff)
60
+ true
61
+ end
62
+
63
+ def exchange_roles *role_names
64
+ options = last_option role_names
65
+ 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)
66
+ common = role_names.to_symbols & roles_list
67
+ if !common.empty?
68
+ with_roles = options[:with]
69
+ set_roles with_roles
70
+ end
71
+ end
72
+
73
+ def select_valid_roles *role_names
74
+ role_names = role_names.flat_uniq.select{|role| valid_role? role }
75
+ end
76
+
77
+ def has_roles?(*roles_names)
78
+ compare_roles = extract_roles(roles_names.flat_uniq)
79
+ (roles_list & compare_roles).not.empty?
80
+ end
81
+
82
+ def get_roles
83
+ self.send(role_attribute)
84
+ end
85
+
86
+ def roles
87
+ get_roles.to_a.map do |role|
88
+ role.respond_to?(:sym) ? role.to_sym : role
89
+ end
90
+ end
91
+
92
+ def roles_list
93
+ my_roles = [roles].flat_uniq
94
+ return [] if my_roles.empty?
95
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
96
+ end
97
+
98
+ # assign multiple roles
99
+ def roles=(*role_names)
100
+ role_names = role_names.flat_uniq
101
+ role_names = extract_roles(role_names)
102
+ return nil if role_names.empty?
103
+
104
+ vrs = select_valid_roles role_names
105
+ set_roles(vrs)
106
+ end
107
+
108
+ def new_roles *role_names
109
+ role_class.find_roles(extract_roles role_names)
110
+ end
111
+
112
+ def present_roles roles_names
113
+ roles_names.to_a.map{|role| role.name.to_s.to_sym}
114
+ end
115
+
116
+ def set_empty_roles
117
+ self.send("#{role_attribute}=", [])
118
+ end
119
+ end
120
+
121
+ extend Roles::Generic::User::Configuration
122
+ configure :type => :role_class
123
+ end
124
+ end
@@ -0,0 +1,83 @@
1
+ require 'mongoid_roles/strategy/multi'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module ManyRoles
5
+ def self.default_role_attribute
6
+ :many_roles
7
+ end
8
+
9
+ def self.included base
10
+ base.extend Roles::Generic::Role::ClassMethods
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def role_attribute
16
+ "#{strategy_class.roles_attribute_name.to_s.singularize}_ids".to_sym
17
+ end
18
+
19
+ def in_role(role_name)
20
+ in_any_role(role_name)
21
+ end
22
+
23
+ def in_any_role(*role_names)
24
+ begin
25
+ role_ids = Role.where(:name.in => role_names.to_strings).to_a.map(&:_id)
26
+ where(:"#{role_attribute}".in => role_ids).to_a
27
+ rescue
28
+ return []
29
+ end
30
+ end
31
+ end
32
+
33
+ module Implementation
34
+ include Mongoid::Roles::Strategy::Multi
35
+
36
+ def has_roles?(*roles_names)
37
+ compare_roles = extract_roles(roles_names.flat_uniq)
38
+ (roles_list & compare_roles).not.empty?
39
+ end
40
+
41
+ def get_roles
42
+ self.send(role_attribute)
43
+ end
44
+
45
+ def roles
46
+ get_roles.to_a.map do |role|
47
+ role.respond_to?(:sym) ? role.to_sym : role
48
+ end
49
+ end
50
+
51
+ def roles_list
52
+ my_roles = [roles].flat_uniq
53
+ return [] if my_roles.empty?
54
+ has_role_class? ? my_roles.map{|r| r.name.to_sym } : my_roles
55
+ end
56
+
57
+ # assign multiple roles
58
+ def roles=(*role_names)
59
+ role_names = role_names.flat_uniq
60
+ role_names = extract_roles(role_names)
61
+ return nil if role_names.empty?
62
+ valids = role_class.find_roles(role_names).to_a
63
+ vrs = select_valid_roles role_names
64
+ set_roles(vrs)
65
+ end
66
+
67
+ def new_roles *role_names
68
+ role_class.find_roles(extract_roles role_names)
69
+ end
70
+
71
+ def present_roles roles_names
72
+ roles_names.to_a.map{|role| role.name.to_s.to_sym}
73
+ end
74
+
75
+ def set_empty_roles
76
+ self.send("#{role_attribute}=", [])
77
+ end
78
+ end
79
+
80
+ extend Roles::Generic::User::Configuration
81
+ configure :type => :role_class
82
+ end
83
+ end
@@ -0,0 +1,54 @@
1
+ require 'mongoid_roles/strategy/multi'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module RoleStrings
5
+ def self.default_role_attribute
6
+ :role_strings
7
+ end
8
+
9
+ def self.included base
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def role_attribute
15
+ strategy_class.roles_attribute_name.to_sym
16
+ end
17
+
18
+ def in_role(role_name)
19
+ in_any_role(role_name)
20
+ end
21
+
22
+ def in_any_role(*role_names)
23
+ begin
24
+ where(role_attribute.in => role_names).to_a
25
+ rescue
26
+ return []
27
+ end
28
+ end
29
+ end
30
+
31
+ module Implementation
32
+ include Mongoid::Roles::Strategy::Multi
33
+
34
+ def new_roles *roles
35
+ ::Set.new select_valid_roles(roles)
36
+ end
37
+
38
+ def select_valid_roles *roles
39
+ roles.flat_uniq.select{|role| valid_role? role }.map(&:to_sym)
40
+ end
41
+
42
+ def set_empty_roles
43
+ self.send("#{role_attribute}=", [])
44
+ end
45
+
46
+ def present_roles roles_names
47
+ roles_names.to_a
48
+ end
49
+ end
50
+
51
+ extend Roles::Generic::User::Configuration
52
+ configure
53
+ end
54
+ end
@@ -0,0 +1,130 @@
1
+ require 'mongoid_roles/strategy/multi'
2
+
3
+ module RoleStrategy::Mongoid
4
+ module RolesMask
5
+ def self.default_role_attribute
6
+ :roles_mask
7
+ end
8
+
9
+ def self.included base
10
+ base.extend ClassMethods
11
+ end
12
+
13
+ module ClassMethods
14
+ def role_attribute
15
+ strategy_class.roles_attribute_name.to_sym
16
+ end
17
+
18
+ def in_role(role)
19
+ mask = calc_index(role.to_s)
20
+ all.select do |user|
21
+ (user.send(role_attribute) & mask) > 0
22
+ end
23
+ end
24
+
25
+ def in_any_role(*roles)
26
+ all.select do |user|
27
+ roles.flatten.any? do |role|
28
+ mask = calc_index(role.to_s)
29
+ (user.send(role_attribute) & mask) > 0
30
+ end
31
+ end
32
+ end
33
+
34
+ def calc_index(r)
35
+ 2**strategy_class.valid_roles.index(r.to_sym)
36
+ end
37
+ end
38
+
39
+ module Implementation
40
+ include Mongoid::Roles::Strategy::Multi
41
+
42
+ class Roles < ::Set # :nodoc:
43
+ attr_reader :model_instance
44
+
45
+ def initialize(sender, *roles)
46
+ super(*roles)
47
+ @model_instance = sender
48
+ end
49
+
50
+ def <<(role)
51
+ model_instance.roles = super.to_a
52
+ self
53
+ end
54
+ end
55
+
56
+ protected
57
+
58
+ def calc_index(r)
59
+ 2**strategy_class.valid_roles.index(r)
60
+ end
61
+
62
+ def get_roles
63
+ strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((get_role || 0) & calc_index(r)).zero? })
64
+ end
65
+
66
+ def new_roles *role_names
67
+ role_names = role_names.flatten.map{ |r| r.to_sym } & strategy_class.valid_roles
68
+ role_names.map { |r| calc_index(r) }.inject { |sum, bitvalue| sum + bitvalue }
69
+ end
70
+
71
+ def set_empty_roles
72
+ self.send("#{role_attribute}=", 0)
73
+ end
74
+
75
+ def present_roles *role_names
76
+ role_names.to_a.to_symbols
77
+ end
78
+
79
+ def set_roles *roles
80
+ roles = roles.flat_uniq
81
+ roles = roles.first if self.class.role_strategy.multiplicity == :single
82
+ vrs = new_roles roles
83
+ self.send("#{role_attribute}=", vrs)
84
+ end
85
+
86
+
87
+ # class Roles < ::Set # :nodoc:
88
+ # attr_reader :model_instance
89
+ #
90
+ # def initialize(sender, *roles)
91
+ # super(*roles)
92
+ # @model_instance = sender
93
+ # end
94
+ #
95
+ # def <<(role)
96
+ # model_instance.roles = super.to_a
97
+ # self
98
+ # end
99
+ # end
100
+ #
101
+ # def role_attribute
102
+ # strategy_class.roles_attribute_name
103
+ # end
104
+ #
105
+ # # assign roles
106
+ # def roles=(*roles)
107
+ # 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 })
108
+ # end
109
+ # alias_method :role=, :roles=
110
+ #
111
+ # # query assigned roles
112
+ # def roles
113
+ # strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
114
+ # end
115
+ #
116
+ # def roles_list
117
+ # roles.to_a
118
+ # end
119
+ #
120
+ # protected
121
+ #
122
+ # def calc_index(r)
123
+ # 2**strategy_class.valid_roles.index(r)
124
+ # end
125
+ end
126
+
127
+ extend Roles::Generic::User::Configuration
128
+ configure
129
+ end
130
+ end