cancan-permits 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -4,10 +4,11 @@ Role specific Permits for use with [CanCan](http://github.com/ryanb/cancan) perm
4
4
 
5
5
  ## Update Oct 13
6
6
 
7
- Now updated to support multiple ownership startegies so that alternative ORMs can be supported. Demonstrates how to use it with Mongoid, including specs to prove it!
8
- Special thanks to Sam (yoda) for this inspiration and help in this regard :)
7
+ Now updated to support multiple ownership startegies so that alternative ORMs can be supported.
8
+ This gem now includes specs that demonstrate how to configure it for use with Mongoid, Mongo Mapper and Data Mapper!
9
+ Special thanks to Sam (yoda) for the initial inspiration and work to ensure support for Mongoid :)
9
10
 
10
- The generator has also been updated slightly to support this new strategy as of version 0.2.1.
11
+ The generator has also been updated slightly to support this new strategy option as of version 0.2.1.
11
12
  In general, the new Permits API now uses an options hash to replace the old optional request parameter.
12
13
  This design allows for better extensibility in the future if needed.
13
14
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.2.2
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cancan-permits}
8
- s.version = "0.2.1"
8
+ s.version = "0.2.2"
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"]
@@ -47,7 +47,15 @@ Gem::Specification.new do |s|
47
47
  "spec/cancan-permits/permits/fixtures/models.rb",
48
48
  "spec/cancan-permits/permits/owner_permits_spec.rb",
49
49
  "spec/cancan-permits/permits/permits_spec.rb",
50
+ "spec/data_mapper/models/all_models.rb",
51
+ "spec/data_mapper/owner_permits_spec.rb",
52
+ "spec/data_mapper/permits_spec.rb",
53
+ "spec/data_mapper/spec_helper.rb",
50
54
  "spec/generators/permit_generator_spec.rb",
55
+ "spec/mongo_mapper/models/all_models.rb",
56
+ "spec/mongo_mapper/owner_permits_spec.rb",
57
+ "spec/mongo_mapper/permits_spec.rb",
58
+ "spec/mongo_mapper/spec_helper.rb",
51
59
  "spec/mongoid/models/all_models.rb",
52
60
  "spec/mongoid/owner_permits_spec.rb",
53
61
  "spec/mongoid/permits_spec.rb",
@@ -66,7 +74,15 @@ Gem::Specification.new do |s|
66
74
  "spec/cancan-permits/permits/fixtures/models.rb",
67
75
  "spec/cancan-permits/permits/owner_permits_spec.rb",
68
76
  "spec/cancan-permits/permits/permits_spec.rb",
77
+ "spec/data_mapper/models/all_models.rb",
78
+ "spec/data_mapper/owner_permits_spec.rb",
79
+ "spec/data_mapper/permits_spec.rb",
80
+ "spec/data_mapper/spec_helper.rb",
69
81
  "spec/generators/permit_generator_spec.rb",
82
+ "spec/mongo_mapper/models/all_models.rb",
83
+ "spec/mongo_mapper/owner_permits_spec.rb",
84
+ "spec/mongo_mapper/permits_spec.rb",
85
+ "spec/mongo_mapper/spec_helper.rb",
70
86
  "spec/mongoid/models/all_models.rb",
71
87
  "spec/mongoid/owner_permits_spec.rb",
72
88
  "spec/mongoid/permits_spec.rb",
@@ -43,21 +43,8 @@ module Permit
43
43
  # puts "Using strategy: #{strategy_used}"
44
44
  begin
45
45
  case strategy_used
46
- when :mongoid
47
- # puts "Ownership with mongoid for class: #{clazz}"
48
- # can :manage, clazz, ownership_relation => user_id
49
- can :manage, clazz do |obj|
50
- # puts "obj: #{obj.inspect}"
51
- # puts "ownership relation: #{ownership_relation}"
52
- # rel = obj.send ownership_relation
53
- #
54
- # puts "related obj: #{rel.inspect}"
55
- # puts "user_id: #{user_id_attribute.inspect}"
56
- # puts "user.user_id: #{user.send(user_id_attribute).inspect}"
57
- # puts user.send(user_id_attribute).to_s
58
- #
59
- obj.send(ownership_relation) == user.send(user_id_attribute).to_s
60
- end
46
+ when :orm
47
+ can :manage, clazz, ownership_relation => user_id.to_s
61
48
  when :default
