ostiary 0.8.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 +7 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +1 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +35 -0
- data/README.md +91 -0
- data/Rakefile +6 -0
- data/lib/ostiary.rb +10 -0
- data/lib/ostiary/controller_helper.rb +46 -0
- data/lib/ostiary/ostiary.rb +29 -0
- data/lib/ostiary/policy.rb +23 -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/ostiary.gemspec +35 -0
- metadata +109 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0f22bf44568c51e44e5421f8071c82e1e2762902
|
4
|
+
data.tar.gz: d24340aae92bf7c098a9f36b6e4bb7b303c61c20
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f3c5b9fbb513a981eb2ad5d564861198a25c31217896e3aa838b5d06f80228945b92ad05d33b2faee871772a6dc2940c23993c8658df77ec04914edbd821b11d
|
7
|
+
data.tar.gz: 22a550c52fd77060cf3f837d4340d3ede44dd5fb987b05e344f48dec5957201a18fd4bb0d177724776f9338fd451c4dd5c4e01a1fed18dd273240f8643fddc1c
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ostiary
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
ruby-2.2.5
|
data/.travis.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
language: ruby
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ostiary (0.6.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.2.5)
|
10
|
+
rake (10.5.0)
|
11
|
+
rspec (3.5.0)
|
12
|
+
rspec-core (~> 3.5.0)
|
13
|
+
rspec-expectations (~> 3.5.0)
|
14
|
+
rspec-mocks (~> 3.5.0)
|
15
|
+
rspec-core (3.5.4)
|
16
|
+
rspec-support (~> 3.5.0)
|
17
|
+
rspec-expectations (3.5.0)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.5.0)
|
20
|
+
rspec-mocks (3.5.0)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.5.0)
|
23
|
+
rspec-support (3.5.0)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.13)
|
30
|
+
ostiary!
|
31
|
+
rake (~> 10.0)
|
32
|
+
rspec (~> 3.0)
|
33
|
+
|
34
|
+
BUNDLED WITH
|
35
|
+
1.13.7
|
data/README.md
ADDED
@@ -0,0 +1,91 @@
|
|
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://travis-ci.com/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
|
+
|
data/Rakefile
ADDED
data/lib/ostiary.rb
ADDED
@@ -0,0 +1,46 @@
|
|
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
|
+
#
|
32
|
+
# One line creates one policy, which are immediately created with the proper class
|
33
|
+
def ostiary_policy(role, actions = {})
|
34
|
+
if actions.empty?
|
35
|
+
self.ostiary.policies << Policy.new(role)
|
36
|
+
elsif actions.has_key?(:only)
|
37
|
+
self.ostiary.policies << PolicyLimited.new(role, actions[:only])
|
38
|
+
elsif actions.has_key?(:except)
|
39
|
+
self.ostiary.policies << PolicyExempted.new(role, actions[:except])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Ostiary
|
2
|
+
class Ostiary
|
3
|
+
attr_accessor :policies
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@policies = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def authorize!(action)
|
10
|
+
policies.each do |policy|
|
11
|
+
next if policy_met?(policy, action, &Proc.new)
|
12
|
+
raise PolicyBroken, policy.error_message(action)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def authorized?(action)
|
17
|
+
policies.all? do |policy|
|
18
|
+
policy_met?(policy, action, &Proc.new)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def policy_met?(policy, action)
|
25
|
+
policy.met?(action) { yield(policy.name) }
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Ostiary
|
2
|
+
class Policy
|
3
|
+
attr_accessor :name, :rules
|
4
|
+
|
5
|
+
def initialize(name, rules = [])
|
6
|
+
@name = name
|
7
|
+
@rules = rules || []
|
8
|
+
end
|
9
|
+
|
10
|
+
def inspect
|
11
|
+
"#{name}"
|
12
|
+
end
|
13
|
+
|
14
|
+
def met?(*)
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
|
18
|
+
def error_message(action)
|
19
|
+
"#{action} requires #{name}"
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Ostiary
|
2
|
+
class PolicyExempted < Policy
|
3
|
+
|
4
|
+
def inspect
|
5
|
+
"#{name} except for #{rules.to_sentence}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def met?(action)
|
9
|
+
return true if rules.include?(action)
|
10
|
+
yield
|
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 #{rules.to_sentence}"
|
6
|
+
end
|
7
|
+
|
8
|
+
def met?(action)
|
9
|
+
return true if !rules.include?(action)
|
10
|
+
yield
|
11
|
+
end
|
12
|
+
|
13
|
+
def error_message(action)
|
14
|
+
"#{action} limited by #{name}"
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
data/ostiary.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ostiary/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ostiary"
|
8
|
+
spec.version = Ostiary::VERSION
|
9
|
+
spec.author = "Jacques Hakvoort"
|
10
|
+
spec.authors = ["Jacques Hakvoort"]
|
11
|
+
spec.email = ["jacques.hakvoort@nedap.com"]
|
12
|
+
|
13
|
+
spec.homepage = "https://github.com/nedap/ostiary"
|
14
|
+
spec.summary = "Limit access to controllers/actions with policies"
|
15
|
+
spec.description = <<-TXT;
|
16
|
+
from wikipedia: "An ostiarius, a Latin word sometimes anglicized as ostiary
|
17
|
+
but often literally translated as porter or doorman, originally was a
|
18
|
+
servant or guard posted at the entrance of a building. See also gatekeeper."
|
19
|
+
Ostiary is a security gem for your controllers & actions.
|
20
|
+
It employs a before_filter-like call to set policies per controller/action.
|
21
|
+
You can pass your own security call in block and handle the PolicyBroken
|
22
|
+
yourself.
|
23
|
+
Policies are also inherited from parent classes.
|
24
|
+
TXT
|
25
|
+
spec.license = 'MIT'
|
26
|
+
|
27
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
28
|
+
f.match(%r{^(test|spec|features)/})
|
29
|
+
end
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ostiary
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.8.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacques Hakvoort
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: |2
|
56
|
+
from wikipedia: "An ostiarius, a Latin word sometimes anglicized as ostiary
|
57
|
+
but often literally translated as porter or doorman, originally was a
|
58
|
+
servant or guard posted at the entrance of a building. See also gatekeeper."
|
59
|
+
Ostiary is a security gem for your controllers & actions.
|
60
|
+
It employs a before_filter-like call to set policies per controller/action.
|
61
|
+
You can pass your own security call in block and handle the PolicyBroken
|
62
|
+
yourself.
|
63
|
+
Policies are also inherited from parent classes.
|
64
|
+
email:
|
65
|
+
- jacques.hakvoort@nedap.com
|
66
|
+
executables: []
|
67
|
+
extensions: []
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- ".ruby-gemset"
|
71
|
+
- ".ruby-version"
|
72
|
+
- ".travis.yml"
|
73
|
+
- Gemfile
|
74
|
+
- Gemfile.lock
|
75
|
+
- README.md
|
76
|
+
- Rakefile
|
77
|
+
- lib/ostiary.rb
|
78
|
+
- lib/ostiary/controller_helper.rb
|
79
|
+
- lib/ostiary/ostiary.rb
|
80
|
+
- lib/ostiary/policy.rb
|
81
|
+
- lib/ostiary/policy_exempted.rb
|
82
|
+
- lib/ostiary/policy_limited.rb
|
83
|
+
- lib/ostiary/version.rb
|
84
|
+
- ostiary.gemspec
|
85
|
+
homepage: https://github.com/nedap/ostiary
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
metadata: {}
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
requirements: []
|
104
|
+
rubyforge_project:
|
105
|
+
rubygems_version: 2.4.8
|
106
|
+
signing_key:
|
107
|
+
specification_version: 4
|
108
|
+
summary: Limit access to controllers/actions with policies
|
109
|
+
test_files: []
|