role_model 0.1.1
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.rdoc +79 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/role_model.rb +33 -0
- data/role_model.gemspec +55 -0
- data/spec/role_model_spec.rb +151 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +87 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Martin Rehfeld
|
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.rdoc
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
= RoleModel
|
2
|
+
|
3
|
+
Ever needed to assign roles to a model, say a User, and build conditional
|
4
|
+
behaviour on top of that?
|
5
|
+
|
6
|
+
Enter RoleModel -- roles have never been easier! No need to build a separate
|
7
|
+
Role model, managing seed data and the like. Just declare your roles and you
|
8
|
+
are done.
|
9
|
+
|
10
|
+
Assigned roles will be stored as a bitmask in an configurable attribute
|
11
|
+
(default: <tt>roles_mask</tt>). Here's everything you need to know:
|
12
|
+
|
13
|
+
# given an User class with a roles_mask attribute
|
14
|
+
class User
|
15
|
+
attr_accessor :roles_mask # in real life this would be an persisted attribute / DB-column
|
16
|
+
|
17
|
+
include RoleModel
|
18
|
+
# optionally set the integer attribute to store the roles in,
|
19
|
+
# :roles_mask is the default
|
20
|
+
roles_attribute :roles_mask
|
21
|
+
|
22
|
+
# declare the valid roles -- do not change the order if you add more
|
23
|
+
# roles later, always append them at the end!
|
24
|
+
roles :admin, :manager, :author
|
25
|
+
end
|
26
|
+
|
27
|
+
>> u = User.new
|
28
|
+
=> #<User ...>
|
29
|
+
|
30
|
+
>> u.roles = [:admin] # ['admin'] works as well
|
31
|
+
=> [:admin]
|
32
|
+
|
33
|
+
>> u.roles # aliased to role_symbols for DeclarativeAuthorization
|
34
|
+
=> [:admin]
|
35
|
+
|
36
|
+
>> u.has_role? :manager
|
37
|
+
=> false
|
38
|
+
|
39
|
+
>> u.has_role? :admin
|
40
|
+
=> true
|
41
|
+
|
42
|
+
>> u.roles_mask
|
43
|
+
=> 1
|
44
|
+
|
45
|
+
Once you have included RoleModel, your model is perfectly fit to be used
|
46
|
+
together with an role-based authorization solution, such as
|
47
|
+
http://github.com/ryanb/cancan or
|
48
|
+
http://github.com/stffn/declarative_authorization .
|
49
|
+
|
50
|
+
== Reasoning
|
51
|
+
|
52
|
+
Whenever I introduce a role-based authorization scheme into a project, the
|
53
|
+
code gets coupled somehow to the available roles. So it usually does not make
|
54
|
+
any sense to have a separate Role model stored within the database. This Role
|
55
|
+
model will contain a predefined set of roles anyhow -- changing that set will
|
56
|
+
need to be reflected within the authorization code. Putting the available
|
57
|
+
roles right into the model that actually uses them, makes things much easier
|
58
|
+
and efficient.
|
59
|
+
|
60
|
+
== Note on Patches/Pull Requests
|
61
|
+
|
62
|
+
* Fork the project.
|
63
|
+
* Make your feature addition or bug fix.
|
64
|
+
* Add tests for it. This is important so I don't break it in a
|
65
|
+
future version unintentionally.
|
66
|
+
* Commit, do not mess with Rakefile, version, or history.
|
67
|
+
(if you want to have your own version, that is fine but bump version in a
|
68
|
+
commit by itself I can ignore when I pull)
|
69
|
+
* Send me a pull request. Bonus points for topic branches.
|
70
|
+
|
71
|
+
== Credits
|
72
|
+
|
73
|
+
RoleModel is an implementation of the Role Based Authorization scheme
|
74
|
+
proposed by Ryan Bates
|
75
|
+
(http://wiki.github.com/ryanb/cancan/role-based-authorization).
|
76
|
+
|
77
|
+
== Copyright
|
78
|
+
|
79
|
+
Copyright (c) 2010 Martin Rehfeld. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "role_model"
|
8
|
+
gem.summary = %Q{Declare, assign and query roles with ease}
|
9
|
+
gem.description = %Q{Ever needed to assign roles to a model, say a User, and build conditional behaviour on top of that? Enter RoleModel -- roles have never been easier! Just declare your roles and you are done. Assigned roles will be stored as a bitmask.}
|
10
|
+
gem.email = "martin.rehfeld@glnetworks.de"
|
11
|
+
gem.homepage = "http://github.com/martinrehfeld/role_model"
|
12
|
+
gem.authors = ["Martin Rehfeld"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "role_model #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/lib/role_model.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
module RoleModel
|
2
|
+
def self.included(base) # :nodoc:
|
3
|
+
|
4
|
+
# add class methods
|
5
|
+
base.instance_eval do
|
6
|
+
def roles_attribute(name)
|
7
|
+
@@roles_attribute = name.to_sym
|
8
|
+
end
|
9
|
+
roles_attribute :roles_mask
|
10
|
+
|
11
|
+
def roles(*roles)
|
12
|
+
@@valid_roles = Array[*roles].flatten.map { |r| r.to_sym }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# add instance methods
|
17
|
+
base.class_eval do
|
18
|
+
def roles=(*roles)
|
19
|
+
self.send("#{@@roles_attribute}=", (Array[*roles].flatten.map { |r| r.to_sym } & @@valid_roles).map { |r| 2**@@valid_roles.index(r) }.inject { |sum, bitvalue| sum + bitvalue })
|
20
|
+
end
|
21
|
+
|
22
|
+
def roles
|
23
|
+
@@valid_roles.reject { |r| ((self.send(@@roles_attribute) || 0) & 2**@@valid_roles.index(r)).zero? }
|
24
|
+
end
|
25
|
+
alias_method :role_symbols, :roles
|
26
|
+
|
27
|
+
def has_role?(role)
|
28
|
+
roles.include?(role.to_sym)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/role_model.gemspec
ADDED
@@ -0,0 +1,55 @@
|
|
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{role_model}
|
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 = ["Martin Rehfeld"]
|
12
|
+
s.date = %q{2010-05-25}
|
13
|
+
s.description = %q{Ever needed to assign roles to a model, say a User, and build conditional behaviour on top of that? Enter RoleModel -- roles have never been easier! Just declare your roles and you are done. Assigned roles will be stored as a bitmask.}
|
14
|
+
s.email = %q{martin.rehfeld@glnetworks.de}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/role_model.rb",
|
27
|
+
"role_model.gemspec",
|
28
|
+
"spec/role_model_spec.rb",
|
29
|
+
"spec/spec.opts",
|
30
|
+
"spec/spec_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/martinrehfeld/role_model}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.6}
|
36
|
+
s.summary = %q{Declare, assign and query roles with ease}
|
37
|
+
s.test_files = [
|
38
|
+
"spec/role_model_spec.rb",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
48
|
+
else
|
49
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
50
|
+
end
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe RoleModel do
|
4
|
+
|
5
|
+
let(:model_class) { Class.new }
|
6
|
+
|
7
|
+
before(:each) do
|
8
|
+
model_class.instance_eval do
|
9
|
+
attr_accessor :roles_mask
|
10
|
+
attr_accessor :custom_roles_mask
|
11
|
+
include RoleModel
|
12
|
+
roles :foo, :bar
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
describe ".roles_attribute" do
|
17
|
+
before(:each) do
|
18
|
+
model_class.instance_eval do
|
19
|
+
roles_attribute :custom_roles_mask
|
20
|
+
roles :sample
|
21
|
+
end
|
22
|
+
end
|
23
|
+
subject { model_class.new }
|
24
|
+
|
25
|
+
it "should change the bitmask attribute used to store the assigned roles" do
|
26
|
+
subject.roles = [:sample]
|
27
|
+
subject.roles_mask.should be_nil
|
28
|
+
subject.custom_roles_mask.should == 1
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".roles" do
|
33
|
+
subject { model_class.new }
|
34
|
+
|
35
|
+
it "should define the valid roles" do
|
36
|
+
subject.roles = %w(foo bar baz)
|
37
|
+
subject.roles.should include(:foo)
|
38
|
+
subject.roles.should include(:bar)
|
39
|
+
subject.roles.should_not include(:baz)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should define the bitvalue of each role by position" do
|
43
|
+
subject.roles = :foo
|
44
|
+
subject.roles_mask.should == 1
|
45
|
+
subject.roles = :bar
|
46
|
+
subject.roles_mask.should == 2
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#roles" do
|
51
|
+
subject { model_class.new }
|
52
|
+
|
53
|
+
it "should return the assigned roles as symbols" do
|
54
|
+
subject.roles = [:foo, :bar]
|
55
|
+
subject.roles.should include(:foo)
|
56
|
+
subject.roles.should include(:bar)
|
57
|
+
subject.roles.should have(2).elements
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should return an empty array when no roles have been assigned" do
|
61
|
+
subject.roles.should be_empty
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#role_symbols" do
|
66
|
+
subject { model_class.new }
|
67
|
+
|
68
|
+
it "should be an alias to roles" do
|
69
|
+
subject.method(:role_symbols).should == subject.method(:roles)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe "#roles=" do
|
74
|
+
subject { model_class.new }
|
75
|
+
|
76
|
+
it "should accept an array of symbols" do
|
77
|
+
subject.roles = [:foo, :bar]
|
78
|
+
subject.roles.should include(:foo)
|
79
|
+
subject.roles.should include(:bar)
|
80
|
+
subject.roles.should have(2).elements
|
81
|
+
subject.roles = [:bar]
|
82
|
+
subject.roles.should include(:bar)
|
83
|
+
subject.roles.should have(1).element
|
84
|
+
end
|
85
|
+
|
86
|
+
it "should accept an array of strings" do
|
87
|
+
subject.roles = %w(foo bar)
|
88
|
+
subject.roles.should include(:foo)
|
89
|
+
subject.roles.should include(:bar)
|
90
|
+
subject.roles.should have(2).elements
|
91
|
+
subject.roles = ['bar']
|
92
|
+
subject.roles.should include(:bar)
|
93
|
+
subject.roles.should have(1).element
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should accept a single symbol" do
|
97
|
+
subject.roles = :foo
|
98
|
+
subject.roles.should include(:foo)
|
99
|
+
subject.roles.should have(1).element
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should accept a single string" do
|
103
|
+
subject.roles = 'foo'
|
104
|
+
subject.roles.should include(:foo)
|
105
|
+
subject.roles.should have(1).element
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should accept multiple arguments as symbols" do
|
109
|
+
subject.send(:roles=, :foo, :bar)
|
110
|
+
subject.roles.should include(:foo)
|
111
|
+
subject.roles.should include(:bar)
|
112
|
+
subject.roles.should have(2).elements
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should accept multiple arguments as strings" do
|
116
|
+
subject.send(:roles=, 'foo', 'bar')
|
117
|
+
subject.roles.should include(:foo)
|
118
|
+
subject.roles.should include(:bar)
|
119
|
+
subject.roles.should have(2).elements
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should silently ignore undefined roles" do
|
123
|
+
subject.roles = :baz
|
124
|
+
subject.roles.should be_empty
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
describe "#has_role?" do
|
129
|
+
subject { model_class.new }
|
130
|
+
|
131
|
+
it "should return true when the given role was assigned" do
|
132
|
+
subject.roles = :foo
|
133
|
+
subject.should have_role(:foo)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should return false when the given role was not assigned" do
|
137
|
+
subject.roles = :bar
|
138
|
+
subject.should_not have_role(:foo)
|
139
|
+
end
|
140
|
+
|
141
|
+
it "should return false when no role was assigned" do
|
142
|
+
subject.should_not have_role(:foo)
|
143
|
+
subject.should_not have_role(:bar)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "should return false when asked for an undefined role" do
|
147
|
+
subject.should_not have_role(:baz)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: role_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Martin Rehfeld
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-25 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
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: Ever needed to assign roles to a model, say a User, and build conditional behaviour on top of that? Enter RoleModel -- roles have never been easier! Just declare your roles and you are done. Assigned roles will be stored as a bitmask.
|
35
|
+
email: martin.rehfeld@glnetworks.de
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/role_model.rb
|
51
|
+
- role_model.gemspec
|
52
|
+
- spec/role_model_spec.rb
|
53
|
+
- spec/spec.opts
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/martinrehfeld/role_model
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: Declare, assign and query roles with ease
|
85
|
+
test_files:
|
86
|
+
- spec/role_model_spec.rb
|
87
|
+
- spec/spec_helper.rb
|