62
49
  # puts "Basic CanCan ownership"
63
50
  can :manage, clazz, ownership_relation => user_id
@@ -0,0 +1,20 @@
1
+ class Comment
2
+ include DataMapper::Resource
3
+
4
+ property :id, Serial
5
+ property :user_id, String
6
+ end
7
+
8
+ class Post
9
+ include DataMapper::Resource
10
+
11
+ property :id, Serial
12
+ property :writer, String
13
+ end
14
+
15
+ class Article
16
+ include DataMapper::Resource
17
+
18
+ property :id, Serial
19
+ property :author, String
20
+ end
@@ -0,0 +1,73 @@
1
+ require 'data_mapper/spec_helper'
2
+
3
+ describe Permits::Ability do
4
+ context "Editor user" do
5
+ context "using default :user_id relation - foreign key to User.id" do
6
+ before :each do
7
+ @editor = User.create(:name => "Kristian", :role => "editor")
8
+ @other_guy = User.create(:name => "Random dude", :role => "admin")
9
+
10
+ @ability = Permits::Ability.new(@editor, :strategy => :orm)
11
+
12
+ @own_comment = Comment.create(:user_id => @editor.id)
13
+ @other_comment = Comment.create(:user_id => @other_guy.id)
14
+ # @post = Post.create(:writer => @editor.id)
15
+ # @article = Article.create(:author => @editor.id)
16
+ end
17
+
18
+ it "should be able to :read Comment he owns" do
19
+ @ability.should be_able_to(:read, Comment)
20
+ @ability.should be_able_to(:read, @own_comment)
21
+ end
22
+
23
+ it "should be able to :update Comment he owns" do
24
+ @ability.should be_able_to(:update, @own_comment)
25
+ end
26
+
27
+ it "should NOT be able to :update Comment he does NOT own" do
28
+ @ability.should_not be_able_to(:update, @other_comment)
29
+ end
30
+
31
+ it "should be able to :delete Comment he owns" do
32
+ @ability.should be_able_to(:delete, @own_comment)
33
+ end
34
+
35
+ it "should NOT be able to :update Comment he does NOT own" do
36
+ @ability.should_not be_able_to(:delete, @other_comment)
37
+ end
38
+ end
39
+
40
+ context "using custom :writer relation - foreign key to User.id" do
41
+ before :each do
42
+ @editor = User.create(:name => "Kristian", :role => "editor")
43
+ @other_guy = User.create(:name => "Random dude", :role => "admin")
44
+
45
+ @ability = Permits::Ability.new(@editor, :strategy => :orm)
46
+
47
+ @own_post = Post.create(:writer => @editor.id)
48
+ @other_post = Post.create(:writer => @other_guy.id)
49
+ end
50
+
51
+ it "should be able to :read Post he owns" do
52
+ @ability.should be_able_to(:read, Post)
53
+ @ability.should be_able_to(:read, @own_post)
54
+ end
55
+
56
+ it "should be able to :update Post he owns" do
57
+ @ability.should be_able_to(:update, @own_post)
58
+ end
59
+
60
+ it "should NOT be able to :update Post he does NOT own" do
61
+ @ability.should_not be_able_to(:update, @other_post)
62
+ end
63
+
64
+ it "should be able to :delete Post he owns" do
65
+ @ability.should be_able_to(:delete, @own_post)
66
+ end
67
+
68
+ it "should NOT be able to :update Post he does NOT own" do
69
+ @ability.should_not be_able_to(:delete, @other_post)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,36 @@
1
+ require 'data_mapper/spec_helper'
2
+
3
+ describe Permits::Ability do
4
+ context "Guest user" do
5
+ before :each do
6
+ @guest = User.create(:name => "Kristian", :role => "guest")
7
+
8
+ @ability = Permits::Ability.new(@guest, :strategy => :orm)
9
+
10
+ @comment = Comment.create(:user_id => @guest.id)
11
+
12
+ @post = Post.create(:writer => @guest.id)
13
+
14
+ @article = Article.create(:author => @guest.id)
15
+ end
16
+
17
+ it "should be able to :read Comment and Post but NOT Article" do
18
+ @ability.can?(:read, Comment).should be_true
19
+ @ability.can?(:read, @comment).should be_true
20
+
21
+ @ability.can?(:read, Post).should be_true
22
+ @ability.can?(:read, @post).should be_true
23
+
24
+ @ability.can?(:read, Article).should be_false
25
+ @ability.can?(:read, @article).should be_false
26
+ end
27
+
28
+ it "should be not able to :update only Comment" do
29
+ @ability.can?(:update, Comment).should be_true
30
+ @ability.can?(:update, @comment).should be_true
31
+
32
+ @ability.can?(:update, Post).should be_false
33
+ @ability.can?(:update, @post).should be_false
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,56 @@
1
+ require 'rspec/core'
2
+
3
+ require 'dm-core'
4
+ require 'dm-types'
5
+ require 'dm-migrations'
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
+ DataMapper::Logger.new($stdout, :debug)
16
+ DataMapper.setup(:default, 'sqlite::memory:')
17
+
18
+ RSpec.configure do |config|
19
+ config.mock_with :mocha
20
+ end
21
+
22
+
23
+ require_all File.dirname(__FILE__) + '/../cancan-permits/fixtures/permits'
24
+ require_all File.dirname(__FILE__) + '/models/all_models'
25
+
26
+ RSpec.configure do |config|
27
+ config.mock_with :mocha
28
+ end
29
+
30
+ module Permits::Roles
31
+ def self.available
32
+ User.roles
33
+ end
34
+ end
35
+
36
+ class User
37
+ include DataMapper::Resource
38
+
39
+ property :id, Serial
40
+ property :role, String
41
+ property :name, String
42
+
43
+
44
+ def self.roles
45
+ [:guest, :admin, :editor]
46
+ end
47
+
48
+ def has_role? role
49
+ self.role.to_sym == role.to_sym
50
+ end
51
+ end
52
+
53
+ DataMapper.finalize
54
+ DataMapper.auto_migrate!
55
+
56
+
@@ -0,0 +1,17 @@
1
+ class Comment
2
+ include MongoMapper::Document
3
+
4
+ key :user_id, String
5
+ end
6
+
7
+ class Post
8
+ include MongoMapper::Document
9
+
10
+ key :writer, String
11
+ end
12
+
13
+ class Article
14
+ include MongoMapper::Document
15
+
16
+ key :author, String
17
+ end
@@ -0,0 +1,73 @@
1
+ require 'mongo_mapper/spec_helper'
2
+
3
+ describe Permits::Ability do
4
+ context "Editor user" do
5
+ context "using default :user_id relation - foreign key to User.id" do
6
+ before :each do
7
+ @editor = User.create(:name => "Kristian", :role => "editor")
8
+ @other_guy = User.create(:name => "Random dude", :role => "admin")
9
+
10
+ @ability = Permits::Ability.new(@editor, :strategy => :orm)
11
+
12
+ @own_comment = Comment.create(:user_id => @editor.id)
13
+ @other_comment = Comment.create(:user_id => @other_guy.id)
14
+ # @post = Post.create(:writer => @editor.id)
15
+ # @article = Article.create(:author => @editor.id)
16
+ end
17
+
18
+ it "should be able to :read Comment he owns" do
19
+ @ability.should be_able_to(:read, Comment)
20
+ @ability.should be_able_to(:read, @own_comment)
21
+ end
22
+
23
+ it "should be able to :update Comment he owns" do
24
+ @ability.should be_able_to(:update, @own_comment)
25
+ end
26
+
27
+ it "should NOT be able to :update Comment he does NOT own" do
28
+ @ability.should_not be_able_to(:update, @other_comment)
29
+ end
30
+
31
+ it "should be able to :delete Comment he owns" do
32
+ @ability.should be_able_to(:delete, @own_comment)
33
+ end
34
+
35
+ it "should NOT be able to :update Comment he does NOT own" do
36
+ @ability.should_not be_able_to(:delete, @other_comment)
37
+ end
38
+ end
39
+
40
+ context "using custom :writer relation - foreign key to User.id" do
41
+ before :each do
42
+ @editor = User.create(:name => "Kristian", :role => "editor")
43
+ @other_guy = User.create(:name => "Random dude", :role => "admin")
44
+
45
+ @ability = Permits::Ability.new(@editor, :strategy => :mongo)
46
+
47
+ @own_post = Post.create(:writer => @editor.id)
48
+ @other_post = Post.create(:writer => @other_guy.id)
49
+ end
50
+
51
+ it "should be able to :read Post he owns" do
52
+ @ability.should be_able_to(:read, Post)
53
+ @ability.should be_able_to(:read, @own_post)
54
+ end
55
+
56
+ it "should be able to :update Post he owns" do
57
+ @ability.should be_able_to(:update, @own_post)
58
+ end
59
+
60
+ it "should NOT be able to :update Post he does NOT own" do
61
+ @ability.should_not be_able_to(:update, @other_post)
62
+ end
63
+
64
+ it "should be able to :delete Post he owns" do
65
+ @ability.should be_able_to(:delete, @own_post)
66
+ end
67
+
68
+ it "should NOT be able to :update Post he does NOT own" do
69
+ @ability.should_not be_able_to(:delete, @other_post)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,36 @@
1
+ require 'mongo_mapper/spec_helper'
2
+
3
+ describe Permits::Ability do
4
+ context "Guest user" do
5
+ before :each do
6
+ @guest = User.create(:name => "Kristian", :role => "guest")
7
+
8
+ @ability = Permits::Ability.new(@guest, :strategy => :orm)
9
+
10
+ @comment = Comment.create(:user_id => @guest.id)
11
+
12
+ @post = Post.create(:writer => @guest.id)
13
+
14
+ @article = Article.create(:author => @guest.id)
15
+ end
16
+
17
+ it "should be able to :read Comment and Post but NOT Article" do
18
+ @ability.can?(:read, Comment).should be_true
19
+ @ability.can?(:read, @comment).should be_true
20
+
21
+ @ability.can?(:read, Post).should be_true
22
+ @ability.can?(:read, @post).should be_true
23
+
24
+ @ability.can?(:read, Article).should be_false
25
+ @ability.can?(:read, @article).should be_false
26
+ end
27
+
28
+ it "should be not able to :update only Comment" do
29
+ @ability.can?(:update, Comment).should be_true
30
+ @ability.can?(:update, @comment).should be_true
31
+
32
+ @ability.can?(:update, Post).should be_false
33
+ @ability.can?(:update, @post).should be_false
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,59 @@
1
+ require 'rspec/core'
2
+ require 'mongo_mapper'
3
+ require 'cancan/matchers'
4
+ require 'cancan-permits'
5
+ require 'cancan-permits/rspec'
6
+
7
+ require_all File.dirname(__FILE__) + '/../cancan-permits/fixtures/permits'
8
+ require_all File.dirname(__FILE__) + '/models/all_models'
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
+ include MongoMapper::Document
22
+
23
+ key :role, String
24
+ key :name, String
25
+
26
+ def self.roles
27
+ [:guest, :admin, :editor]
28
+ end
29
+
30
+ def has_role? role
31
+ self.role.to_sym == role.to_sym
32
+ end
33
+ end
34
+
35
+
36
+ MongoMapper.database = 'cancan-permits_mongo_mapper'
37
+
38
+ module Database
39
+ def self.teardown
40
+ # MongoMapper.database.collections.each {|collection| collection.drop }
41
+ MongoMapper.database.collections.each do |coll|
42
+ coll.drop unless coll.name =~ /(.*\.)?system\..*/
43
+ end
44
+ end
45
+ end
46
+
47
+
48
+ RSpec.configure do |config|
49
+ config.mock_with :mocha
50
+ config.before do
51
+ MongoMapper.database.collections.each do |coll|
52
+ coll.drop unless coll.name =~ /(.*\.)?system\..*/
53
+ end
54
+ end
55
+ end
56
+
57
+
58
+
59
+
@@ -7,7 +7,7 @@ describe Permits::Ability do
7
7
  @editor = User.create(:name => "Kristian", :role => "editor")
