scram 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +173 -0
- data/Rakefile +32 -0
- data/app/models/scram/policy.rb +67 -0
- data/app/models/scram/target.rb +70 -0
- data/lib/scram.rb +11 -0
- data/lib/scram/concerns/aggregate_holder.rb +23 -0
- data/lib/scram/concerns/holder.rb +38 -0
- data/lib/scram/core_ext/symbol_extensions.rb +14 -0
- data/lib/scram/dsl/builders.rb +43 -0
- data/lib/scram/dsl/definitions.rb +36 -0
- data/lib/scram/dsl/model_conditions.rb +50 -0
- data/lib/scram/engine.rb +15 -0
- data/lib/scram/version.rb +3 -0
- data/lib/tasks/scram_tasks.rake +4 -0
- data/spec/config/mongoid.yml +6 -0
- data/spec/dummy/Rakefile +6 -0
- data/spec/dummy/app/assets/config/manifest.js +5 -0
- data/spec/dummy/app/assets/javascripts/application.js +13 -0
- data/spec/dummy/app/assets/javascripts/cable.js +13 -0
- data/spec/dummy/app/assets/stylesheets/application.css +15 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/jobs/application_job.rb +2 -0
- data/spec/dummy/app/mailers/application_mailer.rb +4 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +3 -0
- data/spec/dummy/bin/rails +4 -0
- data/spec/dummy/bin/rake +4 -0
- data/spec/dummy/bin/setup +34 -0
- data/spec/dummy/bin/update +29 -0
- data/spec/dummy/config.ru +5 -0
- data/spec/dummy/config/application.rb +23 -0
- data/spec/dummy/config/boot.rb +5 -0
- data/spec/dummy/config/cable.yml +9 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +51 -0
- data/spec/dummy/config/environments/production.rb +83 -0
- data/spec/dummy/config/environments/test.rb +42 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
- data/spec/dummy/config/initializers/assets.rb +11 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/dummy/config/initializers/inflections.rb +16 -0
- data/spec/dummy/config/initializers/mime_types.rb +4 -0
- data/spec/dummy/config/initializers/new_framework_defaults.rb +21 -0
- data/spec/dummy/config/initializers/session_store.rb +3 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +9 -0
- data/spec/dummy/config/locales/en.yml +23 -0
- data/spec/dummy/config/mongoid.yml +147 -0
- data/spec/dummy/config/puma.rb +47 -0
- data/spec/dummy/config/routes.rb +3 -0
- data/spec/dummy/config/secrets.yml +22 -0
- data/spec/dummy/config/spring.rb +6 -0
- data/spec/dummy/log/development.log +11 -0
- data/spec/dummy/log/test.log +2321 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/factories/policy.rb +0 -0
- data/spec/rails_helper.rb +78 -0
- data/spec/scram/concerns/aggregate_holder_spec.rb +58 -0
- data/spec/scram/concerns/holder_spec.rb +100 -0
- data/spec/scram/dsl_spec.rb +51 -0
- data/spec/scram/policy_spec.rb +28 -0
- data/spec/scram/target_spec.rb +40 -0
- data/spec/scram/test_implementations/simple_aggregate_holder.rb +21 -0
- data/spec/scram/test_implementations/simple_holder.rb +21 -0
- data/spec/scram/test_implementations/test_model.rb +10 -0
- data/spec/scram_spec.rb +11 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/support/factory_girl.rb +9 -0
- metadata +278 -0
@@ -0,0 +1,14 @@
|
|
1
|
+
# Refinements to the Symbol class
|
2
|
+
module SymbolExtensions
|
3
|
+
refine Symbol do
|
4
|
+
# Converts self to a boolean if it is :allow, :abstain, :deny. Assumes that abstain means false.
|
5
|
+
# @return [Boolean] Boolean representation of allow, abstain, or deny. Abstain means false.
|
6
|
+
# @raise [NotImplementedError] If unsupported symbol is converted (not :allow, :abstain, :deny)
|
7
|
+
def to_bool
|
8
|
+
raise NotImplementedError("Cannot convert #{self} to a boolean! #to_bool only supports :allow, :abstain and :deny.") unless %i[allow abstain deny].include? self
|
9
|
+
return true if self == :allow
|
10
|
+
return false if self == :abstain
|
11
|
+
return false if self == :deny
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Scram::DSL
|
2
|
+
# DSL Builders
|
3
|
+
module Builders
|
4
|
+
# Base class for DSL Builder.
|
5
|
+
# Executes initializer block within context of the builder.
|
6
|
+
class Builder
|
7
|
+
def initialize(&block)
|
8
|
+
instance_exec(&block)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# TODO: Flatten comparatorbuilder and conditionbuilder into one "DictionaryBuilder"
|
13
|
+
# Builder used to create comparators.
|
14
|
+
class ComparatorBuilder < Builder
|
15
|
+
attr_accessor :comparators
|
16
|
+
|
17
|
+
def initialize(&block)
|
18
|
+
@comparators = {}
|
19
|
+
super(&block)
|
20
|
+
end
|
21
|
+
|
22
|
+
def comparator name, &block
|
23
|
+
@comparators[name] = block
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Builder used to create conditions.
|
28
|
+
class ConditionBuilder < Builder
|
29
|
+
attr_accessor :conditions
|
30
|
+
|
31
|
+
def initialize(&block)
|
32
|
+
@conditions = {}
|
33
|
+
super(&block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def condition variable, &block
|
37
|
+
@conditions[variable] = block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Scram::DSL
|
2
|
+
# Default definitions from builders
|
3
|
+
module Definitions
|
4
|
+
# Adds a custom comparator using a builder
|
5
|
+
# @param builder [Scram::Builders::ComparatorBuilder] Builder to merge into the usable comparators
|
6
|
+
def self.add_comparators(builder)
|
7
|
+
COMPARATORS.merge!(builder.comparators)
|
8
|
+
end
|
9
|
+
|
10
|
+
# Default comparators.
|
11
|
+
# @note These names are used within the DB as key names for conditions. Pay attention when adding them,
|
12
|
+
# and plan not to be changing them.
|
13
|
+
# TODO: Inclusive inequalities
|
14
|
+
COMPARATORS = Builders::ComparatorBuilder.new do
|
15
|
+
comparator :equals do |a, b|
|
16
|
+
a == b
|
17
|
+
end
|
18
|
+
|
19
|
+
comparator :greater_than do |a, b|
|
20
|
+
a > b
|
21
|
+
end
|
22
|
+
|
23
|
+
comparator :less_than do |a, b|
|
24
|
+
a < b
|
25
|
+
end
|
26
|
+
|
27
|
+
comparator :includes do |a, b|
|
28
|
+
a.send(:include?, b)
|
29
|
+
end
|
30
|
+
|
31
|
+
comparator :not_equals do |a, b|
|
32
|
+
a != b
|
33
|
+
end
|
34
|
+
end.comparators
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Scram::DSL
|
2
|
+
# Module for Models to include to be able to define special condition variables in their targets
|
3
|
+
# @note Changes behavior of method_missing! Calling a method prefixed with * will search for scram conditions to invoke
|
4
|
+
module ModelConditions
|
5
|
+
def self.included(base_class)
|
6
|
+
base_class.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# @return [Hash] Mapping of condition names to Procs to execute them
|
11
|
+
attr_accessor :scram_conditions
|
12
|
+
|
13
|
+
# Method meant to be used in the including class to begin defining conditions
|
14
|
+
#
|
15
|
+
# Example
|
16
|
+
# scram_define do
|
17
|
+
# condition :foo do
|
18
|
+
# |instance| "hello world"
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
def scram_define(&block)
|
22
|
+
@scram_conditions = Builders::ConditionBuilder.new(&block).conditions
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# Methods starting with an asterisk are tested for DSL defined conditions
|
27
|
+
def method_missing(method, *args)
|
28
|
+
if method.to_s.starts_with? "*"
|
29
|
+
condition_name = method.to_s.split("*")[1].to_sym
|
30
|
+
conditions = self.class.scram_conditions
|
31
|
+
if conditions && !conditions[condition_name].nil?
|
32
|
+
return conditions[condition_name].call(self)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
super
|
36
|
+
end
|
37
|
+
|
38
|
+
# Allow DSL condition methods to show up as methods (i.e fix #respond_to?)
|
39
|
+
def respond_to_missing?(method, include_private = false)
|
40
|
+
if method.to_s.starts_with? "*"
|
41
|
+
condition_name = method.to_s.split("*")[1].to_sym
|
42
|
+
conditions = self.class.scram_conditions
|
43
|
+
if conditions && !conditions[condition_name].nil?
|
44
|
+
return true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
super
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/scram/engine.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "rails/mongoid"
|
2
|
+
require "mongoid"
|
3
|
+
|
4
|
+
module Scram
|
5
|
+
class Engine < ::Rails::Engine
|
6
|
+
isolate_namespace Scram
|
7
|
+
|
8
|
+
config.generators do |g|
|
9
|
+
g.test_framework :rspec, :fixture => false
|
10
|
+
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
|
11
|
+
g.assets false
|
12
|
+
g.helper false
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/dummy/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// compiled file. JavaScript code in this file should be added after the last require_* statement.
|
9
|
+
//
|
10
|
+
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
11
|
+
// about supported directives.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
@@ -0,0 +1,13 @@
|
|
1
|
+
// Action Cable provides the framework to deal with WebSockets in Rails.
|
2
|
+
// You can generate new channels where WebSocket features live using the rails generate channel command.
|
3
|
+
//
|
4
|
+
//= require action_cable
|
5
|
+
//= require_self
|
6
|
+
//= require_tree ./channels
|
7
|
+
|
8
|
+
(function() {
|
9
|
+
this.App || (this.App = {});
|
10
|
+
|
11
|
+
App.cable = ActionCable.createConsumer();
|
12
|
+
|
13
|
+
}).call(this);
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/*
|
2
|
+
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
3
|
+
* listed below.
|
4
|
+
*
|
5
|
+
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
6
|
+
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
7
|
+
*
|
8
|
+
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
9
|
+
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
|
10
|
+
* files in this directory. Styles in this file should be added after the last require_* statement.
|
11
|
+
* It is generally better to create a new file per style scope.
|
12
|
+
*
|
13
|
+
*= require_tree .
|
14
|
+
*= require_self
|
15
|
+
*/
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<title>Dummy</title>
|
5
|
+
<%= csrf_meta_tags %>
|
6
|
+
|
7
|
+
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
|
8
|
+
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
|
9
|
+
</head>
|
10
|
+
|
11
|
+
<body>
|
12
|
+
<%= yield %>
|
13
|
+
</body>
|
14
|
+
</html>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= yield %>
|
data/spec/dummy/bin/rake
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
require 'fileutils'
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
# path to your application root.
|
7
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
8
|
+
|
9
|
+
def system!(*args)
|
10
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
11
|
+
end
|
12
|
+
|
13
|
+
chdir APP_ROOT do
|
14
|
+
# This script is a starting point to setup your application.
|
15
|
+
# Add necessary setup steps to this file.
|
16
|
+
|
17
|
+
puts '== Installing dependencies =='
|
18
|
+
system! 'gem install bundler --conservative'
|
19
|
+
system('bundle check') || system!('bundle install')
|
20
|
+
|
21
|
+
# puts "\n== Copying sample files =="
|
22
|
+
# unless File.exist?('config/database.yml')
|
23
|
+
# cp 'config/database.yml.sample', 'config/database.yml'
|
24
|
+
# end
|
25
|
+
|
26
|
+
puts "\n== Preparing database =="
|
27
|
+
system! 'bin/rails db:setup'
|
28
|
+
|
29
|
+
puts "\n== Removing old logs and tempfiles =="
|
30
|
+
system! 'bin/rails log:clear tmp:clear'
|
31
|
+
|
32
|
+
puts "\n== Restarting application server =="
|
33
|
+
system! 'bin/rails restart'
|
34
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'pathname'
|
3
|
+
require 'fileutils'
|
4
|
+
include FileUtils
|
5
|
+
|
6
|
+
# path to your application root.
|
7
|
+
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)
|
8
|
+
|
9
|
+
def system!(*args)
|
10
|
+
system(*args) || abort("\n== Command #{args} failed ==")
|
11
|
+
end
|
12
|
+
|
13
|
+
chdir APP_ROOT do
|
14
|
+
# This script is a way to update your development environment automatically.
|
15
|
+
# Add necessary update steps to this file.
|
16
|
+
|
17
|
+
puts '== Installing dependencies =='
|
18
|
+
system! 'gem install bundler --conservative'
|
19
|
+
system('bundle check') || system!('bundle install')
|
20
|
+
|
21
|
+
puts "\n== Updating database =="
|
22
|
+
system! 'bin/rails db:migrate'
|
23
|
+
|
24
|
+
puts "\n== Removing old logs and tempfiles =="
|
25
|
+
system! 'bin/rails log:clear tmp:clear'
|
26
|
+
|
27
|
+
puts "\n== Restarting application server =="
|
28
|
+
system! 'bin/rails restart'
|
29
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require_relative 'boot'
|
2
|
+
|
3
|
+
# Pick the frameworks you want:
|
4
|
+
# require "active_record/railtie"
|
5
|
+
require "action_controller/railtie"
|
6
|
+
require "action_view/railtie"
|
7
|
+
require "action_mailer/railtie"
|
8
|
+
require "active_job/railtie"
|
9
|
+
require "action_cable/engine"
|
10
|
+
require "rails/test_unit/railtie"
|
11
|
+
require "sprockets/railtie"
|
12
|
+
|
13
|
+
Bundler.require(*Rails.groups)
|
14
|
+
require "scram"
|
15
|
+
|
16
|
+
module Dummy
|
17
|
+
class Application < Rails::Application
|
18
|
+
# Settings in config/environments/* take precedence over those specified here.
|
19
|
+
# Application configuration should go into files in config/initializers
|
20
|
+
# -- all .rb files in that directory are automatically loaded.
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|