simon_says 0.3.0.alpha.3 → 0.3.0.alpha.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab60ddcb71d25abd747c76c6ab0758b7cb3e2e4bc7cc3fef23cab0b42abc1d1f
4
- data.tar.gz: bd0e7a95fcf5669a560028034fcf4f15386b00d73d9170bdaab0cb328ee7099f
3
+ metadata.gz: 2ebffb1d29f995636c2c9997b8e86a795b4a7ec08cc9dfcd6fd1bca31463203c
4
+ data.tar.gz: 0fb849d27ded008910437acd78b9470975b436702f917a52ae101f7ef6a0e2f9
5
5
  SHA512:
6
- metadata.gz: 176d279798d67b2ca54c014397766c79ccb735d35c5ff790eb0a57ae3f27be7c4920a1492a4823d7611d7ad3855dec84012a519d0c9a56ec7be97ff943970ff1
7
- data.tar.gz: '098fb9a5b568ae853a9b7840910777a932a8aa0796a030281acc890b2bdb9006ba87df04b835c394350d2cac1d5d215faf2f139441d97a18ff7506a18e4b2303'
6
+ metadata.gz: cdb55a962f39d5d0df49579a37cf997e4acba5fc41e375429ff38b2ffcaadd6b9377861e661ab573833f4fe8fe58eb9981ae9b8ab5c9ed5d5f7655290c9e022c
7
+ data.tar.gz: ea2a382c7226db07795804807280ee24e4c834bb9d7316a3122c62c718cea195a61a45504668150f208ed420f8b0f34a5613f5facc3bfe11a94397eeb244ff5e
@@ -12,6 +12,7 @@ module SimonSays
12
12
 
13
13
  included do
14
14
  class_attribute :default_authorization_scope
15
+ class_attribute :default_find_attribute
15
16
  end
16
17
 
17
18
  module ClassMethods
@@ -41,7 +42,10 @@ module SimonSays
41
42
  # default, +:id+ is used
42
43
  # @param opts [Symbol] :param_key params key for resource query; by default,
43
44
  # +:id+ is used
44
- # @param opts [Symbol] :through through model to use when finding resource
45
+ # @param opts [Symbol] :through through model to use when finding and
46
+ # authorizing the resource. Mutually exclusive with the :with option.
47
+ # @param opts [Symbol] :with what resource to authorize with. Mutually
48
+ # exclusive with the :through option.
45
49
  # @param opts [Symbol] :namespace resource namespace
46
50
  #
47
51
  # @see #find_resource for finder option examples
@@ -87,22 +91,37 @@ module SimonSays
87
91
  end
88
92
  end
89
93
 
90
- # Authorize against a given resource
94
+ # Authorize against a given resource. This resource should be an instance
95
+ # that includes Roleable.
91
96
  #
92
97
  # @param [Symbol, String] resource name of resource to find
93
98
  # @param [Array<Symbol, String>] roles one or more role symbols or strings
94
99
  # @param [Hash] opts before_action options
95
100
  #
96
101
  # @example Authorize resource
97
- # authorize_resource :admin, :support
98
- def authorize_resource(resource, *roles)
102
+ # authorize_with :admin, :support
103
+ def authorize_with(resource, *roles)
99
104
  opts = roles.extract_options!
100
105
 
101
106
  before_action action_options(opts) do
102
- authorize roles, { resource: resource }
107
+ authorize roles, { with: resource }
103
108
  end
104
109
  end
105
110
 
111
+ # Authorize with the +default_authorization_scope+. The instance returned
112
+ # by the +default_authorization_scope+ should include Roleable.
113
+ #
114
+ # @param [Array<Symbol, String>] roles one or more role symbols or strings
115
+ # @param [Hash] opts before_action options
116
+ #
117
+ # @example Authorize "content" and "marketing" using the current Admin
118
+ # self.default_authorization_scope = :current_admin
119
+ #
120
+ # authorize :content, :marketing
121
+ def authorize(*roles)
122
+ authorize_with(default_authorization_scope, *roles)
123
+ end
124
+
106
125
  # Extract before_action options from Hash
107
126
  #
108
127
  # @private
@@ -145,10 +164,15 @@ module SimonSays
145
164
  # @param [Symbol, String] one or more required roles
146
165
  # @param [Hash] options authorizer options