8
8
  @other_guy = User.create(:name => "Random dude", :role => "admin")
9
9
 
10
- @ability = Permits::Ability.new(@editor, :strategy => :mongoid)
10
+ @ability = Permits::Ability.new(@editor, :strategy => :orm)
11
11
 
12
12
  @own_comment = Comment.create(:user_id => @editor.id)
13
13
  @other_comment = Comment.create(:user_id => @other_guy.id)
@@ -42,7 +42,7 @@ describe Permits::Ability do
42
42
  @editor = User.create(:name => "Kristian", :role => "editor")
43
43
  @other_guy = User.create(:name => "Random dude", :role => "admin")
44
44
 
45
- @ability = Permits::Ability.new(@editor, :strategy => :mongoid)
45
+ @ability = Permits::Ability.new(@editor, :strategy => :mongo)
46
46
 
47
47
  @own_post = Post.create(:writer => @editor.id)
48
48
  @other_post = Post.create(:writer => @other_guy.id)
@@ -1,23 +1,11 @@
1
1
  require 'mongoid/spec_helper'
2
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
-
15
3
  describe Permits::Ability do
16
4
  context "Guest user" do
17
5
  before :each do
18
6
  @guest = User.create(:name => "Kristian", :role => "guest")
19
7
 
20
- @ability = Permits::Ability.new(@guest)
8
+ @ability = Permits::Ability.new(@guest, :strategy => :orm)
21
9
 
