roles_mongoid 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.bundle/config +2 -0
- data/.document +5 -0
- data/.gitignore +21 -0
- data/.rspec +1 -0
- data/Gemfile +15 -0
- data/LICENSE +20 -0
- data/README.markdown +59 -0
- data/Rakefile +24 -0
- data/VERSION +1 -0
- data/lib/generators/mongoid/roles/roles_generator.rb +68 -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/multi/many_roles.rb +67 -0
- data/lib/roles_mongoid/strategy/multi/role_strings.rb +59 -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 +57 -0
- data/lib/roles_mongoid/strategy/single/role_string.rb +51 -0
- data/lib/roles_mongoid/strategy.rb +15 -0
- data/lib/roles_mongoid.rb +10 -0
- data/roles_mongoid.gemspec +105 -0
- data/sandbox/roles_assign.rb +54 -0
- data/sandbox/single_role.rb +68 -0
- data/sandbox/test.rb +47 -0
- data/spec/generator_spec_helper.rb +13 -0
- data/spec/generators/roles_generator_spec.rb +59 -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 +28 -0
- data/tmp/rails/app/models/user.rb +8 -0
- data/tmp/rails/config/routes.rb +2 -0
- metadata +216 -0
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'roles_generic'
|
2
|
+
require 'require_all'
|
3
|
+
require 'set'
|
4
|
+
require 'sugar-high/array'
|
5
|
+
require 'sugar-high/kind_of'
|
6
|
+
require 'mongoid'
|
7
|
+
require 'bson'
|
8
|
+
require 'roles_mongoid/namespaces'
|
9
|
+
require 'roles_mongoid/base'
|
10
|
+
require 'roles_mongoid/strategy'
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{roles_mongoid}
|
8
|
+
s.version = "0.2.4"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Kristian Mandrup"]
|
12
|
+
s.date = %q{2010-10-06}
|
13
|
+
s.description = %q{Makes it easy to set a role strategy on your User model in Mongoid}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".bundle/config",
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
".rspec",
|
24
|
+
"Gemfile",
|
25
|
+
"LICENSE",
|
26
|
+
"README.markdown",
|
27
|
+
"Rakefile",
|
28
|
+
"VERSION",
|
29
|
+
"lib/generators/mongoid/roles/roles_generator.rb",
|
30
|
+
"lib/roles_mongoid.rb",
|
31
|
+
"lib/roles_mongoid/base.rb",
|
32
|
+
"lib/roles_mongoid/namespaces.rb",
|
33
|
+
"lib/roles_mongoid/role.rb",
|
34
|
+
"lib/roles_mongoid/strategy.rb",
|
35
|
+
"lib/roles_mongoid/strategy/multi/many_roles.rb",
|
36
|
+
"lib/roles_mongoid/strategy/multi/role_strings.rb",
|
37
|
+
"lib/roles_mongoid/strategy/multi/roles_mask.rb",
|
38
|
+
"lib/roles_mongoid/strategy/single/admin_flag.rb",
|
39
|
+
"lib/roles_mongoid/strategy/single/one_role.rb",
|
40
|
+
"lib/roles_mongoid/strategy/single/role_string.rb",
|
41
|
+
"roles_mongoid.gemspec",
|
42
|
+
"sandbox/roles_assign.rb",
|
43
|
+
"sandbox/single_role.rb",
|
44
|
+
"sandbox/test.rb",
|
45
|
+
"spec/generator_spec_helper.rb",
|
46
|
+
"spec/generators/roles_generator_spec.rb",
|
47
|
+
"spec/roles_mongoid/admin_flag_spec.rb",
|
48
|
+
"spec/roles_mongoid/many_roles_spec.rb",
|
49
|
+
"spec/roles_mongoid/one_role_spec.rb",
|
50
|
+
"spec/roles_mongoid/role_string_spec.rb",
|
51
|
+
"spec/roles_mongoid/role_strings_spec.rb",
|
52
|
+
"spec/roles_mongoid/roles_mask_spec.rb",
|
53
|
+
"spec/spec_helper.rb",
|
54
|
+
"tmp/rails/app/models/user.rb",
|
55
|
+
"tmp/rails/config/routes.rb"
|
56
|
+
]
|
57
|
+
s.homepage = %q{http://github.com/kristianmandrup/roles_mongoid}
|
58
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
59
|
+
s.require_paths = ["lib"]
|
60
|
+
s.rubygems_version = %q{1.3.7}
|
61
|
+
s.summary = %q{Implementation of Roles generic API for Mongoid}
|
62
|
+
s.test_files = [
|
63
|
+
"spec/generator_spec_helper.rb",
|
64
|
+
"spec/generators/roles_generator_spec.rb",
|
65
|
+
"spec/roles_mongoid/admin_flag_spec.rb",
|
66
|
+
"spec/roles_mongoid/many_roles_spec.rb",
|
67
|
+
"spec/roles_mongoid/one_role_spec.rb",
|
68
|
+
"spec/roles_mongoid/role_string_spec.rb",
|
69
|
+
"spec/roles_mongoid/role_strings_spec.rb",
|
70
|
+
"spec/roles_mongoid/roles_mask_spec.rb",
|
71
|
+
"spec/spec_helper.rb"
|
72
|
+
]
|
73
|
+
|
74
|
+
if s.respond_to? :specification_version then
|
75
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
76
|
+
s.specification_version = 3
|
77
|
+
|
78
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
79
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
80
|
+
s.add_runtime_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
81
|
+
s.add_runtime_dependency(%q<bson>, ["~> 1.0.7"])
|
82
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
|
83
|
+
s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
|
84
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.11"])
|
85
|
+
s.add_runtime_dependency(%q<roles_generic>, ["~> 0.2.7"])
|
86
|
+
else
|
87
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
88
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
89
|
+
s.add_dependency(%q<bson>, ["~> 1.0.7"])
|
90
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
91
|
+
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
|
92
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.11"])
|
93
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.7"])
|
94
|
+
end
|
95
|
+
else
|
96
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
97
|
+
s.add_dependency(%q<mongoid>, ["~> 2.0.0.beta.18"])
|
98
|
+
s.add_dependency(%q<bson>, ["~> 1.0.7"])
|
99
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
100
|
+
s.add_dependency(%q<require_all>, ["~> 1.2.0"])
|
101
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.11"])
|
102
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.7"])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
@@ -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
|
+
|
data/sandbox/test.rb
ADDED
@@ -0,0 +1,47 @@
|
|
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
|
+
def self.get_roles(*ids)
|
26
|
+
arr = ids.flatten
|
27
|
+
criteria.in(:role_ids => arr).to_a
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class Role
|
33
|
+
include Mongoid::Document
|
34
|
+
field :name
|
35
|
+
end
|
36
|
+
|
37
|
+
user = User.create(:name => 'Kristian')
|
38
|
+
user.roles.create(:name => 'guest')
|
39
|
+
user.save
|
40
|
+
#
|
41
|
+
puts "user roles: #{User.first.roles.map(&:_id)}"
|
42
|
+
|
43
|
+
role_id = user.roles.to_a.first._id
|
44
|
+
|
45
|
+
p role_id
|
46
|
+
p User.find_roles role_id
|
47
|
+
p User.get_roles role_id
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'rspec/autorun'
|
3
|
+
require 'rails3_artifactor'
|
4
|
+
require 'generator-spec'
|
5
|
+
require 'roles_generic'
|
6
|
+
|
7
|
+
RSpec::Generator.configure do |config|
|
8
|
+
config.debug = false
|
9
|
+
config.remove_temp_dir = false # true
|
10
|
+
config.default_rails_root(__FILE__)
|
11
|
+
config.lib = File.dirname(__FILE__) + '/../lib'
|
12
|
+
config.logger = :stdout
|
13
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'generator_spec_helper'
|
2
|
+
require_generator :mongoid => :roles
|
3
|
+
|
4
|
+
# root_dir = Rails3::Assist::Directory.rails_root
|
5
|
+
root_dir = File.join(Rails.application.config.root_dir, 'rails')
|
6
|
+
|
7
|
+
describe 'role strategy generator: admin_flag' do
|
8
|
+
describe 'ORM: mongoid' do
|
9
|
+
use_orm :mongoid
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
setup_generator 'mongoid_roles_generator' do
|
13
|
+
tests Mongoid::Generators::RolesGenerator
|
14
|
+
end
|
15
|
+
remove_model :user
|
16
|
+
end
|
17
|
+
|
18
|
+
after :each do
|
19
|
+
# remove_model :user
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should configure 'admin_flag' strategy" do
|
23
|
+
create_model :user do
|
24
|
+
'# content'
|
25
|
+
end
|
26
|
+
with_generator do |g|
|
27
|
+
arguments = "User --strategy admin_flag --roles admin user"
|
28
|
+
puts "arguments: #{arguments}"
|
29
|
+
g.run_generator arguments.args
|
30
|
+
root_dir.should have_model :user do |clazz|
|
31
|
+
clazz.should include_module 'Mongoid::Document'
|
32
|
+
clazz.should include_module 'Roles::Mongoid'
|
33
|
+
puts "clazz: #{clazz}"
|
34
|
+
clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
|
35
|
+
clazz.should have_call :strategy, :args => ":admin_flag"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should configure 'one_role' strategy" do
|
41
|
+
create_model :user do
|
42
|
+
'# content'
|
43
|
+
end
|
44
|
+
with_generator do |g|
|
45
|
+
arguments = "User --strategy one_role --roles admin user"
|
46
|
+
puts "arguments: #{arguments}"
|
47
|
+
g.run_generator arguments.args
|
48
|
+
root_dir.should have_model :user do |clazz|
|
49
|
+
clazz.should include_module 'Mongoid::Document'
|
50
|
+
clazz.should include_module 'Roles::Mongoid'
|
51
|
+
puts "clazz: #{clazz}"
|
52
|
+
clazz.should have_call :valid_roles_are, :args => ':admin, :guest, :user'
|
53
|
+
clazz.should have_call :strategy, :args => ":one_role"
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :admin_flag
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Mongoid::Document
|
6
|
+
include Roles::Mongoid
|
7
|
+
|
8
|
+
strategy :admin_flag, :default
|
9
|
+
valid_roles_are :admin, :guest
|
10
|
+
|
11
|
+
field :name, :type => String
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "Roles for Mongoid" do
|
15
|
+
after :each do
|
16
|
+
Database.teardown
|
17
|
+
end
|
18
|
+
|
19
|
+
before :each do
|
20
|
+
@user = User.create(:name => 'Kristian')
|
21
|
+
@user.roles = :guest
|
22
|
+
@user.save
|
23
|
+
|
24
|
+
@admin_user = User.create(:name => 'Admin user')
|
25
|
+
@admin_user.roles = :admin
|
26
|
+
@admin_user.save
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "Admin flag strategy " do
|
30
|
+
|
31
|
+
describe '#in_role' do
|
32
|
+
it "should return first user maching role" do
|
33
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
34
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'roles API' do
|
39
|
+
it "should have admin user role to :admin" do
|
40
|
+
@admin_user.role.should == :admin
|
41
|
+
@admin_user.roles.should == [:admin]
|
42
|
+
@admin_user.admin?.should be_true
|
43
|
+
|
44
|
+
@admin_user.class.valid_roles.should == [:admin, :guest]
|
45
|
+
|
46
|
+
@admin_user.has_role?(:guest).should be_false
|
47
|
+
|
48
|
+
@admin_user.has_role?(:admin).should be_true
|
49
|
+
@admin_user.is?(:admin).should be_true
|
50
|
+
@admin_user.has_roles?(:admin).should be_true
|
51
|
+
@admin_user.has?(:admin).should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have user role to :guest" do
|
55
|
+
@user.roles.should == [:guest]
|
56
|
+
@user.admin?.should be_false
|
57
|
+
|
58
|
+
@user.has_role?(:guest).should be_true
|
59
|
+
@user.has_role?(:admin).should be_false
|
60
|
+
@user.is?(:admin).should be_false
|
61
|
+
|
62
|
+
@user.has_roles?(:guest).should be_true
|
63
|
+
@user.has?(:admin).should be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set user role to :admin using =" do
|
67
|
+
@user.roles = :admin
|
68
|
+
@user.role.should == :admin
|
69
|
+
@user.has_role?(:admin).should be_true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :many_roles
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Mongoid::Document
|
6
|
+
include Roles::Mongoid
|
7
|
+
|
8
|
+
strategy :many_roles, :default
|
9
|
+
role_class :role
|
10
|
+
|
11
|
+
valid_roles_are :admin, :guest
|
12
|
+
|
13
|
+
field :name, :type => String
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "Roles for Mongoid" do
|
17
|
+
|
18
|
+
after :each do
|
19
|
+
Database.teardown
|
20
|
+
end
|
21
|
+
|
22
|
+
context "default setup" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
@user = User.create(:name => 'Kristian')
|
26
|
+
@user.add_roles :guest
|
27
|
+
@user.save
|
28
|
+
|
29
|
+
@admin_user = User.create(:name => 'Admin user')
|
30
|
+
@admin_user.roles = :admin
|
31
|
+
@admin_user.save
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#in_role' do
|
35
|
+
it "should return first user matching role" do
|
36
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
37
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "Role API" do
|
42
|
+
it "should have admin user role to :admin" do
|
43
|
+
@admin_user.roles_list.first.should == :admin
|
44
|
+
@admin_user.admin?.should be_true
|
45
|
+
|
46
|
+
@admin_user.has_role?(:guest).should be_false
|
47
|
+
|
48
|
+
@admin_user.has_role?(:admin).should be_true
|
49
|
+
@admin_user.is?(:admin).should be_true
|
50
|
+
@admin_user.has_roles?(:admin).should be_true
|
51
|
+
@admin_user.has?(:admin).should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have user role to :guest" do
|
55
|
+
@user.roles_list.first.should == :guest
|
56
|
+
@user.admin?.should be_false
|
57
|
+
|
58
|
+
@user.has_role?(:guest).should be_true
|
59
|
+
@user.has_role?(:admin).should be_false
|
60
|
+
@user.is?(:admin).should be_false
|
61
|
+
|
62
|
+
@user.has_roles?(:admin).should be_false
|
63
|
+
@user.has?(:admin).should be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set user role to :admin using roles=" do
|
67
|
+
@user.roles = :admin
|
68
|
+
@user.roles_list.first.should == :admin
|
69
|
+
@user.has_role?(:admin).should be_true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :one_role
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Mongoid::Document
|
6
|
+
include Roles::Mongoid
|
7
|
+
|
8
|
+
strategy :one_role, :default
|
9
|
+
role_class :role
|
10
|
+
|
11
|
+
valid_roles_are :admin, :guest
|
12
|
+
|
13
|
+
field :name, :type => String
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "Roles for Mongoid" do
|
17
|
+
|
18
|
+
after :each do
|
19
|
+
Database.teardown
|
20
|
+
end
|
21
|
+
|
22
|
+
context "default setup" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
@user = User.create(:name => 'Kristian')
|
26
|
+
@user.role = :guest
|
27
|
+
@user.save
|
28
|
+
|
29
|
+
@admin_user = User.create(:name => 'Admin user')
|
30
|
+
@admin_user.role = Role.where(:name => :admin).to_a.first
|
31
|
+
@admin_user.save
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#in_role' do
|
35
|
+
it "should return first user matching role" do
|
36
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
37
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "Role API" do
|
42
|
+
it "should have admin user role to :admin" do
|
43
|
+
@admin_user.roles_list.first.should == :admin
|
44
|
+
@admin_user.admin?.should be_true
|
45
|
+
|
46
|
+
@admin_user.has_role?(:guest).should be_false
|
47
|
+
|
48
|
+
@admin_user.has_role?(:admin).should be_true
|
49
|
+
@admin_user.is?(:admin).should be_true
|
50
|
+
@admin_user.has_roles?(:admin).should be_true
|
51
|
+
@admin_user.has?(:admin).should be_true
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should have user role to :guest" do
|
55
|
+
@user.roles_list.first.should == :guest
|
56
|
+
@user.admin?.should be_false
|
57
|
+
|
58
|
+
@user.has_role?(:guest).should be_true
|
59
|
+
@user.has_role?(:admin).should be_false
|
60
|
+
@user.is?(:admin).should be_false
|
61
|
+
|
62
|
+
@user.has_roles?(:admin).should be_false
|
63
|
+
@user.has?(:admin).should be_false
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should set user role to :admin using roles=" do
|
67
|
+
@user.roles = :admin
|
68
|
+
@user.roles_list.first.should == :admin
|
69
|
+
@user.has_role?(:admin).should be_true
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
use_roles_strategy :role_string
|
3
|
+
|
4
|
+
class User
|
5
|
+
include Mongoid::Document
|
6
|
+
include Roles::Mongoid
|
7
|
+
|
8
|
+
strategy :role_string, :default
|
9
|
+
|
10
|
+
valid_roles_are :admin, :guest
|
11
|
+
|
12
|
+
field :name, :type => String
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "Roles for Mongoid" do
|
16
|
+
|
17
|
+
after :each do
|
18
|
+
Database.teardown
|
19
|
+
end
|
20
|
+
|
21
|
+
context "default setup" do
|
22
|
+
|
23
|
+
before do
|
24
|
+
@user = User.create(:name => 'Kristian')
|
25
|
+
@user.role = :guest
|
26
|
+
@user.save
|
27
|
+
|
28
|
+
@admin_user = User.create(:name => 'Admin user')
|
29
|
+
@admin_user.roles = :admin
|
30
|
+
@admin_user.save
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#in_role' do
|
34
|
+
it "should return first user matching role" do
|
35
|
+
User.in_role(:guest).first.name.should == 'Kristian'
|
36
|
+
User.in_role(:admin).first.name.should == 'Admin user'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "Role API" do
|
41
|
+
it "should have admin user role to :admin" do
|
42
|
+
@admin_user.roles_list.first.should == :admin
|
43
|
+
@admin_user.admin?.should be_true
|
44
|
+
|
45
|
+
@admin_user.has_role?(:guest).should be_false
|
46
|
+
|
47
|
+
@admin_user.has_role?(:admin).should be_true
|
48
|
+
@admin_user.is?(:admin).should be_true
|
49
|
+
@admin_user.has_roles?(:admin).should be_true
|
50
|
+
@admin_user.has?(:admin).should be_true
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should have user role to :guest" do
|
54
|
+
@user.roles_list.first.should == :guest
|
55
|
+
@user.admin?.should be_false
|
56
|
+
|
57
|
+
@user.has_role?(:guest).should be_true
|
58
|
+
@user.has_role?(:admin).should be_false
|
59
|
+
@user.is?(:admin).should be_false
|
60
|
+
|
61
|
+
@user.has_roles?(:admin).should be_false
|
62
|
+
@user.has?(:admin).should be_false
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should set user role to :admin using roles=" do
|
66
|
+
@user.roles = :admin
|
67
|
+
@user.roles_list.first.should == :admin
|
68
|
+
@user.has_role?(:admin).should be_true
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|