rails-mongoid-gatekeeper 0.1.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/MIT-LICENSE +20 -0
- data/README.md +73 -0
- data/Rakefile +29 -0
- data/app/controllers/concerns/gatekeeper/respondable.rb +107 -0
- data/app/controllers/gatekeeper_controller.rb +26 -0
- data/app/models/gatekeeper/embedded_model.rb +15 -0
- data/app/models/gatekeeper/model.rb +28 -0
- data/app/models/gatekeeper/related_model.rb +15 -0
- data/app/models/gatekeeper/user.rb +12 -0
- data/app/views/gatekeeper/create.js.erb +0 -0
- data/app/views/gatekeeper/destroy.js.erb +0 -0
- data/app/views/gatekeeper/edit.html.erb +0 -0
- data/app/views/gatekeeper/edit.js.erb +0 -0
- data/app/views/gatekeeper/index.html.erb +0 -0
- data/app/views/gatekeeper/index.js.erb +0 -0
- data/app/views/gatekeeper/new.html.erb +0 -0
- data/app/views/gatekeeper/new.js.erb +0 -0
- data/app/views/gatekeeper/show.html.erb +0 -0
- data/app/views/gatekeeper/show.js.erb +0 -0
- data/app/views/gatekeeper/update.js.erb +0 -0
- data/config/initializers/disable_auto_render.rb +8 -0
- data/config/initializers/mongoid/accessibility.rb +95 -0
- data/config/initializers/mongoid/document.rb +57 -0
- data/config/initializers/rainbow.rb +25 -0
- data/config/routes.rb +2 -0
- data/lib/gatekeeper/configuration.rb +15 -0
- data/lib/gatekeeper/engine.rb +8 -0
- data/lib/gatekeeper/version.rb +3 -0
- data/lib/gatekeeper.rb +66 -0
- data/lib/generators/gatekeeper/config_generator.rb +18 -0
- data/lib/generators/templates/gatekeeper.rb +26 -0
- data/lib/tasks/gatekeeper_tasks.rake +4 -0
- metadata +134 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 39a19fffe4acdc5dcb94739544c383afb5cee7105b57c5ab212e3002685f7f25
|
4
|
+
data.tar.gz: 841ac8420ce6f81cb6d9f841e87c0429963f8de3da42e41e0c0b6cf163913088
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 29bc6c8b3580a431c36fd3d83a2af3e1d540c53bad63d39c2e0e41705c0a1a103b7fa8f03814a9d017704340a6fb650ceb1cdfd2028d3748b6dcf75c212aca74
|
7
|
+
data.tar.gz: cc812465b5effcc6fa6121df2620a72073d345cb30c6aaf906e28146212142eb5eef40e117f90e71e7c006b0b160b9e7d962f617067e0a67fab3d72cc1c557ed
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2019 Francesco Ballardin
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
# Gatekeeper
|
2
|
+
Gatekeeper is a Rails engine for MongoDB which adds two simple functionalities:
|
3
|
+
|
4
|
+
* Model methods to control which informations can be seen by a specific user.
|
5
|
+
* Controller concern to handle HTML, JS and JSON responses.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'gatekeeper'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
### Models
|
22
|
+
The first basic use is to define a model for your application and the information that can be accessed.
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
# app/models/book.rb
|
26
|
+
class Book
|
27
|
+
|
28
|
+
include Mongoid::Document
|
29
|
+
|
30
|
+
field :name, type: String
|
31
|
+
field :internal_id, type: Integer
|
32
|
+
|
33
|
+
allowed_info do |user|
|
34
|
+
case user.role
|
35
|
+
when :librarian
|
36
|
+
[ :name, :internal_id ]
|
37
|
+
when :customer
|
38
|
+
[ :name ]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
# app/models/user.rb
|
47
|
+
class User
|
48
|
+
|
49
|
+
include Mongoid::Document
|
50
|
+
|
51
|
+
field :name, type: String
|
52
|
+
field :role, type: Symbol
|
53
|
+
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
When accessing the model info:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
book = Book.new(name: 'Lord of the Rings', internal_id: 1234567)
|
61
|
+
librarian = User.new(name: 'Tony', role: :librarian)
|
62
|
+
customer = User.new(name: 'Bob', role: :customer)
|
63
|
+
|
64
|
+
book.info # { :name => "Lord of the Rings", :internal_id => 1234567 }
|
65
|
+
book.info(librarian) # { :name => "Lord of the Rings", :internal_id => 1234567 }
|
66
|
+
book.info(customer) # { :name => "Lord of the Rings" }
|
67
|
+
```
|
68
|
+
|
69
|
+
### Controllers
|
70
|
+
On controllers, you can include `Gatekeeper::Respondable` to generate automatic responses for your HTML, JS, or JSON views. These responses contains information based on the `allowed_info` method specified in your models.
|
71
|
+
|
72
|
+
## License
|
73
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'Gatekeeper'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
load 'rails/tasks/statistics.rake'
|
18
|
+
|
19
|
+
require 'bundler/gem_tasks'
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << 'test'
|
25
|
+
t.pattern = 'test/**/*_test.rb'
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module Gatekeeper
|
2
|
+
module Respondable
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
|
8
|
+
respond_to :html, :js, :json
|
9
|
+
|
10
|
+
before_action :set_current_user
|
11
|
+
after_action :handle_response
|
12
|
+
|
13
|
+
##
|
14
|
+
# Handles response based on request format.
|
15
|
+
def handle_response
|
16
|
+
fatto = performed?
|
17
|
+
unless performed?
|
18
|
+
if @error.present?
|
19
|
+
handle_error(@error)
|
20
|
+
else
|
21
|
+
respond_with do |format|
|
22
|
+
# Browser scope.
|
23
|
+
format.html do
|
24
|
+
handle_response_html
|
25
|
+
end
|
26
|
+
# Rails remote form.
|
27
|
+
format.js do
|
28
|
+
handle_response_js
|
29
|
+
end
|
30
|
+
# API / xhr scope.
|
31
|
+
format.json do
|
32
|
+
handle_response_json
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Handles any error case.
|
41
|
+
def handle_error(error)
|
42
|
+
# Can be overridden
|
43
|
+
render plain: error.inspect, status: 500
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# Sets current user.
|
48
|
+
def set_current_user
|
49
|
+
# Can be overridden
|
50
|
+
@current_user = Gatekeeper::User.new
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
##
|
56
|
+
# Handles HTML response.
|
57
|
+
def handle_response_html
|
58
|
+
if @errors.present? and action_name.in? [ 'create', 'update' ]
|
59
|
+
case action_name
|
60
|
+
when 'create' then render 'new.html'
|
61
|
+
when 'update' then render 'edit.html'
|
62
|
+
end
|
63
|
+
else
|
64
|
+
if @redirect_to.present?
|
65
|
+
redirect_to @redirect_to
|
66
|
+
else
|
67
|
+
case action_name
|
68
|
+
when 'create', 'update' then redirect_to(action: 'index')
|
69
|
+
when 'destroy' then redirect_to(action: 'index')
|
70
|
+
else render "#{action_name}.html" end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
##
|
76
|
+
# Handles JS response.
|
77
|
+
def handle_response_js
|
78
|
+
render "#{action_name}.js"
|
79
|
+
end
|
80
|
+
|
81
|
+
##
|
82
|
+
# Gestisce il tipo di risposta JSON.
|
83
|
+
def handle_response_json
|
84
|
+
assigns = {}
|
85
|
+
if instance_variables.include? :@errors
|
86
|
+
assigned_variables = [ :@errors ]
|
87
|
+
else
|
88
|
+
assigned_variables = instance_variables.reject do |variable|
|
89
|
+
variable.to_s.starts_with?('@_') or variable.in? Gatekeeper.response_ignored_variables
|
90
|
+
end
|
91
|
+
end
|
92
|
+
assigned_variables.each do |variable|
|
93
|
+
variable_value = instance_variable_get(variable)
|
94
|
+
assigns[variable.to_s.gsub('@', '').camelize(:lower)] =
|
95
|
+
if variable_value.respond_to? :info
|
96
|
+
variable_value.info(@current_user, keys: :camelized)
|
97
|
+
else
|
98
|
+
variable_value.as_json
|
99
|
+
end
|
100
|
+
end
|
101
|
+
render json: assigns
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class GatekeeperController < ApplicationController
|
2
|
+
|
3
|
+
include Gatekeeper::Respondable
|
4
|
+
|
5
|
+
def index
|
6
|
+
end
|
7
|
+
|
8
|
+
def show
|
9
|
+
end
|
10
|
+
|
11
|
+
def new
|
12
|
+
end
|
13
|
+
|
14
|
+
def create
|
15
|
+
end
|
16
|
+
|
17
|
+
def edit
|
18
|
+
end
|
19
|
+
|
20
|
+
def update
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
##
|
2
|
+
# This is an example embedded model used for testing purposes.
|
3
|
+
# It represents a real world model with various fields and methods.
|
4
|
+
|
5
|
+
module Gatekeeper
|
6
|
+
class EmbeddedModel
|
7
|
+
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :string_field, type: String
|
11
|
+
|
12
|
+
embedded_in :model, class_name: 'Gatekeeper::Model'
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
##
|
2
|
+
# This is an example model used for testing purposes.
|
3
|
+
# It represents a real world model with various fields and methods.
|
4
|
+
|
5
|
+
module Gatekeeper
|
6
|
+
class Model
|
7
|
+
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :string_field, type: String
|
11
|
+
field :number_field, type: Float
|
12
|
+
field :date_field, type: Date
|
13
|
+
|
14
|
+
embeds_many :embedded_models, class_name: 'Gatekeeper::EmbeddedModel'
|
15
|
+
|
16
|
+
has_many :related_models, class_name: 'Gatekeeper::RelatedModel'
|
17
|
+
|
18
|
+
##
|
19
|
+
# Allowed info.
|
20
|
+
allowed_info do |user|
|
21
|
+
case user.role
|
22
|
+
when :editor
|
23
|
+
document_fields
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
##
|
2
|
+
# This is an example related model used for testing purposes.
|
3
|
+
# It represents a real world model with various fields and methods.
|
4
|
+
|
5
|
+
module Gatekeeper
|
6
|
+
class RelatedModel
|
7
|
+
|
8
|
+
include Mongoid::Document
|
9
|
+
|
10
|
+
field :string_field, type: String
|
11
|
+
|
12
|
+
belongs_to :model, class_name: 'Gatekeeper::Model'
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Document
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
##
|
11
|
+
# Defines a user's accessible info.
|
12
|
+
def allowed_info(&block)
|
13
|
+
define_singleton_method :allowed_info_names do |user|
|
14
|
+
Gatekeeper.default_allowed_info_names(user, self, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# Returns fields, embeds and relations based on current user's allowed info.
|
22
|
+
def info(current_user = nil, options = {})
|
23
|
+
if current_user.present?
|
24
|
+
if self.class.respond_to? :allowed_info_names
|
25
|
+
exposed_info = self.class.allowed_info_names(current_user)
|
26
|
+
else
|
27
|
+
exposed_info = Gatekeeper.default_allowed_info_names(current_user, self.class)
|
28
|
+
end
|
29
|
+
else
|
30
|
+
exposed_info = document_fields | document_embeds
|
31
|
+
end
|
32
|
+
|
33
|
+
output = {}
|
34
|
+
# Maps id
|
35
|
+
output.store :id, self.id.to_s
|
36
|
+
# Maps all fields
|
37
|
+
(self.class.document_fields & exposed_info).each do |field_name|
|
38
|
+
output.store field_name, self.send(field_name)
|
39
|
+
end
|
40
|
+
# Maps embedded relations
|
41
|
+
(self.class.document_embeds & exposed_info).each do |embed_name|
|
42
|
+
embed = self.send(embed_name)
|
43
|
+
if embed.is_a? Array
|
44
|
+
output[embed_name] = embed.map do |embedded_document|
|
45
|
+
embedded_document.info(current_user)
|
46
|
+
end
|
47
|
+
else
|
48
|
+
output[embed_name] = embed.info(current_user)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
# Maps standard relations (only if included in the options)
|
52
|
+
self.class.document_relations & ([ options[:include] ].compact.flatten).each do |relation_name|
|
53
|
+
relation = self.send(relation_name)
|
54
|
+
if relation.is_a? Mongoid::Association::Referenced::HasMany::Targets::Enumerable
|
55
|
+
output[relation_name] = relation.map do |related_document|
|
56
|
+
related_document.info(current_user)
|
57
|
+
end
|
58
|
+
else
|
59
|
+
output[relation_name] = relation.info(current_user)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Transforms output keys
|
64
|
+
if options[:keys].present?
|
65
|
+
case options[:keys]
|
66
|
+
when :camelized
|
67
|
+
output.transform_keys! do |key|
|
68
|
+
key.to_s.camelize(:lower).to_sym
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
output
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
class Criteria
|
79
|
+
|
80
|
+
##
|
81
|
+
# Returns allowed info for current user.
|
82
|
+
def info(current_user, options = {})
|
83
|
+
if @included_relations.present? and @included_relations.any?
|
84
|
+
self.map do |document|
|
85
|
+
document.info(current_user, options.merge(include: @included_relations))
|
86
|
+
end
|
87
|
+
else
|
88
|
+
self.map do |document|
|
89
|
+
document.info(current_user, options)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Document
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
|
10
|
+
##
|
11
|
+
# Returns all 'fields' of a document.
|
12
|
+
def document_fields
|
13
|
+
self.fields.keys
|
14
|
+
.reject { |field| field.ends_with?('_id') or field.ends_with?('_ids') }
|
15
|
+
.map(&:to_sym)
|
16
|
+
end
|
17
|
+
|
18
|
+
##
|
19
|
+
# Returns all 'embedded' relation names of a document.
|
20
|
+
def document_embeds
|
21
|
+
self.relations.keys
|
22
|
+
.select { |relation_name| self.relations[relation_name].embedded? }
|
23
|
+
.reject { |relation_name| self.relations[relation_name].is_a? Mongoid::Association::Embedded::EmbeddedIn }
|
24
|
+
.map(&:to_sym)
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Returns all 'standard' relation names of a document.
|
29
|
+
def document_relations
|
30
|
+
self.relations.keys
|
31
|
+
.reject { |relation_name| self.relations[relation_name].embedded? }
|
32
|
+
.map(&:to_sym)
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Returns all document info.
|
37
|
+
def document_all_info
|
38
|
+
document_fields | document_embeds | document_relations
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
class Criteria
|
46
|
+
|
47
|
+
attr_accessor :included_relations
|
48
|
+
|
49
|
+
##
|
50
|
+
# Stores included relations.
|
51
|
+
def includes(*relations)
|
52
|
+
@included_relations = relations
|
53
|
+
super
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
##
|
2
|
+
# Crea un metodo per ogni colore da poter chiamare su un'istanza di una stringa.
|
3
|
+
|
4
|
+
class String
|
5
|
+
|
6
|
+
%i( aliceblue antiquewhite aqua aquamarine azure beige bisque blanchedalmond blue blueviolet brown burlywood
|
7
|
+
cadetblue chartreuse chocolate coral cornflower cornsilk crimson cyan darkblue darkcyan darkgoldenrod
|
8
|
+
darkgray darkgreen darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon
|
9
|
+
darkseagreen darkslateblue darkslategray darkturquoise darkviolet deeppink deepskyblue dimgray
|
10
|
+
dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray
|
11
|
+
green greenyellow honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen
|
12
|
+
lemonchiffon lightblue lightcoral lightcyan lightgoldenrod lightgray lightgreen lightpink lightsalmon
|
13
|
+
lightseagreen lightskyblue lightslategray lightsteelblue lightyellow lime limegreen linen magenta maroon
|
14
|
+
mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen
|
15
|
+
mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navyblue oldlace
|
16
|
+
olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip
|
17
|
+
peachpuff peru pink plum powderblue purple rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown
|
18
|
+
seagreen seashell sienna silver skyblue slateblue slategray snow springgreen steelblue tan teal
|
19
|
+
thistle tomato turquoise violet webgray webgreen webmaroon webpurple wheat whitesmoke yellow yellowgreen ).each do |color|
|
20
|
+
self.define_method color do
|
21
|
+
Rainbow(self).send(:color, color)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
data/config/routes.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Gatekeeper
|
2
|
+
class Configuration
|
3
|
+
|
4
|
+
# @return [Proc] a block to return bypess all info allowances (ex. for an admin user).
|
5
|
+
attr_accessor :bypass_allowed_info
|
6
|
+
# @return [Array<Symbol>] a list of ignored variables in controller response.
|
7
|
+
attr_accessor :response_ignored_variables
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@bypass_allowed_info = nil
|
11
|
+
@response_ignored_variables = []
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
data/lib/gatekeeper.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require "gatekeeper/engine"
|
2
|
+
require "gatekeeper/configuration"
|
3
|
+
|
4
|
+
module Gatekeeper
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
# @return [Gatekeeper::Configuration] the configuration class for Gatekeeper.
|
9
|
+
attr_accessor :configuration
|
10
|
+
|
11
|
+
##
|
12
|
+
# Initializes configuration.
|
13
|
+
#
|
14
|
+
# @return [Gatekeeper::Configuration] the configuration class for Gatekeeper.
|
15
|
+
def configuration
|
16
|
+
@configuration || Gatekeeper::Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Method to configure various Gatekeeper options.
|
21
|
+
#
|
22
|
+
# @return [nil]
|
23
|
+
def configure
|
24
|
+
@configuration ||= Gatekeeper::Configuration.new
|
25
|
+
yield @configuration
|
26
|
+
end
|
27
|
+
|
28
|
+
##
|
29
|
+
# Returns default allowed info names for a user.
|
30
|
+
#
|
31
|
+
# @param [Object] the user.
|
32
|
+
# @param [Class] the model class.
|
33
|
+
#
|
34
|
+
# @return [Array<Symbol>] default allowed info names.
|
35
|
+
def default_allowed_info_names(user, model_class, &block)
|
36
|
+
if @configuration.bypass_allowed_info.respond_to? :call
|
37
|
+
bypassable = @configuration.bypass_allowed_info.call(user)
|
38
|
+
else
|
39
|
+
bypassable = false
|
40
|
+
end
|
41
|
+
|
42
|
+
if bypassable
|
43
|
+
# Sees everything!
|
44
|
+
model_class.document_all_info
|
45
|
+
else
|
46
|
+
if block_given?
|
47
|
+
block.call(user) || []
|
48
|
+
else
|
49
|
+
[]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# Variables to not include in controller response.
|
56
|
+
#
|
57
|
+
# @return [Array<Symbol>] ignored variables.
|
58
|
+
def response_ignored_variables
|
59
|
+
Gatekeeper.configuration.response_ignored_variables | [
|
60
|
+
:@marked_for_same_origin_verification, :@browser,
|
61
|
+
:@behavior, :@options, :@args, :@shell, :@destination_stack ]
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module Gatekeeper
|
4
|
+
module Generators
|
5
|
+
|
6
|
+
class ConfigGenerator < Rails::Generators::Base
|
7
|
+
source_root File.expand_path("../../templates", __FILE__)
|
8
|
+
|
9
|
+
desc "Creates a gatekeeper configuration file."
|
10
|
+
|
11
|
+
def copy_config
|
12
|
+
template "gatekeeper.rb", "config/initializers/gatekeeper.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
##
|
2
|
+
# Example configuration file for rails-gatekeeper gem.
|
3
|
+
Gatekeeper.configure do |config|
|
4
|
+
|
5
|
+
##
|
6
|
+
# Determines which user can bypass all allowed info, and
|
7
|
+
# see everything of a specified model. Usually this applies
|
8
|
+
# to admin users.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
# config.bypass_allowed_info = proc do |user|
|
12
|
+
# user.is_admin?
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# Defaults to nil, which is equal to no bypassable users.
|
16
|
+
#
|
17
|
+
# config.bypass_allowed_info = nil
|
18
|
+
|
19
|
+
##
|
20
|
+
# Sets which controller instance variables to not include in the response.
|
21
|
+
#
|
22
|
+
# Defaults to empty array.
|
23
|
+
#
|
24
|
+
# config.response_ignored_variables = []
|
25
|
+
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rails-mongoid-gatekeeper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Francesco Ballardin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-11 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: 5.2.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mongoid
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 7.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 7.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bson_ext
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: responders
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Provides various access control methods to models and controllers. To
|
70
|
+
use with MongoDB.
|
71
|
+
email:
|
72
|
+
- francesco.ballardin@develonproject.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- MIT-LICENSE
|
78
|
+
- README.md
|
79
|
+
- Rakefile
|
80
|
+
- app/controllers/concerns/gatekeeper/respondable.rb
|
81
|
+
- app/controllers/gatekeeper_controller.rb
|
82
|
+
- app/models/gatekeeper/embedded_model.rb
|
83
|
+
- app/models/gatekeeper/model.rb
|
84
|
+
- app/models/gatekeeper/related_model.rb
|
85
|
+
- app/models/gatekeeper/user.rb
|
86
|
+
- app/views/gatekeeper/create.js.erb
|
87
|
+
- app/views/gatekeeper/destroy.js.erb
|
88
|
+
- app/views/gatekeeper/edit.html.erb
|
89
|
+
- app/views/gatekeeper/edit.js.erb
|
90
|
+
- app/views/gatekeeper/index.html.erb
|
91
|
+
- app/views/gatekeeper/index.js.erb
|
92
|
+
- app/views/gatekeeper/new.html.erb
|
93
|
+
- app/views/gatekeeper/new.js.erb
|
94
|
+
- app/views/gatekeeper/show.html.erb
|
95
|
+
- app/views/gatekeeper/show.js.erb
|
96
|
+
- app/views/gatekeeper/update.js.erb
|
97
|
+
- config/initializers/disable_auto_render.rb
|
98
|
+
- config/initializers/mongoid/accessibility.rb
|
99
|
+
- config/initializers/mongoid/document.rb
|
100
|
+
- config/initializers/rainbow.rb
|
101
|
+
- config/routes.rb
|
102
|
+
- lib/gatekeeper.rb
|
103
|
+
- lib/gatekeeper/configuration.rb
|
104
|
+
- lib/gatekeeper/engine.rb
|
105
|
+
- lib/gatekeeper/version.rb
|
106
|
+
- lib/generators/gatekeeper/config_generator.rb
|
107
|
+
- lib/generators/templates/gatekeeper.rb
|
108
|
+
- lib/tasks/gatekeeper_tasks.rake
|
109
|
+
homepage: https://github.com/Pluvie/rails-gatekeeper
|
110
|
+
licenses:
|
111
|
+
- MIT
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.7.6
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Provides various access control methods to models and controllers. To use
|
133
|
+
with MongoDB.
|
134
|
+
test_files: []
|