cancan-permits 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -96,6 +96,46 @@ class AdminPermit < Permit::Base
96
96
  end
97
97
  </code>
98
98
 
99
+ The call to #load_rules will call both #load_user_roles and #load_role_rules.
100
+ If you want you can call these methods individually, fx if you only want to apply one set of rules.
101
+
102
+ *Permit rules:*
103
+ - config/permits.yml
104
+
105
+ Each key at the top level is expected to match a permit/role name.
106
+
107
+ Example yml config file:
108
+ <code>
109
+ admin:
110
+ can:
111
+ manage:
112
+ - Article
113
+ - Post
114
+ guest:
115
+ can:
116
+ manage:
117
+ - all
118
+ cannot:
119
+ manage:
120
+ - User
121
+ </code>
122
+
123
+ Usage in a license
124
+
125
+ <code>
126
+ class GuestPermit < Permit::Base
127
+ def initialize(ability, options = {})
128
+ super
129
+ end
130
+
131
+ def permit?(user, options = {})
132
+ super
133
+ return if !role_match? user
134
+ can :manage, :all
135
+ load_rules user
136
+ end
137
+ end
138
+ </code>
99
139
 
100
140
  *License permissions:*
101
141
  - config/licenses.yml
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.5
1
+ 0.3.6
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cancan-permits}
8
- s.version = "0.3.5"
8
+ s.version = "0.3.6"
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-11-29}
12
+ s.date = %q{2010-11-30}
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 = [
@@ -62,10 +62,11 @@ Gem::Specification.new do |s|
62
62
  "spec/active_record/owner_permits_spec.rb",
63
63
  "spec/active_record/permits_spec.rb",
64
64
  "spec/active_record/spec_helper.rb",
65
- "spec/cancan-permits/license_loader/license_loader_spec.rb",
66
- "spec/cancan-permits/license_loader/licenses.yml",
67
- "spec/cancan-permits/user_permissions_loader/user_permissions.yml",
68
- "spec/cancan-permits/user_permissions_loader/user_permissions_loader.rb",
65
+ "spec/cancan-permits/loader/config/licenses.yml",
66
+ "spec/cancan-permits/loader/config/user_permissions.yml",
67
+ "spec/cancan-permits/loader/license_loader_spec.rb",
68
+ "spec/cancan-permits/loader/permits_loader_spec.rb",
69
+ "spec/cancan-permits/loader/user_permissions_loader.rb",
69
70
  "spec/data_mapper/models/all_models.rb",
70
71
  "spec/data_mapper/owner_permits_spec.rb",
71
72
  "spec/data_mapper/permits_spec.rb",
@@ -118,8 +119,9 @@ Gem::Specification.new do |s|
118
119
  "spec/active_record/owner_permits_spec.rb",
119
120
  "spec/active_record/permits_spec.rb",
120
121
  "spec/active_record/spec_helper.rb",
121
- "spec/cancan-permits/license_loader/license_loader_spec.rb",
122
- "spec/cancan-permits/user_permissions_loader/user_permissions_loader.rb",
122
+ "spec/cancan-permits/loader/license_loader_spec.rb",
123
+ "spec/cancan-permits/loader/permits_loader_spec.rb",
124
+ "spec/cancan-permits/loader/user_permissions_loader.rb",
123
125
  "spec/data_mapper/models/all_models.rb",
124
126
  "spec/data_mapper/owner_permits_spec.rb",
125
127
  "spec/data_mapper/permits_spec.rb",
@@ -16,7 +16,7 @@ module License
16
16
  def load_rules name = nil
17
17
  return if !licenses || licenses.empty?
18
18
 
19
- name ||= self.class.to_s..gsub(/License$/, "").underscore.to_sym
19
+ name ||= self.class.to_s.gsub(/License$/, "").underscore.to_sym
20
20
 
21
21
  licenses[name].can_statement do |permission_statement|
22
22
  instance_eval permission_statement
@@ -6,7 +6,7 @@ class PermissionsLoader
6
6
  begin
7
7
  if file_name.nil? || !File.file?(file_name)
8
8
  # raise ArgumentError, "PermissionsLoader Error: The permissions file #{file_name} could not be found"
9
- puts "PermissionsLoader Error: The permissions file #{file_name} could not be found"
9
+ # puts "PermissionsLoader Error: The permissions file #{file_name} could not be found"
10
10
  return nil
11
11
  end
12
12
 
@@ -33,14 +33,34 @@ class PermissionsLoader
33
33
  name ||= licenses_config_file
34
34
  PermissionsLoader.new name
35
35
  end
36
+
37
+ def self.load_permits name = nil
38
+ name ||= permits_config_file
39
+ PermissionsLoader.new name
40
+ end
41
+
42
+ def self.permits_config_file
43
+ # raise '#user_permissions_config_file only works in a Rails app enviroment' if !defined? Rails
44
+ get_config_file 'permits'
45
+ end
36
46
 
37
47
  def self.user_permissions_config_file
38
48
  # raise '#user_permissions_config_file only works in a Rails app enviroment' if !defined? Rails
39
- File.join(::Rails.root, 'config', 'user_permissions.yml') if defined? Rails
49
+ get_config_file 'user_permissions'
40
50
  end
41
51
 
42
52
  def self.licenses_config_file
43
53
  # raise '#licenses_config_file only works in a Rails app enviroment' if !defined? Rails
44
- File.join(::Rails.root, 'config', 'licenses.yml') if defined? Rails
54
+ get_config_file 'licenses'
55
+ end
56
+
57
+ protected
58
+
59
+ def self.get_config_file name
60
+ File.join(::Rails.root, 'config', "#{name}.yml") if rails?
61
+ end
62
+
63
+ def self.rails?
64
+ defined?(Rails) && Rails.respond_to?(:root)
45
65
  end
46
66
  end
@@ -25,9 +25,27 @@ module Permit
25
25
  end
26
26
  end
27
27
 
28
- def load_rules user
28
+ def load_rules user
29
+ load_role_rules
30
+ load_user_rules user
31
+ end
32
+
33
+ def load_role_rules
34
+ return if !role_permissions || role_permissions.empty?
35
+ name ||= self.class.to_s.gsub(/Permit$/, "").underscore.to_sym
36
+
37
+ role_permissions[name].can_statement do |permission_statement|
38
+ instance_eval permission_statement
39
+ end
40
+
41
+ role_permissions[name].cannot_statement do |permission_statement|
42
+ instance_eval permission_statement
43
+ end
44
+ end
45
+
46
+ def load_user_rules user
29
47
  return if !user_permissions || user_permissions.empty?
30
- raise "#load_enforcements expects the user to have an email property: #{user.inspect}" if !user || !user.respond_to?(:email)
48
+ raise "#load_user_rules expects the user to have an email property: #{user.inspect}" if !user || !user.respond_to?(:email)
31
49
 
32
50
  id = user.email
33
51
  return nil if id.strip.empty?
@@ -44,7 +62,8 @@ module Permit
44
62
  def initialize ability, options = {}
45
63
  @ability = ability
46
64
  @strategy = options[:strategy] || Permits::Ability.strategy || :default
47
- @user_permissions = ::PermissionsLoader.load_user_permissions options[:permissions_file]
65
+ @user_permissions = ::PermissionsLoader.load_user_permissions options[:user_permissions_file]
66
+ @role_permissions = ::PermissionsLoader.load_permits options[:permits_file]
48
67
  end
49
68
 
50
69
  def permit?(user, options = {})
@@ -59,14 +59,22 @@ module Permits
59
59
  protected
60
60
 
61
61
  include Permit::Util
62
+
63
+ def self.get_permit role
64
+ begin
65
+ clazz_name = "#{role.to_s.camelize}Permit"
66
+ clazz_name.constantize
67
+ rescue
68
+ raise "Permit #{clazz_name} not loaded and thus not defined"
69
+ end
70
+ end
62
71
 
63
72
  def self.make_permit role, ability, options = {}
64
73
  begin
65
- clazz_name = "#{role.to_s.camelize}Permit"
66
- permit_clazz = clazz_name.constantize
74
+ permit_clazz = get_permit role
67
75
  permit_clazz.new(ability, options) if permit_clazz && permit_clazz.kind_of?(Class)
68
- rescue
69
- raise "Permit #{clazz_name} not found"
76
+ rescue RuntimeError => e
77
+ raise "Error instantiating Permit instance for #{permit_clazz}, cause #{e}"
70
78
  end
71
79
  end
72
80
  end
@@ -3,9 +3,9 @@ require 'cancan-permits'
3
3
 
4
4
  DIR = File.dirname(__FILE__)
5
5
 
6
- describe 'Load License permissions' do
6
+ describe 'Load License rules' do
7
7
  before :each do
8
- @permissions_file = File.join(DIR, 'licenses.yml')
8
+ @permissions_file = File.join(DIR, 'config', 'licenses.yml')
9
9
  end
10
10
 
11
11
  it "should load a licenses permission file" do
@@ -19,7 +19,7 @@ describe 'Load License permissions' do
19
19
  end
20
20
 
21
21
  it "should be able to instantiate a base permit without permission file" do
22
- Permit::Base.new 'x'
22
+ License::Base.new 'x'
23
23
  end
24
24
  end
25
25
 
@@ -0,0 +1,28 @@
1
+ require 'rspec/core'
2
+ require 'cancan-permits'
3
+
4
+ DIR = File.dirname(__FILE__)
5
+
6
+ describe 'Load Permits rules' do
7
+ before :each do
8
+ @permission_file = File.join(DIR, 'config', 'permits.yml')
9
+ end
10
+
11
+ it "should load a licenses permission file" do
12
+ loader = PermissionsLoader.new @permissions_file
13
+ # puts "loaded permissions #{loader.permissions}"
14
+ loader.permissions.should_not be_empty
15
+ end
16
+
17
+ it "should be able to instantiate a base license with permission file" do
18
+ Permit::Base.new 'x', @permissions_file
19
+ end
20
+
21
+ it "should be able to instantiate a base permit without permission file" do
22
+ Permit::Base.new 'x'
23
+ end
24
+ end
25
+
26
+
27
+
28
+
@@ -5,7 +5,7 @@ DIR = File.dirname(__FILE__)
5
5
 
6
6
  describe 'User Permissions Loader' do
7
7
  before :each do
8
- @permissions_file = File.join(DIR, 'user_permissions.yml')
8
+ @permissions_file = File.join(DIR, 'config', 'user_permissions.yml')
9
9
  end
10
10
 
11
11
  it "should load a user permissions file" do
@@ -15,7 +15,7 @@ describe 'User Permissions Loader' do
15
15
  end
16
16
 
17
17
  it "should be able to instantiate a base permit with permission file" do
18
- Permit::Base.new 'x', :permissions_file => @permissions_file
18
+ Permit::Base.new 'x', :user_permissions_file => @permissions_file
19
19
  end
20
20
 
21
21
  it "should be able to instantiate a base permit without permission file" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 5
9
- version: 0.3.5
8
+ - 6
9
+ version: 0.3.6
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-11-29 00:00:00 +01:00
17
+ date: 2010-11-30 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -207,10 +207,11 @@ files:
207
207
  - spec/active_record/owner_permits_spec.rb
208
208
  - spec/active_record/permits_spec.rb
209
209
  - spec/active_record/spec_helper.rb
210
- - spec/cancan-permits/license_loader/license_loader_spec.rb
211
- - spec/cancan-permits/license_loader/licenses.yml
212
- - spec/cancan-permits/user_permissions_loader/user_permissions.yml
213
- - spec/cancan-permits/user_permissions_loader/user_permissions_loader.rb
210
+ - spec/cancan-permits/loader/config/licenses.yml
211
+ - spec/cancan-permits/loader/config/user_permissions.yml
212
+ - spec/cancan-permits/loader/license_loader_spec.rb
213
+ - spec/cancan-permits/loader/permits_loader_spec.rb
214
+ - spec/cancan-permits/loader/user_permissions_loader.rb
214
215
  - spec/data_mapper/models/all_models.rb
215
216
  - spec/data_mapper/owner_permits_spec.rb
216
217
  - spec/data_mapper/permits_spec.rb
@@ -290,8 +291,9 @@ test_files:
290
291
  - spec/active_record/owner_permits_spec.rb
291
292
  - spec/active_record/permits_spec.rb
292
293
  - spec/active_record/spec_helper.rb
293
- - spec/cancan-permits/license_loader/license_loader_spec.rb
294
- - spec/cancan-permits/user_permissions_loader/user_permissions_loader.rb
294
+ - spec/cancan-permits/loader/license_loader_spec.rb
295
+ - spec/cancan-permits/loader/permits_loader_spec.rb
296
+ - spec/cancan-permits/loader/user_permissions_loader.rb
295
297
  - spec/data_mapper/models/all_models.rb
296
298
  - spec/data_mapper/owner_permits_spec.rb
297
299
  - spec/data_mapper/permits_spec.rb