mongoid_roles 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +80 -0
- data/lib/mongoid/roles/role.rb +17 -8
- data/lib/mongoid/roles/subject.rb +7 -3
- data/lib/mongoid/roles/version.rb +1 -1
- data/mongoid_roles.gemspec +3 -2
- data/spec/models/project.rb +6 -0
- data/spec/models/user.rb +6 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/subject_spec.rb +88 -0
- metadata +47 -8
- data/README.md +0 -4
data/README.textile
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
h1. Mongoid Roles
|
2
|
+
|
3
|
+
Role-based authorization system for Rails & Mongoid.
|
4
|
+
|
5
|
+
Interface is based on "acl9":https://github.com/be9/acl9.
|
6
|
+
|
7
|
+
|
8
|
+
h2. Installation
|
9
|
+
|
10
|
+
Add this line to your Gemfile:
|
11
|
+
|
12
|
+
@gem 'mongoid_roles'@
|
13
|
+
|
14
|
+
h2. Subject model
|
15
|
+
|
16
|
+
<pre><code>
|
17
|
+
class User
|
18
|
+
include Mongoid::Document
|
19
|
+
include Mongoid::Roles::Subject
|
20
|
+
end
|
21
|
+
</code></pre>
|
22
|
+
|
23
|
+
|
24
|
+
h2. Object model
|
25
|
+
|
26
|
+
<pre><code>
|
27
|
+
class Foo
|
28
|
+
include Mongoid::Document
|
29
|
+
include Mongoid::Roles::Object
|
30
|
+
end
|
31
|
+
|
32
|
+
class Bar
|
33
|
+
include Mongoid::Document
|
34
|
+
include Mongoid::Roles::Object
|
35
|
+
end
|
36
|
+
</code></pre>
|
37
|
+
|
38
|
+
|
39
|
+
h2. Interface
|
40
|
+
|
41
|
+
|
42
|
+
h3. Subject model
|
43
|
+
|
44
|
+
A call of @include Mongoid::Roles::Subject@ defines following methods on the model:
|
45
|
+
|
46
|
+
@subject.has_role?(role, object = nil)@. Returns @true@ of @false@ (has or has not).
|
47
|
+
|
48
|
+
@subject.has_role!(role, object = nil)@. Assigns a @role@ for the @object@ to the @subject@.
|
49
|
+
Does nothing is subject already has such a role.
|
50
|
+
|
51
|
+
@subject.has_no_role!(role, object = nil)@. Unassigns a role from the @subject@.
|
52
|
+
|
53
|
+
@subject.has_roles_for?(object)@. Does the @subject@ has any roles for @object@? (@true@ of @false@)
|
54
|
+
|
55
|
+
@subject.has_role_for?(object)@. Same as @has_roles_for?@.
|
56
|
+
|
57
|
+
@subject.roles_for(object)@. Returns an array of @Role@ instances, corresponding to @subject@ 's roles on
|
58
|
+
@object@. E.g. @subject.roles_for(object).map(&:name).sort@ will give you role names in alphabetical order.
|
59
|
+
|
60
|
+
@subject.has_no_roles_for!(object)@. Unassign any @subject@ 's roles for a given @object@.
|
61
|
+
|
62
|
+
@subject.has_no_roles!@. Unassign all roles from @subject@.
|
63
|
+
|
64
|
+
|
65
|
+
h3. Object model
|
66
|
+
|
67
|
+
A call of @include Mongoid::Roles::Object@ defines following methods on the model:
|
68
|
+
|
69
|
+
@object.accepts_role?(role_name, subject)@. An alias for @subject.has_role?(role_name, object)@.
|
70
|
+
|
71
|
+
@object.accepts_role!(role_name, subject)@. An alias for @subject.has_role!(role_name, object)@.
|
72
|
+
|
73
|
+
@object.accepts_no_role!(role_name, subject)@. An alias for @subject.has_no_role!(role_name, object)@.
|
74
|
+
|
75
|
+
@object.accepts_roles_by?(subject)@. An alias for @subject.has_roles_for?(object)@.
|
76
|
+
|
77
|
+
@object.accepts_role_by?(subject)@. Same as @accepts_roles_by?@.
|
78
|
+
|
79
|
+
@object.accepts_roles_by(subject)@. An alias for @subject.roles_for(object)@.
|
80
|
+
|
data/lib/mongoid/roles/role.rb
CHANGED
@@ -9,15 +9,17 @@ module Mongoid
|
|
9
9
|
field :auth_object_type, :type => String
|
10
10
|
field :auth_object_id, :type => String
|
11
11
|
|
12
|
-
scope :
|
13
|
-
scope :
|
14
|
-
|
15
|
-
|
12
|
+
scope :find_object, lambda {|object| where(:auth_object_type => object.class.name, :auth_object_id => object.id)}
|
13
|
+
scope :find_role, lambda {|role, object = nil|
|
14
|
+
object ? where(:role => role, :auth_object_type => object.class.name, :auth_object_id => object.id) :
|
15
|
+
where(:role => role, :auth_object_type => nil, :auth_object_id => nil)
|
16
|
+
}
|
17
|
+
|
16
18
|
class << self
|
17
19
|
|
18
20
|
# subject.has_role?(role, object = nil). Returns true of false (has or has not).
|
19
|
-
def has_role? (role, object)
|
20
|
-
|
21
|
+
def has_role? (role, object = nil)
|
22
|
+
find_role(role,object).count == 1
|
21
23
|
end
|
22
24
|
|
23
25
|
# subject.has_role!(role, object = nil). Assigns a role for the object to the subject.
|
@@ -25,8 +27,8 @@ module Mongoid
|
|
25
27
|
# IMPELEMENTED IN MongoidRoles::Subject
|
26
28
|
|
27
29
|
# subject.has_no_role!(role, object = nil). Unassigns a role from the subject.
|
28
|
-
def has_no_role! (role, object)
|
29
|
-
|
30
|
+
def has_no_role! (role, object = nil)
|
31
|
+
find_role(role,object).destroy_all
|
30
32
|
end
|
31
33
|
|
32
34
|
# subject.has_roles_for?(object). Does the subject has any roles for object? (true of false)
|
@@ -55,6 +57,11 @@ module Mongoid
|
|
55
57
|
criteria.destroy_all
|
56
58
|
end
|
57
59
|
|
60
|
+
|
61
|
+
|
62
|
+
# def find_role(role, object = nil)
|
63
|
+
# object ? find_role_with_object(role,object) : find_role(role)
|
64
|
+
# end
|
58
65
|
end
|
59
66
|
|
60
67
|
def auth_object
|
@@ -64,6 +71,8 @@ module Mongoid
|
|
64
71
|
end
|
65
72
|
|
66
73
|
def auth_object=(auth_object)
|
74
|
+
return unless auth_object
|
75
|
+
|
67
76
|
self.auth_object_type = auth_object.class.name
|
68
77
|
self.auth_object_id = auth_object.id
|
69
78
|
end
|
@@ -34,7 +34,7 @@ module Mongoid
|
|
34
34
|
# find all subjects which has given role for given object
|
35
35
|
def with_role role, object
|
36
36
|
c = where('roles.auth_object_type' => object.class.name, 'roles.role' => role)
|
37
|
-
c.selector['roles.auth_object_id'] = object.id.to_s # FIX,
|
37
|
+
c.selector['roles.auth_object_id'] = object.id.to_s # FIX, we want string instead of object_id...
|
38
38
|
return c
|
39
39
|
end
|
40
40
|
|
@@ -42,8 +42,12 @@ module Mongoid
|
|
42
42
|
|
43
43
|
# Assigns a role for the object to the subject.
|
44
44
|
# Does nothing is subject already has such a role.
|
45
|
-
def has_role! (role,
|
46
|
-
roles.
|
45
|
+
def has_role! (role, auth_object = nil)
|
46
|
+
# p roles.find(:role => role, :auth_object => auth_object).first
|
47
|
+
auth_object ?
|
48
|
+
roles.find_or_create_by(:role => role, :auth_object_id => auth_object.id.to_s, :auth_object_type => auth_object.class.name) :
|
49
|
+
roles.find_or_create_by(:role => role, :auth_object_id => nil, :auth_object_type => nil)
|
50
|
+
|
47
51
|
end
|
48
52
|
end
|
49
53
|
end
|
data/mongoid_roles.gemspec
CHANGED
@@ -12,11 +12,12 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.summary = %q{Mongoid Roles}
|
13
13
|
s.description = %q{Mongoid Roles}
|
14
14
|
|
15
|
-
s.
|
15
|
+
s.add_development_dependency 'rspec'
|
16
|
+
s.add_development_dependency 'rspec-rails'
|
17
|
+
s.add_development_dependency 'mongoid', '2.0.0.rc.7'
|
16
18
|
|
17
19
|
s.files = `git ls-files`.split("\n")
|
18
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
21
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
22
|
s.require_paths = ["lib"]
|
21
|
-
|
22
23
|
end
|
data/spec/models/user.rb
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rails/all'
|
3
|
+
require 'mongoid'
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
require 'mongoid_roles'
|
7
|
+
require 'models/user'
|
8
|
+
require 'models/project'
|
9
|
+
|
10
|
+
Mongoid.configure do |config|
|
11
|
+
host = "localhost"
|
12
|
+
config.master = Mongo::Connection.new.db('mongoid_roles_test')
|
13
|
+
config.persist_in_safe_mode = false
|
14
|
+
end
|
15
|
+
|
16
|
+
RSpec.configure do |config|
|
17
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Roles::Subject do
|
4
|
+
before(:each) do
|
5
|
+
@bob = User.new
|
6
|
+
@proj = Project.new
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'has_role!' do
|
10
|
+
it 'should set role' do
|
11
|
+
@bob.has_role?(:manager, @proj).should be_false
|
12
|
+
@bob.has_role!(:manager, @proj)
|
13
|
+
@bob.has_role?(:manager, @proj).should be_true
|
14
|
+
|
15
|
+
@bob.has_role?(:manager).should be_false
|
16
|
+
@bob.has_role!(:manager)
|
17
|
+
@bob.has_role?(:manager).should be_true
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'has_no_roles!' do
|
22
|
+
it 'should remove all roles' do
|
23
|
+
@bob.has_role!(:manager)
|
24
|
+
@bob.has_role!(:manager, @proj)
|
25
|
+
@bob.roles.count.should == 2
|
26
|
+
|
27
|
+
@bob.has_no_roles!
|
28
|
+
@bob.roles.count.should == 0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'has_no_role!' do
|
33
|
+
it 'should remove particular role' do
|
34
|
+
@bob.has_role!(:manager)
|
35
|
+
@bob.has_role!(:manager, @proj)
|
36
|
+
|
37
|
+
@bob.has_role?(:manager).should be_true
|
38
|
+
@bob.has_no_role!(:manager)
|
39
|
+
@bob.has_role?(:manager).should be_false
|
40
|
+
|
41
|
+
@bob.has_role?(:manager, @proj).should be_true
|
42
|
+
@bob.has_no_role!(:manager, @proj)
|
43
|
+
@bob.has_role?(:manager, @proj).should be_false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'has_roles_for?' do
|
48
|
+
it 'should check if role exists for object' do
|
49
|
+
@bob.has_roles_for?(@proj).should be_false
|
50
|
+
@bob.has_role!(:manager, @proj)
|
51
|
+
@bob.has_roles_for?(@proj).should be_true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context 'has_role_for?' do
|
56
|
+
it 'should do the same as has_roles_for?' do
|
57
|
+
@bob.has_roles_for?(@proj).should be_false
|
58
|
+
@bob.has_role!(:manager, @proj)
|
59
|
+
@bob.has_roles_for?(@proj).should be_true
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'roles_for' do
|
64
|
+
it 'should return all roles for object' do
|
65
|
+
proj2 = Project.new
|
66
|
+
|
67
|
+
@bob.has_role!(:manager, @proj)
|
68
|
+
@bob.has_role!(:leader, @proj)
|
69
|
+
@bob.has_role!(:manager, proj2)
|
70
|
+
|
71
|
+
@bob.roles_for(@proj).count.should == 2
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'has_no_roles_for!' do
|
76
|
+
it 'should remove roles for object' do
|
77
|
+
proj2 = Project.new
|
78
|
+
|
79
|
+
@bob.has_role!(:manager, @proj)
|
80
|
+
@bob.has_role!(:leader, @proj)
|
81
|
+
@bob.has_role!(:manager, proj2)
|
82
|
+
|
83
|
+
@bob.has_no_roles_for!(@proj)
|
84
|
+
|
85
|
+
@bob.roles.count.should == 1
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: mongoid_roles
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.4
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Michal Wychowaniec
|
@@ -10,10 +10,42 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-03-21 00:00:00 +01:00
|
14
14
|
default_executable:
|
15
|
-
dependencies:
|
16
|
-
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "0"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec-rails
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: mongoid
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - "="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.0.0.rc.7
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
17
49
|
description: Mongoid Roles
|
18
50
|
email:
|
19
51
|
- michal.wychowaniec@gmail.com
|
@@ -26,7 +58,7 @@ extra_rdoc_files: []
|
|
26
58
|
files:
|
27
59
|
- .gitignore
|
28
60
|
- Gemfile
|
29
|
-
- README.
|
61
|
+
- README.textile
|
30
62
|
- Rakefile
|
31
63
|
- lib/mongoid/roles.rb
|
32
64
|
- lib/mongoid/roles/object.rb
|
@@ -35,6 +67,10 @@ files:
|
|
35
67
|
- lib/mongoid/roles/version.rb
|
36
68
|
- lib/mongoid_roles.rb
|
37
69
|
- mongoid_roles.gemspec
|
70
|
+
- spec/models/project.rb
|
71
|
+
- spec/models/user.rb
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/subject_spec.rb
|
38
74
|
has_rdoc: true
|
39
75
|
homepage: ""
|
40
76
|
licenses: []
|
@@ -58,10 +94,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
94
|
version: "0"
|
59
95
|
requirements: []
|
60
96
|
|
61
|
-
rubyforge_project:
|
97
|
+
rubyforge_project:
|
62
98
|
rubygems_version: 1.5.3
|
63
99
|
signing_key:
|
64
100
|
specification_version: 3
|
65
101
|
summary: Mongoid Roles
|
66
|
-
test_files:
|
67
|
-
|
102
|
+
test_files:
|
103
|
+
- spec/models/project.rb
|
104
|
+
- spec/models/user.rb
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
- spec/subject_spec.rb
|
data/README.md
DELETED