cancan-permits 0.2.5 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/.gitignore +2 -0
  2. data/Changelog.txt +12 -0
  3. data/README.markdown +2 -11
  4. data/Rakefile +2 -2
  5. data/VERSION +1 -1
  6. data/cancan-permits.gemspec +38 -9
  7. data/lib/cancan-permits/{permit → license}/base_license.rb +0 -0
  8. data/lib/cancan-permits/main.rb +2 -1
  9. data/lib/cancan-permits/permit/base_permit.rb +6 -2
  10. data/lib/cancan-permits/permit/util.rb +7 -0
  11. data/lib/cancan-permits/permits/ability.rb +6 -2
  12. data/spec/active_record/owner_permits_spec.rb +1 -73
  13. data/spec/active_record/permits_spec.rb +1 -37
  14. data/spec/active_record/spec_helper.rb +2 -0
  15. data/spec/data_mapper/owner_permits_spec.rb +1 -74
  16. data/spec/data_mapper/permits_spec.rb +1 -36
  17. data/spec/data_mapper/spec_helper.rb +2 -0
  18. data/spec/fixtures/permits/any_permit.rb +9 -0
  19. data/spec/fixtures/permits/super_admin_permit.rb +12 -0
  20. data/spec/fixtures/permits/system_permit.rb +9 -0
  21. data/spec/generic/api/basic/config.rb +15 -0
  22. data/spec/generic/api/basic/xgroup.rb +55 -0
  23. data/spec/generic/api/basic/xgroup_orm.rb +20 -0
  24. data/spec/generic/api/owner/config.rb +25 -0
  25. data/spec/generic/api/owner/xgroup.rb +59 -0
  26. data/spec/generic/api/owner/xgroup_orm.rb +23 -0
  27. data/spec/generic/owner_permits_spec.rb +1 -27
  28. data/spec/generic/permits_spec.rb +1 -65
  29. data/spec/generic/spec_helper.rb +15 -1
  30. data/spec/mongo_mapper/owner_permits_spec.rb +1 -74
  31. data/spec/mongo_mapper/permits_spec.rb +1 -35
  32. data/spec/mongo_mapper/spec_helper.rb +5 -4
  33. data/spec/mongoid/owner_permits_spec.rb +1 -73
  34. data/spec/mongoid/permits_spec.rb +1 -65
  35. data/spec/mongoid/spec_helper.rb +4 -3
  36. data/spec/simply_stored/CouchDB.txt +104 -0
  37. data/spec/simply_stored/models/all_models.rb +17 -0
  38. data/spec/simply_stored/owner_permits_spec.rb +2 -0
  39. data/spec/simply_stored/permits_spec.rb +2 -0
  40. data/spec/simply_stored/spec_helper.rb +53 -0
  41. metadata +39 -10
data/.gitignore CHANGED
@@ -18,4 +18,6 @@ coverage
18
18
  rdoc
19
19
  pkg
20
20
 
21
+ *.orig
22
+
21
23
  ## PROJECT::SPECIFIC
@@ -0,0 +1,12 @@
1
+ ## Oct 23, 2010
2
+
3
+ The API specs are now stored in spec/generic/api. To spec that an ORM implementation of cancan-permits work, simply require the 'xgroup_orm' file from the /basic and /owner
4
+ API folders. See current specs for various ORMs for how to do this. In special cases, override the configuration methods present in fx basic/config.rb and owner/config.rb
5
+
6
+ ## Oct 22, 2010
7
+
8
+ * Added support for Simply Stored ORM that can be used with Couch DB.
9
+
10
+ See specs in /simply_stored folder. It took only about 2 minutes to create this support, demonstrating how easy it now is to add support for new ORMs and data stores :)
11
+
12
+
@@ -2,18 +2,9 @@
2
2
 
3
3
  Role specific Permits for use with [CanCan](http://github.com/ryanb/cancan) permission system.
4
4
 
5
- ## Update Oct 14
5
+ ## Changes
6
6
 
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 Active Record, Data Mapper, Mongoid, Mongo Mapper
9
- Special thanks to Sam (yoda) for the initial inspiration and work to ensure support for Mongoid :)
10
-
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.
16
- This design allows for better extensibility in the future if needed.
7
+ See Changelog.txt
17
8
 
18
9
  ## Install
19
10
 