147
166
  def authorize(required = nil, options)
148
- if through = options[:through]
149
- name = through.to_s.singularize.to_sym
167
+ if options.key? :through
168
+ name = options[:through].to_s.singularize.to_sym
169
+ elsif options.key? :with
170
+ name = options[:with].to_s.singularize.to_sym
150
171
  else
151
- name = options[:resource]
172
+ raise ArgumentError, 'find_and_authorize must be called with either '\
173
+ ':through or :with option. The resource referenced by the value '\
174
+ 'of this option should be an instance of a class that includes '\
175
+ 'Roleable.'
152
176
  end
153
177
 
154
178
  record = instance_variable_get("@#{name}")
@@ -176,7 +200,7 @@ module SimonSays
176
200
  # @private
177
201
  def resource_scope_and_query(resource, options)
178
202
  if options[:through]
179
- field = "#{resource}_id"
203
+ field = :"#{resource}_id"
180
204
 
181
205
  query = { field => params[field] } if params[field]
182
206
  scope = send(self.class.default_authorization_scope)
@@ -191,7 +215,10 @@ module SimonSays
191
215
  scope = klass.classify.constantize
192
216
  end
193
217
 
194
- field ||= options.fetch(:find_attribute, :id)
218
+ field ||= options.fetch(:find_attribute) do
219
+ self.class.default_find_attribute&.call(resource) || :id
220
+ end
221
+
195
222
  query ||= { field => params[options.fetch(:param_key, :id)] }
196
223
 
197
224
  return scope, query
@@ -1,3 +1,3 @@
1
1
  module SimonSays
2
- VERSION = '0.3.0.alpha.3'
2
+ VERSION = '0.3.0.alpha.4'
3
3
  end
@@ -0,0 +1,9 @@
1
+ class Admin::CommReportsController < ApplicationController
2
+ find_and_authorize :report, :comms, with: :admin, namespace: :admin
3
+
4
+ respond_to :json
5
+
6
+ def show
7
+ respond_with @report
8
+ end
9
+ end
@@ -1,7 +1,9 @@
1
1
  class Admin::ReportsController < ApplicationController
2
2
  respond_to :json
3
3
 
4
- authorize_resource :admin, :support
4
+ self.default_authorization_scope = :current_admin
5
+
6
+ authorize :support
5
7
  find_resource :report, namespace: :admin, except: [:index, :new, :create]
6
8
 
7
9
  def index
@@ -0,0 +1,2 @@
1
+ class Client < ApplicationRecord
2
+ end
@@ -0,0 +1,7 @@
1
+ class CreateClients < ActiveRecord::Migration[5.1]
2
+ def change
3
+ create_table :clients, primary_key: :client_id do |t|
4
+ t.timestamps
5
+ end
6
+ end
7
+ end
@@ -10,7 +10,7 @@
10
10
  #
11
11
  # It's strongly recommended that you check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(version: 20160823220959) do
13
+ ActiveRecord::Schema.define(version: 20190711184853) do
14
14
 
15
15
  create_table "admin_reports", force: :cascade do |t|
16
16
  t.string "title"
@@ -24,6 +24,11 @@ ActiveRecord::Schema.define(version: 20160823220959) do
24
24
  t.datetime "updated_at", null: false
25
25
  end
26
26
 
27
+ create_table "clients", primary_key: "client_id", force: :cascade do |t|
28
+ t.datetime "created_at", null: false
29
+ t.datetime "updated_at", null: false
30
+ end
31
+
27
32
  create_table "documents", force: :cascade do |t|
28
33
  t.string "title"
29
34
  t.datetime "created_at", null: false
@@ -0,0 +1 @@
1
+ alice: {}
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ClientTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -29,6 +29,25 @@ class AuthorizerTest < ActiveSupport::TestCase
29
29
  @controller.params = { id: documents(:alpha).id }
30
30
  end
31
31
 
32
+ def with_params(params)
33
+ default_params = @controller.params
34
+ @controller.params = params
35
+
36
+ yield
37
+
38
+ ensure
39
+ @controller.params = default_params
40
+ end
41
+
42
+ def with_default_find_attribute(callalbe)
43
+ @controller.class.default_find_attribute = callalbe
44
+
45
+ yield
46
+
47
+ ensure
48
+ @controller.class.default_find_attribute = nil
49
+ end
50
+
32
51
  test "find_resource" do
