guachiman 1.0.2 → 1.1.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a18c344b744428ee649a57ba3f4268d4aabc793e
4
- data.tar.gz: d5ce9e297a202f2baa78966042610216aa39a862
3
+ metadata.gz: 23e59b97a4747e67da8f84f640afd49494443bd8
4
+ data.tar.gz: 870d34b5ec00a52361a7baf3d250040cb453d91f
5
5
  SHA512:
6
- metadata.gz: fdb2fc2a9913e3bbdcadb0904004f471f2a4893f1a684b9ef1051d26a5287c4e8910bfcef6109ae772a72c83ed18b412c8be4e9a158238bb261f47e30b996214
7
- data.tar.gz: 40234abb8475dcc0d0f98e54388b3b21ce42047f4433472ef201102cd8abf76ed549e61683e3cec7de12d511fc1b7c0dff0f0e1ac41fa1da4a4dbf839266c39e
6
+ metadata.gz: 04a0fbb692957e468cb8276d29db2422cc70e5621b8b90188cd51ac5bdf6f72f37b1a6bdbeea7cb5fb7520626be54f9f0a3b389a3982d6d8ecf6fb54a54334c3
7
+ data.tar.gz: abce3073ffb2cad35f010fc6dd929223311c956204f3d9ebd118d35a6b4aa483d2a9473d70371afa5b8eb5f4fd94c775254376b1926a4eaf22f027b7ff865536
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- Guachiman
1
+ guachiman
2
2
  =========
3
3
 
4
4
  Minimal authorization library inspired by [RailsCast #385 Authorization from Scratch][1] by Ryan Bates.
@@ -55,24 +55,16 @@ class Authorization
55
55
  include Guachiman
56
56
 
57
57
  def initialize(user = nil)
58
- if @current_user = user
59
- user_authorization
60
- else
61
- guest_authorization
62
- end
63
- end
64
-
65
- private
66
-
67
- def guest_authorization
68
- allow :sessions, [:new]
69
- end
70
-
71
- def user_authorization
72
- guest_authorization
73
-
74
- allow :users, [:show, :edit, :update] do |user_id|
75
- @current_user.id == user_id
58
+ allow :sessions, [:new, :create]
59
+
60
+ if user
61
+ if user.admin?
62
+ @allow_all = true
63
+ else
64
+ allow :users, [:show, :edit, :update] do |user_id|
65
+ user.id == user_id
66
+ end
67
+ end
76
68
  end
77
69
  end
78
70
  end
@@ -82,9 +74,11 @@ So that you can use them like this:
82
74
 
83
75
  ```ruby
84
76
  user = User.find(user_id)
77
+ admin = User.find(admin_id)
85
78
 
86
- guest_authorization = Authorization.new
87
- user_authorization = Authorization.new(user)
79
+ guest_authorization = Authorization.new
80
+ user_authorization = Authorization.new(user)
81
+ admin_authorization = Authorization.new(admin)
88
82
 
89
83
  guest_authorization.allow?(:sessions, :new)
90
84
  # => true
@@ -92,6 +86,9 @@ guest_authorization.allow?(:sessions, :new)
92
86
  user_authorization.allow?(:users, :show)
93
87
  # => false
94
88
 
89
+ admin_authorization.allow?(:users, :show)
90
+ # => true
91
+
95
92
  user_authorization.allow?(:users, :show, user.id)
96
93
  # => true
97
94
  ```
@@ -103,7 +100,7 @@ This is what you use to set permissions. It takes two parameters, `group` and `p
103
100
  ### `#allow?`
104
101
 
105
102
  This is what you use to check permissions. It takes a `group` param, a `permission` param, and an optional `object`
106
- param to evaluate in the block.
103
+ param to evaluate in the block. **If the instance variable `@allow_all` is set to `true` it will always return `true`.**
107
104
 
108
105
 
109
106
  License
@@ -18,6 +18,6 @@ Gem::Specification.new do |spec|
18
18
  spec.require_paths = ['lib']
19
19
 
20
20
  spec.add_development_dependency 'rake', '~> 10.3', '>= 10.3.0'
21
- spec.add_development_dependency 'minitest', '~> 5.3', '>= 5.3.3'
21
+ spec.add_development_dependency 'minitest', '~> 5.4', '>= 5.4.0'
22
22
  spec.add_development_dependency 'bundler', '~> 1.6', '>= 1.6.0'
23
23
  end
@@ -13,6 +13,8 @@ module Guachiman
13
13
  end
14
14
 
15
15
  def allow?(group, permission, object = nil)
16
+ return true if instance_variable_defined?(:@allow_all) && @allow_all
17
+
16
18
  if rule = rules[group] && rules[group][permission]
17
19
  rule == true || object && rule.call(object)
18
20
  else
@@ -1,3 +1,3 @@
1
1
  module Guachiman
2
- VERSION = "1.0.2"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -7,14 +7,16 @@ class GuachimanTest < MiniTest::Test
7
7
  @authorization = Class.new do
8
8
  include Guachiman
9
9
 
10
- def initialize
10
+ def initialize(user = 1)
11
11
  allow :group, [:permission1, :permission2]
12
12
 
13
13
  allow :group, [:permission3, :permission4] do |object|
14
- object == 1
14
+ object == user
15
15
  end
16
16
  end
17
17
  end.new
18
+
19
+ p @authorization
18
20
  end
19
21
 
20
22
  def test_basic_rules
@@ -37,4 +39,12 @@ class GuachimanTest < MiniTest::Test
37
39
  assert @authorization.allow?(:group, :permission3, 1)
38
40
  assert @authorization.allow?(:group, :permission4, 1)
39
41
  end
42
+
43
+ def test_allow_all
44
+ refute @authorization.allow?(:group, :permission0)
45
+
46
+ @authorization.instance_variable_set(:@allow_all, true)
47
+
48
+ assert @authorization.allow?(:group, :permission0)
49
+ end
40
50
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guachiman
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francesco Rodriguez
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-23 00:00:00.000000000 Z
12
+ date: 2014-10-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -37,20 +37,20 @@ dependencies:
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.3'
40
+ version: '5.4'
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: 5.3.3
43
+ version: 5.4.0
44
44
  type: :development
45
45
  prerelease: false
46
46
  version_requirements: !ruby/object:Gem::Requirement
47
47
  requirements:
48
48
  - - "~>"
49
49
  - !ruby/object:Gem::Version
50
- version: '5.3'
50
+ version: '5.4'
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 5.3.3
53
+ version: 5.4.0
54
54
  - !ruby/object:Gem::Dependency
55
55
  name: bundler
56
56
  requirement: !ruby/object:Gem::Requirement