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

Commit af81da2

Browse files
committed
closes #61 closes #59 closes #75
1 parent 95ca6ab commit af81da2

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

lib/active_record_base.rb

+8-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ def regulate_scope(name, &block)
193193

194194
# regulate_default_scope
195195

196-
def regulate_default_scope(&block)
196+
def regulate_default_scope(*args, &block)
197+
block = __synchromesh_parse_regulator_params({ all: args[0] }, block).last unless args.empty?
197198
regulate_scope(:all, &block)
198199
end
199200

@@ -311,6 +312,12 @@ def synchromesh_after_destroy
311312
return if do_not_synchronize?
312313
ReactiveRecord::Broadcast.after_commit :destroy, self
313314
end
315+
316+
def __hyperloop_secure_attributes(acting_user)
317+
accessible_attributes =
318+
Hyperloop::InternalPolicy.accessible_attributes_for(self, acting_user)
319+
attributes.select { |attr| accessible_attributes.include? attr.to_sym }
320+
end
314321
end
315322
end
316323

lib/reactive_record/active_record/reactive_record/isomorphic_base.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -447,23 +447,23 @@ def self.save_records(models, associations, acting_user, validate, save)
447447

448448
saved_models = reactive_records.collect do |reactive_record_id, model|
449449
#puts "saving rr_id: #{reactive_record_id} model.object_id: #{model.object_id} frozen? <#{model.frozen?}>"
450-
if model and (model.frozen? or dont_save_list.include?(model) or model.changed.include?(model.class.primary_key))
450+
if model && (model.frozen? || dont_save_list.include?(model) || model.changed.include?(model.class.primary_key))
451451
# the above check for changed including the private key happens if you have an aggregate that includes its own id
452-
#puts "validating frozen model #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
452+
# puts "validating frozen model #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
453453
valid = model.valid?
454-
#puts "has_errors before = #{has_errors}, validate= #{validate}, !valid= #{!valid} (validate and !valid) #{validate and !valid}"
454+
# puts "has_errors before = #{has_errors}, validate= #{validate}, !valid= #{!valid} (validate and !valid) #{validate and !valid}"
455455
has_errors ||= (validate and !valid)
456-
#puts "validation complete errors = <#{!valid}>, #{model.errors.messages} has_errors #{has_errors}"
456+
# puts "validation complete errors = <#{!valid}>, #{model.errors.messages} has_errors #{has_errors}"
457457
error_messages << [model, model.errors.messages] unless valid
458-
[reactive_record_id, model.class.name, model.attributes, (valid ? nil : model.errors.messages)]
458+
[reactive_record_id, model.class.name, model.__hyperloop_secure_attributes(acting_user), (valid ? nil : model.errors.messages)]
459459
elsif model and (!model.id or model.changed?)
460-
#puts "saving #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
460+
# puts "saving #{model.class.name} #{model} (reactive_record_id = #{reactive_record_id})"
461461
saved = model.check_permission_with_acting_user(acting_user, new_models.include?(model) ? :create_permitted? : :update_permitted?).save(validate: validate)
462462
has_errors ||= !saved
463463
messages = model.errors.messages if (validate and !saved) or (!validate and !model.valid?)
464464
error_messages << [model, messages] if messages
465-
#puts "saved complete errors = <#{!saved}>, #{messages} has_errors #{has_errors}"
466-
[reactive_record_id, model.class.name, model.attributes, messages]
465+
# puts "saved complete errors = <#{!saved}>, #{messages} has_errors #{has_errors}"
466+
[reactive_record_id, model.class.name, model.__hyperloop_secure_attributes(acting_user), messages]
467467
end
468468
end.compact
469469

lib/reactive_record/permissions.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def check_permission_with_acting_user(user, permission, *args)
103103
self.acting_user = old
104104
self
105105
else
106-
raise ReactiveRecord::AccessViolation, "for #{permission}(#{args})"
106+
raise Hyperloop::AccessViolation, "for #{permission}(#{args})"
107107
end
108108
end
109109

spec/batch1/crud_access_regulation/broadcast_controls_access_spec.rb

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@
3333
it "will disallow access if acting_user is not allowed to connect" do
3434
m = FactoryBot.create(:test_model, test_attribute: "hello")
3535
expect { m.check_permission_with_acting_user(nil, :view_permitted?, :test_attribute) }.
36-
to raise_error(ReactiveRecord::AccessViolation)
36+
to raise_error(Hyperloop::AccessViolation)
3737
expect { m.check_permission_with_acting_user(nil, :view_permitted?, :created_at) }.
38-
to raise_error(ReactiveRecord::AccessViolation)
38+
to raise_error(Hyperloop::AccessViolation)
3939
end
4040

4141
it "will disallow access to attributes not broadcast by the model" do
4242
m = FactoryBot.create(:test_model, test_attribute: "bogus")
4343
expect { m.check_permission_with_acting_user("user", :view_permitted?, :test_attribute) }.
4444
not_to raise_error
4545
expect { m.check_permission_with_acting_user("user", :view_permitted?, :created_at) }.
46-
to raise_error(ReactiveRecord::AccessViolation)
46+
to raise_error(Hyperloop::AccessViolation)
4747
end
4848