33
52
  @controller.find_resource :document
34
53
 
@@ -96,19 +115,19 @@ class AuthorizerTest < ActiveSupport::TestCase
96
115
  test "authorize with membership role" do
97
116
  @controller.instance_variable_set :@membership, documents(:alpha).memberships.first
98
117
 
99
- assert @controller.authorize(:fork, resource: :membership)
118
+ assert @controller.authorize(:fork, with: :membership)
100
119
  end
101
120
 
102
121
  test "authorize with current_admin" do
103
122
  @controller.current_admin = admins(:support)
104
123
 
105
- assert @controller.authorize(:support, resource: :admin)
124
+ assert @controller.authorize(:support, with: :admin)
106
125
  end
107
126
 
108
127
  test "authorize with multiple roles" do
109
128
  @controller.instance_variable_set :@membership, documents(:alpha).memberships.first
110
129
 
111
- assert @controller.authorize([:update, :delete], resource: :membership)
130
+ assert @controller.authorize([:update, :delete], with: :membership)
112
131
  end
113
132
 
114
133
  test "authorize with through" do
@@ -121,14 +140,14 @@ class AuthorizerTest < ActiveSupport::TestCase
121
140
  @controller.current_admin = admins(:marketing)
122
141
 
123
142
  @controller.expects(:authenticate_admin!).once
124
- @controller.authorize(:marketing, resource: :admin)
143
+ @controller.authorize(:marketing, with: :admin)
125
144
  end
126
145
 
127
146
  test "authorization failure single role" do
128
147
  assert_raises SimonSays::Authorizer::Denied do
129
148
  @controller.instance_variable_set :@membership, documents(:beta).memberships.first
130
149
 
131
- @controller.authorize(:delete, resource: :membership)
150
+ @controller.authorize(:delete, with: :membership)
132
151
  end
133
152
  end
134
153
 
@@ -136,8 +155,17 @@ class AuthorizerTest < ActiveSupport::TestCase
136
155
  @controller.instance_variable_set :@membership, documents(:beta).memberships.first
137
156
 
138
157
  assert_raises SimonSays::Authorizer::Denied do
139
- @controller.authorize([:update, :delete], resource: :membership)
158
+ @controller.authorize([:update, :delete], with: :membership)
140
159
  end
141
160
  end
142
- end
143
161
 
162
+ test 'Authorizer.default_find_attribute proc' do
163
+ with_default_find_attribute ->(resource) { :"#{resource}_id" } do
164
+ with_params id: clients(:alice).client_id do
165
+ @controller.find_resource :client
166
+ end
167
+ end
168
+
169
+ assert_equal clients(:alice), @controller[:client]
170
+ end
171
+ end
data/test/test_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'minitest/unit'
1
+ require "minitest/autorun"
2
2
  require 'mocha/minitest'
3
3
 
4
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simon_says
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0.alpha.3
4
+ version: 0.3.0.alpha.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Coyne
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-05-22 00:00:00.000000000 Z
13
+ date: 2019-07-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -136,6 +136,7 @@ files:
136
136
  - test/rails_app/app/assets/images/.keep
137
137
  - test/rails_app/app/assets/javascripts/application.js
138
138
  - test/rails_app/app/assets/stylesheets/application.css
139
+ - test/rails_app/app/controllers/admin/comm_reports_controller.rb
139
140
  - test/rails_app/app/controllers/admin/reports_controller.rb
140
141
  - test/rails_app/app/controllers/application_controller.rb
141
142
  - test/rails_app/app/controllers/concerns/.keep
@@ -147,6 +148,7 @@ files:
147
148
  - test/rails_app/app/models/admin.rb
148
149
  - test/rails_app/app/models/admin/report.rb
149
150
  - test/rails_app/app/models/application_record.rb
151
+ - test/rails_app/app/models/client.rb
150
152
  - test/rails_app/app/models/concerns/.keep
151
153
  - test/rails_app/app/models/document.rb
152
154
  - test/rails_app/app/models/image.rb
@@ -182,6 +184,7 @@ files:
182
184
  - test/rails_app/db/migrate/20141016183642_create_documents.rb
183
185
  - test/rails_app/db/migrate/20141017140833_create_admin_reports.rb