data/Rakefile CHANGED
@@ -9,11 +9,11 @@ begin
9
9
  gem.authors = ["Kristian Mandrup"]
10
10
  gem.add_development_dependency "rspec", "~> 2.0.0"
11
11
  gem.add_development_dependency 'code-spec', "~> 0.2.5"
12
- gem.add_development_dependency 'rails-app-spec', "~> 0.2.14"
12
+ gem.add_development_dependency 'rails-app-spec', "~> 0.3.0"
13
13
 
14
14
  gem.add_dependency 'cancan', "~> 1.4.0"
15
15
  gem.add_dependency 'require_all', "~> 1.2.0"
16
- gem.add_dependency 'sugar-high', "~> 0.2.10"
16
+ gem.add_dependency 'sugar-high', "~> 0.3.0"
17
17
  end
18
18
  Jeweler::GemcutterTasks.new
19
19
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.7
@@ -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.5"
8
+ s.version = "0.2.7"
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-17}
12
+ s.date = %q{2010-10-23}
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 = [
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  ".document",
21
21
  ".gitignore",
22
22
  ".rspec",
23
+ "Changelog.txt",
23
24
  "LICENSE",
24
25
  "README.markdown",
25
26
  "Rakefile",
@@ -27,10 +28,11 @@ Gem::Specification.new do |s|
27
28
  "cancan-permits.gemspec",
28
29
  "development.sqlite3",
29
30
  "lib/cancan-permits.rb",
31
+ "lib/cancan-permits/license/base_license.rb",
30
32
  "lib/cancan-permits/main.rb",
31
33
  "lib/cancan-permits/namespaces.rb",
32
- "lib/cancan-permits/permit/base_license.rb",
33
34
  "lib/cancan-permits/permit/base_permit.rb",
35
+ "lib/cancan-permits/permit/util.rb",
34
36
  "lib/cancan-permits/permits/ability.rb",
35
37
  "lib/cancan-permits/permits/configuration.rb",
36
38
  "lib/cancan-permits/permits/roles.rb",
@@ -56,9 +58,18 @@ Gem::Specification.new do |s|
56
58
  "spec/data_mapper/permits_spec.rb",
57
59
  "spec/data_mapper/spec_helper.rb",
58
60
  "spec/fixtures/permits/admin_permit.rb",
61
+ "spec/fixtures/permits/any_permit.rb",
59
62
  "spec/fixtures/permits/editor_permit.rb",
60
63
  "spec/fixtures/permits/guest_permit.rb",
64
+ "spec/fixtures/permits/super_admin_permit.rb",
65
+ "spec/fixtures/permits/system_permit.rb",
61
66
  "spec/generators/permit_generator_spec.rb",
67
+ "spec/generic/api/basic/config.rb",
68
+ "spec/generic/api/basic/xgroup.rb",
69
+ "spec/generic/api/basic/xgroup_orm.rb",
70
+ "spec/generic/api/owner/config.rb",
71
+ "spec/generic/api/owner/xgroup.rb",
72
+ "spec/generic/api/owner/xgroup_orm.rb",
62
73
  "spec/generic/models/all_models.rb",
63
74
  "spec/generic/owner_permits_spec.rb",
64
75
  "spec/generic/permits_spec.rb",
@@ -71,6 +82,11 @@ Gem::Specification.new do |s|
71
82
  "spec/mongoid/owner_permits_spec.rb",
72
83
  "spec/mongoid/permits_spec.rb",
73
84
  "spec/mongoid/spec_helper.rb",
85
+ "spec/simply_stored/CouchDB.txt",
86
+ "spec/simply_stored/models/all_models.rb",
87
+ "spec/simply_stored/owner_permits_spec.rb",
88
+ "spec/simply_stored/permits_spec.rb",
89
+ "spec/simply_stored/spec_helper.rb",
74
90
  "spec/spec_helper.rb"
75
91
  ]