22
10
  @comment = Comment.create(:user_id => @guest.id)
23
11
 
@@ -26,10 +14,6 @@ describe Permits::Ability do
26
14
  @article = Article.create(:author => @guest.id)
27
15
  end
28
16
 
29
- # can :read, [Comment, Post]
30
- # can [:update, :destroy], [Comment]
31
- # can :create, Article
32
-
33
17
  it "should be able to :read Comment and Post but NOT Article" do
34
18
  @ability.can?(:read, Comment).should be_true
35
19
  @ability.can?(:read, @comment).should be_true
@@ -48,7 +32,6 @@ describe Permits::Ability do
48
32
  @ability.can?(:update, Post).should be_false
49
33
  @ability.can?(:update, @post).should be_false
50
34
  end
51
-
52
35
  end
53
36
 
54
37
  context "Admin user" do
@@ -56,9 +39,7 @@ describe Permits::Ability do
56
39
  @admin = User.create(:role => 'admin')
57
40
  @ability = Permits::Ability.new(@admin)
58
41
  end
59
- #
60
- # # can :manage, :all
61
- #
42
+
62
43
  it "should be able to :read anything" do
63
44
  @ability.can?(:read, Comment).should be_true
64
45
  @ability.can?(:read, Post).should be_true
@@ -43,10 +43,6 @@ module Database
43
43
  end
