roles_for_mongoid 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- 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
data/spec/spec_helper.rb
CHANGED
@@ -1,20 +1,30 @@
|
|
1
1
|
require 'rspec'
|
2
2
|
require 'rspec/autorun'
|
3
3
|
require 'mongoid'
|
4
|
-
require '
|
4
|
+
require 'roles_mongoid'
|
5
5
|
|
6
|
-
Mongoid.configure.master = Mongo::Connection.new.db('
|
6
|
+
Mongoid.configure.master = Mongo::Connection.new.db('roles_mongoid')
|
7
7
|
|
8
8
|
module Database
|
9
|
-
def self.teardown
|
10
|
-
Mongoid.database.collections.each do |coll|
|
11
|
-
|
12
|
-
end
|
9
|
+
def self.teardown
|
10
|
+
# Mongoid.database.collections.each do |coll|
|
11
|
+
# coll.remove
|
12
|
+
# end
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
|
+
Mongoid.database.collections.each do |coll|
|
17
|
+
coll.remove
|
18
|
+
end
|
19
|
+
|
20
|
+
|
16
21
|
RSpec.configure do |config|
|
17
|
-
|
22
|
+
config.mock_with :mocha
|
23
|
+
# config.before do
|
24
|
+
# Mongoid.database.collections.each do |coll|
|
25
|
+
# coll.remove
|
26
|
+
# end
|
27
|
+
# end
|
18
28
|
end
|
19
29
|
|
20
30
|
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
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-
|
17
|
+
date: 2010-09-06 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -30,10 +30,72 @@ dependencies:
|
|
30
30
|
- 0
|
31
31
|
- 0
|
32
32
|
- beta
|
33
|
-
-
|
34
|
-
version: 2.0.0.beta.
|
33
|
+
- 19
|
34
|
+
version: 2.0.0.beta.19
|
35
35
|
type: :development
|
36
36
|
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: mongoid
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 0
|
48
|
+
- 0
|
49
|
+
- beta
|
50
|
+
- 17
|
51
|
+
version: 2.0.0.beta.17
|
52
|
+
type: :runtime
|
53
|
+
version_requirements: *id002
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: require_all
|
56
|
+
prerelease: false
|
57
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ~>
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 1
|
64
|
+
- 1
|
65
|
+
- 0
|
66
|
+
version: 1.1.0
|
67
|
+
type: :runtime
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sugar-high
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
- 2
|
80
|
+
- 3
|
81
|
+
version: 0.2.3
|
82
|
+
type: :runtime
|
83
|
+
version_requirements: *id004
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: roles_generic
|
86
|
+
prerelease: false
|
87
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ~>
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
- 2
|
95
|
+
- 2
|
96
|
+
version: 0.2.2
|
97
|
+
type: :runtime
|
98
|
+
version_requirements: *id005
|
37
99
|
description: Facilitates adding Roles to your Mongoid user models
|
38
100
|
email: kmandrup@gmail.com
|
39
101
|
executables: []
|
@@ -46,27 +108,33 @@ extra_rdoc_files:
|
|
46
108
|
files:
|
47
109
|
- .document
|
48
110
|
- .gitignore
|
111
|
+
- .rspec
|
49
112
|
- LICENSE
|
50
113
|
- README.markdown
|
51
114
|
- Rakefile
|
52
115
|
- VERSION
|
53
116
|
- lib/generators/mongoid/roles/roles_generator.rb
|
54
|
-
- lib/
|
55
|
-
- lib/
|
56
|
-
- lib/
|
57
|
-
- lib/
|
58
|
-
- lib/
|
59
|
-
- lib/
|
60
|
-
- lib/
|
61
|
-
- lib/
|
117
|
+
- lib/roles_mongoid.rb
|
118
|
+
- lib/roles_mongoid/base.rb
|
119
|
+
- lib/roles_mongoid/namespaces.rb
|
120
|
+
- lib/roles_mongoid/role.rb
|
121
|
+
- lib/roles_mongoid/strategy.rb
|
122
|
+
- lib/roles_mongoid/strategy/multi/many_roles.rb
|
123
|
+
- lib/roles_mongoid/strategy/multi/role_strings.rb
|
124
|
+
- lib/roles_mongoid/strategy/multi/roles_mask.rb
|
125
|
+
- lib/roles_mongoid/strategy/single/admin_flag.rb
|
126
|
+
- lib/roles_mongoid/strategy/single/one_role.rb
|
127
|
+
- lib/roles_mongoid/strategy/single/role_string.rb
|
62
128
|
- roles_for_mongoid.gemspec
|
129
|
+
- sandbox/roles_assign.rb
|
130
|
+
- sandbox/single_role.rb
|
63
131
|
- sandbox/test.rb
|
64
|
-
- spec/
|
65
|
-
- spec/
|
66
|
-
- spec/
|
67
|
-
- spec/
|
68
|
-
- spec/
|
69
|
-
- spec/
|
132
|
+
- spec/roles_mongoid/admin_flag_spec.rb
|
133
|
+
- spec/roles_mongoid/many_roles_spec.rb
|
134
|
+
- spec/roles_mongoid/one_role_spec.rb
|
135
|
+
- spec/roles_mongoid/role_string_spec.rb
|
136
|
+
- spec/roles_mongoid/role_strings_spec.rb
|
137
|
+
- spec/roles_mongoid/roles_mask_spec.rb
|
70
138
|
- spec/spec_helper.rb
|
71
139
|
has_rdoc: true
|
72
140
|
homepage: http://github.com/kristianmandrup/roles_for_mm
|
@@ -101,9 +169,10 @@ signing_key:
|
|
101
169
|
specification_version: 3
|
102
170
|
summary: Facilitates adding Roles to your Mongoid user models
|
103
171
|
test_files:
|
104
|
-
- spec/
|
105
|
-
- spec/
|
106
|
-
- spec/
|
107
|
-
- spec/
|
108
|
-
- spec/
|
172
|
+
- spec/roles_mongoid/admin_flag_spec.rb
|
173
|
+
- spec/roles_mongoid/many_roles_spec.rb
|
174
|
+
- spec/roles_mongoid/one_role_spec.rb
|
175
|
+
- spec/roles_mongoid/role_string_spec.rb
|
176
|
+
- spec/roles_mongoid/role_strings_spec.rb
|
177
|
+
- spec/roles_mongoid/roles_mask_spec.rb
|
109
178
|
- spec/spec_helper.rb
|
data/lib/roles_for_mongoid.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'roles_for_mongoid/user'
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module Mongoid::AdminFlag
|
2
|
-
def self.included base
|
3
|
-
base.extend ClassMethods
|
4
|
-
base.field :admin, :type => Boolean
|
5
|
-
end
|
6
|
-
|
7
|
-
module ClassMethods
|
8
|
-
def in_role(role_name)
|
9
|
-
case role_name.downcase.to_sym
|
10
|
-
when :admin
|
11
|
-
where(:admin => true)
|
12
|
-
else
|
13
|
-
where(:admin => false)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module Mongoid::InlineRoles
|
2
|
-
def self.included base
|
3
|
-
base.extend ClassMethods
|
4
|
-
base.field :roles, :type => Array
|
5
|
-
base.scope :in_role, lambda { |role_name| base.where(:roles => [role_name]) }
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
def in_roles(*roles)
|
10
|
-
where(:roles.in => roles.flatten)
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'roles_for_mongoid/role'
|
2
|
-
|
3
|
-
module Mongoid::RoleRelations
|
4
|
-
def self.included base
|
5
|
-
base.extend ClassMethods
|
6
|
-
base.references_many :roles, :stored_as => :array
|
7
|
-
end
|
8
|
-
|
9
|
-
module ClassMethods
|
10
|
-
def in_role(role_name)
|
11
|
-
in_roles(role_name)
|
12
|
-
end
|
13
|
-
|
14
|
-
# old way!
|
15
|
-
# def in_roles *role_names
|
16
|
-
# roles = Role.where(:name.in => role_names.flatten).to_a
|
17
|
-
# arr_ids = roles.map{|role| role._id}
|
18
|
-
# all.to_a.select do |user|
|
19
|
-
# user.roles.to_a.any? do |role|
|
20
|
-
# arr_ids.include?(role._id)
|
21
|
-
# end
|
22
|
-
# end
|
23
|
-
# end
|
24
|
-
|
25
|
-
def in_roles(*role_names)
|
26
|
-
begin
|
27
|
-
role_ids = Role.where(:name.in => role_names.flatten).to_a.map(&:_id)
|
28
|
-
criteria.in(:role_ids => role_ids).to_a
|
29
|
-
rescue
|
30
|
-
return []
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
module Mongoid
|
2
|
-
module RolesMask
|
3
|
-
def self.included base
|
4
|
-
base.extend ClassMethods
|
5
|
-
base.field :roles_mask, :type => Integer, :default => 1
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
attr_accessor :available_roles
|
10
|
-
|
11
|
-
def in_role(role)
|
12
|
-
mask = calc_index(role.to_s)
|
13
|
-
all.select do |user|
|
14
|
-
(user.roles_mask & mask) > 0
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def in_roles(*roles)
|
19
|
-
all.select do |user|
|
20
|
-
roles.flatten.any? do |role|
|
21
|
-
mask = calc_index(role.to_s)
|
22
|
-
(user.roles_mask & mask) > 0
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def calc_index(r)
|
28
|
-
2**available_roles.index(r)
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def available_roles
|
33
|
-
self.class.available_roles
|
34
|
-
end
|
35
|
-
|
36
|
-
def calc_index(r)
|
37
|
-
self.class.calc_index(r)
|
38
|
-
end
|
39
|
-
|
40
|
-
def roles=(*roles)
|
41
|
-
self.roles_mask = (roles.flatten & available_roles).map { |r| calc_index(r) }.sum
|
42
|
-
end
|
43
|
-
|
44
|
-
def roles
|
45
|
-
self.available_roles.reject { |r| ((roles_mask || 0) & self.calc_index(r)).zero? }
|
46
|
-
end
|
47
|
-
|
48
|
-
def role?(role)
|
49
|
-
roles.include?(role.to_s)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'require_all'
|
2
|
-
require 'active_support/inflector'
|
3
|
-
|
4
|
-
class User
|
5
|
-
include Mongoid::Document
|
6
|
-
|
7
|
-
def self.role_strategy type
|
8
|
-
include "Mongoid::#{type.to_s.camelize}".constantize
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
require_all File.dirname(__FILE__) + '/strategies'
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
User.role_strategy :admin_flag
|
4
|
-
|
5
|
-
describe "Roles for Mongoid" do
|
6
|
-
after :each do
|
7
|
-
Database.teardown
|
8
|
-
end
|
9
|
-
|
10
|
-
before :each do
|
11
|
-
guest_user = User.new :name => 'Kristian', :admin => false
|
12
|
-
admin_user = User.new :name => 'Admin user', :admin => true
|
13
|
-
admin_user.save
|
14
|
-
guest_user.save
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "Admin flag strategy " do
|
18
|
-
|
19
|
-
it "should return first user maching role" do
|
20
|
-
User.in_role('guest').first.name.should == 'Kristian'
|
21
|
-
User.in_role('admin').first.name.should == 'Admin user'
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
User.role_strategy :inline_role
|
4
|
-
|
5
|
-
describe "Roles for Mongoid" do
|
6
|
-
after :each do
|
7
|
-
Database.teardown
|
8
|
-
end
|
9
|
-
|
10
|
-
before :each do
|
11
|
-
guest_user = User.new :name => 'Kristian', :role => 'guest'
|
12
|
-
admin_user = User.new :name => 'Admin user', :role => 'admin'
|
13
|
-
admin_user.save
|
14
|
-
guest_user.save
|
15
|
-
end
|
16
|
-
|
17
|
-
describe "Inline role strategy" do
|
18
|
-
it "should return first user maching role" do
|
19
|
-
User.in_role('guest').first.name.should == 'Kristian'
|
20
|
-
User.in_role('admin').first.name.should == 'Admin user'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
User.role_strategy :inline_roles
|
4
|
-
|
5
|
-
describe "Roles for Mongoid" do
|
6
|
-
after :each do
|
7
|
-
Database.teardown
|
8
|
-
end
|
9
|
-
|
10
|
-
before :each do
|
11
|
-
guest_user = User.new(:name => 'Kristian', :roles => ['guest']).save
|
12
|
-
admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
|
13
|
-
author_user = User.new(:name => 'Author', :roles => ['author']).save
|
14
|
-
end
|
15
|
-
|
16
|
-
describe "Inline roles strategy" do
|
17
|
-
|
18
|
-
it "should return first user matching role" do
|
19
|
-
User.in_role('admin').first.name.should == 'Admin user'
|
20
|
-
end
|
21
|
-
|
22
|
-
it "should return all users matching any of the roles" do
|
23
|
-
User.in_roles(['guest', 'admin']).to_a.size.should == 2
|
24
|
-
User.in_roles('guest', 'admin').to_a.size.should == 2
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
User.role_strategy :role_relations
|
4
|
-
|
5
|
-
describe "Roles for Mongoid" do
|
6
|
-
|
7
|
-
after :each do
|
8
|
-
Database.teardown
|
9
|
-
end
|
10
|
-
|
11
|
-
before :each do
|
12
|
-
guest_user = User.create(:name => 'Kristian')
|
13
|
-
admin_user = User.create(:name => 'Admin user')
|
14
|
-
author_user = User.create(:name => 'Author')
|
15
|
-
|
16
|
-
guest_user.roles.create(:name => 'guest')
|
17
|
-
guest_user.save
|
18
|
-
|
19
|
-
admin_user.roles.create(:name => 'admin')
|
20
|
-
admin_user.save
|
21
|
-
|
22
|
-
author_user.roles.create(:name => 'author')
|
23
|
-
author_user.save
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "Role relations strategy" do
|
27
|
-
it "should return first user matching role" do
|
28
|
-
User.in_role('guest').first.name.should == 'Kristian'
|
29
|
-
User.in_role('admin').first.name.should == 'Admin user'
|
30
|
-
end
|
31
|
-
|
32
|
-
# it "should return all users matching any of the roles" do
|
33
|
-
# User.in_roles(['guest', 'admin']).all.size.should == 2
|
34
|
-
# User.in_roles('guest', 'admin').all.size.should == 2
|
35
|
-
# end
|
36
|
-
end
|
37
|
-
end
|