4949
it "will allow access to attributes broadcast over an instance channel" do
@@ -61,14 +61,14 @@
6161
TestApplicationPolicy.class_eval do
6262
always_allow_connection
6363
regulate_broadcast(TestModel) do |policy|
64-
policy.send_all_but(:created_at).to(TestApplication)
64+
policy.send_all_but(:created_at).to(TestApplication)
6565
end
6666
end
6767
m = FactoryBot.create(:test_model)
6868
expect { m.check_permission_with_acting_user(nil, :view_permitted?, :test_attribute) }.
6969
not_to raise_error
7070
expect { m.check_permission_with_acting_user(nil, :view_permitted?, :created_at) }.
71-
to raise_error(ReactiveRecord::AccessViolation)
71+
to raise_error(Hyperloop::AccessViolation)
7272
end
7373

7474
it "will include :id as read attribute as long as any other attribute is readable" do
@@ -91,7 +91,7 @@
9191
end
9292
m = FactoryBot.create(:test_model)
9393
expect { m.check_permission_with_acting_user(nil, :view_permitted?, :id) }.
94-
to raise_error(ReactiveRecord::AccessViolation)
94+
to raise_error(Hyperloop::AccessViolation)
9595
end
9696

9797
it "will ignore auto_connect: false " do

spec/batch5/authorization_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ def log(*args)
9090
expect_evaluate_ruby('ReactiveRecord::Base.last_log_message').to eq(['Fetch failed', 'error'])
9191
end
9292

93+
it 'will only return authorized attributes on creation' do
94+
client_option raise_on_js_errors: :off
95+
TestModel.class_eval do
96+
def create_permitted?
97+
true
98+
end
99+
end
100+
mount 'TestComponent2'
101+
wait_for_ajax
102+
ApplicationController.acting_user = User.new(name: 'fred')
103+
page.evaluate_ruby('Hyperloop.connect("TestApplication")')
104+
TestModel.before_save { self.test_attribute ||= 'top secret' }
105+
expect_promise do
106+
model = TestModel.new(updated_at: 12)
107+
model.save.then do
108+
model.attributes.keys
109+
end
110+
end.to contain_exactly("id", "created_at", "updated_at", "child_models")
111+
end
112+
93113
it "will only synchronize the connected channels" do
94114
mount "TestComponent2"
95115
model1 = FactoryBot.create(:test_model, test_attribute: "hello")

spec/batch5/save_while_loading_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@
2222
expect_promise do
2323
TodoItem.create(user: User.find_by_first_name('Ima'))
2424
end.to include('success' => true)
25-
# strangely sometimes AR does not seem to be updated even though promise has returned
26-
# huh?
27-
wait_for(user.todo_items).to match_array([TodoItem.first])
25+
expect(user.todo_items.to_a).to match_array([TodoItem.first])
2826
end
27+
2928
it "with push" do
3029
user = FactoryBot.create(:user, first_name: 'Ima')
3130
expect_promise do
3231
User.find(1).todo_items << TodoItem.new
3332
User.find(1).save
3433
end.to include('success' => true)
35-
wait_for(user.todo_items).to match_array([TodoItem.first])
34+
expect(user.todo_items).to match_array([TodoItem.first])
3635
end
36+
3737
it "with assignment" do
3838
user = FactoryBot.create(:user, first_name: 'Ima')
3939
expect_promise do
4040
todo = TodoItem.new
4141
todo.user = User.find_by_first_name('Ima')
4242
todo.save
4343
end.to include('success' => true)
44-
wait_for(user.todo_items).to match_array([TodoItem.first])
44+
expect(user.todo_items).to match_array([TodoItem.first])
4545
end
4646
end

spec/batch5/relationship_permissions_spec.rb renamed to spec/batch5/zzz_must_be_last_relationship_permissions_spec.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
# Something about this spec can cause havoc on specs following.
2+
# somehow in the following specs AR objects are getting created in two different classes
3+
# so when you compare for example User.first.todos.first == Todos.first they are NOT equal huh!
4+
# I suspect that its to with the fact that we remove and reload the classes
5+
# but I got as far as proving that you have to actually create a todoitem and an associated comment
6+
# once you do that the tests after will fail on stmts like this expect(user.todo_items.to_a).to match_array([TodoItem.first])
7+
# because the class of user.todo_items.class != TodoItems.first even though they look exactly the same!!!
8+
19
require 'spec_helper'
210
require 'test_components'
311

412
describe "relationship permissions" do#, dont_override_default_scope_permissions: true do
513

614
before(:all) do
7-
# Hyperloop.configuration do |config|
8-
# config.transport = :simple_poller
9-
# # slow down the polling so wait_for_ajax works
10-
# config.opts = { seconds_between_poll: 2 }
11-
# end
1215

1316
require 'pusher'
1417
require 'pusher-fake'
@@ -39,6 +42,7 @@
3942

4043
before(:each) do
4144
ActiveRecord::Base.regulate_scope unscoped: nil
45+
ActiveRecord::Base.regulate_default_scope nil
4246
end
4347

4448
after(:each) do
@@ -244,7 +248,6 @@
244248
end
245249
end
246250
end
247-
248251
context 'integration test', js: true do
249252
before(:each) do
250253
client_option raise_on_js_errors: :off

0 commit comments

Comments
 (0)