roles_mongo_mapper 0.1.1
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/.rspec +1 -0
- data/LICENSE +20 -0
- data/README.markdown +62 -0
- data/Rakefile +26 -0
- data/VERSION +1 -0
- data/lib/generators/mongo_mapper/roles/roles_generator.rb +69 -0
- data/lib/roles_mongo_mapper.rb +8 -0
- data/lib/roles_mongo_mapper/base.rb +34 -0
- data/lib/roles_mongo_mapper/namespaces.rb +12 -0
- data/lib/roles_mongo_mapper/role.rb +31 -0
- data/lib/roles_mongo_mapper/strategy.rb +15 -0
- data/lib/roles_mongo_mapper/strategy/multi/many_roles.rb +69 -0
- data/lib/roles_mongo_mapper/strategy/multi/role_strings.rb +59 -0
- data/lib/roles_mongo_mapper/strategy/multi/roles_mask.rb +81 -0
- data/lib/roles_mongo_mapper/strategy/single/admin_flag.rb +50 -0
- data/lib/roles_mongo_mapper/strategy/single/one_role.rb +72 -0
- data/lib/roles_mongo_mapper/strategy/single/role_string.rb +51 -0
- data/logging.log +8 -0
- data/roles_mongo_mapper.gemspec +103 -0
- data/sandbox/test.rb +19 -0
- data/spec/generator_spec_helper.rb +12 -0
- data/spec/roles_mongo_mapper/generators/roles_generator_spec.rb +59 -0
- data/spec/roles_mongo_mapper/strategy/admin_flag_spec.rb +76 -0
- data/spec/roles_mongo_mapper/strategy/many_roles_spec.rb +69 -0
- data/spec/roles_mongo_mapper/strategy/one_role_spec.rb +66 -0
- data/spec/roles_mongo_mapper/strategy/role_string_spec.rb +68 -0
- data/spec/roles_mongo_mapper/strategy/role_strings_spec.rb +69 -0
- data/spec/roles_mongo_mapper/strategy/roles_mask_spec.rb +69 -0
- data/spec/spec_helper.rb +27 -0
- metadata +224 -0
@@ -0,0 +1,81 @@
|
|
1
|
+
module RoleStrategy::MongoMapper
|
2
|
+
module RolesMask
|
3
|
+
def self.default_role_attribute
|
4
|
+
:roles_mask
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def role_attribute
|
13
|
+
strategy_class.roles_attribute_name.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_role(role)
|
17
|
+
mask = calc_index(role.to_s)
|
18
|
+
all.select do |user|
|
19
|
+
(user.send(role_attribute) & mask) > 0
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def in_roles(*roles)
|
24
|
+
all.select do |user|
|
25
|
+
roles.flatten.any? do |role|
|
26
|
+
mask = calc_index(role.to_s)
|
27
|
+
(user.send(role_attribute) & mask) > 0
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def calc_index(r)
|
33
|
+
2**strategy_class.valid_roles.index(r.to_sym)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Implementation
|
38
|
+
class Roles < ::Set # :nodoc:
|
39
|
+
attr_reader :model_instance
|
40
|
+
|
41
|
+
def initialize(sender, *roles)
|
42
|
+
super(*roles)
|
43
|
+
@model_instance = sender
|
44
|
+
end
|
45
|
+
|
46
|
+
def <<(role)
|
47
|
+
model_instance.roles = super.to_a
|
48
|
+
self
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def role_attribute
|
53
|
+
strategy_class.roles_attribute_name
|
54
|
+
end
|
55
|
+
|
56
|
+
# assign roles
|
57
|
+
def roles=(*roles)
|
58
|
+
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 })
|
59
|
+
end
|
60
|
+
alias_method :role=, :roles=
|
61
|
+
|
62
|
+
# query assigned roles
|
63
|
+
def roles
|
64
|
+
strategy_class::Roles.new(self, strategy_class.valid_roles.reject { |r| ((self.send(role_attribute) || 0) & calc_index(r)).zero? })
|
65
|
+
end
|
66
|
+
|
67
|
+
def roles_list
|
68
|
+
roles.to_a
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def calc_index(r)
|
74
|
+
2**strategy_class.valid_roles.index(r)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
extend Roles::Generic::User::Configuration
|
79
|
+
configure
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module RoleStrategy::MongoMapper
|
2
|
+
module AdminFlag
|
3
|
+
|
4
|
+
def self.default_role_attribute
|
5
|
+
:admin_flag
|
6
|
+
end
|
7
|
+
|
8
|
+
def self.included base
|
9
|
+
base.extend ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def role_attribute
|
14
|
+
strategy_class.roles_attribute_name.to_sym
|
15
|
+
end
|
16
|
+
|
17
|
+
def in_role(role_name)
|
18
|
+
case role_name.downcase.to_sym
|
19
|
+
when :admin
|
20
|
+
where(role_attribute => true)
|
21
|
+
else
|
22
|
+
where(role_attribute => false)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Implementation
|
28
|
+
# assign roles
|
29
|
+
def roles=(*new_roles)
|
30
|
+
first_role = new_roles.flatten.first
|
31
|
+
if valid_role?(first_role)
|
32
|
+
self.send("#{role_attribute}=", new_roles.flatten.first.admin?)
|
33
|
+
else
|
34
|
+
raise ArgumentError, "The role #{first_role} is not a valid role"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# query assigned roles
|
39
|
+
def roles
|
40
|
+
role = self.send(role_attribute) ? strategy_class.admin_role_key : strategy_class.default_role_key
|
41
|
+
[role]
|
42
|
+
end
|
43
|
+
alias_method :roles_list, :roles
|
44
|
+
|
45
|
+
end # Implementation
|
46
|
+
|
47
|
+
extend Roles::Generic::User::Configuration
|
48
|
+
configure :num => :single
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module RoleStrategy::MongoMapper
|
2
|
+
module OneRole
|
3
|
+
def self.default_role_attribute
|
4
|
+
:one_role
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend Roles::Generic::Role::ClassMethods
|
9
|
+
base.extend ClassMethods
|
10
|
+
base.key :one_role_id, ObjectId
|
11
|
+
base.key :one_role, Role
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
def role_attribute
|
16
|
+
strategy_class.roles_attribute_name.to_sym
|
17
|
+
end
|
18
|
+
|
19
|
+
def role_id_attribute
|
20
|
+
"#{role_attribute}_id".to_sym
|
21
|
+
end
|
22
|
+
|
23
|
+
def in_role(role_name)
|
24
|
+
begin
|
25
|
+
role = Role.find_role(role_name)
|
26
|
+
all(role_id_attribute => role.id)
|
27
|
+
rescue
|
28
|
+
return []
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def in_roles(*role_names)
|
33
|
+
begin
|
34
|
+
role_ids = Role.find_roles(role_names)
|
35
|
+
my_id = send(role_attribute).id
|
36
|
+
all(role_id_attribute.in => role_ids)
|
37
|
+
rescue
|
38
|
+
return []
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
module Implementation
|
44
|
+
def role_id_attribute
|
45
|
+
"#{role_attribute}_id".to_sym
|
46
|
+
end
|
47
|
+
|
48
|
+
# assign roles
|
49
|
+
def roles=(*roles)
|
50
|
+
raise "Role class #{role_class} does not have a #find_role(role) method" if !role_class.respond_to? :find_role
|
51
|
+
first_role = roles.flatten.first
|
52
|
+
role_relation = role_class.find_role(first_role)
|
53
|
+
if role_relation
|
54
|
+
self.send("#{role_attribute}=", role_relation)
|
55
|
+
self.send("#{role_id_attribute}=", role_relation.id)
|
56
|
+
self.save
|
57
|
+
end
|
58
|
+
end
|
59
|
+
alias_method :role=, :roles=
|
60
|
+
|
61
|
+
# query assigned roles
|
62
|
+
def roles
|
63
|
+
[self.send(role_attribute).name.to_sym]
|
64
|
+
end
|
65
|
+
alias_method :roles_list, :roles
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
extend Roles::Generic::User::Configuration
|
70
|
+
configure :num => :single, :type => :role_class
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module RoleStrategy::MongoMapper
|
2
|
+
module RoleString
|
3
|
+
def self.default_role_attribute
|
4
|
+
:role_string
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.included base
|
8
|
+
base.extend ClassMethods
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def role_attribute
|
13
|
+
strategy_class.roles_attribute_name.to_sym
|
14
|
+
end
|
15
|
+
|
16
|
+
def in_role(role_name)
|
17
|
+
where(role_attribute => role_name)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
|
22
|
+
module Implementation
|
23
|
+
def role_attribute
|
24
|
+
strategy_class.roles_attribute_name
|
25
|
+
end
|
26
|
+
|
27
|
+
# assign roles
|
28
|
+
def roles=(*roles)
|
29
|
+
self.role = roles.select_labels.first.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
def role= role_name
|
33
|
+
if role_name.kind_of_label? && valid_role?(role_name)
|
34
|
+
self.send("#{role_attribute}=", role_name.to_s)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# query assigned roles
|
39
|
+
def roles
|
40
|
+
role = self.send(role_attribute)
|
41
|
+
[role.to_sym]
|
42
|
+
end
|
43
|
+
alias_method :roles_list, :roles
|
44
|
+
end
|
45
|
+
|
46
|
+
extend Roles::Generic::User::Configuration
|
47
|
+
configure :num => :single
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
|
data/logging.log
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
[Rails::Assist::Logging]
|
2
|
+
apply_role_strategy for : admin_flag in model User
|
3
|
+
[Rails::Assist::Logging]
|
4
|
+
apply_role_strategy for : one_role in model User
|
5
|
+
[Rails3::Assist::Logging]
|
6
|
+
apply_role_strategy for : admin_flag in model User
|
7
|
+
[Rails3::Assist::Logging]
|
8
|
+
apply_role_strategy for : one_role in model User
|
@@ -0,0 +1,103 @@
|
|
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_mongo_mapper}
|
8
|
+
s.version = "0.1.1"
|
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-09-20}
|
13
|
+
s.description = %q{Makes it easy to set a role strategy on your User model in MongoMapper}
|
14
|
+
s.email = %q{kmandrup@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
".rspec",
|
23
|
+
"LICENSE",
|
24
|
+
"README.markdown",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/generators/mongo_mapper/roles/roles_generator.rb",
|
28
|
+
"lib/roles_mongo_mapper.rb",
|
29
|
+
"lib/roles_mongo_mapper/base.rb",
|
30
|
+
"lib/roles_mongo_mapper/namespaces.rb",
|
31
|
+
"lib/roles_mongo_mapper/role.rb",
|
32
|
+
"lib/roles_mongo_mapper/strategy.rb",
|
33
|
+
"lib/roles_mongo_mapper/strategy/multi/many_roles.rb",
|
34
|
+
"lib/roles_mongo_mapper/strategy/multi/role_strings.rb",
|
35
|
+
"lib/roles_mongo_mapper/strategy/multi/roles_mask.rb",
|
36
|
+
"lib/roles_mongo_mapper/strategy/single/admin_flag.rb",
|
37
|
+
"lib/roles_mongo_mapper/strategy/single/one_role.rb",
|
38
|
+
"lib/roles_mongo_mapper/strategy/single/role_string.rb",
|
39
|
+
"logging.log",
|
40
|
+
"roles_mongo_mapper.gemspec",
|
41
|
+
"sandbox/test.rb",
|
42
|
+
"spec/generator_spec_helper.rb",
|
43
|
+
"spec/roles_mongo_mapper/generators/roles_generator_spec.rb",
|
44
|
+
"spec/roles_mongo_mapper/strategy/admin_flag_spec.rb",
|
45
|
+
"spec/roles_mongo_mapper/strategy/many_roles_spec.rb",
|
46
|
+
"spec/roles_mongo_mapper/strategy/one_role_spec.rb",
|
47
|
+
"spec/roles_mongo_mapper/strategy/role_string_spec.rb",
|
48
|
+
"spec/roles_mongo_mapper/strategy/role_strings_spec.rb",
|
49
|
+
"spec/roles_mongo_mapper/strategy/roles_mask_spec.rb",
|
50
|
+
"spec/spec_helper.rb"
|
51
|
+
]
|
52
|
+
s.homepage = %q{http://github.com/kristianmandrup/roles_mongo_mapper}
|
53
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
54
|
+
s.require_paths = ["lib"]
|
55
|
+
s.rubygems_version = %q{1.3.7}
|
56
|
+
s.summary = %q{Implementation of Roles generic API for MongoMapper}
|
57
|
+
s.test_files = [
|
58
|
+
"spec/generator_spec_helper.rb",
|
59
|
+
"spec/roles_mongo_mapper/generators/roles_generator_spec.rb",
|
60
|
+
"spec/roles_mongo_mapper/strategy/admin_flag_spec.rb",
|
61
|
+
"spec/roles_mongo_mapper/strategy/many_roles_spec.rb",
|
62
|
+
"spec/roles_mongo_mapper/strategy/one_role_spec.rb",
|
63
|
+
"spec/roles_mongo_mapper/strategy/role_string_spec.rb",
|
64
|
+
"spec/roles_mongo_mapper/strategy/role_strings_spec.rb",
|
65
|
+
"spec/roles_mongo_mapper/strategy/roles_mask_spec.rb",
|
66
|
+
"spec/spec_helper.rb"
|
67
|
+
]
|
68
|
+
|
69
|
+
if s.respond_to? :specification_version then
|
70
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
71
|
+
s.specification_version = 3
|
72
|
+
|
73
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
74
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
75
|
+
s.add_development_dependency(%q<generator-spec>, ["~> 0.6.4"])
|
76
|
+
s.add_runtime_dependency(%q<mongo_mapper>, ["~> 0.8.4"])
|
77
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
|
78
|
+
s.add_runtime_dependency(%q<require_all>, ["~> 1.1.0"])
|
79
|
+
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.10"])
|
80
|
+
s.add_runtime_dependency(%q<roles_generic>, ["~> 0.2.6"])
|
81
|
+
s.add_runtime_dependency(%q<logging_assist>, ["~> 0.1.3"])
|
82
|
+
else
|
83
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
84
|
+
s.add_dependency(%q<generator-spec>, ["~> 0.6.4"])
|
85
|
+
s.add_dependency(%q<mongo_mapper>, ["~> 0.8.4"])
|
86
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
87
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
88
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
|
89
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.6"])
|
90
|
+
s.add_dependency(%q<logging_assist>, ["~> 0.1.3"])
|
91
|
+
end
|
92
|
+
else
|
93
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0.beta.22"])
|
94
|
+
s.add_dependency(%q<generator-spec>, ["~> 0.6.4"])
|
95
|
+
s.add_dependency(%q<mongo_mapper>, ["~> 0.8.4"])
|
96
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
|
97
|
+
s.add_dependency(%q<require_all>, ["~> 1.1.0"])
|
98
|
+
s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
|
99
|
+
s.add_dependency(%q<roles_generic>, ["~> 0.2.6"])
|
100
|
+
s.add_dependency(%q<logging_assist>, ["~> 0.1.3"])
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
data/sandbox/test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
MongoMapper.database = 'roles_for_mm'
|
4
|
+
|
5
|
+
class User
|
6
|
+
include MongoMapper::Document
|
7
|
+
|
8
|
+
key :name, String
|
9
|
+
end
|
10
|
+
|
11
|
+
User.create(:name => 'Kristian').save
|
12
|
+
|
13
|
+
puts User.first.id.inspect
|
14
|
+
|
15
|
+
u = User.where(:'_id' => User.first.id).first
|
16
|
+
puts u.inspect
|
17
|
+
|
18
|
+
|
19
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'rspec/core'
|
2
|
+
require 'rails3_artifactor'
|
3
|
+
require 'generator-spec'
|
4
|
+
require 'roles_generic'
|
5
|
+
|
6
|
+
RSpec::Generator.configure do |config|
|
7
|
+
config.debug = false
|
8
|
+
config.remove_temp_dir = true # false
|
9
|
+
config.default_rails_root(__FILE__)
|
10
|
+
config.lib = File.dirname(__FILE__) + '/../lib'
|
11
|
+
config.logger = :stdout
|
12
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'generator_spec_helper'
|
2
|
+
require_generator :mongo_mapper => :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: mongo_mapper' do
|
9
|
+
use_orm :mongo_mapper
|
10
|
+
|
11
|
+
before :each do
|
12
|
+
setup_generator 'mongo_mapper_roles_generator' do
|
13
|
+
tests MongoMapper::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
|
+
g.should generate_model :user do |clazz|
|
31
|
+
clazz.should include_module 'MongoMapper::Document'
|
32
|
+
clazz.should include_module 'Roles::MongoMapper'
|
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
|
+
g.should generate_model :user do |clazz|
|
49
|
+
clazz.should include_module 'MongoMapper::Document'
|
50
|
+
clazz.should include_module 'Roles::MongoMapper'
|
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
|
+
|