action_authorizer 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NDUwOGM3ZjMzYTYwNzllMmJkYTQ5ZDNiMDQ5YTRlNDRhYjRlMTM5ZA==
5
+ data.tar.gz: !binary |-
6
+ YWM4Y2JmNzczOTViMWU0YWFmMTIxZmQ1YjUyMzBjODdlM2RmZTU4Yg==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZDE4ODdkYTEyYzY4NDFhZWRlYTQ4NGI0MWI2YWUzMWE0NWE5MGNmOTBjNTVj
10
+ ODQyM2EwMzA5MWJjZWIwNDBhN2Q1ZDIyYjJlOWUyYmFkNzgwZThlOGFhNjA0
11
+ MzQ2NDI2OGJjMGJlNDFmYjJjNDMxMGExMGRlNTNhZTI2YTNjOGE=
12
+ data.tar.gz: !binary |-
13
+ YjQ2NmZmYzdhOWRlNWZiMWE4ZTYyNDAwNTZlNjM1M2MxNjEwZDIxZmIyZmE5
14
+ ZDU2YjE1YzYxNjFiMzc4MGVlZjhiYmJmZDcxYzUzNTU3YTBkNWIyOTQ5YTNk
15
+ OTc2YWRkZTBjNmI1YjkwZGU5NjdlZjQwMmM5Y2IyMGYzZjI1ZTg=
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2016 Marcelo Junior
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,472 @@
1
+ # Action Authorizer
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/action_authorizer.svg)](https://badge.fury.io/rb/action_authorizer)
4
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=1.3.0)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
5
+
6
+ Rails authorization with controllers's actions.
7
+
8
+ ActionAuthorizer is a gem to authorize the controllers's actions. Designed to work with RSpec and Devise. Where each controller will have an authorizer with the same actions. Each authorizer's action will return your permission's result.
9
+
10
+ ## Tested against Rails >= 3.0 and Ruby >= 1.9
11
+
12
+ - Rails 3.0 and Ruby 1.9
13
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-3.0)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
14
+ - Rails 3.1 and Ruby 1.9
15
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-3.1)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
16
+ - Rails 3.2 and Ruby 2.0
17
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-3.2)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
18
+ - Rails 4.0 and Ruby 2.0
19
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-4.0)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
20
+ - Rails 4.1 and Ruby 2.1
21
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-4.1)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
22
+ - Rails 4.2 and Ruby 2.2
23
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-4.2)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
24
+ - Rails 5.0 and Ruby 2.3
25
+ [![Build Status](https://travis-ci.org/juniormesquitadandao/action_authorizer.svg?branch=rails-5.0)](https://travis-ci.org/juniormesquitadandao/action_authorizer/branches)
26
+
27
+ ## Getting Started
28
+
29
+ After setting up your Rspec and Devise! Set up your ActionAuthorizer.
30
+
31
+ now configure your: Gemfile
32
+ ```ruby
33
+ gem 'action_authorizer', '~> 1.3'
34
+ ```
35
+
36
+ run
37
+ ```console
38
+ bundle install
39
+ rails generate action_authorizer:install
40
+ ```
41
+
42
+ it will be generated: app/authorizers/application_authorizer.rb
43
+ ```ruby
44
+ class ApplicationAuthorizer < ActionAuthorizer::Base
45
+ end
46
+ ```
47
+
48
+ it will be generated: app/helpers/action_authorizer_helper.rb
49
+ ```ruby
50
+ module ActionAuthorizerHelper
51
+ # Add helpers to check authorization authenticated.
52
+ # def unauthorized? controller, action, params = {}
53
+ # def authorized? controller, action, params = {}
54
+ # ex.:
55
+ # <%= link_to 'Models', models_path if authorized? :models, :index %>
56
+ # <%= link_to 'Models Dashboard', dashboard_models_path if authorized? 'dashborad/models', :index %>
57
+ # <%= link_to 'Model', model_path(@model) if authorized? :models, :show, id: @model.id %>
58
+ # <%= link_to 'Model', edit_model_path(@model) if authorized? :models, :edit, id: @model.to_param %>
59
+ #
60
+ # <%= link_to 'Models', models_path if unauthorized? :models, :index %>
61
+ # <%= link_to 'Models Dashboard', dashboard_models_path if unauthorized? 'dashborad/models', :index %>
62
+ # <%= link_to 'Model', model_path(@model) if unauthorized? :models, :show, id: @model.id %>
63
+ # <%= link_to 'Model', edit_model_path(@model) if unauthorized? :models, :edit, id: @model.to_param %>
64
+ include ActionAuthorizer::Helper
65
+
66
+ # def authenticated
67
+ # current_user
68
+ # end
69
+ end
70
+ ```
71
+
72
+ now configure your: spec/rails_helper.rb
73
+ ```ruby
74
+ RSpec.configure do |config|
75
+ # Controllers Spec with ActionAuthorizer
76
+ config.before :each, type: :controller do
77
+ allow(controller).to receive(:authorize!)
78
+ end
79
+
80
+ # Views Spec with ActionAuthorizer
81
+ # case rails < 3.1
82
+ # config.before :each, type: :view do
83
+ # view.extend ActionAuthorizerHelper
84
+ # end
85
+ end
86
+ ```
87
+
88
+ now configure your: app/controllers/application_controller.rb
89
+ ```ruby
90
+ class ApplicationController < ActionController::Base
91
+ include ActionAuthorizer::Config
92
+
93
+ # Prevent CSRF attacks by raising an exception.
94
+ # For APIs, you may want to use :null_session instead.
95
+ protect_from_forgery with: :exception
96
+
97
+ before_action :authenticate_user!
98
+ before_action :authorize!, unless: :devise_controller?
99
+
100
+ # def authenticated
101
+ # current_user
102
+ # end
103
+
104
+ # def respond_unauthorized_on_production_environment
105
+ # render file: Rails.root.join('public/404'), layout: false, status: :not_found
106
+ # end
107
+ end
108
+ ```
109
+
110
+ ### Authorizing models with module
111
+
112
+ run
113
+ ```console
114
+ rails generate action_authorizer:authorizer namespace/model
115
+ ```
116
+
117
+ ### Authorizing models without module
118
+
119
+ run
120
+ ```console
121
+ rails generate action_authorizer:authorizer model
122
+ ```
123
+
124
+ it will be generated: app/authorizers/models_authorizer.rb
125
+ ```ruby
126
+ # Authorize reference controller actions when return:
127
+ # Present values different hash:
128
+ # ex.:
129
+ # true
130
+ # 'nil'
131
+ # 'false'
132
+ # 0
133
+ # '0'
134
+ # [0]
135
+ # Empty requested params:
136
+ # ex. to requested params {}:
137
+ # { id: [1, 2] }
138
+ # { id: ['1', '2'] }
139
+ # { id: ['one', 'two'] }
140
+ # A hash with key:values including requested params key:value:
141
+ # ex. to requested params {id: 1, other: 3}:
142
+ # { id: [1, 2] }
143
+ # { id: ['1', '2'] }
144
+ # ex. to requested params {id: 'one', other: 'three'}:
145
+ # { id: ['one', 'two'] }
146
+ # A hash with keys different requested params keys:
147
+ # ex. to requested params {other: 3}:
148
+ # { id: [1, 2] }
149
+ # { id: ['1', '2'] }
150
+ # ex. to requested params {other: 'three'}:
151
+ # { id: ['one', 'two'] }
152
+ #
153
+ # Unauthorize reference controller actions when return:
154
+ # Blank values different hash:
155
+ # ex.:
156
+ # nil
157
+ # false
158
+ # ''
159
+ # ' '
160
+ # []
161
+ # A hash with key:values excluding requested params key:value:
162
+ # ex. to requested params {id: 3, other: 3}:
163
+ # { id: [1, 2] }
164
+ # { id: ['1', '2'] }
165
+ # ex. to requested params {id: 'three', other: 'three'}:
166
+ # { id: ['one', 'two'] }
167
+ class ModelsAuthorizer < ApplicationAuthorizer
168
+ # All actions automatically validating the need of user logged.
169
+ # Skip this check for all actions:
170
+ # skip_authentication
171
+ # Or skip_authentication_only for some actions:
172
+ # skip_authentication_only :index, :new, :destroy, ...
173
+
174
+ def index
175
+ # true
176
+ end
177
+
178
+ def show
179
+ # true
180
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
181
+ # { id: authenticated.model_ids }
182
+ end
183
+
184
+ def new
185
+ # true
186
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
187
+ end
188
+
189
+ def edit
190
+ # true
191
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
192
+ # { id: authenticated.model_ids }
193
+ end
194
+
195
+ def create
196
+ # true
197
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
198
+ end
199
+
200
+ def update
201
+ # true
202
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
203
+ # { id: authenticated.model_ids }
204
+ end
205
+
206
+ def destroy
207
+ # true
208
+ # Model.where(user: authenticated).find(params[:id]).avaliable?
209
+ # { id: authenticated.model_ids }
210
+ end
211
+
212
+ end
213
+ ```
214
+
215
+ it will be generated: spec/authorizers/models_authorizer_spec.rb
216
+ ```ruby
217
+ require 'rails_helper'
218
+
219
+ RSpec.describe ModelsAuthorizer, type: :authorizer do
220
+
221
+ # let(:guest_user) { nil }
222
+ # let(:one_user) { double('Authenticated', user_group?: true, model_ids: [1]) }
223
+ # let(:two_user) { double('Authenticated', user_group?: true, model_ids: [2]) }
224
+ # let(:admin_user) { double('Authenticated', admin_group?: true) }
225
+
226
+ # context '#index' do
227
+ # describe 'authorize' do
228
+ # it { expect(ModelsAuthorizer.new(one_user, :index)).to be_authorized }
229
+ # it { expect(ModelsAuthorizer.new(two_user, :index)).to be_authorized }
230
+ # it { expect(ModelsAuthorizer.new(admin_user, :index)).to be_authorized }
231
+ # end
232
+ #
233
+ # describe 'not authorize' do
234
+ # it { expect(ModelsAuthorizer.new(guest_user, :index)).to be_unauthorized }
235
+ # end
236
+ # end
237
+
238
+ # context '#show' do
239
+ # describe 'authorize' do
240
+ # it { expect(ModelsAuthorizer.new(one_user, :show, id: 1)).to be_authorized }
241
+ # it { expect(ModelsAuthorizer.new(two_user, :show, id: 2)).to be_authorized }
242
+ # it { expect(ModelsAuthorizer.new(admin_user, :show, id: 1)).to be_authorized }
243
+ # it { expect(ModelsAuthorizer.new(admin_user, :show, id: 2)).to be_authorized }
244
+ # end
245
+ #
246
+ # describe 'not authorize' do
247
+ # it { expect(ModelsAuthorizer.new(guest_user, :show, id: 1)).to be_unauthorized }
248
+ # it { expect(ModelsAuthorizer.new(one_user, :show, id: 2)).to be_unauthorized }
249
+ # it { expect(ModelsAuthorizer.new(two_user, :show, id: 1)).to be_unauthorized }
250
+ # end
251
+ # end
252
+
253
+ # context '#new' do
254
+ # describe 'authorize' do
255
+ # it { expect(ModelsAuthorizer.new(one_user, :new)).to be_authorized }
256
+ # it { expect(ModelsAuthorizer.new(two_user, :new)).to be_authorized }
257
+ # it { expect(ModelsAuthorizer.new(admin_user, :new)).to be_authorized }
258
+ # end
259
+ #
260
+ # describe 'not authorize' do
261
+ # it { expect(ModelsAuthorizer.new(guest_user, :new)).to be_unauthorized }
262
+ # end
263
+ # end
264
+
265
+ # context '#edit' do
266
+ # describe 'authorize' do
267
+ # it { expect(ModelsAuthorizer.new(one_user, :edit, id: 1)).to be_authorized }
268
+ # it { expect(ModelsAuthorizer.new(two_user, :edit, id: 2)).to be_authorized }
269
+ # it { expect(ModelsAuthorizer.new(admin_user, :edit, id: 1)).to be_authorized }
270
+ # it { expect(ModelsAuthorizer.new(admin_user, :edit, id: 2)).to be_authorized }
271
+ # end
272
+ #
273
+ # describe 'not authorize' do
274
+ # it { expect(ModelsAuthorizer.new(guest_user, :edit, id: 1)).to be_unauthorized }
275
+ # it { expect(ModelsAuthorizer.new(one_user, :edit, id: 2)).to be_unauthorized }
276
+ # it { expect(ModelsAuthorizer.new(two_user, :edit, id: 1)).to be_unauthorized }
277
+ # end
278
+ # end
279
+
280
+ # context '#create' do
281
+ # describe 'authorize' do
282
+ # it { expect(ModelsAuthorizer.new(one_user, :create)).to be_authorized }
283
+ # it { expect(ModelsAuthorizer.new(two_user, :create)).to be_authorized }
284
+ # it { expect(ModelsAuthorizer.new(admin_user, :create)).to be_authorized }
285
+ # end
286
+ #
287
+ # describe 'not authorize' do
288
+ # it { expect(ModelsAuthorizer.new(guest_user, :create)).to be_unauthorized }
289
+ # end
290
+ # end
291
+
292
+ # context '#update' do
293
+ # describe 'authorize' do
294
+ # it { expect(ModelsAuthorizer.new(one_user, :update, id: 1)).to be_authorized }
295
+ # it { expect(ModelsAuthorizer.new(two_user, :update, id: 2)).to be_authorized }
296
+ # it { expect(ModelsAuthorizer.new(admin_user, :update, id: 1)).to be_authorized }
297
+ # it { expect(ModelsAuthorizer.new(admin_user, :update, id: 2)).to be_authorized }
298
+ # end
299
+ #
300
+ # describe 'not authorize' do
301
+ # it { expect(ModelsAuthorizer.new(guest_user, :update, id: 1)).to be_unauthorized }
302
+ # it { expect(ModelsAuthorizer.new(one_user, :update, id: 2)).to be_unauthorized }
303
+ # it { expect(ModelsAuthorizer.new(two_user, :update, id: 1)).to be_unauthorized }
304
+ # end
305
+ # end
306
+
307
+ # context '#destroy' do
308
+ # describe 'authorize' do
309
+ # it { expect(ModelsAuthorizer.new(one_user, :destroy, id: 1)).to be_authorized }
310
+ # it { expect(ModelsAuthorizer.new(two_user, :destroy, id: 2)).to be_authorized }
311
+ # it { expect(ModelsAuthorizer.new(admin_user, :destroy, id: 1)).to be_authorized }
312
+ # it { expect(ModelsAuthorizer.new(admin_user, :destroy, id: 2)).to be_authorized }
313
+ # end
314
+ #
315
+ # describe 'not authorize' do
316
+ # it { expect(ModelsAuthorizer.new(guest_user, :destroy, id: 1)).to be_unauthorized }
317
+ # it { expect(ModelsAuthorizer.new(one_user, :destroy, id: 2)).to be_unauthorized }
318
+ # it { expect(ModelsAuthorizer.new(two_user, :destroy, id: 1)).to be_unauthorized }
319
+ # end
320
+ # end
321
+
322
+ end
323
+ ```
324
+
325
+ ## Authorizing Account's Record or Cancellation (Optional)
326
+
327
+ now edit your: app/controllers/application_controller.rb
328
+ ```ruby
329
+ class ApplicationController < ActionController::Base
330
+ include ActionAuthorizer::Config
331
+
332
+ # Prevent CSRF attacks by raising an exception.
333
+ # For APIs, you may want to use :null_session instead.
334
+ protect_from_forgery with: :exception
335
+
336
+ before_action :authenticate_user!
337
+ before_action :authorize!, if: :authorizer_controller?
338
+
339
+ # def authenticated
340
+ # current_user
341
+ # end
342
+
343
+ # def respond_unauthorized_on_production_environment
344
+ # render file: Rails.root.join('public/404'), layout: false, status: :not_found
345
+ # end
346
+
347
+ private
348
+
349
+ def authorizer_controller?
350
+ controller_path == 'devise/registrations' || !devise_controller?
351
+ end
352
+ end
353
+ ```
354
+
355
+ now edit your: /app/views/devise/registrations/edit.html.erb
356
+ ```ruby
357
+ <% if authorized? 'devise/registrations', :cancel %>
358
+ <h3>Cancel my account</h3>
359
+
360
+ <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p>
361
+ <% end %>
362
+ ```
363
+
364
+ now edit your: /app/views/devise/shared/_links.html.erb
365
+ ```ruby
366
+ <%- if devise_mapping.registerable? && controller_name != 'registrations' %>
367
+ <%= link_to "Sign up", new_registration_path(resource_name) if authorized? 'devise/registrations', :new %><br />
368
+ <% end -%>
369
+ ```
370
+
371
+ run
372
+ ```console
373
+ rails generate action_authorizer:authorizer devise/registration
374
+ ```
375
+
376
+ now edit your: app/authorizers/devise/registrations_authorizer.rb
377
+ ```ruby
378
+ class Devise::RegistrationsAuthorizer < ApplicationAuthorizer
379
+ skip_authentication_only :new, :create
380
+
381
+ # Account registration page
382
+ def new
383
+ end
384
+
385
+ # Account edition page
386
+ def edit
387
+ true
388
+ end
389
+
390
+ # Account registration submition
391
+ def create
392
+ end
393
+
394
+ # Account edition submition
395
+ def update
396
+ true
397
+ end
398
+
399
+ # Account cancellation page
400
+ def destroy
401
+ end
402
+
403
+ # Account cancellation submition
404
+ def cancel
405
+ end
406
+
407
+ end
408
+ ```
409
+
410
+ now edit your: /spec/authorizers/devise/registrations_authorizer_spec.rb
411
+ ```ruby
412
+ require 'rails_helper'
413
+
414
+ RSpec.describe Devise::RegistrationsAuthorizer, type: :authorizer do
415
+
416
+ let(:guest_user) { nil }
417
+ let(:user) { double('Authenticated') }
418
+
419
+ context '#new' do
420
+ describe 'not authorize' do
421
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :new)).to be_unauthorized }
422
+
423
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :new)).to be_unauthorized }
424
+ end
425
+ end
426
+
427
+ context '#edit' do
428
+ describe 'authorize' do
429
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :edit)).to be_authorized }
430
+ end
431
+
432
+ describe 'not authorize' do
433
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :edit)).to be_unauthorized }
434
+ end
435
+ end
436
+
437
+ context '#create' do
438
+ describe 'not authorize' do
439
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :create)).to be_unauthorized }
440
+
441
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :create)).to be_unauthorized }
442
+ end
443
+ end
444
+
445
+ context '#update' do
446
+ describe 'authorize' do
447
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :update)).to be_authorized }
448
+ end
449
+
450
+ describe 'not authorize' do
451
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :update)).to be_unauthorized }
452
+ end
453
+ end
454
+
455
+ context '#destroy' do
456
+ describe 'not authorize' do
457
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :destroy)).to be_unauthorized }
458
+
459
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :destroy)).to be_unauthorized }
460
+ end
461
+ end
462
+
463
+ context '#cancel' do
464
+ describe 'not authorize' do
465
+ it { expect(Devise::RegistrationsAuthorizer.new(user, :cancel)).to be_unauthorized }
466
+
467
+ it { expect(Devise::RegistrationsAuthorizer.new(guest_user, :cancel)).to be_unauthorized }
468
+ end
469
+ end
470
+
471
+ end
472
+ ```
@@ -0,0 +1,5 @@
1
+ module ActionAuthorizer
2
+ load 'action_authorizer/base.rb'
3
+ load 'action_authorizer/config.rb'
4
+ load 'action_authorizer/helper.rb'
5
+ end
@@ -0,0 +1,60 @@
1
+ class ActionAuthorizer::Base
2
+
3
+ def initialize(authenticated, action, params = {})
4
+ @authenticated = authenticated
5
+ @action = action.to_sym
6
+ @params = params
7
+ end
8
+
9
+ def unauthorized?
10
+ return true if !authenticated && !skip_authentication && skip_authentication_only.exclude?(action)
11
+
12
+ @result = send(action)
13
+ if !result.kind_of?(Hash)
14
+ result.blank?
15
+ elsif params.empty?
16
+ false
17
+ else
18
+ unauthorized_params?
19
+ end
20
+ end
21
+
22
+ def authorized?
23
+ !unauthorized?
24
+ end
25
+
26
+ class << self
27
+ def skip_authentication
28
+ define_method(:skip_authentication) do
29
+ true
30
+ end
31
+ end
32
+
33
+ def skip_authentication_only *actions
34
+ define_method(:skip_authentication_only) do
35
+ actions
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ attr_reader :authenticated, :action, :params, :result
43
+
44
+ def unauthorized_params?
45
+ unauthorized_params = result.select do |name, values|
46
+ param_value = params[name].to_s
47
+ param_value.present? && values.map!{|v| v.to_s}.exclude?(param_value)
48
+ end
49
+ unauthorized_params.any?
50
+ end
51
+
52
+ def skip_authentication
53
+ false
54
+ end
55
+
56
+ def skip_authentication_only
57
+ []
58
+ end
59
+
60
+ end
@@ -0,0 +1,31 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActionAuthorizer::Config
4
+ extend ActiveSupport::Concern
5
+
6
+ def authenticated
7
+ current_user
8
+ end
9
+
10
+ def respond_unauthorized_on_production_environment
11
+ render file: Rails.root.join('public/404'), layout: false, status: :not_found
12
+ end
13
+
14
+ def authorize!
15
+ unauthorize! if unauthorized?
16
+ end
17
+
18
+ def unauthorized?
19
+ authorizer = "#{controller_path}_authorizer".classify.constantize
20
+ authorizer.new(authenticated, action_name.to_sym, params.except(:controller, :action)).unauthorized?
21
+ end
22
+
23
+ def unauthorize!
24
+ if Rails.env.production?
25
+ respond_unauthorized_on_production_environment
26
+ else
27
+ raise ActionController::RoutingError, "No route matches [#{env['REQUEST_METHOD']}] #{env['PATH_INFO'].inspect}"
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActionAuthorizer::Helper
4
+ extend ActiveSupport::Concern
5
+
6
+ def authenticated
7
+ current_user
8
+ end
9
+
10
+ def authorized? controller, action, params = {}
11
+ !unauthorized? controller, action, params
12
+ end
13
+
14
+ def unauthorized? controller, action, params = {}
15
+ authorizer = "#{controller.to_s}_authorizer".classify.constantize
16
+ authorizer.new(authenticated, action.to_sym, params).unauthorized?
17
+ end
18
+
19
+ end
@@ -0,0 +1,3 @@
1
+ module ActionAuthorizer
2
+ VERSION = "1.3.1".freeze
3
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate authorizer Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/resource_helpers'
2
+
3
+ class ActionAuthorizer::AuthorizerGenerator < Rails::Generators::NamedBase
4
+ include Rails::Generators::ResourceHelpers
5
+
6
+ source_root File.expand_path('../templates', __FILE__)
7
+
8
+ argument :actions, type: :array, default: [], banner: 'action action'
9
+
10
+ check_class_collision suffix: 'Authorizer'
11
+
12
+ def create_authorizer_file
13
+ template 'authorizer.rb', File.join('app/authorizers', controller_class_path, "#{controller_file_name}_authorizer.rb")
14
+ end
15
+
16
+ def create_authorizer_spec_file
17
+ template 'authorizer_spec.rb', File.join('spec/authorizers', controller_class_path, "#{controller_file_name}_authorizer_spec.rb")
18
+ end
19
+ end
@@ -0,0 +1,87 @@
1
+ # Authorize reference controller actions when return:
2
+ # Present values different hash:
3
+ # ex.:
4
+ # true
5
+ # 'nil'
6
+ # 'false'
7
+ # 0
8
+ # '0'
9
+ # [0]
10
+ # Empty requested params:
11
+ # ex. to requested params {}:
12
+ # { id: [1, 2] }
13
+ # { id: ['1', '2'] }
14
+ # { id: ['one', 'two'] }
15
+ # A hash with key:values including requested params key:value:
16
+ # ex. to requested params {id: 1, other: 3}:
17
+ # { id: [1, 2] }
18
+ # { id: ['1', '2'] }
19
+ # ex. to requested params {id: 'one', other: 'three'}:
20
+ # { id: ['one', 'two'] }
21
+ # A hash with keys different requested params keys:
22
+ # ex. to requested params {other: 3}:
23
+ # { id: [1, 2] }
24
+ # { id: ['1', '2'] }
25
+ # ex. to requested params {other: 'three'}:
26
+ # { id: ['one', 'two'] }
27
+
28
+ # Unauthorize reference controller actions when return:
29
+ # Blank values different hash:
30
+ # ex.:
31
+ # nil
32
+ # false
33
+ # ''
34
+ # ' '
35
+ # []
36
+ # A hash with key:values excluding requested params key:value:
37
+ # ex. to requested params {id: 3, other: 3}:
38
+ # { id: [1, 2] }
39
+ # { id: ['1', '2'] }
40
+ # ex. to requested params {id: 'three', other: 'three'}:
41
+ # { id: ['one', 'two'] }
42
+ class <%= controller_class_name %>Authorizer < ApplicationAuthorizer
43
+ # All actions automatically validating the need of user logged.
44
+ # Skip this check for all actions:
45
+ # skip_authentication
46
+ # Or skip_authentication_only for some actions:
47
+ # skip_authentication_only :index, :new, :destroy, ...
48
+
49
+ <% unless options[:singleton] -%>
50
+ def index
51
+ # true
52
+ end
53
+ <% end -%>
54
+
55
+ def show
56
+ # true
57
+ # <%= singular_table_name.classify %>.where(user: authenticated).find(params[:id]).avaliable?
58
+ # { id: authenticated.<%= singular_table_name %>_ids }
59
+ end
60
+
61
+ def new
62
+ # true
63
+ end
64
+
65
+ def edit
66
+ # true
67
+ # <%= singular_table_name.classify %>.where(user: authenticated).find(params[:id]).avaliable?
68
+ # { id: authenticated.<%= singular_table_name %>_ids }
69
+ end
70
+
71
+ def create
72
+ # true
73
+ end
74
+
75
+ def update
76
+ # true
77
+ # <%= singular_table_name.classify %>.where(user: authenticated).find(params[:id]).avaliable?
78
+ # { id: authenticated.<%= singular_table_name %>_ids }
79
+ end
80
+
81
+ def destroy
82
+ # true
83
+ # <%= singular_table_name.classify %>.where(user: authenticated).find(params[:id]).avaliable?
84
+ # { id: authenticated.<%= singular_table_name %>_ids }
85
+ end
86
+
87
+ end
@@ -0,0 +1,134 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%= controller_class_name %>Authorizer, type: :authorizer do
4
+
5
+ # let(:guest_user) { nil }
6
+ # let(:one_user) { double('Authenticated', admin_group?: false, user_group?: true, <%= singular_table_name %>_ids: [1]) }
7
+ # let(:two_user) { double('Authenticated', admin_group?: false, user_group?: true, <%= singular_table_name %>_ids: [2]) }
8
+ # let(:admin_user) { double('Authenticated', admin_group?: true, user_group?: false) }
9
+
10
+ <% unless options[:singleton] -%>
11
+ # context '#index' do
12
+ # describe 'authorize' do
13
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :index)).to be_authorized }
14
+
15
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :index)).to be_authorized }
16
+
17
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :index)).to be_authorized }
18
+ # end
19
+
20
+ # describe 'not authorize' do
21
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :index)).to be_unauthorized }
22
+ # end
23
+ # end
24
+ <% end -%>
25
+
26
+ # context '#show' do
27
+ # describe 'authorize' do
28
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :show, id: 1)).to be_authorized }
29
+
30
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :show, id: 2)).to be_authorized }
31
+
32
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :show, id: 1)).to be_authorized }
33
+
34
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :show, id: 2)).to be_authorized }
35
+ # end
36
+
37
+ # describe 'not authorize' do
38
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :show, id: 1)).to be_unauthorized }
39
+
40
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :show, id: 2)).to be_unauthorized }
41
+
42
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :show, id: 1)).to be_unauthorized }
43
+ # end
44
+ # end
45
+
46
+ # context '#new' do
47
+ # describe 'authorize' do
48
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :new)).to be_authorized }
49
+
50
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :new)).to be_authorized }
51
+
52
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :new)).to be_authorized }
53
+ # end
54
+
55
+ # describe 'not authorize' do
56
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :new)).to be_unauthorized }
57
+ # end
58
+ # end
59
+
60
+ # context '#edit' do
61
+ # describe 'authorize' do
62
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :edit, id: 1)).to be_authorized }
63
+
64
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :edit, id: 2)).to be_authorized }
65
+
66
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :edit, id: 1)).to be_authorized }
67
+
68
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :edit, id: 2)).to be_authorized }
69
+ # end
70
+
71
+ # describe 'not authorize' do
72
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :edit, id: 1)).to be_unauthorized }
73
+
74
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :edit, id: 2)).to be_unauthorized }
75
+
76
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :edit, id: 1)).to be_unauthorized }
77
+ # end
78
+ # end
79
+
80
+ # context '#create' do
81
+ # describe 'authorize' do
82
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :create)).to be_authorized }
83
+
84
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :create)).to be_authorized }
85
+
86
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :create)).to be_authorized }
87
+ # end
88
+
89
+ # describe 'not authorize' do
90
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :create)).to be_unauthorized }
91
+ # end
92
+ # end
93
+
94
+ # context '#update' do
95
+ # describe 'authorize' do
96
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :update, id: 1)).to be_authorized }
97
+
98
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :update, id: 2)).to be_authorized }
99
+
100
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :update, id: 1)).to be_authorized }
101
+
102
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :update, id: 2)).to be_authorized }
103
+ # end
104
+
105
+ # describe 'not authorize' do
106
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :update, id: 1)).to be_unauthorized }
107
+
108
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :update, id: 2)).to be_unauthorized }
109
+
110
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :update, id: 1)).to be_unauthorized }
111
+ # end
112
+ # end
113
+
114
+ # context '#destroy' do
115
+ # describe 'authorize' do
116
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :destroy, id: 1)).to be_authorized }
117
+
118
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :destroy, id: 2)).to be_authorized }
119
+
120
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :destroy, id: 1)).to be_authorized }
121
+
122
+ # it { expect(<%= controller_class_name %>Authorizer.new(admin_user, :destroy, id: 2)).to be_authorized }
123
+ # end
124
+
125
+ # describe 'not authorize' do
126
+ # it { expect(<%= controller_class_name %>Authorizer.new(guest_user, :destroy, id: 1)).to be_unauthorized }
127
+
128
+ # it { expect(<%= controller_class_name %>Authorizer.new(one_user, :destroy, id: 2)).to be_unauthorized }
129
+
130
+ # it { expect(<%= controller_class_name %>Authorizer.new(two_user, :destroy, id: 1)).to be_unauthorized }
131
+ # end
132
+ # end
133
+
134
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate authorizer Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,31 @@
1
+ class ActionAuthorizer::InstallGenerator < Rails::Generators::Base # :nodoc:
2
+ desc 'Creates an initializer with default action_authorizer configuration and copy locale file'
3
+
4
+ def create_application_authorize_file
5
+ create_file 'app/authorizers/application_authorizer.rb', <<-RUBY
6
+ class ApplicationAuthorizer < ActionAuthorizer::Base
7
+ end
8
+ RUBY
9
+ end
10
+
11
+ def create_action_authorizer_helper_file
12
+ create_file 'app/helpers/action_authorizer_helper.rb', <<-RUBY
13
+ module ActionAuthorizerHelper
14
+ # Add helpers to check authorization authenticated.
15
+ # def unauthorized? controller, action, params = {}
16
+ # def authorized? controller, action, params = {}
17
+ # ex.:
18
+ # <%= link_to 'Models', models_path if authorized? :models, :index %>
19
+ # <%= link_to 'Models Dashboard', dashboard_models_path if authorized? 'dashborad/models', :index %>
20
+ # <%= link_to 'Model', model_path(@model) if authorized? :models, :show, id: @model.id %>
21
+ # <%= link_to 'Model', edit_model_path(@model) if authorized? :models, :edit, id: @model.to_param %>
22
+ include ActionAuthorizer::Helper
23
+
24
+ # def authenticated
25
+ # current_user
26
+ # end
27
+ end
28
+ RUBY
29
+ end
30
+
31
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :action_authorizer do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_authorizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Marcelo Junior
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ description: Rails authorization with controllers's actions.
28
+ email: juniormesquitadandao@gmail.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - MIT-LICENSE
34
+ - README.md
35
+ - lib/action_authorizer.rb
36
+ - lib/action_authorizer/base.rb
37
+ - lib/action_authorizer/config.rb
38
+ - lib/action_authorizer/helper.rb
39
+ - lib/action_authorizer/version.rb
40
+ - lib/generators/action_authorizer/authorizer/USAGE
41
+ - lib/generators/action_authorizer/authorizer/authorizer_generator.rb
42
+ - lib/generators/action_authorizer/authorizer/templates/authorizer.rb
43
+ - lib/generators/action_authorizer/authorizer/templates/authorizer_spec.rb
44
+ - lib/generators/action_authorizer/install/USAGE
45
+ - lib/generators/action_authorizer/install/install_generator.rb
46
+ - lib/tasks/action_authorizer_tasks.rake
47
+ homepage: https://github.com/juniormesquitadandao/action_authorizer
48
+ licenses:
49
+ - MIT
50
+ metadata: {}
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '1.9'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 2.4.8
68
+ signing_key:
69
+ specification_version: 4
70
+ summary: Action Authorizer
71
+ test_files: []