indulgence 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -24,7 +24,21 @@ Indulgence can be added to a class via acts_as_indulgent:
24
24
  end
25
25
 
26
26
  Used in this way, permissions need to be defined in an Indulgence::Permission
27
- object called ThingPermission, with an instance method :default
27
+ object called ThingPermission.
28
+
29
+ class ThingPermission < Indulgence::Permission
30
+
31
+ end
32
+
33
+ This needs to be available to the Thing class. For example, in a rails app, by
34
+ placing it in app/permissions/thing_permission.rb
35
+
36
+ === Default permissions
37
+
38
+ The Permission class has a default method, that matches all the CRUD actions to
39
+ the ability *none*.
40
+
41
+ This behaviour can be overridden by explicitly defining the default method.
28
42
 
29
43
  class ThingPermission < Indulgence::Permission
30
44
 
@@ -39,13 +53,10 @@ object called ThingPermission, with an instance method :default
39
53
 
40
54
  end
41
55
 
42
- This needs to be available to the Thing class. For example, in a rails app, by
43
- placing it in app/permissions/thing_permission.rb
44
-
45
56
  == Users and Roles
46
57
 
47
58
  Indulgence assumes that permissions will be tested against an entity object
48
- (e.g. User). The default behaviour assumes that the entity object has a :role
59
+ (e.g. User). The standard behaviour assumes that the entity object has a :role
49
60
  method that returns the role object, and that the role object has a :name method.
50
61
 
51
62
  So typically, these objects could look like this:
@@ -73,7 +84,7 @@ Simple true/false permission can be determined using the :indulge? method:
73
84
  thing = Thing.first
74
85
 
75
86
  thing.indulge?(user, :create) == false
76
- thing.indulge?(user, :read) == true
87
+ thing.indulge?(user, :read) == true # Note default has be overridden
77
88
  thing.indulge?(user, :update) == false
78
89
  thing.indulge?(user, :delete) == false
79
90
 
@@ -94,8 +105,8 @@ So to find all the blue things that the user has permission to read:
94
105
 
95
106
  === Adding other roles
96
107
 
97
- Up until now, all users get the same permissions irrespective of role. Let's
98
- give Emperors the right to see and do anything by first creating an emperor
108
+ Up until now, all users get the same permissions (default) irrespective of role.
109
+ Let's give Emperors the right to see and do anything by first creating an emperor
99
110
 
100
111
  emperor = Role.create(:name => 'emperor')
101
112
  caesar = User.create(
@@ -14,7 +14,12 @@ module Indulgence
14
14
  end
15
15
 
16
16
  def default
17
- raise 'There must always be a default'
17
+ {
18
+ create: none,
19
+ read: none,
20
+ update: none,
21
+ delete: none
22
+ }
18
23
  end
19
24
 
20
25
  def filter_many(things)
@@ -1,9 +1,12 @@
1
1
  module Indulgence
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
+ #
8
+ # 0.0.6 Specifies the default behaviour as assigning the none ability to
9
+ # all CRUD actions.
7
10
  #
8
11
  # 0.0.5 Allows simplified ability definition to be used with has_many
9
12
  #
Binary file
@@ -11,19 +11,10 @@ class ThingPermission < Indulgence::Permission
11
11
  }
12
12
  end
13
13
 
14
-
15
- def default
16
- {
17
- create: none,
18
- read: all,
19
- update: none,
20
- delete: none
21
- }
22
- end
23
-
24
14
  def god
25
15
  {
26
16
  create: all,
17
+ read: all,
27
18
  update: all,
28
19
  delete: all
29
20
  }
@@ -32,6 +23,7 @@ class ThingPermission < Indulgence::Permission
32
23
  def demigod
33
24
  {
34
25
  create: things_they_own,
26
+ read: all,
35
27
  update: things_they_own,
36
28
  delete: things_they_own
37
29
  }
@@ -39,12 +31,14 @@ class ThingPermission < Indulgence::Permission
39
31
 
40
32
  def thief
41
33
  {
34
+ read: all,
42
35
  update: things_they_stole
43
36
  }
44
37
  end
45
38
 
46
39
  def friend
47
40
  {
41
+ read: all,
48
42
  update: things_they_borrow
49
43
  }
50
44
  end
