scram 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +173 -0
  4. data/Rakefile +32 -0
  5. data/app/models/scram/policy.rb +67 -0
  6. data/app/models/scram/target.rb +70 -0
  7. data/lib/scram.rb +11 -0
  8. data/lib/scram/concerns/aggregate_holder.rb +23 -0
  9. data/lib/scram/concerns/holder.rb +38 -0
  10. data/lib/scram/core_ext/symbol_extensions.rb +14 -0
  11. data/lib/scram/dsl/builders.rb +43 -0
  12. data/lib/scram/dsl/definitions.rb +36 -0
  13. data/lib/scram/dsl/model_conditions.rb +50 -0
  14. data/lib/scram/engine.rb +15 -0
  15. data/lib/scram/version.rb +3 -0
  16. data/lib/tasks/scram_tasks.rake +4 -0
  17. data/spec/config/mongoid.yml +6 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/assets/config/manifest.js +5 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  21. data/spec/dummy/app/assets/javascripts/cable.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  23. data/spec/dummy/app/channels/application_cable/channel.rb +4 -0
  24. data/spec/dummy/app/channels/application_cable/connection.rb +4 -0
  25. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  26. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  27. data/spec/dummy/app/jobs/application_job.rb +2 -0
  28. data/spec/dummy/app/mailers/application_mailer.rb +4 -0
  29. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
  31. data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
  32. data/spec/dummy/bin/bundle +3 -0
  33. data/spec/dummy/bin/rails +4 -0
  34. data/spec/dummy/bin/rake +4 -0
  35. data/spec/dummy/bin/setup +34 -0
  36. data/spec/dummy/bin/update +29 -0
  37. data/spec/dummy/config.ru +5 -0
  38. data/spec/dummy/config/application.rb +23 -0
  39. data/spec/dummy/config/boot.rb +5 -0
  40. data/spec/dummy/config/cable.yml +9 -0
  41. data/spec/dummy/config/environment.rb +5 -0
  42. data/spec/dummy/config/environments/development.rb +51 -0
  43. data/spec/dummy/config/environments/production.rb +83 -0
  44. data/spec/dummy/config/environments/test.rb +42 -0
  45. data/spec/dummy/config/initializers/application_controller_renderer.rb +6 -0
  46. data/spec/dummy/config/initializers/assets.rb +11 -0
  47. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  48. data/spec/dummy/config/initializers/cookies_serializer.rb +5 -0
  49. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/spec/dummy/config/initializers/inflections.rb +16 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  52. data/spec/dummy/config/initializers/new_framework_defaults.rb +21 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +9 -0
  55. data/spec/dummy/config/locales/en.yml +23 -0
  56. data/spec/dummy/config/mongoid.yml +147 -0
  57. data/spec/dummy/config/puma.rb +47 -0
  58. data/spec/dummy/config/routes.rb +3 -0
  59. data/spec/dummy/config/secrets.yml +22 -0
  60. data/spec/dummy/config/spring.rb +6 -0
  61. data/spec/dummy/log/development.log +11 -0
  62. data/spec/dummy/log/test.log +2321 -0
  63. data/spec/dummy/public/404.html +67 -0
  64. data/spec/dummy/public/422.html +67 -0
  65. data/spec/dummy/public/500.html +66 -0
  66. data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
  67. data/spec/dummy/public/apple-touch-icon.png +0 -0
  68. data/spec/dummy/public/favicon.ico +0 -0
  69. data/spec/factories/policy.rb +0 -0
  70. data/spec/rails_helper.rb +78 -0
  71. data/spec/scram/concerns/aggregate_holder_spec.rb +58 -0
  72. data/spec/scram/concerns/holder_spec.rb +100 -0
  73. data/spec/scram/dsl_spec.rb +51 -0
  74. data/spec/scram/policy_spec.rb +28 -0
  75. data/spec/scram/target_spec.rb +40 -0
  76. data/spec/scram/test_implementations/simple_aggregate_holder.rb +21 -0
  77. data/spec/scram/test_implementations/simple_holder.rb +21 -0
  78. data/spec/scram/test_implementations/test_model.rb +10 -0
  79. data/spec/scram_spec.rb +11 -0
  80. data/spec/spec_helper.rb +99 -0
  81. data/spec/support/factory_girl.rb +9 -0
  82. 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
@@ -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
@@ -0,0 +1,3 @@
1
+ module Scram
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :scram do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,6 @@
1
+ test:
2
+ clients:
3
+ default:
4
+ database: scram_db
5
+ hosts:
6
+ - localhost:27017
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require_relative 'config/application'
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,5 @@
1
+
2
+ //= link_tree ../images
3
+ //= link_directory ../javascripts .js
4
+ //= link_directory ../stylesheets .css
5
+ //= link scram_manifest.js
@@ -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,4 @@
1
+ module ApplicationCable
2
+ class Channel < ActionCable::Channel::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module ApplicationCable
2
+ class Connection < ActionCable::Connection::Base
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class ApplicationJob < ActiveJob::Base
2
+ end
@@ -0,0 +1,4 @@
1
+ class ApplicationMailer < ActionMailer::Base
2
+ default from: 'from@example.com'
3
+ layout 'mailer'
4
+ end
@@ -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,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <style>
6
+ /* Email styles need to be inline */
7
+ </style>
8
+ </head>
9
+
10
+ <body>
11
+ <%= yield %>
12
+ </body>
13
+ </html>
@@ -0,0 +1 @@
1
+ <%= yield %>
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
3
+ load Gem.bin_path('bundler', 'bundle')
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ APP_PATH = File.expand_path('../config/application', __dir__)
3
+ require_relative '../config/boot'
4
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../config/boot'
3
+ require 'rake'
4
+ Rake.application.run
@@ -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,5 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require_relative 'config/environment'
4
+
5
+ run Rails.application
@@ -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
+
@@ -0,0 +1,5 @@
1
+ # Set up gems listed in the Gemfile.
2
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)
3
+
4
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
5
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
@@ -0,0 +1,9 @@
1
+ development:
2
+ adapter: async
3
+
4
+ test:
5
+ adapter: async
6
+
7
+ production:
8
+ adapter: redis
9
+ url: redis://localhost:6379/1
@@ -0,0 +1,5 @@
1
+ # Load the Rails application.
2
+ require_relative 'application'
3
+
4
+ # Initialize the Rails application.
5
+ Rails.application.initialize!