cancan-permits 0.2.3 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +64 -4
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/cancan-permits.gemspec +19 -17
- data/lib/cancan-permits/permit/base_permit.rb +2 -2
- data/lib/cancan-permits/permits/ability.rb +16 -16
- data/lib/generators/permits/permits_generator.rb +18 -0
- data/spec/active_record/owner_permits_spec.rb +2 -0
- data/spec/active_record/permits_spec.rb +2 -0
- data/spec/active_record/spec_helper.rb +1 -6
- data/spec/data_mapper/owner_permits_spec.rb +3 -1
- data/spec/data_mapper/permits_spec.rb +3 -2
- data/spec/data_mapper/spec_helper.rb +1 -11
- data/spec/{generic/fixtures → fixtures}/permits/admin_permit.rb +0 -0
- data/spec/{generic/fixtures → fixtures}/permits/editor_permit.rb +0 -0
- data/spec/{generic/fixtures → fixtures}/permits/guest_permit.rb +0 -0
- data/spec/generators/permit_generator_spec.rb +17 -6
- data/spec/generic/{permits/fixtures/models.rb → models/all_models.rb} +0 -0
- data/spec/generic/{permits/owner_permits_spec.rb → owner_permits_spec.rb} +1 -1
- data/spec/generic/{permits/permits_spec.rb → permits_spec.rb} +1 -14
- data/spec/generic/spec_helper.rb +32 -0
- data/spec/mongo_mapper/owner_permits_spec.rb +4 -2
- data/spec/mongo_mapper/permits_spec.rb +3 -1
- data/spec/mongo_mapper/spec_helper.rb +1 -5
- data/spec/mongoid/owner_permits_spec.rb +4 -2
- data/spec/mongoid/permits_spec.rb +2 -0
- data/spec/mongoid/spec_helper.rb +1 -5
- data/spec/spec_helper.rb +1 -32
- metadata +18 -18
data/README.markdown
CHANGED
@@ -2,14 +2,17 @@
|
|
2
2
|
|
3
3
|
Role specific Permits for use with [CanCan](http://github.com/ryanb/cancan) permission system.
|
4
4
|
|
5
|
-
## Update Oct
|
5
|
+
## Update Oct 14
|
6
6
|
|
7
|
-
Now updated to support multiple ownership startegies so that alternative ORMs can be supported.
|
7
|
+
Now updated to support multiple ownership startegies so that alternative ORMs can be supported.
|
8
8
|
This gem now includes specs that demonstrate how to configure it for use with Active Record, Data Mapper, Mongoid, Mongo Mapper
|
9
9
|
Special thanks to Sam (yoda) for the initial inspiration and work to ensure support for Mongoid :)
|
10
10
|
|
11
|
-
The
|
12
|
-
|
11
|
+
The built in ownership strategies are :default and :string. The default strategy can be used for generic models and Active Record, where there is no need of
|
12
|
+
type conversion. For some ORMs, like fx Mongoid, there is a need to convert the key (BSON Identifier) to a string for the "comparison" to work, hence the strategy is named :string.
|
13
|
+
|
14
|
+
The Permits generator has also been updated slightly to support this new strategy option as of version 0.2.1.
|
15
|
+
In general, the new Permits API now uses an options hash to replace the previous optional request parameter.
|
13
16
|
This design allows for better extensibility in the future if needed.
|
14
17
|
|
15
18
|
## Install
|
@@ -84,6 +87,63 @@ You can be enable this simply by setting the following class instance variable:
|
|
84
87
|
|
85
88
|
<code>Permits::Configuration.localhost_manager = true</code>
|
86
89
|
|
90
|
+
### Licenses
|
91
|
+
|
92
|
+
Permits also supports creation more fine-grained permits through the use of Licenses.
|
93
|
+
Licenses are a way to group logical fragments of permission statements to be reused across multiple permits.
|
94
|
+
The generator will create a licenses.rb file in the permits folder where you can put your licenses. For more complex scenarios, you might want to have a separate
|
95
|
+
licenses subfolder where you put your license files.
|
96
|
+
|
97
|
+
License example:
|
98
|
+
<pre>
|
99
|
+
class BloggingLicense < License::Base
|
100
|
+
def initialize name
|
101
|
+
super
|
102
|
+
end
|
103
|
+
|
104
|
+
def enforce!
|
105
|
+
can(:read, Blog)
|
106
|
+
can(:create, Post)
|
107
|
+
owns(user, Post)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
</pre>
|
111
|
+
|
112
|
+
Usage example:
|
113
|
+
|
114
|
+
<pre>
|
115
|
+
class GuestPermit < Permit::Base
|
116
|
+
def initialize(ability, options = {})
|
117
|
+
super
|
118
|
+
end
|
119
|
+
|
120
|
+
def permit?(user, options = {})
|
121
|
+
super
|
122
|
+
return if !role_match? user
|
123
|
+
|
124
|
+
licenses :user_admin, :blogging
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
</pre>
|
129
|
+
|
130
|
+
By convention the permits system will try to find a license named UserAdminLicense and BloggingLicense in this example and call enforce! on each license.
|
131
|
+
|
132
|
+
## ORMs
|
133
|
+
|
134
|
+
The easiest option is to directly set the orm as a class variable. An appropriate ownership strategy will be selected accordingly for the ORM.
|
135
|
+
|
136
|
+
<pre>
|
137
|
+
Permits::Ability.orm = :data_mapper
|
138
|
+
</pre>
|
139
|
+
|
140
|
+
Alternatively set it for the Ability instance for more fine grained control
|
141
|
+
<pre>
|
142
|
+
ability = Permits::Ability.new(@editor, :strategy => :string)
|
143
|
+
</pre>
|
144
|
+
|
145
|
+
The ORMs currently supported (and tested) are :active_record, :data_mapper, :mongoid, :mongo_mapper
|
146
|
+
|
87
147
|
## Permits Generator
|
88
148
|
|
89
149
|
Options
|
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ begin
|
|
7
7
|
gem.email = "kmandrup@gmail.com"
|
8
8
|
gem.homepage = "http://github.com/kristianmandrup/cancan-permits"
|
9
9
|
gem.authors = ["Kristian Mandrup"]
|
10
|
-
gem.add_development_dependency "rspec", "~> 2.0.0
|
10
|
+
gem.add_development_dependency "rspec", "~> 2.0.0"
|
11
11
|
gem.add_development_dependency 'code-spec', "~> 0.2.5"
|
12
12
|
gem.add_development_dependency 'rails-app-spec', "~> 0.2.14"
|
13
13
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.5
|
data/cancan-permits.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{cancan-permits}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.5"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kristian Mandrup"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-17}
|
13
13
|
s.description = %q{Role specific Permits for use with CanCan permission system}
|
14
14
|
s.email = %q{kmandrup@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -55,13 +55,14 @@ Gem::Specification.new do |s|
|
|
55
55
|
"spec/data_mapper/owner_permits_spec.rb",
|
56
56
|
"spec/data_mapper/permits_spec.rb",
|
57
57
|
"spec/data_mapper/spec_helper.rb",
|
58
|
+
"spec/fixtures/permits/admin_permit.rb",
|
59
|
+
"spec/fixtures/permits/editor_permit.rb",
|
60
|
+
"spec/fixtures/permits/guest_permit.rb",
|
58
61
|
"spec/generators/permit_generator_spec.rb",
|
59
|
-
"spec/generic/
|
60
|
-
"spec/generic/
|
61
|
-
"spec/generic/
|
62
|
-
"spec/generic/
|
63
|
-
"spec/generic/permits/owner_permits_spec.rb",
|
64
|
-
"spec/generic/permits/permits_spec.rb",
|
62
|
+
"spec/generic/models/all_models.rb",
|
63
|
+
"spec/generic/owner_permits_spec.rb",
|
64
|
+
"spec/generic/permits_spec.rb",
|
65
|
+
"spec/generic/spec_helper.rb",
|
65
66
|
"spec/mongo_mapper/models/all_models.rb",
|
66
67
|
"spec/mongo_mapper/owner_permits_spec.rb",
|
67
68
|
"spec/mongo_mapper/permits_spec.rb",
|
@@ -90,13 +91,14 @@ Gem::Specification.new do |s|
|
|
90
91
|
"spec/data_mapper/owner_permits_spec.rb",
|
91
92
|
"spec/data_mapper/permits_spec.rb",
|
92
93
|
"spec/data_mapper/spec_helper.rb",
|
94
|
+
"spec/fixtures/permits/admin_permit.rb",
|
95
|
+
"spec/fixtures/permits/editor_permit.rb",
|
96
|
+
"spec/fixtures/permits/guest_permit.rb",
|
93
97
|
"spec/generators/permit_generator_spec.rb",
|
94
|
-
"spec/generic/
|
95
|
-
"spec/generic/
|
96
|
-
"spec/generic/
|
97
|
-
"spec/generic/
|
98
|
-
"spec/generic/permits/owner_permits_spec.rb",
|
99
|
-
"spec/generic/permits/permits_spec.rb",
|
98
|
+
"spec/generic/models/all_models.rb",
|
99
|
+
"spec/generic/owner_permits_spec.rb",
|
100
|
+
"spec/generic/permits_spec.rb",
|
101
|
+
"spec/generic/spec_helper.rb",
|
100
102
|
"spec/mongo_mapper/models/all_models.rb",
|
101
103
|
"spec/mongo_mapper/owner_permits_spec.rb",
|
102
104
|
"spec/mongo_mapper/permits_spec.rb",
|
@@ -113,14 +115,14 @@ Gem::Specification.new do |s|
|
|
113
115
|
s.specification_version = 3
|
114
116
|
|
115
117
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
116
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.0.0
|
118
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
|
117
119
|
s.add_development_dependency(%q<code-spec>, ["~> 0.2.5"])
|
118
120
|
s.add_development_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
|
119
121
|
s.add_runtime_dependency(%q<cancan>, ["~> 1.4.0"])
|
120
122
|
s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
|
121
123
|
s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.10"])
|
122
124
|
else
|
123
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0
|
125
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
124
126
|
s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
|
125
127
|
s.add_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
|
126
128
|
s.add_dependency(%q<cancan>, ["~> 1.4.0"])
|
@@ -128,7 +130,7 @@ Gem::Specification.new do |s|
|
|
128
130
|
s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
|
129
131
|
end
|
130
132
|
else
|
131
|
-
s.add_dependency(%q<rspec>, ["~> 2.0.0
|
133
|
+
s.add_dependency(%q<rspec>, ["~> 2.0.0"])
|
132
134
|
s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
|
133
135
|
s.add_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
|
134
136
|
s.add_dependency(%q<cancan>, ["~> 1.4.0"])
|
@@ -10,14 +10,14 @@ module Permit
|
|
10
10
|
clazz = module_name.constantize
|
11
11
|
clazz.new(self).enforce!
|
12
12
|
rescue
|
13
|
-
|
13
|
+
raise "License #{module_name} not found"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
18
|
def initialize ability, options = {}
|
19
19
|
@ability = ability
|
20
|
-
@strategy = options[:strategy] || :default
|
20
|
+
@strategy = options[:strategy] || Permits::Ability.strategy || :default
|
21
21
|
end
|
22
22
|
|
23
23
|
def permit?(user, options = {})
|
@@ -2,45 +2,47 @@ module Permits
|
|
2
2
|
class Ability
|
3
3
|
include CanCan::Ability
|
4
4
|
|
5
|
+
class << self
|
6
|
+
attr_accessor :orm, :strategy
|
7
|
+
|
8
|
+
def orm= orm
|
9
|
+
@orm = orm
|
10
|
+
case orm
|
11
|
+
when :active_record, :generic
|
12
|
+
@strategy = :default
|
13
|
+
else
|
14
|
+
@strategy = :string
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
5
19
|
# set up each Permit instance to share this same Ability
|
6
20
|
# so that the can and cannot operations work on the same permission collection!
|
7
21
|
def self.permits ability, options = {}
|
8
22
|
special_permits = []
|
9
23
|
special_permits << [:system, :any].map{|role| make_permit(role, ability, options)}
|
10
|
-
# puts "Available roles: #{Permits::Roles.available}"
|
11
24
|
role_permits = Permits::Roles.available.inject([]) do |permits, role|
|
12
25
|
permit = make_permit(role, ability, options)
|
13
|
-
# puts "made permit: #{permit}"
|
14
26
|
permits << permit if permit
|
15
27
|
end
|
16
|
-
# puts "role_permits: #{role_permits.inspect}"
|
17
28
|
(special_permits + role_permits).flatten.compact
|
18
29
|
end
|
19
30
|
|
20
31
|
def initialize user, options = {}
|
21
32
|
# put ability logic here!
|
22
|
-
user ||= Guest.new
|
23
|
-
|
33
|
+
user ||= Guest.new
|
24
34
|
all_permits = Permits::Ability.permits(self, options)
|
25
|
-
# puts "Trying permits: #{all_permits.inspect}"
|
26
35
|
all_permits.each do |permit|
|
27
36
|
# get role name of permit
|
28
37
|
permit_role = permit.class.demodulize.gsub(/Permit$/, '').underscore.to_sym
|
29
|
-
|
30
|
-
# puts "Permit role: #{permit_role.inspect}"
|
31
38
|
if permit_role == :system
|
32
39
|
# always execute system permit
|
33
40
|
result = permit.permit?(user, options)
|
34
41
|
break if result == :break
|
35
42
|
else
|
36
43
|
# only execute the permit if the user has the role of the permit or is for any role
|
37
|
-
# puts "does user have_role? #{user.has_role?(permit_role)}, #{user.inspect}"
|
38
44
|
if user.has_role?(permit_role) || permit_role == :any
|
39
|
-
# puts "user: #{user} of #{permit_role} has permit?"
|
40
|
-
# puts "permit: #{permit.inspect}"
|
41
45
|
permit.permit?(user, options)
|
42
|
-
# else
|
43
|
-
# puts "Permit #{permit} not used for role #{permit_role}"
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
@@ -51,12 +53,10 @@ module Permits
|
|
51
53
|
def self.make_permit role, ability, options = {}
|
52
54
|
begin
|
53
55
|
clazz_name = "#{role.to_s.camelize}Permit"
|
54
|
-
# puts "Attempting to load #{clazz_name} permition class"
|
55
56
|
permit_clazz = clazz_name.constantize
|
56
57
|
permit_clazz.new(ability, options) if permit_clazz && permit_clazz.kind_of?(Class)
|
57
58
|
rescue
|
58
|
-
|
59
|
-
nil
|
59
|
+
raise "Permit #{clazz_name} not found"
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'sugar-high/array'
|
2
2
|
require 'active_support/inflector'
|
3
|
+
require 'rails3_artifactor'
|
4
|
+
require 'logging_assist'
|
3
5
|
|
4
6
|
class PermitsGenerator < Rails::Generators::Base
|
5
7
|
desc "Creates a Permit for each role in 'app/permits' and ensures that the permit folder is added to Rails load path."
|
@@ -20,16 +22,32 @@ class PermitsGenerator < Rails::Generators::Base
|
|
20
22
|
template_permit role if !role == :admin
|
21
23
|
end
|
22
24
|
template "licenses.rb", "app/permits/licenses.rb"
|
25
|
+
permits_initializer
|
23
26
|
end
|
24
27
|
|
25
28
|
protected
|
26
29
|
|
30
|
+
include Rails3::Assist::BasicLogger
|
31
|
+
extend Rails3::Assist::UseMacro
|
32
|
+
|
33
|
+
use_helpers :app, :file, :special
|
34
|
+
|
27
35
|
attr_accessor :permit_name, :permit_logic
|
28
36
|
|
29
37
|
# TODO: merge with any registered roles in application
|
30
38
|
def roles
|
31
39
|
options[:roles].uniq.to_symbols
|
32
40
|
end
|
41
|
+
|
42
|
+
def orm
|
43
|
+
options[:orm]
|
44
|
+
end
|
45
|
+
|
46
|
+
def permits_initializer
|
47
|
+
create_initializer :permits do
|
48
|
+
"Permits::Ability.orm = :#{orm}"
|
49
|
+
end
|
50
|
+
end
|
33
51
|
|
34
52
|
def template_permit name, template_name=nil
|
35
53
|
permit_logic = send "#{name}_logic" if [:admin, :system, :any].include?(name)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
require 'rails'
|
4
4
|
require 'active_record'
|
@@ -8,10 +8,6 @@ require 'yaml'
|
|
8
8
|
require 'logger'
|
9
9
|
require 'database_cleaner'
|
10
10
|
|
11
|
-
require 'cancan/matchers'
|
12
|
-
require 'cancan-permits'
|
13
|
-
require 'cancan-permits/rspec'
|
14
|
-
|
15
11
|
module Rails
|
16
12
|
def self.config_root_dir
|
17
13
|
File.dirname(__FILE__)
|
@@ -60,7 +56,6 @@ RSpec.configure do |config|
|
|
60
56
|
end
|
61
57
|
end
|
62
58
|
|
63
|
-
require_all File.dirname(__FILE__) + '/../generic/fixtures/permits'
|
64
59
|
require_all File.dirname(__FILE__) + '/models/all_models'
|
65
60
|
|
66
61
|
module Permits::Roles
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'data_mapper/spec_helper'
|
2
2
|
|
3
|
+
Permits::Ability.orm = :data_mapper
|
4
|
+
|
3
5
|
describe Permits::Ability do
|
4
6
|
context "Editor user" do
|
5
7
|
context "using default :user_id relation - foreign key to User.id" do
|
@@ -7,7 +9,7 @@ describe Permits::Ability do
|
|
7
9
|
@editor = User.create(:name => "Kristian", :role => "editor")
|
8
10
|
@other_guy = User.create(:name => "Random dude", :role => "admin")
|
9
11
|
|
10
|
-
@ability = Permits::Ability.new(@editor
|
12
|
+
@ability = Permits::Ability.new(@editor)
|
11
13
|
|
12
14
|
@own_comment = Comment.create(:user_id => @editor.id)
|
13
15
|
@other_comment = Comment.create(:user_id => @other_guy.id)
|
@@ -1,11 +1,12 @@
|
|
1
1
|
require 'data_mapper/spec_helper'
|
2
2
|
|
3
|
+
Permits::Ability.orm = :data_mapper
|
4
|
+
|
3
5
|
describe Permits::Ability do
|
4
6
|
context "Guest user" do
|
5
7
|
before :each do
|
6
8
|
@guest = User.create(:name => "Kristian", :role => "guest")
|
7
|
-
|
8
|
-
@ability = Permits::Ability.new(@guest, :strategy => :string)
|
9
|
+
@ability = Permits::Ability.new(@guest)
|
9
10
|
|
10
11
|
@comment = Comment.create(:user_id => @guest.id)
|
11
12
|
|
@@ -1,17 +1,9 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
require 'dm-core'
|
4
4
|
require 'dm-types'
|
5
5
|
require 'dm-migrations'
|
6
6
|
|
7
|
-
require 'cancan/matchers'
|
8
|
-
require 'cancan-permits'
|
9
|
-
require 'cancan-permits/rspec'
|
10
|
-
|
11
|
-
# gem install dm-core dm-sqlite-adapter
|
12
|
-
# gem install dm-types dm-validations dm-timestamps dm-aggregates dm-adjust dm-is-list dm-is-tree dm-is-versioned dm-is-nested_set
|
13
|
-
# gem install rails_datamapper dm-migrations dm-observer
|
14
|
-
|
15
7
|
DataMapper::Logger.new($stdout, :debug)
|
16
8
|
DataMapper.setup(:default, 'sqlite::memory:')
|
17
9
|
|
@@ -19,8 +11,6 @@ RSpec.configure do |config|
|
|
19
11
|
config.mock_with :mocha
|
20
12
|
end
|
21
13
|
|
22
|
-
|
23
|
-
require_all File.dirname(__FILE__) + '/../generic/fixtures/permits'
|
24
14
|
require_all File.dirname(__FILE__) + '/models/all_models'
|
25
15
|
|
26
16
|
RSpec.configure do |config|
|
File without changes
|
File without changes
|
File without changes
|
@@ -12,7 +12,9 @@ RSpec::Generator.configure do |config|
|
|
12
12
|
end
|
13
13
|
|
14
14
|
|
15
|
-
describe 'Permits generator' do
|
15
|
+
describe 'Permits generator' do
|
16
|
+
use_helpers :controller, :special, :file
|
17
|
+
|
16
18
|
setup_generator :permits do
|
17
19
|
tests PermitsGenerator
|
18
20
|
end
|
@@ -20,20 +22,29 @@ describe 'Permits generator' do
|
|
20
22
|
describe 'result of running generator with default profile' do
|
21
23
|
before :each do
|
22
24
|
@generator = with_generator do |g|
|
23
|
-
|
25
|
+
arguments = "--orm mongoid".args
|
26
|
+
g.run_generator arguments
|
24
27
|
end
|
25
28
|
end
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
+
|
30
|
+
describe 'result of running Permits generator' do
|
31
|
+
it "should create Admin permit" do
|
32
|
+
@generator.should generate_permit :admin
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should generate a permits initializer file with orm set to mongoid" do
|
36
|
+
File.read(initializer_file(:permits)).should match /Permits::Application.orm = mongoid/
|
37
|
+
end
|
29
38
|
end
|
30
39
|
end
|
31
40
|
|
41
|
+
# TODO
|
42
|
+
|
32
43
|
# describe 'result of running generator with option to create permit for each registered role' do
|
33
44
|
# context "Registered roles :guest, :admin"
|
34
45
|
# before :each do
|
35
46
|
# with_generator do |g|
|
36
|
-
# g.run_generator "--roles admin
|
47
|
+
# g.run_generator "--roles admin editor"
|
37
48
|
# end
|
38
49
|
# end
|
39
50
|
#
|
File without changes
|
@@ -1,17 +1,4 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
class Comment
|
4
|
-
attr_accessor :owner
|
5
|
-
end
|
6
|
-
|
7
|
-
class Post
|
8
|
-
attr_accessor :writer
|
9
|
-
end
|
10
|
-
|
11
|
-
class Article
|
12
|
-
attr_accessor :author
|
13
|
-
end
|
14
|
-
|
1
|
+
require 'generic/spec_helper'
|
15
2
|
|
16
3
|
describe Permits::Ability do
|
17
4
|
context "Guest user" do
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
require_all File.dirname(__FILE__) + '/models/all_models'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.mock_with :mocha
|
7
|
+
end
|
8
|
+
|
9
|
+
module Permits::Roles
|
10
|
+
def self.available
|
11
|
+
User.roles
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class User
|
16
|
+
attr_accessor :id, :role, :name
|
17
|
+
|
18
|
+
def self.roles
|
19
|
+
[:guest, :admin, :editor]
|
20
|
+
end
|
21
|
+
|
22
|
+
def initialize id, role, name = nil
|
23
|
+
self.id = id
|
24
|
+
raise ArgumentError, "Role #{role} is not in list of available roles: #{self.class.roles}" if !self.class.roles.include? role
|
25
|
+
self.role = role
|
26
|
+
self.name = name || role.to_s
|
27
|
+
end
|
28
|
+
|
29
|
+
def has_role? role
|
30
|
+
self.role == role
|
31
|
+
end
|
32
|
+
end
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'mongo_mapper/spec_helper'
|
2
2
|
|
3
|
+
Permits::Ability.orm = :mongo_mapper
|
4
|
+
|
3
5
|
describe Permits::Ability do
|
4
6
|
context "Editor user" do
|
5
7
|
context "using default :user_id relation - foreign key to User.id" do
|
@@ -7,7 +9,7 @@ describe Permits::Ability do
|
|
7
9
|
@editor = User.create(:name => "Kristian", :role => "editor")
|
8
10
|
@other_guy = User.create(:name => "Random dude", :role => "admin")
|
9
11
|
|
10
|
-
@ability = Permits::Ability.new(@editor
|
12
|
+
@ability = Permits::Ability.new(@editor)
|
11
13
|
|
12
14
|
@own_comment = Comment.create(:user_id => @editor.id)
|
13
15
|
@other_comment = Comment.create(:user_id => @other_guy.id)
|
@@ -42,7 +44,7 @@ describe Permits::Ability do
|
|
42
44
|
@editor = User.create(:name => "Kristian", :role => "editor")
|
43
45
|
@other_guy = User.create(:name => "Random dude", :role => "admin")
|
44
46
|
|
45
|
-
@ability = Permits::Ability.new(@editor
|
47
|
+
@ability = Permits::Ability.new(@editor)
|
46
48
|
|
47
49
|
@own_post = Post.create(:writer => @editor.id)
|
48
50
|
@other_post = Post.create(:writer => @other_guy.id)
|
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'mongo_mapper/spec_helper'
|
2
2
|
|
3
|
+
Permits::Ability.orm = :mongo_mapper
|
4
|
+
|
3
5
|
describe Permits::Ability do
|
4
6
|
context "Guest user" do
|
5
7
|
before :each do
|
6
8
|
@guest = User.create(:name => "Kristian", :role => "guest")
|
7
9
|
|
8
|
-
@ability = Permits::Ability.new(@guest
|
10
|
+
@ability = Permits::Ability.new(@guest)
|
9
11
|
|
10
12
|
@comment = Comment.create(:user_id => @guest.id)
|
11
13
|
|
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'mongo_mapper'
|
3
|
-
require 'cancan/matchers'
|
4
|
-
require 'cancan-permits'
|
5
|
-
require 'cancan-permits/rspec'
|
6
3
|
|
7
|
-
require_all File.dirname(__FILE__) + '/../generic/fixtures/permits'
|
8
4
|
require_all File.dirname(__FILE__) + '/models/all_models'
|
9
5
|
|
10
6
|
RSpec.configure do |config|
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require 'mongoid/spec_helper'
|
2
2
|
|
3
|
+
Permits::Ability.orm = :mongoid
|
4
|
+
|
3
5
|
describe Permits::Ability do
|
4
6
|
context "Editor user" do
|
5
7
|
context "using default :user_id relation - foreign key to User.id" do
|
@@ -7,7 +9,7 @@ describe Permits::Ability do
|
|
7
9
|
@editor = User.create(:name => "Kristian", :role => "editor")
|
8
10
|
@other_guy = User.create(:name => "Random dude", :role => "admin")
|
9
11
|
|
10
|
-
@ability = Permits::Ability.new(@editor
|
12
|
+
@ability = Permits::Ability.new(@editor)
|
11
13
|
|
12
14
|
@own_comment = Comment.create(:user_id => @editor.id)
|
13
15
|
@other_comment = Comment.create(:user_id => @other_guy.id)
|
@@ -42,7 +44,7 @@ describe Permits::Ability do
|
|
42
44
|
@editor = User.create(:name => "Kristian", :role => "editor")
|
43
45
|
@other_guy = User.create(:name => "Random dude", :role => "admin")
|
44
46
|
|
45
|
-
@ability = Permits::Ability.new(@editor
|
47
|
+
@ability = Permits::Ability.new(@editor)
|
46
48
|
|
47
49
|
@own_post = Post.create(:writer => @editor.id)
|
48
50
|
@other_post = Post.create(:writer => @other_guy.id)
|
data/spec/mongoid/spec_helper.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'spec_helper'
|
2
2
|
require 'mongoid'
|
3
|
-
require 'cancan/matchers'
|
4
|
-
require 'cancan-permits'
|
5
|
-
require 'cancan-permits/rspec'
|
6
3
|
|
7
|
-
require_all File.dirname(__FILE__) + '/../generic/fixtures/permits'
|
8
4
|
require_all File.dirname(__FILE__) + '/models/all_models'
|
9
5
|
|
10
6
|
RSpec.configure do |config|
|
data/spec/spec_helper.rb
CHANGED
@@ -3,35 +3,4 @@ require 'cancan/matchers'
|
|
3
3
|
require 'cancan-permits'
|
4
4
|
require 'cancan-permits/rspec'
|
5
5
|
|
6
|
-
require_all File.dirname(__FILE__) + '/
|
7
|
-
|
8
|
-
require_all File.dirname(__FILE__) + '/generic/permits/fixtures'
|
9
|
-
|
10
|
-
RSpec.configure do |config|
|
11
|
-
config.mock_with :mocha
|
12
|
-
end
|
13
|
-
|
14
|
-
module Permits::Roles
|
15
|
-
def self.available
|
16
|
-
User.roles
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
class User
|
21
|
-
attr_accessor :id, :role, :name
|
22
|
-
|
23
|
-
def self.roles
|
24
|
-
[:guest, :admin, :editor]
|
25
|
-
end
|
26
|
-
|
27
|
-
def initialize id, role, name = nil
|
28
|
-
self.id = id
|
29
|
-
raise ArgumentError, "Role #{role} is not in list of available roles: #{self.class.roles}" if !self.class.roles.include? role
|
30
|
-
self.role = role
|
31
|
-
self.name = name || role.to_s
|
32
|
-
end
|
33
|
-
|
34
|
-
def has_role? role
|
35
|
-
self.role == role
|
36
|
-
end
|
37
|
-
end
|
6
|
+
require_all File.dirname(__FILE__) + '/fixtures/permits'
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 2
|
8
|
-
-
|
9
|
-
version: 0.2.
|
8
|
+
- 5
|
9
|
+
version: 0.2.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Kristian Mandrup
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-17 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -29,9 +29,7 @@ dependencies:
|
|
29
29
|
- 2
|
30
30
|
- 0
|
31
31
|
- 0
|
32
|
-
|
33
|
-
- 22
|
34
|
-
version: 2.0.0.beta.22
|
32
|
+
version: 2.0.0
|
35
33
|
type: :development
|
36
34
|
version_requirements: *id001
|
37
35
|
- !ruby/object:Gem::Dependency
|
@@ -157,13 +155,14 @@ files:
|
|
157
155
|
- spec/data_mapper/owner_permits_spec.rb
|
158
156
|
- spec/data_mapper/permits_spec.rb
|
159
157
|
- spec/data_mapper/spec_helper.rb
|
158
|
+
- spec/fixtures/permits/admin_permit.rb
|
159
|
+
- spec/fixtures/permits/editor_permit.rb
|
160
|
+
- spec/fixtures/permits/guest_permit.rb
|
160
161
|
- spec/generators/permit_generator_spec.rb
|
161
|
-
- spec/generic/
|
162
|
-
- spec/generic/
|
163
|
-
- spec/generic/
|
164
|
-
- spec/generic/
|
165
|
-
- spec/generic/permits/owner_permits_spec.rb
|
166
|
-
- spec/generic/permits/permits_spec.rb
|
162
|
+
- spec/generic/models/all_models.rb
|
163
|
+
- spec/generic/owner_permits_spec.rb
|
164
|
+
- spec/generic/permits_spec.rb
|
165
|
+
- spec/generic/spec_helper.rb
|
167
166
|
- spec/mongo_mapper/models/all_models.rb
|
168
167
|
- spec/mongo_mapper/owner_permits_spec.rb
|
169
168
|
- spec/mongo_mapper/permits_spec.rb
|
@@ -218,13 +217,14 @@ test_files:
|
|
218
217
|
- spec/data_mapper/owner_permits_spec.rb
|
219
218
|
- spec/data_mapper/permits_spec.rb
|
220
219
|
- spec/data_mapper/spec_helper.rb
|
220
|
+
- spec/fixtures/permits/admin_permit.rb
|
221
|
+
- spec/fixtures/permits/editor_permit.rb
|
222
|
+
- spec/fixtures/permits/guest_permit.rb
|
221
223
|
- spec/generators/permit_generator_spec.rb
|
222
|
-
- spec/generic/
|
223
|
-
- spec/generic/
|
224
|
-
- spec/generic/
|
225
|
-
- spec/generic/
|
226
|
-
- spec/generic/permits/owner_permits_spec.rb
|
227
|
-
- spec/generic/permits/permits_spec.rb
|
224
|
+
- spec/generic/models/all_models.rb
|
225
|
+
- spec/generic/owner_permits_spec.rb
|
226
|
+
- spec/generic/permits_spec.rb
|
227
|
+
- spec/generic/spec_helper.rb
|
228
228
|
- spec/mongo_mapper/models/all_models.rb
|
229
229
|
- spec/mongo_mapper/owner_permits_spec.rb
|
230
230
|
- spec/mongo_mapper/permits_spec.rb
|