@@ -6,10 +6,9 @@ require 'ability'
6
6
  module Indulgence
7
7
  class PermissionTest < Test::Unit::TestCase
8
8
 
9
- def test_creation_fails_as_methods_undefined_in_parent_class
10
- assert_raise RuntimeError do
11
- Permission.new(User.create(:name => 'Whisp'), :read)
12
- end
9
+ def test_creation
10
+ permission = Permission.new(User.create(:name => 'Whisp'), :read)
11
+ assert_equal Permission.none, permission.default[:read]
13
12
  end
14
13
 
15
14
  def test_define_ability_uses_cache_rather_than_duplicates
@@ -25,7 +25,7 @@ class ThingPermissionTest < Test::Unit::TestCase
25
25
 
26
26
  def test_default_permissions
27
27
  assert_equal Permission.none, ThingPermission.new(@user, :create).ability
28
- assert_equal Permission.all, ThingPermission.new(@user, :read).ability
28
+ assert_equal Permission.none, ThingPermission.new(@user, :read).ability
29
29
  assert_equal Permission.none, ThingPermission.new(@user, :update).ability
30
30
  assert_equal Permission.none, ThingPermission.new(@user, :delete).ability
31
31
  end
@@ -21,7 +21,7 @@ class ThingTest < Test::Unit::TestCase
21
21
 
22
22
  def test_indulge
23
23
  make_second_thing
24
- assert_equal(true, @thing.indulge?(@owner, :read))
24
+ assert_equal(false, @thing.indulge?(@owner, :read))
25
25
  assert_equal(false, @thing.indulge?(@owner, :delete))
26
26
  assert_equal(false, @other_thing.indulge?(@owner, :delete))
27
27
  end
@@ -66,7 +66,7 @@ class ThingTest < Test::Unit::TestCase
66
66
 
67
67
  def test_indulge_other_thing
68
68
  other_thing = OtherThing.create(:name => 'Other Stuff', :owner => @owner)
69
- assert_equal(true, other_thing.indulge?(@owner, :read))
69
+ assert_equal(false, other_thing.indulge?(@owner, :read))
70
70
  assert_equal(false, other_thing.indulge?(@owner, :delete))
71
71
  end
72
72
 
@@ -74,7 +74,9 @@ class ThingTest < Test::Unit::TestCase
74
74
  make_second_thing
75
75
  @owner.update_attribute(:role, @demigod)
76
76
  assert_equal(Thing.order('id'), Thing.indulgence(@owner, :read).order('id'))
77
- assert_equal(Thing.order('id'), Thing.indulgence(@user, :read).order('id'))
77
+ assert_raise ActiveRecord::RecordNotFound do
78
+ Thing.indulgence(@user, :read).order('id')
79
+ end
78
80
  assert_equal([@thing], Thing.indulgence(@owner, :delete))
79
81
  assert_raise ActiveRecord::RecordNotFound do
80
82
  Thing.indulgence(@user, :delete)
@@ -110,7 +112,7 @@ class ThingTest < Test::Unit::TestCase
110
112
 
111
113
  def test_aliased_compare_single_method
112
114
  make_second_thing
113
- assert_equal(true, @thing.permit?(@owner, :read))
115
+ assert_equal(false, @thing.permit?(@owner, :read))
114
116
  assert_equal(false, @thing.permit?(@owner, :delete))
115
117
  assert_equal(false, @other_thing.permit?(@owner, :delete))
116
118
  end
@@ -119,7 +121,9 @@ class ThingTest < Test::Unit::TestCase
119
121
  make_second_thing
120
122
  @owner.update_attribute(:role, @demigod)
121
123
  assert_equal(Thing.order('id'), Thing.permitted(@owner, :read).order('id'))
122
- assert_equal(Thing.order('id'), Thing.permitted(@user, :read).order('id'))
124
+ assert_raise ActiveRecord::RecordNotFound do
125
+ Thing.permitted(@user, :read).order('id')
126
+ end
123
127
  assert_equal([@thing], Thing.permitted(@owner, :delete))
124
128
  assert_raise ActiveRecord::RecordNotFound do
125
129
  Thing.permitted(@user, :delete)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: indulgence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-20 00:00:00.000000000 Z
12
+ date: 2013-06-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord