rolify 0.0.1 → 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/Gemfile +5 -0
- data/README.rdoc +12 -0
- data/lib/rolify/role.rb +22 -0
- data/lib/rolify/version.rb +1 -1
- data/lib/rolify.rb +3 -3
- data/rolify.gemspec +4 -1
- data/spec/rolify/role_spec.rb +87 -0
- data/spec/spec_helper.rb +9 -2
- data/spec/support/data.rb +5 -0
- data/spec/support/models.rb +12 -0
- data/spec/support/schema.rb +23 -0
- metadata +40 -7
data/Gemfile
CHANGED
data/README.rdoc
CHANGED
@@ -8,6 +8,18 @@ Let's see an example:
|
|
8
8
|
|
9
9
|
This library was intended to be used with CanCan[https://github.com/ryanb/cancan] and devise[https://github.com/plataformatec/devise/] but should be generic enough to be used by any other authentication/authorization solutions.
|
10
10
|
|
11
|
+
== DISCLAIMER : still in early stage of development. Like a pre-alpha release, kind of ;-) You've been warned !
|
12
|
+
|
13
|
+
Status:
|
14
|
+
|
15
|
+
(x) generators
|
16
|
+
|
17
|
+
(x) migration file
|
18
|
+
|
19
|
+
( ) specs
|
20
|
+
|
21
|
+
( ) code
|
22
|
+
|
11
23
|
== Requirements
|
12
24
|
|
13
25
|
* >= Rails 3
|
data/lib/rolify/role.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module Rolify
|
2
|
+
def has_role(role, resource = nil)
|
3
|
+
role = Role.find_or_create_by_name_and_resource_type_and_resource_id( :name => role,
|
4
|
+
:resource_type => (resource.class.name if resource),
|
5
|
+
:resource_id => (resource.id if resource))
|
6
|
+
self.roles << role if !roles.include?(role)
|
7
|
+
end
|
8
|
+
|
9
|
+
def has_role?(role, resource = nil)
|
10
|
+
global_role_query = "((name = ?) AND (resource_type IS NULL) AND (resource_id IS NULL))"
|
11
|
+
if resource
|
12
|
+
self.roles.where("#{global_role_query} OR ((name = ?) AND (resource_type = ?) AND (resource_id = ?))",
|
13
|
+
role, role, resource.class.name, resource.id).size > 0
|
14
|
+
else
|
15
|
+
self.roles.where(global_role_query, role).size > 0
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def roles_name
|
20
|
+
self.roles.select(:name).map { |r| r.name }
|
21
|
+
end
|
22
|
+
end
|
data/lib/rolify/version.rb
CHANGED
data/lib/rolify.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
require 'rolify/role'
|
data/rolify.gemspec
CHANGED
@@ -18,4 +18,7 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
-
|
21
|
+
|
22
|
+
s.add_development_dependency "sqlite3"
|
23
|
+
s.add_dependency "activerecord", "~> 3.1.0.rc1"
|
24
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Rolify::Role" do
|
4
|
+
|
5
|
+
context "with a global role" do
|
6
|
+
before(:all) do
|
7
|
+
@admin = User.first
|
8
|
+
@admin.has_role "admin"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should set a global role" do
|
12
|
+
expect { @admin.has_role "superadmin" }.to change{ Role.count }.by(1)
|
13
|
+
superadmin = Role.last
|
14
|
+
superadmin.name.should eq("superadmin")
|
15
|
+
superadmin.resource.should be(nil)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should not create another role if already existing" do
|
19
|
+
expect { @admin.has_role "admin" }.not_to change{ Role.count }
|
20
|
+
expect { @admin.has_role "admin" }.not_to change{ @admin.roles.size }
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should get a global role" do
|
24
|
+
@admin.has_role?("admin").should be(true)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should get any resource request" do
|
28
|
+
@admin.has_role?("admin", Forum.first).should be(true)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not get another global role" do
|
32
|
+
Role.create(:name => "global")
|
33
|
+
@admin.has_role?("global").should be(false)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not get a scoped role" do
|
37
|
+
@admin.has_role?("moderator", Forum.last).should be(false)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should not get inexisting role" do
|
41
|
+
@admin.has_role?("dummy").should be(false)
|
42
|
+
@admin.has_role?("dumber", Forum.first).should be(false)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with a scoped role" do
|
47
|
+
before(:all) do
|
48
|
+
@moderator = User.last
|
49
|
+
@moderator.has_role "moderator", Forum.first
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should set a scoped role" do
|
53
|
+
@moderator
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not create another role if already existing" do
|
57
|
+
expect { @moderator.has_role "moderator", Forum.first }.not_to change{ Role.count }
|
58
|
+
expect { @moderator.has_role "moderator" , Forum.first }.not_to change{ @moderator.roles.size }
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should get a scoped role" do
|
62
|
+
expect { @moderator.has_role "supermodo", Forum.last }.to change{ Role.count }.by(1)
|
63
|
+
supermodo = Role.last
|
64
|
+
supermodo.name.should eq("supermodo")
|
65
|
+
supermodo.resource.should eq(Forum.last)
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should not get a global role" do
|
69
|
+
@moderator.has_role?("admin").should be(false)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should not get the same role on another resource" do
|
73
|
+
Role.create(:name => "moderator", :resource => Forum.last)
|
74
|
+
@moderator.has_role?("moderator", Forum.last).should be(false)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should not get the another role on the same resource" do
|
78
|
+
Role.create(:name => "tourist", :resource => Forum.first)
|
79
|
+
@moderator.has_role?("tourist", Forum.first).should be(false)
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should not get inexisting role" do
|
83
|
+
@moderator.has_role?("dummy", Forum.last).should be(false)
|
84
|
+
@moderator.has_role?("dumber").should be(false)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
-
|
1
|
+
require 'rubygems'
|
2
2
|
require 'bundler/setup'
|
3
3
|
|
4
|
-
Bundler.require(:default)
|
4
|
+
Bundler.require(:default, :test)
|
5
|
+
|
6
|
+
require 'rolify'
|
7
|
+
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
|
8
|
+
|
9
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
10
|
+
load File.dirname(__FILE__) + '/support/models.rb'
|
11
|
+
load File.dirname(__FILE__) + '/support/data.rb'
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class User < ActiveRecord::Base
|
2
|
+
has_and_belongs_to_many :roles, :join_table => :users_roles
|
3
|
+
include Rolify
|
4
|
+
end
|
5
|
+
|
6
|
+
class Role < ActiveRecord::Base
|
7
|
+
has_and_belongs_to_many :users, :join_table => :users_roles
|
8
|
+
belongs_to :resource, :polymorphic => true
|
9
|
+
end
|
10
|
+
|
11
|
+
class Forum < ActiveRecord::Base
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
ActiveRecord::Schema.define do
|
2
|
+
self.verbose = false
|
3
|
+
|
4
|
+
create_table(:roles) do |t|
|
5
|
+
t.string :name
|
6
|
+
t.references :resource, :polymorphic => true
|
7
|
+
|
8
|
+
t.timestamps
|
9
|
+
end
|
10
|
+
|
11
|
+
create_table(:users) do |t|
|
12
|
+
t.string :login
|
13
|
+
end
|
14
|
+
|
15
|
+
create_table(:users_roles, :id => false) do |t|
|
16
|
+
t.references :user
|
17
|
+
t.references :role
|
18
|
+
end
|
19
|
+
|
20
|
+
create_table(:forums) do |t|
|
21
|
+
t.string :name
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: rolify
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Florent Monbillard
|
@@ -10,9 +10,31 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-06-
|
14
|
-
|
15
|
-
|
13
|
+
date: 2011-06-04 00:00:00 -04:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: sqlite3
|
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: activerecord
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ~>
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.1.0.rc1
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
16
38
|
description: "Very simple Roles library without any authorization enforcement (built to use with cancan) supporting scope on object: user.is_moderator?(Forum.first) => # return false if user is moderator of another Forum "
|
17
39
|
email:
|
18
40
|
- f.monbillard@gmail.com
|
@@ -32,10 +54,16 @@ files:
|
|
32
54
|
- lib/generators/rolify/role/templates/migration.rb
|
33
55
|
- lib/generators/rolify/role/templates/role.rb
|
34
56
|
- lib/rolify.rb
|
57
|
+
- lib/rolify/role.rb
|
35
58
|
- lib/rolify/version.rb
|
36
59
|
- rolify.gemspec
|
60
|
+
- spec/rolify/role_spec.rb
|
37
61
|
- spec/spec.opts
|
38
62
|
- spec/spec_helper.rb
|
63
|
+
- spec/support/data.rb
|
64
|
+
- spec/support/models.rb
|
65
|
+
- spec/support/schema.rb
|
66
|
+
has_rdoc: true
|
39
67
|
homepage: https://github.com/EppO/rolify
|
40
68
|
licenses: []
|
41
69
|
|
@@ -59,9 +87,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
87
|
requirements: []
|
60
88
|
|
61
89
|
rubyforge_project: rolify
|
62
|
-
rubygems_version: 1.
|
90
|
+
rubygems_version: 1.6.2
|
63
91
|
signing_key:
|
64
92
|
specification_version: 3
|
65
93
|
summary: Roles library with object scoping
|
66
|
-
test_files:
|
67
|
-
|
94
|
+
test_files:
|
95
|
+
- spec/rolify/role_spec.rb
|
96
|
+
- spec/spec.opts
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
- spec/support/data.rb
|
99
|
+
- spec/support/models.rb
|
100
|
+
- spec/support/schema.rb
|