184
186
  - test/rails_app/db/migrate/20160823220959_create_images.rb
187
+ - test/rails_app/db/migrate/20190711184853_create_clients.rb
185
188
  - test/rails_app/db/schema.rb
186
189
  - test/rails_app/db/seeds.rb
187
190
  - test/rails_app/lib/assets/.keep
@@ -196,6 +199,7 @@ files:
196
199
  - test/rails_app/test/fixtures/.keep
197
200
  - test/rails_app/test/fixtures/admin/reports.yml
198
201
  - test/rails_app/test/fixtures/admins.yml
202
+ - test/rails_app/test/fixtures/clients.yml
199
203
  - test/rails_app/test/fixtures/documents.yml
200
204
  - test/rails_app/test/fixtures/images.yml
201
205
  - test/rails_app/test/fixtures/memberships.yml
@@ -205,6 +209,7 @@ files:
205
209
  - test/rails_app/test/mailers/.keep
206
210
  - test/rails_app/test/models/.keep
207
211
  - test/rails_app/test/models/admin/report_test.rb
212
+ - test/rails_app/test/models/client_test.rb
208
213
  - test/rails_app/test/models/document_test.rb
209
214
  - test/rails_app/test/models/image_test.rb
210
215
  - test/rails_app/test/models/membership_test.rb
@@ -235,8 +240,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
235
240
  - !ruby/object:Gem::Version
236
241
  version: 1.3.1
237
242
  requirements: []
238
- rubyforge_project:
239
- rubygems_version: 2.7.9
243
+ rubygems_version: 3.0.3
240
244
  signing_key:
241
245
  specification_version: 4
242
246
  summary: Light-weight, declarative authorization and access control for Rails
@@ -250,6 +254,7 @@ test_files:
250
254
  - test/rails_app/app/assets/images/.keep
251
255
  - test/rails_app/app/assets/javascripts/application.js
252
256
  - test/rails_app/app/assets/stylesheets/application.css
257
+ - test/rails_app/app/controllers/admin/comm_reports_controller.rb
253
258
  - test/rails_app/app/controllers/admin/reports_controller.rb
254
259
  - test/rails_app/app/controllers/application_controller.rb
255
260
  - test/rails_app/app/controllers/concerns/.keep
@@ -261,6 +266,7 @@ test_files:
261
266
  - test/rails_app/app/models/admin.rb
262
267
  - test/rails_app/app/models/admin/report.rb
263
268
  - test/rails_app/app/models/application_record.rb
269
+ - test/rails_app/app/models/client.rb
264
270
  - test/rails_app/app/models/concerns/.keep
265
271
  - test/rails_app/app/models/document.rb
266
272
  - test/rails_app/app/models/image.rb
@@ -296,6 +302,7 @@ test_files:
296
302
  - test/rails_app/db/migrate/20141016183642_create_documents.rb
297
303
  - test/rails_app/db/migrate/20141017140833_create_admin_reports.rb
298
304
  - test/rails_app/db/migrate/20160823220959_create_images.rb
305
+ - test/rails_app/db/migrate/20190711184853_create_clients.rb
299
306
  - test/rails_app/db/schema.rb
300
307
  - test/rails_app/db/seeds.rb
301
308
  - test/rails_app/lib/assets/.keep
@@ -310,6 +317,7 @@ test_files:
310
317
  - test/rails_app/test/fixtures/.keep
311
318
  - test/rails_app/test/fixtures/admin/reports.yml
312
319
  - test/rails_app/test/fixtures/admins.yml
320
+ - test/rails_app/test/fixtures/clients.yml
313
321
  - test/rails_app/test/fixtures/documents.yml
314
322
  - test/rails_app/test/fixtures/images.yml
315
323
  - test/rails_app/test/fixtures/memberships.yml
@@ -319,6 +327,7 @@ test_files:
319
327
  - test/rails_app/test/mailers/.keep
320
328
  - test/rails_app/test/models/.keep
321
329
  - test/rails_app/test/models/admin/report_test.rb
330
+ - test/rails_app/test/models/client_test.rb
322
331
  - test/rails_app/test/models/document_test.rb
323
332
  - test/rails_app/test/models/image_test.rb
324
333
  - test/rails_app/test/models/membership_test.rb