ostiary 0.14.0 → 0.15.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
  SHA256:
3
- metadata.gz: '0594884e87d985f4f25c92d557de11a346ce8a7bb53aad64c1c47ad3183b901b'
4
- data.tar.gz: d8ba5b7ba7456ae202f00f8cf6a6a1fc02fb2348dbf5e22721b8379c8d3ea0e2
3
+ metadata.gz: 06dd41598d092805491bf1002d20b09e64794171c9e5c1544cbe9602d9ce7cd8
4
+ data.tar.gz: fd650ed79a11cd624500bb3e3f6d4b0d21fc87f80a09de630cc06b535f9e765b
5
5
  SHA512:
6
- metadata.gz: 3fff7e6700a4354a458de759c89fb36ceb70e335231211f0e0f942c64faa54b8fce84cbe3546e061d96fc23300c830eb467f8f6994f6ebab56bd293558a5169f
7
- data.tar.gz: b6423e7a2837470cd9c548e2d40aa3e026b4cd2599e263f6e63f142d33faefddde95771d6e41c5112059f58b255491aeba799c4244e1570f08b0e225e5e8f394
6
+ metadata.gz: 88c015f225cb267ab1b52d2732e78125d9e9fc3c04f4ae5cedf1704dc55d8da7f4867dba3df03d4783ff993f5fa6b15ec4889d1be47b673c3d67c4a7f58c6b7e
7
+ data.tar.gz: ea35d66e93f5b9da36b5d4bf88d9bc2ccb8820e5a629f2908217c27d5d0f350e8a7a9704446ca9918184c6cc6cf03cf5647b75e79aaf9744994288987cc8a289
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Nedap
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Ostiary
2
+
3
+ An ostiarius, a Latin word sometimes anglicized as ostiary but often literally translated as porter or doorman, originally was a servant or guard posted at the entrance of a building. See also gatekeeper.
4
+
5
+ ## Functionality
6
+
7
+ [![CircleCI Status](https://circleci.com/gh/nedap/ostiary.svg?style=svg)](https://circleci.com/gh/nedap/ostiary)
8
+
9
+ This gem will help you enforce 'policies' when viewing controllers/actions.
10
+ This is done by requiring certain roles for controllers, where you can
11
+ optionally include or exclude certain actions.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'ostiary'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install ostiary
28
+
29
+ ## Usage
30
+
31
+ ### Primary setup
32
+
33
+ In your base Controller class do the following for Rails:
34
+
35
+ ```ruby
36
+ # This class creates a class accessor called :ostiary on each (inherited) controller.
37
+ # With each controller created, it will stack the policies you add
38
+ include Ostiary::ControllerHelper
39
+
40
+ before_filter :ensure_authorized!
41
+
42
+ # Because each ostiary is unique for a controller, you only have to supply the current action.
43
+ # With this, it can check if there are certain policies that will be broken.
44
+ def ensure_authorized!
45
+ self.class.ostiary.authorize!(action) do |name|
46
+ # Your authorization method using name.
47
+ # e.g. `current_user.has_right?(name)`
48
+ end
49
+ rescue Ostiary::PolicyBroken => error
50
+ # We re-raise the Error as a RoutingError in Rails
51
+ # You can also do `return head :forbidden` if that's more in line with your needs.
52
+ raise ActionController::RoutingError.new(error.message)
53
+ end
54
+ ```
55
+
56
+ ### Securing controllers
57
+
58
+ In each controller you wish to secure, you can call `ostiary_policy`, just like `before_filter` & `after_filter` of Rails.
59
+
60
+ ```ruby
61
+ # Require the :list role on the entire controller
62
+ ostiary_policy :list
63
+
64
+ # Require the :view role only on the index & show actions
65
+ ostiary_policy :view, only: [:index, :show]
66
+
67
+ # Require the :edit role except on the index & show actions
68
+ ostiary_policy :edit, except: [:index, :show]
69
+ ```
70
+
71
+ These policies will be added to the ostiary instance created for each Controller Class. It will also include each policy inherited from parent classes.
72
+
73
+ ### Checking for a right
74
+
75
+ You can also ask if a user is authorized to access to a certain path (url).
76
+
77
+ in your Controller:
78
+
79
+ ```ruby
80
+ def authorized?(path)
81
+ # recognize_path is a Rails Routing helper that will return a hash with the controller
82
+ # and action of the path you supplied. We'll have to transform that String of the
83
+ # controller into an actual Class.
84
+ return false unless route = Rails.application.routes.recognize_path(path)
85
+ requested_controller = "#{route[:controller]}_controller".camelize.constantize
86
+ requested_controller.ostiary.authorized?(route[:action]) do |role|
87
+ # Your authorization method using name.
88
+ end
89
+ end
90
+ ```
91
+
92
+ ## License
93
+
94
+ ostiary is Copyright 2017 nedap and released under the MIT license which you should find included in the [LICENSE.txt](LICENSE.txt) file.
@@ -0,0 +1,52 @@
1
+ module Ostiary
2
+ module ControllerHelper
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def inherited(subclass)
11
+ subclass.ostiary.policies += self.ostiary.policies
12
+ super
13
+ end
14
+
15
+ def ostiary
16
+ @ostiary ||= Ostiary.new
17
+ end
18
+
19
+ def required_application_role(role, actions = {})
20
+ warn "[DEPRECATION] `required_application_role` is deprecated. Please use `ostiary_policy` instead."
21
+ ostiary_policy(role, actions)
22
+ end
23
+
24
+ # We want to give the option of setting a policy for one action, based on ::Role
25
+ # So we'll copy the way the filters in controllers work.
26
+ # Only apply the role to certain action(s)
27
+ # only: [*actions]
28
+ # Exclude action(s) from requiring a role
29
+ # except: [*actions]
30
+ # By default a given role will be required for every action
31
+ # Override role checking by passing a symbol as method;
32
+ # ostiary_policy method: :master?, only: :show
33
+ # One line creates one policy, which are immediately created with the proper class
34
+ def ostiary_policy(role = nil, only: nil, except: nil, method: nil)
35
+ raise ArgumentError, "Use at least role or method" unless method || role
36
+ raise ArgumentError, "Use either role or method" if method && role
37
+ raise ArgumentError, "Use either only or except" if except && only
38
+ raise ArgumentError, "Use a symbol for method:" if method && !(method.is_a? Symbol)
39
+
40
+ if only
41
+ ostiary.policies << PolicyLimited.new(role, only, method: method&.to_proc)
42
+ elsif except
43
+ ostiary.policies << PolicyExempted.new(role, except, method: method&.to_proc)
44
+ else
45
+ ostiary.policies << Policy.new(role, method: method&.to_proc)
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,23 @@
1
+ module Ostiary
2
+ class Ostiary
3
+ attr_accessor :policies
4
+
5
+ def initialize
6
+ @policies = []
7
+ end
8
+
9
+ def authorize!(action, &block)
10
+ policies.each do |policy|
11
+ next if policy.met?(action, &block)
12
+ raise PolicyBroken, policy.error_message(action)
13
+ end
14
+ end
15
+
16
+ def authorized?(action, &block)
17
+ policies.all? do |policy|
18
+ policy.met?(action, block)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,25 @@
1
+ module Ostiary
2
+ class Policy
3
+ attr_reader :name, :method, :actions
4
+
5
+ def initialize(name, actions = [], method: nil)
6
+ @name = name
7
+ @method = method
8
+ @actions = actions
9
+ end
10
+
11
+ def inspect
12
+ "#{name}"
13
+ end
14
+
15
+ def met?(_action, &block)
16
+ return yield name unless method
17
+ method.call
18
+ end
19
+
20
+ def error_message(action)
21
+ "#{action} requires #{name}"
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,18 @@
1
+ module Ostiary
2
+ class PolicyExempted < Policy
3
+
4
+ def inspect
5
+ "#{name} except for #{actions.to_sentence}"
6
+ end
7
+
8
+ def met?(action, &block)
9
+ return true if actions.include?(action)
10
+ super
11
+ end
12
+
13
+ def error_message(action)
14
+ "#{action} not exempted for #{name}"
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ module Ostiary
2
+ class PolicyLimited < Policy
3
+
4
+ def inspect
5
+ "#{name} only for #{actions.to_sentence}"
6
+ end
7
+
8
+ def met?(action, &block)
9
+ return true unless actions.include?(action)
10
+ super
11
+ end
12
+
13
+ def error_message(action)
14
+ "#{action} limited by #{name}"
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Ostiary
2
+ VERSION = "0.15.0"
3
+ end
data/lib/ostiary.rb ADDED
@@ -0,0 +1,10 @@
1
+ require "ostiary/version"
2
+ require "ostiary/ostiary"
3
+ require "ostiary/policy"
4
+ require "ostiary/policy_limited"
5
+ require "ostiary/policy_exempted"
6
+ require "ostiary/controller_helper"
7
+
8
+ module Ostiary
9
+ class PolicyBroken < StandardError; end
10
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ostiary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacques Hakvoort
@@ -80,7 +80,16 @@ email:
80
80
  executables: []
81
81
  extensions: []
82
82
  extra_rdoc_files: []
83
- files: []
83
+ files:
84
+ - LICENSE.txt
85
+ - README.md
86
+ - lib/ostiary.rb
87
+ - lib/ostiary/controller_helper.rb
88
+ - lib/ostiary/ostiary.rb
89
+ - lib/ostiary/policy.rb
90
+ - lib/ostiary/policy_exempted.rb
91
+ - lib/ostiary/policy_limited.rb
92
+ - lib/ostiary/version.rb
84
93
  homepage: https://github.com/nedap/ostiary
85
94
  licenses:
86
95
  - MIT