roles_for_mm 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.markdown +168 -0
- data/Rakefile +43 -0
- data/VERSION +1 -0
- data/lib/generators/mongo_mapper/roles/roles_generator.rb +46 -0
- data/lib/roles_for_mm.rb +1 -0
- data/lib/roles_for_mm/role.rb +4 -0
- data/lib/roles_for_mm/strategies/admin_flag.rb +20 -0
- data/lib/roles_for_mm/strategies/inline_role.rb +8 -0
- data/lib/roles_for_mm/strategies/inline_roles.rb +15 -0
- data/lib/roles_for_mm/strategies/role_relations.rb +23 -0
- data/lib/roles_for_mm/strategies/roles_mask.rb +52 -0
- data/lib/roles_for_mm/user.rb +12 -0
- data/spec/roles_for_mm/user_admin_field_spec.rb +24 -0
- data/spec/roles_for_mm/user_inline_role_spec.rb +24 -0
- data/spec/roles_for_mm/user_inline_roles_spec.rb +27 -0
- data/spec/roles_for_mm/user_role_relations_spec.rb +37 -0
- data/spec/roles_for_mm/user_roles_mask_spec.rb +29 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +21 -0
- metadata +122 -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,168 @@
|
|
1
|
+
# Roles for Mongo Mapper
|
2
|
+
|
3
|
+
# Install
|
4
|
+
|
5
|
+
<code>gem install roles_for_mm</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
|
+
Creates and uses a binary field 'admin', which when true signals that this user is an administrator and otherwise a normal user.
|
20
|
+
|
21
|
+
<pre>
|
22
|
+
require 'roles_for_mm'
|
23
|
+
|
24
|
+
User.role_strategy :admin_flag
|
25
|
+
|
26
|
+
before :each do
|
27
|
+
User.new(:name => 'Guest user', :role => 'guest').save
|
28
|
+
User.new(:name => 'Admin user', :role => 'admin').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
|
+
before :each do
|
38
|
+
User.new(:name => 'Guest user', :role => 'guest').save
|
39
|
+
User.new(:name => 'Admin user', :role => 'admin').save
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return first user maching role" do
|
43
|
+
User.in_role('guest').first.name.should == 'Guest user'
|
44
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
45
|
+
end
|
46
|
+
</pre>
|
47
|
+
|
48
|
+
## Example : Single inline role
|
49
|
+
|
50
|
+
Creates and uses a single role name, a string
|
51
|
+
|
52
|
+
<pre>
|
53
|
+
User.role_strategy :inline_role
|
54
|
+
|
55
|
+
# same as the previous admin_flag example
|
56
|
+
...
|
57
|
+
</pre>
|
58
|
+
|
59
|
+
## Example : Multiple inline roles
|
60
|
+
|
61
|
+
Creates and uses an array of role names as strings
|
62
|
+
|
63
|
+
<pre>
|
64
|
+
User.role_strategy :inline_roles
|
65
|
+
|
66
|
+
before :each do
|
67
|
+
guest_user = User.new(:name => 'Guest user', :roles => ['guest']).save
|
68
|
+
admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
|
69
|
+
author_user = User.new(:name => 'Author', :roles => ['author']).save
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should return first user matching role" do
|
73
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should return all users matching any of the roles" do
|
77
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
78
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
79
|
+
end
|
80
|
+
</pre>
|
81
|
+
|
82
|
+
## Example : Multiple role relations
|
83
|
+
|
84
|
+
Creates and uses a 1-M relation to a Role model, which the User model stores as an array of BSON ObjectIDs
|
85
|
+
|
86
|
+
<pre>
|
87
|
+
require 'roles_for_mm'
|
88
|
+
|
89
|
+
User.role_strategy :roles_relations
|
90
|
+
|
91
|
+
before :each do
|
92
|
+
guest_user = User.new(:name => 'Guest user')
|
93
|
+
admin_user = User.new(:name => 'Admin user')
|
94
|
+
author_user = User.new(:name => 'Author')
|
95
|
+
|
96
|
+
guest_user.roles << Role.new(:name => 'guest')
|
97
|
+
guest_user.save
|
98
|
+
|
99
|
+
admin_user.roles << Role.new(:name => 'admin')
|
100
|
+
admin_user.save
|
101
|
+
|
102
|
+
author_user.roles << Role.new(:name => 'author')
|
103
|
+
author_user.save
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should return first user matching role" do
|
107
|
+
User.in_role('guest').first.name.should == 'Guest user'
|
108
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should return all users matching any of the roles" do
|
112
|
+
User.in_roles(['guest', 'admin']).all.size.should == 2
|
113
|
+
User.in_roles('guest', 'admin').all.size.should == 2
|
114
|
+
end
|
115
|
+
</pre>
|
116
|
+
|
117
|
+
## Example : Roles mask
|
118
|
+
|
119
|
+
Creates and uses an inline Integer field in the User model called 'role_mask'
|
120
|
+
|
121
|
+
<pre>
|
122
|
+
before :each do
|
123
|
+
User.available_roles = ['guest', 'admin', 'author']
|
124
|
+
|
125
|
+
User.new(:name => 'Kristian', :roles => ['guest']).save
|
126
|
+
User.new(:name => 'Admin user', :roles => ['admin']).save
|
127
|
+
User.new(:name => 'Author', :roles => ['author']).save
|
128
|
+
end
|
129
|
+
|
130
|
+
it "should return first user matching role" do
|
131
|
+
User.in_role('admin').first.name.should == 'Admin user'
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should return all users matching any of the roles" do
|
135
|
+
User.in_roles(['guest', 'admin']).size.should == 2
|
136
|
+
User.in_roles('guest', 'admin').size.should == 2
|
137
|
+
end
|
138
|
+
</pre>
|
139
|
+
|
140
|
+
## Rails generator
|
141
|
+
|
142
|
+
The library comes with a Rails 3 generator that lets you populate a user model with a given role strategy
|
143
|
+
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.
|
144
|
+
|
145
|
+
* admin_flag
|
146
|
+
* inline_role
|
147
|
+
* inline_roles
|
148
|
+
* role_relations
|
149
|
+
* role_mask
|
150
|
+
|
151
|
+
Example:
|
152
|
+
|
153
|
+
<code>$ rails g mongo_mapper:roles user admin_flag</code>
|
154
|
+
|
155
|
+
|
156
|
+
## Note on Patches/Pull Requests
|
157
|
+
|
158
|
+
* Fork the project.
|
159
|
+
* Make your feature addition or bug fix.
|
160
|
+
* Add tests for it. This is important so I don't break it in a
|
161
|
+
future version unintentionally.
|
162
|
+
* Commit, do not mess with rakefile, version, or history.
|
163
|
+
(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)
|
164
|
+
* Send me a pull request. Bonus points for topic branches.
|
165
|
+
|
166
|
+
## Copyright
|
167
|
+
|
168
|
+
Copyright (c) 2010 Kristian Mandrup. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gem|
|
4
|
+
gem.name = "roles_for_mm"
|
5
|
+
gem.summary = %Q{Faciliatates adding a role strategy to your Mongo Mapper user model}
|
6
|
+
gem.description = %Q{Faciliatates adding a role strategy to your Mongo Mapper user model}
|
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.19"
|
11
|
+
gem.add_dependency "mongo_mapper", ">= 0.8.2"
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
# require 'spec/rake/spectask'
|
20
|
+
# Spec::Rake::SpecTask.new(:spec) do |spec|
|
21
|
+
# spec.libs << 'lib' << 'spec'
|
22
|
+
# spec.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
# end
|
24
|
+
#
|
25
|
+
# Spec::Rake::SpecTask.new(:rcov) do |spec|
|
26
|
+
# spec.libs << 'lib' << 'spec'
|
27
|
+
# spec.pattern = 'spec/**/*_spec.rb'
|
28
|
+
# spec.rcov = true
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# task :spec => :check_dependencies
|
32
|
+
#
|
33
|
+
# task :default => :spec
|
34
|
+
#
|
35
|
+
# require 'rake/rdoctask'
|
36
|
+
# Rake::RDocTask.new do |rdoc|
|
37
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
38
|
+
#
|
39
|
+
# rdoc.rdoc_dir = 'rdoc'
|
40
|
+
# rdoc.title = "roles_for_mm #{version}"
|
41
|
+
# rdoc.rdoc_files.include('README*')
|
42
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
43
|
+
# end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MongoMapper
|
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 self.source_root
|
11
|
+
@source_root ||= File.expand_path("../../templates", __FILE__)
|
12
|
+
end
|
13
|
+
|
14
|
+
def apply_role_strategy
|
15
|
+
insert_into_model('user', role_strategy_statement)
|
16
|
+
end
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def match_expr
|
21
|
+
/include MongoMapper::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}.rb")
|
34
|
+
end
|
35
|
+
|
36
|
+
def insert_into_model(model_name, insert_text)
|
37
|
+
model_name = model_name.to_s
|
38
|
+
file = File.new(model_file(model_name))
|
39
|
+
return if (file.read =~ /#{insert_text}/)
|
40
|
+
gsub_file model_file(model_name), match_expr do |match|
|
41
|
+
match << insert_text
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/roles_for_mm.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'roles_for_mm/user'
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module AdminFlag
|
3
|
+
|
4
|
+
def self.included base
|
5
|
+
base.extend ClassMethods
|
6
|
+
base.key :admin, Boolean
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def in_role(role_name)
|
11
|
+
case role_name.downcase.to_sym
|
12
|
+
when :admin
|
13
|
+
where(:admin => true)
|
14
|
+
else
|
15
|
+
where(:admin => false)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module InlineRoles
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.key :roles, Array
|
6
|
+
base.scope :in_role, lambda { |role_name| base.where(:roles => [role_name]) }
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def in_roles(*roles)
|
11
|
+
where(:roles => roles.flatten)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'roles_for_mm/role'
|
2
|
+
|
3
|
+
module MongoMapper
|
4
|
+
module RoleRelations
|
5
|
+
def self.included base
|
6
|
+
base.extend ClassMethods
|
7
|
+
base.key :role_ids, Array, :index => true, :typecast => 'ObjectId'
|
8
|
+
base.many :roles, :in => :role_ids
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def in_role(role_name)
|
13
|
+
role = Role.where(:name => role_name).first
|
14
|
+
all(:role_ids => role.id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def in_roles(*roles)
|
18
|
+
role_ids = Role.where(:name => roles.flatten).map{|role| role.id}
|
19
|
+
where(:role_ids => role_ids)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
module RolesMask
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
base.key :roles_mask, Integer
|
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 MongoMapper::Document
|
6
|
+
|
7
|
+
def self.role_strategy type
|
8
|
+
include "MongoMapper::#{type.to_s.camelize}".constantize
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
require_all File.dirname(__FILE__) + '/strategies'
|
@@ -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 MongoMapper" do
|
6
|
+
describe "Inline admin field" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
guest_user = User.new :name => 'Kristian', :admin => false
|
14
|
+
admin_user = User.new :name => 'Admin user', :admin => true
|
15
|
+
admin_user.save
|
16
|
+
guest_user.save
|
17
|
+
end
|
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,24 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :inline_role
|
4
|
+
|
5
|
+
describe "Roles for MongoMapper" do
|
6
|
+
describe "Inline role" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
guest_user = User.new :name => 'Kristian', :role => 'guest'
|
14
|
+
admin_user = User.new :name => 'Admin user', :role => 'admin'
|
15
|
+
admin_user.save
|
16
|
+
guest_user.save
|
17
|
+
end
|
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,27 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
User.role_strategy :inline_roles
|
4
|
+
|
5
|
+
describe "Roles for MongoMapper" do
|
6
|
+
describe "Inline roles" do
|
7
|
+
|
8
|
+
after :each do
|
9
|
+
Database.teardown
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
guest_user = User.new(:name => 'Kristian', :roles => ['guest']).save
|
14
|
+
admin_user = User.new(:name => 'Admin user', :roles => ['admin']).save
|
15
|
+
author_user = User.new(:name => 'Author', :roles => ['author']).save
|
16
|
+
end
|
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']).all.size.should == 2
|
24
|
+
User.in_roles('guest', 'admin').all.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 MongoMapper" do
|
6
|
+
|
7
|
+
after :each do
|
8
|
+
Database.teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
guest_user = User.new(:name => 'Kristian')
|
13
|
+
admin_user = User.new(:name => 'Admin user')
|
14
|
+
author_user = User.new(:name => 'Author')
|
15
|
+
|
16
|
+
guest_user.roles << Role.new(:name => 'guest')
|
17
|
+
guest_user.save
|
18
|
+
|
19
|
+
admin_user.roles << Role.new(:name => 'admin')
|
20
|
+
admin_user.save
|
21
|
+
|
22
|
+
author_user.roles << Role.new(:name => 'author')
|
23
|
+
author_user.save
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Role relations" 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 MongoMapper" do
|
6
|
+
describe "Inline roles" 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,21 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'mongo_mapper'
|
4
|
+
require 'roles_for_mm'
|
5
|
+
|
6
|
+
MongoMapper.database = 'roles_for_mm'
|
7
|
+
|
8
|
+
module Database
|
9
|
+
def self.teardown
|
10
|
+
# MongoMapper.database.collections.each {|collection| collection.drop }
|
11
|
+
MongoMapper.database.collections.each do |coll|
|
12
|
+
coll.drop unless coll.name =~ /(.*\.)?system\..*/
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
RSpec.configure do |config|
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: roles_for_mm
|
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
|
+
- 19
|
34
|
+
version: 2.0.0.beta.19
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: mongo_mapper
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
- 8
|
48
|
+
- 2
|
49
|
+
version: 0.8.2
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
52
|
+
description: Faciliatates adding a role strategy to your Mongo Mapper user model
|
53
|
+
email: kmandrup@gmail.com
|
54
|
+
executables: []
|
55
|
+
|
56
|
+
extensions: []
|
57
|
+
|
58
|
+
extra_rdoc_files:
|
59
|
+
- LICENSE
|
60
|
+
- README.markdown
|
61
|
+
files:
|
62
|
+
- .document
|
63
|
+
- .gitignore
|
64
|
+
- LICENSE
|
65
|
+
- README.markdown
|
66
|
+
- Rakefile
|
67
|
+
- VERSION
|
68
|
+
- lib/generators/mongo_mapper/roles/roles_generator.rb
|
69
|
+
- lib/roles_for_mm.rb
|
70
|
+
- lib/roles_for_mm/role.rb
|
71
|
+
- lib/roles_for_mm/strategies/admin_flag.rb
|
72
|
+
- lib/roles_for_mm/strategies/inline_role.rb
|
73
|
+
- lib/roles_for_mm/strategies/inline_roles.rb
|
74
|
+
- lib/roles_for_mm/strategies/role_relations.rb
|
75
|
+
- lib/roles_for_mm/strategies/roles_mask.rb
|
76
|
+
- lib/roles_for_mm/user.rb
|
77
|
+
- spec/roles_for_mm/user_admin_field_spec.rb
|
78
|
+
- spec/roles_for_mm/user_inline_role_spec.rb
|
79
|
+
- spec/roles_for_mm/user_inline_roles_spec.rb
|
80
|
+
- spec/roles_for_mm/user_role_relations_spec.rb
|
81
|
+
- spec/roles_for_mm/user_roles_mask_spec.rb
|
82
|
+
- spec/spec.opts
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc: true
|
85
|
+
homepage: http://github.com/kristianmandrup/roles_for_mm
|
86
|
+
licenses: []
|
87
|
+
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options:
|
90
|
+
- --charset=UTF-8
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
segments:
|
107
|
+
- 0
|
108
|
+
version: "0"
|
109
|
+
requirements: []
|
110
|
+
|
111
|
+
rubyforge_project:
|
112
|
+
rubygems_version: 1.3.7
|
113
|
+
signing_key:
|
114
|
+
specification_version: 3
|
115
|
+
summary: Faciliatates adding a role strategy to your Mongo Mapper user model
|
116
|
+
test_files:
|
117
|
+
- spec/roles_for_mm/user_admin_field_spec.rb
|
118
|
+
- spec/roles_for_mm/user_inline_role_spec.rb
|
119
|
+
- spec/roles_for_mm/user_inline_roles_spec.rb
|
120
|
+
- spec/roles_for_mm/user_role_relations_spec.rb
|
121
|
+
- spec/roles_for_mm/user_roles_mask_spec.rb
|
122
|
+
- spec/spec_helper.rb
|