u-authorization 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/authorization.rb +31 -12
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06c108c527980c647df86862a7f4f0499d536cbb2c44c36cf440d835871ee0de
4
- data.tar.gz: dcf79b3d63c780012dd6dbb8caf22bb56499ad3244823135fac23cfadbb06034
3
+ metadata.gz: f562f7c1da45026cc68c44e36fc1e60b029d4b16bc4e433c33e772653379c47b
4
+ data.tar.gz: 16bc62864439ae91aa54adc79fcdb898b29bd79aeffef171a002aaca3f95a85c
5
5
  SHA512:
6
- metadata.gz: 327d5890be7a1b2efbc51d22ef14f106510989096eb6d7d46f9c17eadc51ba7fd9dac5671b0cd90ebee162009899c988e47cc304bac5e8f905eace61cfef1e51
7
- data.tar.gz: 2bd82ecd1054b103b1b0d544941b97c40e21799d90dcb09849f00d0a3f72b5f9cea131b929ba1f8c34b8eb1c6644b59ad3bf677a96a9c0f0049799358af7ef23
6
+ metadata.gz: 9115178e4d98dd710bee877720c711533c7aa046b6b70cd96b4463809d703f9dc28f55456e48321130438edde094b9d10245a0c46fa43890e2af879cd8c096a0
7
+ data.tar.gz: ef15b3487d9a8cf7ff3108a7078246d27406bfc82eb8d487779e6d1432146da066a54cb39c991559954d423a6494303b2becdee3494ade7a031f58b409395dea
data/authorization.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Authorization
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
 
6
6
  MapValuesAsDowncasedStrings = -> (values) do
7
7
  Array(values).map { |value| String(value).downcase }
@@ -38,6 +38,19 @@ module Authorization
38
38
  end
39
39
  end
40
40
 
41
+ class FeaturesPermissionChecker
42
+ attr_reader :required_features
43
+
44
+ def initialize(role, features)
45
+ @role = role
46
+ @required_features = MapValuesAsDowncasedStrings.(features)
47
+ end
48
+
49
+ def context?(context)
50
+ CheckRolePermission.call(context, @role, @required_features)
51
+ end
52
+ end
53
+
41
54
  class Permissions
42
55
  attr_reader :role, :context
43
56
 
@@ -49,21 +62,22 @@ module Authorization
49
62
 
50
63
  def initialize(role_permissions, context: [])
51
64
  @role = role_permissions.dup.freeze
65
+ @cache = {}
52
66
  @context = MapValuesAsDowncasedStrings.(context).freeze
67
+ end
53
68
 
54
- @cache = {}
69
+ def to(features)
70
+ FeaturesPermissionChecker.new(@role, features)
55
71
  end
56
72
 
57
73
  def to?(features = nil)
58
- required_features = MapValuesAsDowncasedStrings.(features)
74
+ has_permission_to = to(features)
59
75
 
60
- cache_key = required_features.inspect
76
+ cache_key = has_permission_to.required_features.inspect
61
77
 
62
78
  return @cache[cache_key] unless @cache[cache_key].nil?
63
79
 
64
- @cache[cache_key] = CheckRolePermission.call(
65
- @context, @role, required_features
66
- )
80
+ @cache[cache_key] = has_permission_to.context?(@context)
67
81
  end
68
82
 
69
83
  def to_not?(features = nil)
@@ -78,8 +92,8 @@ module Authorization
78
92
  raise ArgumentError, "policy must be a #{self.name}"
79
93
  end
80
94
 
81
- def initialize(user, subject = nil, permissions: nil)
82
- @user = user
95
+ def initialize(context, subject = nil, permissions: nil)
96
+ @context = context
83
97
  @subject = subject
84
98
  @permissions = permissions
85
99
  end
@@ -91,9 +105,13 @@ module Authorization
91
105
 
92
106
  private
93
107
 
94
- def user; @user; end
95
- def subject; @subject; end
96
108
  def permissions; @permissions; end
109
+ def context; @context; end
110
+ def subject; @subject; end
111
+ def user
112
+ @user ||=
113
+ context.is_a?(Hash) ? context[:user] || context[:current_user] : context
114
+ end
97
115
  end
98
116
 
99
117
  class Model
@@ -166,7 +184,8 @@ module Authorization
166
184
  private
167
185
 
168
186
  def fetch_policy(policy_key)
169
- value = @policies.fetch(policy_key, Policy)
187
+ data = @policies[policy_key]
188
+ value = data || @policies.fetch(:default, Policy)
170
189
  value.is_a?(Symbol) ? fetch_policy(value) : value
171
190
  end
172
191
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: u-authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rodrigo Serradura
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-09 00:00:00.000000000 Z
11
+ date: 2018-11-10 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple authorization library and role managment for Ruby.
14
14
  email: rodrigo.serradura@gmail.com