44
44
  end
45
45
 
46
- Mongoid.database.collections.each do |coll|
47
- coll.remove
48
- end
49
-
50
46
  RSpec.configure do |config|
51
47
  config.mock_with :mocha
52
48
  config.before do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 1
9
- version: 0.2.1
8
+ - 2
9
+ version: 0.2.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kristian Mandrup
@@ -149,7 +149,15 @@ files:
149
149
  - spec/cancan-permits/permits/fixtures/models.rb
150
150
  - spec/cancan-permits/permits/owner_permits_spec.rb
151
151
  - spec/cancan-permits/permits/permits_spec.rb
152
+ - spec/data_mapper/models/all_models.rb
153
+ - spec/data_mapper/owner_permits_spec.rb
154
+ - spec/data_mapper/permits_spec.rb
155
+ - spec/data_mapper/spec_helper.rb
152
156
  - spec/generators/permit_generator_spec.rb
157
+ - spec/mongo_mapper/models/all_models.rb
158
+ - spec/mongo_mapper/owner_permits_spec.rb
159
+ - spec/mongo_mapper/permits_spec.rb
160
+ - spec/mongo_mapper/spec_helper.rb
153
161
  - spec/mongoid/models/all_models.rb
154
162
  - spec/mongoid/owner_permits_spec.rb
155
163
  - spec/mongoid/permits_spec.rb
@@ -194,7 +202,15 @@ test_files:
194
202
  - spec/cancan-permits/permits/fixtures/models.rb
195
203
  - spec/cancan-permits/permits/owner_permits_spec.rb
196
204
  - spec/cancan-permits/permits/permits_spec.rb
205
+ - spec/data_mapper/models/all_models.rb
206
+ - spec/data_mapper/owner_permits_spec.rb
207
+ - spec/data_mapper/permits_spec.rb
208
+ - spec/data_mapper/spec_helper.rb
197
209
  - spec/generators/permit_generator_spec.rb
210
+ - spec/mongo_mapper/models/all_models.rb
211
+ - spec/mongo_mapper/owner_permits_spec.rb
212
+ - spec/mongo_mapper/permits_spec.rb
213
+ - spec/mongo_mapper/spec_helper.rb
198
214
  - spec/mongoid/models/all_models.rb
199
215
  - spec/mongoid/owner_permits_spec.rb
200
216
  - spec/mongoid/permits_spec.rb