76
92
  s.homepage = %q{http://github.com/kristianmandrup/cancan-permits}
@@ -92,9 +108,18 @@ Gem::Specification.new do |s|
92
108
  "spec/data_mapper/permits_spec.rb",
93
109
  "spec/data_mapper/spec_helper.rb",
94
110
  "spec/fixtures/permits/admin_permit.rb",
111
+ "spec/fixtures/permits/any_permit.rb",
95
112
  "spec/fixtures/permits/editor_permit.rb",
96
113
  "spec/fixtures/permits/guest_permit.rb",
114
+ "spec/fixtures/permits/super_admin_permit.rb",
115
+ "spec/fixtures/permits/system_permit.rb",
97
116
  "spec/generators/permit_generator_spec.rb",
117
+ "spec/generic/api/basic/config.rb",
118
+ "spec/generic/api/basic/xgroup.rb",
119
+ "spec/generic/api/basic/xgroup_orm.rb",
120
+ "spec/generic/api/owner/config.rb",
121
+ "spec/generic/api/owner/xgroup.rb",
122
+ "spec/generic/api/owner/xgroup_orm.rb",
98
123
  "spec/generic/models/all_models.rb",
99
124
  "spec/generic/owner_permits_spec.rb",
100
125
  "spec/generic/permits_spec.rb",
@@ -107,6 +132,10 @@ Gem::Specification.new do |s|
107
132
  "spec/mongoid/owner_permits_spec.rb",
108
133
  "spec/mongoid/permits_spec.rb",
109
134
  "spec/mongoid/spec_helper.rb",
135
+ "spec/simply_stored/models/all_models.rb",
136
+ "spec/simply_stored/owner_permits_spec.rb",
137
+ "spec/simply_stored/permits_spec.rb",
138
+ "spec/simply_stored/spec_helper.rb",
110
139
  "spec/spec_helper.rb"
111
140
  ]
112
141
 
@@ -117,25 +146,25 @@ Gem::Specification.new do |s|
117
146
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
118
147
  s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
119
148
  s.add_development_dependency(%q<code-spec>, ["~> 0.2.5"])
120
- s.add_development_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
149
+ s.add_development_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
121
150
  s.add_runtime_dependency(%q<cancan>, ["~> 1.4.0"])
122
151
  s.add_runtime_dependency(%q<require_all>, ["~> 1.2.0"])
123
- s.add_runtime_dependency(%q<sugar-high>, ["~> 0.2.10"])
152
+ s.add_runtime_dependency(%q<sugar-high>, ["~> 0.3.0"])
124
153
  else
125
154
  s.add_dependency(%q<rspec>, ["~> 2.0.0"])
126
155
  s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
127
- s.add_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
156
+ s.add_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
128
157
  s.add_dependency(%q<cancan>, ["~> 1.4.0"])
129
158
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
130
- s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
159
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
131
160
  end
132
161
  else
133
162
  s.add_dependency(%q<rspec>, ["~> 2.0.0"])
134
163
  s.add_dependency(%q<code-spec>, ["~> 0.2.5"])
135
- s.add_dependency(%q<rails-app-spec>, ["~> 0.2.14"])
164
+ s.add_dependency(%q<rails-app-spec>, ["~> 0.3.0"])
136
165
  s.add_dependency(%q<cancan>, ["~> 1.4.0"])
137
166
  s.add_dependency(%q<require_all>, ["~> 1.2.0"])
138
- s.add_dependency(%q<sugar-high>, ["~> 0.2.10"])
167
+ s.add_dependency(%q<sugar-high>, ["~> 0.3.0"])
139
168
  end
140
169
  end
141
170
 
@@ -1,3 +1,4 @@
1
1
  require 'cancan-permits/namespaces'
2
2
  require_all File.dirname(__FILE__) + '/permit'
3
- require_all File.dirname(__FILE__) + '/permits'
3
+ require_all File.dirname(__FILE__) + '/permits'
4
+ require_all File.dirname(__FILE__) + '/license'
@@ -1,3 +1,5 @@
1
+ require 'cancan-permits/permit/util'
2
+
1
3
  module Permit
2
4
  class Base
3
5
  attr_reader :ability
@@ -55,14 +57,16 @@ module Permit
55
57
  end
56
58
  end
57
59
 
58
- protected
60
+ protected
61
+
62
+ include Permit::Util
59
63
 
60
64
  def localhost_manager?
61
65
  Permits::Configuration.localhost_manager
62
66
  end
63
67
 
64
68
  def role_match? user
65
- user.has_role? self.class.last_name.gsub(/Permit$/, '').downcase.to_sym
69
+ user.has_role? permit_name(self.class)
66
70
  end
67
71
 
68
72
  def can_definitions
