Skip to content
This repository has been archived by the owner on Oct 19, 2018. It is now read-only.

Commit

Permalink
closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
catmando committed May 11, 2018
1 parent ed6fac2 commit 3df1ee0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source 'https://rubygems.org'
gem "opal-jquery", git: "https://github.com/opal/opal-jquery.git", branch: "master"
gem 'hyperloop-config', path: '../hyperloop-config'
gem 'hyper-operation', path: '../hyper-operation'
gemspec
6 changes: 3 additions & 3 deletions lib/active_record_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Relation
def __secure_collection_check(acting_user)
return self if __synchromesh_permission_granted
return self if __secure_remote_access_to_unscoped(self, acting_user).__synchromesh_permission_granted
denied!
Hyperloop::InternalPolicy.raise_operation_access_violation(:scoped_permission_not_granted, "Last relation: #{self}, acting_user: #{acting_user}")
end
end
# Monkey patches and extensions to base
Expand All @@ -56,7 +56,7 @@ class << self
# access protection fault.

def denied!
Hyperloop::InternalPolicy.raise_operation_access_violation
Hyperloop::InternalPolicy.raise_operation_access_violation(:scoped_denied, "#{self} regulation denies scope access. Called from #{caller_locations(1)}")
end

# Here we set up the base `all` and `unscoped` methods. See below for more on how
Expand Down Expand Up @@ -275,7 +275,7 @@ def __secure_remote_access_to_find_by(_self, _acting_user, *args)
end

def denied!
Hyperloop::InternalPolicy.raise_operation_access_violation
Hyperloop::InternalPolicy.raise_operation_access_violation(:scoped_denied, "#{self.class} regulation denies scope access. Called from #{caller_locations(1)}")
end

# call do_not_synchronize to block synchronization of a model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def self.deserialize_response(response)
end
# fetch queued up records from the server
# subclass of ControllerOp so we can pass the controller
# along to on_fetch_error
# along to on_error
class Fetch < Base
param :acting_user, nils: true
param models: []
Expand All @@ -65,7 +65,8 @@ class Fetch < Base
]
end
failed do |e|
ReactiveRecord.on_fetch_error(e, params.to_h)
# AccessViolations are already sent to on_error
Hyperloop.on_error(e, :fetch_error, params.to_h) unless e.is_a? Hyperloop::AccessViolation
raise e
end
end
Expand Down
3 changes: 1 addition & 2 deletions lib/reactive_record/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,14 @@ def belongs_to(attr_name, scope = nil, options = {})
end
end


def check_permission_with_acting_user(user, permission, *args)
old = acting_user
self.acting_user = user
if self.send(permission, *args)
self.acting_user = old
self
else
raise Hyperloop::AccessViolation, "for #{permission}(#{args})"
Hyperloop::InternalPolicy.raise_operation_access_violation(:crud_access_violation, "for #{permission}(#{args}) acting_user: #{user}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/reactive_record/server_data_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def self.get_model(str)
#
# If str is not already loaded then we have an access violation.
unless const_defined? str
Hyperloop::InternalPolicy.raise_operation_access_violation
Hyperloop::InternalPolicy.raise_operation_access_violation(:undefined_const, "#{str} is not a loaded constant")
end
str.constantize
end
Expand Down
25 changes: 16 additions & 9 deletions spec/batch6/on_fetch_error_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'
require 'test_components'

describe "ReactiveRecord.on_fetch_error", js: true do
describe "Hyperloop.on_error (for fetches) ", js: true do

before(:all) do
require 'pusher'
Expand Down Expand Up @@ -39,21 +39,27 @@
ApplicationController.acting_user = nil
end

it 'call ReactiveRecord.on_fetch_error for access violations' do
it 'call Hyperloop.on_error for access violations' do
TodoItem.class_eval do
TodoItem.regulate_relationship(:comments) { acting_user == user }
end
todo_item1 = TodoItem.create(user: ApplicationController.acting_user)
todo_item2 = TodoItem.create(user: nil)
Comment.create(todo_item: todo_item1)
Comment.create(todo_item: todo_item1)
expect(ReactiveRecord).to receive(:on_fetch_error).once.with(
# expect(Hyperloop).to receive(:on_error).once.with(
# Hyperloop::AccessViolation,
# :fetch_error,
# 'acting_user' => ApplicationController.acting_user,
# 'controller' => kind_of(ActionController::Base),
# 'pending_fetches' => [['TodoItem', ['find_by', { 'id' => 2 }], 'comments', '*count']],
# 'models' => [],
# 'associations' => []
# )
expect(Hyperloop).to receive(:on_error).once.with(
Hyperloop::AccessViolation,
'acting_user' => ApplicationController.acting_user,
'controller' => kind_of(ActionController::Base),
'pending_fetches' => [['TodoItem', ['find_by', { 'id' => 2 }], 'comments', '*count']],
'models' => [],
'associations' => []
:scoped_permission_not_granted,
anything
)
expect_promise("ReactiveRecord.load { TodoItem.find(#{todo_item1.id}).comments.count }")
.to eq(2)
Expand All @@ -68,8 +74,9 @@ def title
end
end
TodoItem.create(user: nil)
expect(ReactiveRecord).to receive(:on_fetch_error).once.with(
expect(Hyperloop).to receive(:on_error).once.with(
Exception,
:fetch_error,
hash_including(:acting_user, :controller, :pending_fetches, :models, :associations)
)
evaluate_ruby('TodoItem.find(1).title')
Expand Down

0 comments on commit 3df1ee0

Please sign in to comment.