subroutine 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/subroutine/association.rb +5 -2
- data/lib/subroutine/auth.rb +15 -2
- data/lib/subroutine/version.rb +1 -1
- data/test/subroutine/auth_test.rb +20 -0
- data/test/support/ops.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2703769996395de8478c8c16b7eb8f2826c903b3
|
4
|
+
data.tar.gz: 42bf18f9555f5977ffcb93508468d399fe62d993
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7be70968a4c5a9bbc85feabdcef6a1ded8fd04b797acd62266bdab558a51d64cafb93229d220222e5515978c187f328c24f337c0572c135b5b6f6f20e7e9c16
|
7
|
+
data.tar.gz: 6c04f8721942a5ff271d06065a4b9ebce38f830b5727d0c3b31d7734da379ba613ecab9ee6a319794442fa641ed5c5d7383cb7a07ba5b6814c5c54e5a26f06be
|
@@ -8,6 +8,9 @@ module Subroutine
|
|
8
8
|
alias_method :field_without_associations, :field
|
9
9
|
alias_method :field, :field_with_associations
|
10
10
|
end
|
11
|
+
|
12
|
+
base.send(:alias_method, :setup_fields_without_association, :setup_fields)
|
13
|
+
base.send(:alias_method, :setup_fields, :setup_fields_with_association)
|
11
14
|
end
|
12
15
|
|
13
16
|
module ClassMethods
|
@@ -99,8 +102,8 @@ module Subroutine
|
|
99
102
|
end
|
100
103
|
end
|
101
104
|
|
102
|
-
def
|
103
|
-
|
105
|
+
def setup_fields_with_association(*args)
|
106
|
+
setup_fields_without_association(*args)
|
104
107
|
|
105
108
|
_fields.each_pair do |field, config|
|
106
109
|
next unless config[:association]
|
data/lib/subroutine/auth.rb
CHANGED
@@ -24,12 +24,21 @@ module Subroutine
|
|
24
24
|
base.instance_eval do
|
25
25
|
extend ::Subroutine::Auth::ClassMethods
|
26
26
|
|
27
|
-
class_attribute :authorization_declared
|
27
|
+
class_attribute :authorization_declared, instance_writer: false
|
28
28
|
self.authorization_declared = false
|
29
|
+
|
30
|
+
class_attribute :user_class_name, instance_writer: false
|
31
|
+
self.user_class_name = "User"
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
32
35
|
module ClassMethods
|
36
|
+
|
37
|
+
def supported_user_class_names
|
38
|
+
[user_class_name, "Integer", "NilClass"].compact
|
39
|
+
end
|
40
|
+
|
41
|
+
|
33
42
|
def authorize(validation_name)
|
34
43
|
validate validation_name, unless: :skip_auth_checks?
|
35
44
|
end
|
@@ -95,6 +104,10 @@ module Subroutine
|
|
95
104
|
super(args.extract_options!)
|
96
105
|
@skip_auth_checks = false
|
97
106
|
@current_user = args.shift
|
107
|
+
|
108
|
+
unless self.class.supported_user_class_names.include?(@current_user.class.name)
|
109
|
+
raise ArgumentError, "current_user must be one of the following types {#{self.class.supported_user_class_names.join(",")}} but was #{@current_user.class.name}"
|
110
|
+
end
|
98
111
|
end
|
99
112
|
|
100
113
|
def skip_auth_checks!
|
@@ -107,7 +120,7 @@ module Subroutine
|
|
107
120
|
end
|
108
121
|
|
109
122
|
def current_user
|
110
|
-
@current_user =
|
123
|
+
@current_user = self.user_class_name.constantize.find(@current_user) if ::Integer === @current_user
|
111
124
|
@current_user
|
112
125
|
end
|
113
126
|
|
data/lib/subroutine/version.rb
CHANGED
@@ -4,6 +4,7 @@ require 'test_helper'
|
|
4
4
|
|
5
5
|
module Subroutine
|
6
6
|
class AuthTest < TestCase
|
7
|
+
|
7
8
|
def user
|
8
9
|
@user ||= ::User.new(email_address: 'doug@example.com')
|
9
10
|
end
|
@@ -50,6 +51,25 @@ module Subroutine
|
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
54
|
+
def test_the_current_user_can_be_defined_by_an_id
|
55
|
+
user = CustomAuthorizeOp.new(1).current_user
|
56
|
+
assert_equal 1, user.id
|
57
|
+
assert_equal true, user.is_a?(::User)
|
58
|
+
assert_equal false, user.is_a?(::AdminUser)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_the_user_class_can_be_overridden
|
62
|
+
user = DifferentUserClassOp.new(1).current_user
|
63
|
+
assert_equal 1, user.id
|
64
|
+
assert_equal true, user.is_a?(::AdminUser)
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_another_class_cant_be_used_as_the_user
|
68
|
+
assert_raises "current_user must be one of the following types {AdminUser,Integer,NilClass} but was String" do
|
69
|
+
DifferentUserClassOp.new("doug")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
53
73
|
def test_it_does_not_run_authorizations_if_explicitly_bypassed
|
54
74
|
op = CustomAuthorizeOp.new User.new(email_address: 'foo@bar.com')
|
55
75
|
op.skip_auth_checks!
|
data/test/support/ops.rb
CHANGED
@@ -13,6 +13,10 @@ class User
|
|
13
13
|
attr_accessor :password
|
14
14
|
|
15
15
|
validates :email_address, presence: true
|
16
|
+
|
17
|
+
def self.find(id)
|
18
|
+
new(id: id)
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
class AdminUser < ::User
|
@@ -137,6 +141,11 @@ class NoUserRequirementsOp < OpWithAuth
|
|
137
141
|
no_user_requirements!
|
138
142
|
end
|
139
143
|
|
144
|
+
class DifferentUserClassOp < OpWithAuth
|
145
|
+
self.user_class_name = "AdminUser"
|
146
|
+
require_user!
|
147
|
+
end
|
148
|
+
|
140
149
|
class CustomAuthorizeOp < OpWithAuth
|
141
150
|
require_user!
|
142
151
|
authorize :authorize_user_is_correct
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: subroutine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Nelson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-06-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|