@@ -0,0 +1,7 @@
1
+ module Permit
2
+ module Util
3
+ def permit_name clazz
4
+ clazz.demodulize.gsub(/Permit$/, '').underscore.to_sym
5
+ end
6
+ end
7
+ end
@@ -1,3 +1,5 @@
1
+ require 'cancan-permits/permit/util'
2
+
1
3
  module Permits
2
4
  class Ability
3
5
  include CanCan::Ability
@@ -30,11 +32,11 @@ module Permits
30
32
 
31
33
  def initialize user, options = {}
32
34
  # put ability logic here!
33
- user ||= Guest.new
35
+ user ||= Guest.create
34
36
  all_permits = Permits::Ability.permits(self, options)
35
37
  all_permits.each do |permit|
36
38
  # get role name of permit
37
- permit_role = permit.class.demodulize.gsub(/Permit$/, '').underscore.to_sym
39
+ permit_role = permit_name(permit.class)
38
40
  if permit_role == :system
39
41
  # always execute system permit
40
42
  result = permit.permit?(user, options)
@@ -49,6 +51,8 @@ module Permits
49
51
  end
50
52
 
51
53
  protected
54
+
55
+ include Permit::Util
52
56
 
53
57
  def self.make_permit role, ability, options = {}
54
58
  begin
@@ -1,75 +1,3 @@
1
1
  require 'active_record/spec_helper'
2
+ require 'generic/api/owner/xgroup_orm'
2
3
 
3
- Permits::Ability.orm = :active_record
4
-
5
- describe Permits::Ability do
6
- context "Editor user" do
7
- context "using default :user_id relation - foreign key to User.id" do
8
- before :each do
9
- @editor = User.create(:name => "Kristian", :role => "editor")
10
- @other_guy = User.create(:name => "Random dude", :role => "admin")
11
-
12
- @ability = Permits::Ability.new(@editor)
13
-
14
- @own_comment = Comment.create(:user_id => @editor.id)
15
- @other_comment = Comment.create(:user_id => @other_guy.id)
16
- # @post = Post.create(:writer => @editor.id)
17
- # @article = Article.create(:author => @editor.id)
18
- end
19
-
20
- it "should be able to :read Comment he owns" do
21
- @ability.should be_able_to(:read, Comment)
22
- @ability.should be_able_to(:read, @own_comment)
23
- end
24
-
25
- it "should be able to :update Comment he owns" do
26
- @ability.should be_able_to(:update, @own_comment)
27
- end
28
-
29
- it "should NOT be able to :update Comment he does NOT own" do
30
- @ability.should_not be_able_to(:update, @other_comment)
31
- end
32
-
33
- it "should be able to :delete Comment he owns" do
34
- @ability.should be_able_to(:delete, @own_comment)
35
- end
36
-
37
- it "should NOT be able to :update Comment he does NOT own" do
38
- @ability.should_not be_able_to(:delete, @other_comment)
39
- end
40
- end
41
-
42
- context "using custom :writer relation - foreign key to User.id" do
43
- before :each do
44
- @editor = User.create(:name => "Kristian", :role => "editor")
45
- @other_guy = User.create(:name => "Random dude", :role => "admin")
46
-
47
- @ability = Permits::Ability.new(@editor)
48
-
49
- @own_post = Post.create(:writer => @editor.id)
50
- @other_post = Post.create(:writer => @other_guy.id)
51
- end
52
-
53
- it "should be able to :read Post he owns" do
54
- @ability.should be_able_to(:read, Post)
55
- @ability.should be_able_to(:read, @own_post)
56
- end
57
-
58
- it "should be able to :update Post he owns" do
59
- @ability.should be_able_to(:update, @own_post)
60
- end
61
-
62
- it "should NOT be able to :update Post he does NOT own" do
63
- @ability.should_not be_able_to(:update, @other_post)
64
- end
65
-
66
- it "should be able to :delete Post he owns" do
67
- @ability.should be_able_to(:delete, @own_post)
68
- end
69
-
70
- it "should NOT be able to :update Post he does NOT own" do
71
- @ability.should_not be_able_to(:delete, @other_post)
72
- end
73
- end
74
- end
75
- end
@@ -1,38 +1,2 @@
1
1
  require 'active_record/spec_helper'
