Skip to content

Commit

Permalink
Merge pull request #5 from skreem/engine
Browse files Browse the repository at this point in the history
Engine
  • Loading branch information
skipperguy12 committed Feb 25, 2017
2 parents 25995e9 + 33858e3 commit 6e062c6
Show file tree
Hide file tree
Showing 81 changed files with 3,685 additions and 122 deletions.
Binary file added .DS_Store
Binary file not shown.
12 changes: 11 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in scram.gemspec
# Declare your gem's dependencies in scram.gemspec.
# Bundler will treat runtime dependencies like base dependencies, and
# development dependencies will be added by default to the :development group.
gemspec

# Declare any dependencies that are still in development here instead of in
# your gemspec. These might include edge Rails or gems from your path or
# Git. Remember to move these dependencies to your gemspec before releasing
# your gem to rubygems.org.

# To use a debugger
# gem 'byebug', group: [:development, :test]
103 changes: 94 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,52 @@ PATH
remote: .
specs:
scram (0.1.0)
activesupport (~> 5.0)
mongoid (~> 6.1)
rails (~> 5.0, >= 5.0.1)

GEM
remote: https://rubygems.org/
specs:
actioncable (5.0.1)
actionpack (= 5.0.1)
nio4r (~> 1.2)
websocket-driver (~> 0.6.1)
actionmailer (5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.0.1)
actionview (= 5.0.1)
activesupport (= 5.0.1)
rack (~> 2.0)
rack-test (~> 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.0.1)
activesupport (= 5.0.1)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
activejob (5.0.1)
activesupport (= 5.0.1)
globalid (>= 0.3.6)
activemodel (5.0.1)
activesupport (= 5.0.1)
activerecord (5.0.1)
activemodel (= 5.0.1)
activesupport (= 5.0.1)
arel (~> 7.0)
activesupport (5.0.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (~> 0.7)
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (7.1.4)
bson (4.2.1)
builder (3.2.3)
concurrent-ruby (1.0.4)
coveralls (0.8.19)
json (>= 1.8, < 3)
Expand All @@ -26,21 +58,58 @@ GEM
database_cleaner (1.5.3)
diff-lcs (1.3)
docile (1.1.5)
erubis (2.7.0)
factory_girl (4.8.0)
activesupport (>= 3.0.0)
globalid (0.3.7)
activesupport (>= 4.1.0)
i18n (0.8.0)
json (2.0.3)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.4)
mime-types (>= 1.16, < 4)
method_source (0.8.2)
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
minitest (5.10.1)
mongo (2.4.1)
bson (>= 4.2.1, < 5.0.0)
mongoid (6.1.0)
activemodel (~> 5.0)
mongo (>= 2.4.1, < 3.0.0)
rake (10.5.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
nio4r (1.2.1)
nokogiri (1.7.0.1)
mini_portile2 (~> 2.1.0)
rack (2.0.1)
rack-test (0.6.3)
rack (>= 1.0)
rails (5.0.1)
actioncable (= 5.0.1)
actionmailer (= 5.0.1)
actionpack (= 5.0.1)
actionview (= 5.0.1)
activejob (= 5.0.1)
activemodel (= 5.0.1)
activerecord (= 5.0.1)
activesupport (= 5.0.1)
bundler (>= 1.3.0, < 2.0)
railties (= 5.0.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.2)
activesupport (>= 4.2.0, < 6.0)
nokogiri (~> 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
railties (5.0.1)
actionpack (= 5.0.1)
activesupport (= 5.0.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (12.0.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
Expand All @@ -49,30 +118,46 @@ GEM
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-rails (3.5.2)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
simplecov (0.12.0)
docile (~> 1.1.0)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.0)
sprockets (3.7.1)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
sprockets-rails (3.2.0)
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
term-ansicolor (1.4.0)
tins (~> 1.0)
thor (0.19.4)
thread_safe (0.3.5)
tins (1.13.2)
tzinfo (1.2.2)
thread_safe (~> 0.1)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.2)

PLATFORMS
ruby

DEPENDENCIES
bundler (~> 1.13)
coveralls (~> 0.8.19)
database_cleaner (~> 1.5)
factory_girl (~> 4.8)
rake (~> 10.0)
rspec (~> 3.0)
rspec-rails
scram!

BUNDLED WITH
Expand Down
20 changes: 20 additions & 0 deletions MIT-LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright 2017 skreem

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ To do this, we'll need to define a non-model Policy (because our object is a str
```ruby
user = ...
policy = Scram::Policy.new
policy.collection_name = "global-strings-policy"
policy.name = "global-strings-policy" # Note that we're setting name, and we will leave context nil.
policy.context = nil # This would be nil by default as well. By not setting this to anything, we let this Policy handle String permissions, and not be bound to a model.
policy.save
user.policies << policy
user.save
Expand Down Expand Up @@ -97,7 +98,8 @@ Lets make a Policy that handles post related permissions.
```ruby
user = ...
policy = Scram::Policy.new
policy.collection_name = Post.class.name
policy.name = "Post Stuff" # This name is just for organizational/display purposes
policy.context = Post.to_s # Note: By setting context, we bind this policy to the model "Post"
policy.save
user.policies << policy
user.save
Expand Down
38 changes: 29 additions & 9 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rdoc/task"

RSpec::Core::RakeTask.new(:spec)
begin
require 'bundler/setup'
rescue LoadError
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
end

task :default => :spec
require 'rdoc/task'

Rake::RDocTask.new do |rd|
rd.rdoc_files.include("lib/**/*.rb")
rd.rdoc_dir = "rdoc"
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'Scram'
rdoc.options << '--line-numbers'
rdoc.rdoc_files.include('README.md')
rdoc.rdoc_files.include('lib/**/*.rb')
end

APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
load 'rails/tasks/engine.rake'

load 'rails/tasks/statistics.rake'



require 'bundler/gem_tasks'

require 'rspec/core'
require 'rspec/core/rake_task'

desc "Run all specs in spec directory (excluding plugin specs)"
RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')

task :default => :spec
23 changes: 15 additions & 8 deletions lib/scram/app/models/policy.rb → app/models/scram/policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ class Policy

embeds_many :targets, class_name: "Scram::Target"

validates_presence_of :context
validates_presence_of :name

# @return [Integer] Priority to allow this policy to override another conflicting {Scram::Policy} opinion.
field :priority, type: Integer, default: 0
# @return [String] Name for this Policy. Purely for organizational purposes.
field :name, type: String

# @return [String] What this Policy applies to. Usually it will be the name of a Model, but it can also be a String for a "global" policy for non model-bound permissions
# @return [String] What model this Policy applies to. Will be nil if this Policy is not bound to a model.
field :context, type: String

# @return [Integer] Priority to allow this policy to override another conflicting {Scram::Policy} opinion.
field :priority, type: Integer, default: 0

# Helper method to easily tell if this policy is bound to a model
# @note Unnecessary since we can just call model.nil?, but it is helpful nonetheless
# @return [Boolean] True if this Policy is bound to a model, false otherwise
Expand All @@ -25,7 +28,7 @@ def model?
def model
begin
return Module.const_get(context)
rescue NameError
rescue # NameError if context as a constant doesn't exist, TypeError if context nil
return nil
end
end
Expand All @@ -39,13 +42,17 @@ def model
def can? holder, action, obj
obj = obj.to_s if obj.is_a? Symbol
action = action.to_s

# Abstain if policy doesn't apply to the obj
if obj.is_a? String # String permissions
return :abstain if self.model? # abstain if policy doesn't handle strings
else #
else # Model permissions
return :abstain if !self.model? # abstain if policy doesn't handle models
return :abstain if self.context != obj.class.name # abstain if policy doesn't handle these types of models

if obj.is_a?(Class) # Passed in a class, need to check based off the passed in model's name
return :abstain if self.context != obj.to_s # abstain if policy doesn't handle these types of models
else # Passed in an instance of a model, need to check based off the instance's class's name
return :abstain if self.context != obj.class.name
end
end

# Checks targets in priority order for explicit allow or deny.
Expand Down
File renamed without changes.
14 changes: 0 additions & 14 deletions bin/console

This file was deleted.

13 changes: 13 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path('../..', __FILE__)
ENGINE_PATH = File.expand_path('../../lib/scram/engine', __FILE__)

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])

require 'rails/all'
require 'rails/engine/commands'
8 changes: 0 additions & 8 deletions bin/setup

This file was deleted.

8 changes: 2 additions & 6 deletions lib/scram.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
require "scram/version"
require "mongoid"
require "scram/engine"

require "scram/core_ext/symbol_extensions.rb"
require "scram/dsl/builders.rb"
require "scram/dsl/definitions.rb"
require "scram/dsl/model_conditions.rb"
#require "scram/comparator_definitions.rb"
require "scram/concerns/holder.rb"
require "scram/concerns/aggregate_holder.rb"
require "scram/app/models/policy.rb"
#require "scram/app/models/permission_node.rb"
require "scram/app/models/target.rb"

module Scram
end
15 changes: 15 additions & 0 deletions lib/scram/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails/mongoid"
require "mongoid"

module Scram
class Engine < ::Rails::Engine
isolate_namespace Scram

config.generators do |g|
g.test_framework :rspec, :fixture => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.assets false
g.helper false
end
end
end
2 changes: 1 addition & 1 deletion lib/scram/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Scram
VERSION = "0.1.0"
VERSION = '0.1.0'
end

0 comments on commit 6e062c6

Please sign in to comment.