roles_for_mongoid 0.1.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/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +136 -0
- data/Rakefile +42 -0
- data/VERSION +1 -0
- data/lib/generators/mongoid/roles/roles_generator.rb +45 -0
- data/lib/roles_for_mongoid.rb +1 -0
- data/lib/roles_for_mongoid/role.rb +4 -0
- data/lib/roles_for_mongoid/strategies/admin_flag.rb +17 -0
- data/lib/roles_for_mongoid/strategies/inline_role.rb +6 -0
- data/lib/roles_for_mongoid/strategies/inline_roles.rb +13 -0
- data/lib/roles_for_mongoid/strategies/role_relations.rb +29 -0
- data/lib/roles_for_mongoid/strategies/roles_mask.rb +52 -0
- data/lib/roles_for_mongoid/user.rb +12 -0
- data/sandbox/test.rb +48 -0
- data/spec/roles_for_mongoid/user_admin_field_spec.rb +24 -0
- data/spec/roles_for_mongoid/user_inline_role_spec.rb +23 -0
- data/spec/roles_for_mongoid/user_inline_roles_spec.rb +27 -0
- data/spec/roles_for_mongoid/user_role_relations_spec.rb +37 -0
- data/spec/roles_for_mongoid/user_roles_mask_spec.rb +29 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +20 -0
- metadata +108 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Kristian Mandrup
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
# Roles for Mongoid
|
2
|
+
|
3
|
+
# Install
|
4
|
+
|
5
|
+
<code>gem install roles_for_mongoid</code>
|
6
|
+
|
7
|
+
# Usage
|
8
|
+
|
9
|
+
The library comes with the following role models built-in:
|
10
|
+
|
11
|
+
* Single inline role (string)
|
12
|
+
* Multiple inline role (strings)
|
13
|
+
* Multiple role relations (many relationship using array of object ids)
|
14
|
+
|
15
|
+
Note: The following examples use RSpec to demonstrate usage scenarios.
|
16
|
+
|
17
|
+
## Example : Admin flag
|
18
|
+
|
19
|
+
<pre>
|
20
|
+
require 'roles_for_mongoid'
|
21
|
+
|
22
|
+
User.role_strategy :admin_flag
|
23
|
+
|
24
|
+
before :each do
|
25
|
+
guest_user = User.new :name => 'Kristian', :role => 'guest'
|
26
|
+
admin_user = User.new :name => 'Admin user', :role => 'admin'
|
27
|
+
admin_user.save
|
28
|
+
guest_user.save
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return first user maching role" do
|
32
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
33
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
34
|
+
end
|
35
|
+
</pre>
|
36
|
+
|
37
|
+
|
38
|
+
## Example : Single inline role
|
39
|
+
|
40
|
+
<pre>
|
41
|
+
User.role_strategy :inline_role
|
42
|
+
|
43
|
+
before :each do
|
44
|
+
guest_user = User.new :name => 'Kristian', :role => 'guest'
|
45
|
+
admin_user = User.new :name => 'Admin user', :role => 'admin'
|
46
|
+
admin_user.save
|
47
|
+
guest_user.save
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should return first user maching role" do
|
51
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
52
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
53
|
+
end
|
54
|
+
</pre>
|
55
|
+
|
56
|
+
## Example : Multiple inline roles
|
57
|
+
|
58
|
+
<pre>
|
59
|
+
User.role_strategy :inline_roles
|
60
|
+
|
61
|
+
before :each do
|
62
|
+
guest_user = User.new(:name => 'Kristian', :roles => ['guest']).save
|
63
|
+
admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
|
64
|
+
author_user = User.new(:name => 'Author', :roles => ['author']).save
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should return first user matching role" do
|
68
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should return all users matching any of the roles" do
|
72
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
73
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
74
|
+
end
|
75
|
+
</pre>
|
76
|
+
|
77
|
+
## Example : Multiple role relations
|
78
|
+
|
79
|
+
<pre>
|
80
|
+
User.role_strategy :role_relations
|
81
|
+
|
82
|
+
before :each do
|
83
|
+
guest_user = User.new(:name => 'Kristian')
|
84
|
+
admin_user = User.new(:name => 'Admin user')
|
85
|
+
author_user = User.new(:name => 'Author')
|
86
|
+
|
87
|
+
guest_user.roles << Role.new(:name => 'guest')
|
88
|
+
guest_user.save
|
89
|
+
|
90
|
+
admin_user.roles << Role.new(:name => 'admin')
|
91
|
+
admin_user.save
|
92
|
+
|
93
|
+
author_user.roles << Role.new(:name => 'author')
|
94
|
+
author_user.save
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should return first user matching role" do
|
98
|
+
User.in_role('guest').first.name.should == 'Kristian'
|
99
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return all users matching any of the roles" do
|
103
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
104
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
105
|
+
end
|
106
|
+
</pre>
|
107
|
+
|
108
|
+
## Rails generator
|
109
|
+
|
110
|
+
The library comes with a Rails 3 generator that lets you populate a user model with a given role strategy
|
111
|
+
The following role strategies are included by default. Add your own by adding extra files inside the strategy folder, one file for each role strategy is recommended.
|
112
|
+
|
113
|
+
* admin_flag
|
114
|
+
* inline_role
|
115
|
+
* inline_roles
|
116
|
+
* role_relations
|
117
|
+
* role_mask
|
118
|
+
|
119
|
+
Example:
|
120
|
+
|
121
|
+
<code>$ rails g mongoid:roles user admin_flag</code>
|
122
|
+
|
123
|
+
|
124
|
+
## Note on Patches/Pull Requests
|
125
|
+
|
126
|
+
* Fork the project.
|
127
|
+
* Make your feature addition or bug fix.
|
128
|
+
* Add tests for it. This is important so I don't break it in a
|
129
|
+
future version unintentionally.
|
130
|
+
* Commit, do not mess with rakefile, version, or history.
|
131
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
132
|
+
* Send me a pull request. Bonus points for topic branches.
|
133
|
+
|
134
|
+
## Copyright
|
135
|
+
|
136
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gem|
|
4
|
+
gem.name = "roles_for_mongoid"
|
5
|
+
gem.summary = %Q{Facilitates adding Roles to your Mongoid user models}
|
6
|
+
gem.description = %Q{Facilitates adding Roles to your Mongoid user models}
|
7
|
+
gem.email = "kmandrup@gmail.com"
|
8
|
+
gem.homepage = "http://github.com/kristianmandrup/roles_for_mm"
|
9
|
+
gem.authors = ["Kristian Mandrup"]
|
10
|
+
gem.add_development_dependency "rspec", ">= 2.0.0.beta.17"
|
11
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
12
|
+
end
|
13
|
+
Jeweler::GemcutterTasks.new
|
14
|
+
rescue LoadError
|
15
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
16
|
+
end
|
17
|
+
|
18
|
+
# require 'spec/rake/spectask'
|
19
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
20
|
+
# spec.libs << 'lib' << 'spec'
|
21
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
25
|
+
# spec.libs << 'lib' << 'spec'
|
26
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
27
|
+
# spec.rcov = true
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# task :spec => :check_dependencies
|
31
|
+
#
|
32
|
+
# task :default => :spec
|
33
|
+
#
|
34
|
+
# require 'rake/rdoctask'
|
35
|
+
# Rake::RDocTask.new do |rdoc|
|
36
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
37
|
+
#
|
38
|
+
# rdoc.rdoc_dir = 'rdoc'
|
39
|
+
# rdoc.title = "roles_for_mm #{version}"
|
40
|
+
# rdoc.rdoc_files.include('README*')
|
41
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
42
|
+
# end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Generators
|
3
|
+
class RolesGenerator < Rails::Generators::NamedBase
|
4
|
+
desc "Generate roles model for User"
|
5
|
+
|
6
|
+
argument :role_strategy, :type => :string, :aliases => "-r", :default => 'inline_role', :desc => "Create roles model for user"
|
7
|
+
|
8
|
+
hook_for :orm
|
9
|
+
|
10
|
+
def apply_role_strategy
|
11
|
+
insert_into_model(model_name, role_strategy_statement)
|
12
|
+
end
|
13
|
+
|
14
|
+
protected
|
15
|
+
|
16
|
+
def model_name
|
17
|
+
name
|
18
|
+
end
|
19
|
+
|
20
|
+
def match_expr
|
21
|
+
/include Mongoid::Document/
|
22
|
+
end
|
23
|
+
|
24
|
+
def role_strategy_statement
|
25
|
+
"role_strategy #{role_strategy}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def role_strategy
|
29
|
+
options[:role_strategy]
|
30
|
+
end
|
31
|
+
|
32
|
+
def model_file(name)
|
33
|
+
File.join(Rails.root, "app/models/#{name.to_s}.rb")
|
34
|
+
end
|
35
|
+
|
36
|
+
def insert_into_model(m_name, insert_text)
|
37
|
+
file = File.new(model_file(m_name))
|
38
|
+
return if (file.read =~ /#{insert_text}/)
|
39
|
+
gsub_file model_file(m_name), match_expr do |match|
|
40
|
+
match << insert_text
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'roles_for_mongoid/user'
|
@@ -0,0 +1,17 @@
|
|
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
|
@@ -0,0 +1,13 @@
|
|
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
|
@@ -0,0 +1,29 @@
|
|
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
|
+
def in_roles *role_names
|
15
|
+
roles = Role.where(:name.in => role_names.flatten).to_a
|
16
|
+
arr_ids = roles.map{|role| role._id}
|
17
|
+
all.to_a.select do |user|
|
18
|
+
user.roles.to_a.any? do |role|
|
19
|
+
arr_ids.include?(role._id)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# def in_roles(*roles)
|
25
|
+
# role_ids = Role.where(:name => roles.flatten).to_a
|
26
|
+
# where(:roles.in => role_ids).to_a
|
27
|
+
# end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
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
|
@@ -0,0 +1,12 @@
|
|
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'
|
data/sandbox/test.rb
ADDED
@@ -0,0 +1,48 @@
|
|
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
|
+
class User
|
12
|
+
include Mongoid::Document
|
13
|
+
field :name
|
14
|
+
references_many :roles, :stored_as => :array
|
15
|
+
|
16
|
+
def self.find_roles *ids
|
17
|
+
arr_ids = ids.flatten
|
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
|
+
# todo: make this work instead!
|
26
|
+
# def self.get_roles(*ids)
|
27
|
+
# arr = ids.flatten
|
28
|
+
# where(:'roles._id'.in => arr).to_a
|
29
|
+
# end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
class Role
|
34
|
+
include Mongoid::Document
|
35
|
+
field :name
|
36
|
+
end
|
37
|
+
|
38
|
+
user = User.create(:name => 'Kristian')
|
39
|
+
user.roles.create(:name => 'guest')
|
40
|
+
user.save
|
41
|
+
#
|
42
|
+
puts "user roles: #{User.first.roles.map(&:_id)}"
|
43
|
+
|
44
|
+
role_id = user.roles.to_a.first._id
|
45
|
+
|
46
|
+
p role_id
|
47
|
+
p User.find_roles role_id
|
48
|
+
#p User.get_roles role_id
|
@@ -0,0 +1,24 @@
|
|
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
|
@@ -0,0 +1,23 @@
|
|
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
|
@@ -0,0 +1,27 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
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
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :roles_mask
|
4
|
+
|
5
|
+
describe "Roles for Mongoid" do
|
6
|
+
describe "Roles mask strategy" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
User.available_roles = ['guest', 'admin', 'author']
|
14
|
+
|
15
|
+
User.new(:name => 'Kristian', :roles => ['guest']).save
|
16
|
+
User.new(:name => 'Admin user', :roles => ['admin']).save
|
17
|
+
User.new(:name => 'Author', :roles => ['author']).save
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return first user matching role" do
|
21
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return all users matching any of the roles" do
|
25
|
+
User.in_roles(['guest', 'admin']).size.should == 2
|
26
|
+
User.in_roles('guest', 'admin').size.should == 2
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'mongoid'
|
4
|
+
require 'roles_for_mongoid'
|
5
|
+
|
6
|
+
Mongoid.configure.master = Mongo::Connection.new.db('roles_for_mongoid')
|
7
|
+
|
8
|
+
module Database
|
9
|
+
def self.teardown
|
10
|
+
Mongoid.database.collections.each do |coll|
|
11
|
+
coll.remove
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roles_for_mongoid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Kristian Mandrup
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-02 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
- beta
|
33
|
+
- 17
|
34
|
+
version: 2.0.0.beta.17
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Facilitates adding Roles to your Mongoid user models
|
38
|
+
email: kmandrup@gmail.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- LICENSE
|
45
|
+
- README.markdown
|
46
|
+
files:
|
47
|
+
- .document
|
48
|
+
- .gitignore
|
49
|
+
- LICENSE
|
50
|
+
- README.markdown
|
51
|
+
- Rakefile
|
52
|
+
- VERSION
|
53
|
+
- lib/generators/mongoid/roles/roles_generator.rb
|
54
|
+
- lib/roles_for_mongoid.rb
|
55
|
+
- lib/roles_for_mongoid/role.rb
|
56
|
+
- lib/roles_for_mongoid/strategies/admin_flag.rb
|
57
|
+
- lib/roles_for_mongoid/strategies/inline_role.rb
|
58
|
+
- lib/roles_for_mongoid/strategies/inline_roles.rb
|
59
|
+
- lib/roles_for_mongoid/strategies/role_relations.rb
|
60
|
+
- lib/roles_for_mongoid/strategies/roles_mask.rb
|
61
|
+
- lib/roles_for_mongoid/user.rb
|
62
|
+
- sandbox/test.rb
|
63
|
+
- spec/roles_for_mongoid/user_admin_field_spec.rb
|
64
|
+
- spec/roles_for_mongoid/user_inline_role_spec.rb
|
65
|
+
- spec/roles_for_mongoid/user_inline_roles_spec.rb
|
66
|
+
- spec/roles_for_mongoid/user_role_relations_spec.rb
|
67
|
+
- spec/roles_for_mongoid/user_roles_mask_spec.rb
|
68
|
+
- spec/spec.opts
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/kristianmandrup/roles_for_mm
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --charset=UTF-8
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
none: false
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.7
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: Facilitates adding Roles to your Mongoid user models
|
102
|
+
test_files:
|
103
|
+
- spec/roles_for_mongoid/user_admin_field_spec.rb
|
104
|
+
- spec/roles_for_mongoid/user_inline_role_spec.rb
|
105
|
+
- spec/roles_for_mongoid/user_inline_roles_spec.rb
|
106
|
+
- spec/roles_for_mongoid/user_role_relations_spec.rb
|
107
|
+
- spec/roles_for_mongoid/user_roles_mask_spec.rb
|
108
|
+
- spec/spec_helper.rb
|