2
-
3
- Permits::Ability.orm = :active_record
4
-
5
- describe Permits::Ability do
6
- context "Guest user" do
7
- before :each do
8
- @guest = User.create(:name => "Kristian", :role => "guest")
9
-
10
- @ability = Permits::Ability.new(@guest)
11
-
12
- @comment = Comment.create(:user_id => @guest.id)
13
-
14
- @post = Post.create(:writer => @guest.id)
15
-
16
- @article = Article.create(:author => @guest.id)
17
- end
18
-
19
- it "should be able to :read Comment and Post but NOT Article" do
20
- @ability.can?(:read, Comment).should be_true
21
- @ability.can?(:read, @comment).should be_true
22
-
23
- @ability.can?(:read, Post).should be_true
24
- @ability.can?(:read, @post).should be_true
25
-
26
- @ability.can?(:read, Article).should be_false
27
- @ability.can?(:read, @article).should be_false
28
- end
29
-
30
- it "should be not able to :update only Comment" do
31
- @ability.can?(:update, Comment).should be_true
32
- @ability.can?(:update, @comment).should be_true
33
-
34
- @ability.can?(:update, Post).should be_false
35
- @ability.can?(:update, @post).should be_false
36
- end
37
- end
38
- end
2
+ require 'generic/api/basic/xgroup_orm'
@@ -14,6 +14,8 @@ module Rails
14
14
  end
15
15
  end
16
16
 
17
+ Permits::Ability.orm = :active_record
18
+
17
19
  path = File.dirname(__FILE__) + '/db/database.yml'
18
20
  dbfile = File.open(path)
19
21
  dbconfig = YAML::load(dbfile)
@@ -1,75 +1,2 @@
1
1
  require 'data_mapper/spec_helper'
2
-
3
- Permits::Ability.orm = :data_mapper
4
-
5
- describe Permits::Ability do
6
- context "Editor user" do
7
- context "using default :user_id relation - foreign key to User.id" do
8
- before :each do
9
- @editor = User.create(:name => "Kristian", :role => "editor")
10
- @other_guy = User.create(:name => "Random dude", :role => "admin")
11
-
12
- @ability = Permits::Ability.new(@editor)
13
-
14
- @own_comment = Comment.create(:user_id => @editor.id)
15
- @other_comment = Comment.create(:user_id => @other_guy.id)
16
- # @post = Post.create(:writer => @editor.id)
17
- # @article = Article.create(:author => @editor.id)
18
- end
19
-
20
- it "should be able to :read Comment he owns" do
21
- @ability.should be_able_to(:read, Comment)
22
- @ability.should be_able_to(:read, @own_comment)
23
- end
24
-
25
- it "should be able to :update Comment he owns" do
26
- @ability.should be_able_to(:update, @own_comment)
27
- end
28
-
29
- it "should NOT be able to :update Comment he does NOT own" do
30
- @ability.should_not be_able_to(:update, @other_comment)
31
- end
32
-
33
- it "should be able to :delete Comment he owns" do
34
- @ability.should be_able_to(:delete, @own_comment)
35
- end
36
-
37
- it "should NOT be able to :update Comment he does NOT own" do
38
- @ability.should_not be_able_to(:delete, @other_comment)
39
- end
40
- end
41
-
42
- context "using custom :writer relation - foreign key to User.id" do
43
- before :each do
44
- @editor = User.create(:name => "Kristian", :role => "editor")
45
- @other_guy = User.create(:name => "Random dude", :role => "admin")
46
-
47
- @ability = Permits::Ability.new(@editor, :strategy => :string)
48
-
49
- @own_post = Post.create(:writer => @editor.id)
50
- @other_post = Post.create(:writer => @other_guy.id)
51
- end
52
-
53
- it "should be able to :read Post he owns" do
54
- @ability.should be_able_to(:read, Post)
55
- @ability.should be_able_to(:read, @own_post)
56
- end
57
-
58
- it "should be able to :update Post he owns" do
59
- @ability.should be_able_to(:update, @own_post)
60
- end
61
-
62
- it "should NOT be able to :update Post he does NOT own" do
63
- @ability.should_not be_able_to(:update, @other_post)
64
- end
65
-
66
- it "should be able to :delete Post he owns" do
67
- @ability.should be_able_to(:delete, @own_post)
68
- end
69
-
70
- it "should NOT be able to :update Post he does NOT own" do
71
- @ability.should_not be_able_to(:delete, @other_post)
72
- end
73
- end
74
- end
75
- end
2
+ require 'generic/api/owner/xgroup_orm'