ostiary 0.14.0 → 0.16.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 +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +94 -0
- data/lib/ostiary/controller_helper.rb +52 -0
- data/lib/ostiary/ostiary.rb +23 -0
- data/lib/ostiary/policy.rb +25 -0
- data/lib/ostiary/policy_exempted.rb +18 -0
- data/lib/ostiary/policy_limited.rb +18 -0
- data/lib/ostiary/version.rb +3 -0
- data/lib/ostiary.rb +10 -0
- metadata +12 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 141a30a0a8c0e30ce90d58c1c7e0a4bc1862392b3d40cffb7aee8b5e02063590
|
4
|
+
data.tar.gz: d575cddd304e6c37791925158e8ccd4064a752fc5b329f7c7d83bb619aef98e6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe7e8743a82157914d01cb69952ede6ef7aca2e1635f272f2d0ad42a1629372bd673adbe38ef2bd4854f364ddd8c7db2ebe3bda2fb70b598d5f4c44b788b2c7e
|
7
|
+
data.tar.gz: 5eb7cf2ce6f07b7c14f08c91926e4d5c9cec6b5df88bfee70eeb02ce43279e0ca3934ec8c140ca3e4d62600a43433ed1ad2653a7e9081689b845fc05f2eb4493
|
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
|
+
[](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
|
data/lib/ostiary.rb
ADDED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ostiary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.16.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacques Hakvoort
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -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
|