sequel-privacy 0.6 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6aa19c5eda5b7d5be5a491fd8aa877037c1b79014245d2a400f647fb7aa2d174
4
- data.tar.gz: f29f6df4a6418febe64508727df1cb8e2e611b31ea02ae3b8dda9516cdc5fd90
3
+ metadata.gz: 627da75fe2db0e41382bd4b6a6e44cf85bb01a3a52aef9c1c16df85b0b4af5d8
4
+ data.tar.gz: e0cfd6b79e35d81a51d6897fa37f9130e5fb567f3422c0014500be6bb0d95183
5
5
  SHA512:
6
- metadata.gz: 9a466793f9a3ddb2c3938f6434a56a7262569820ae3a9c8fb752c0f7d163f3b8bdfad3d09f9750fe4771d28fbb8171a780e9569963429dda2edc5e0f2b2ac68a
7
- data.tar.gz: bf7059897a808ff8466b2054f632a682bbf5e8201995760999259baaac3753d7116f6fe1159ec868ca1dfc40d5166697982a47f4b8dc494615c0337fdad14f2f
6
+ metadata.gz: c1450ee72abf6ef060a7d2f8d541f3d418724cca9650370746e996427d4da438e93fa96410b47b6606ab4a41542d014b46dbf97a08e3e4623cf120b419736372
7
+ data.tar.gz: 5ccadc4dfb1b84fb7ab9d3beefb2b396b2b1302239c0b63fc97a2c34a6fb9b5a025857542814167e007326a17e08e1697d56731cdff690a5c9d72d850cb96529
@@ -371,13 +371,13 @@ module Sequel
371
371
 
372
372
  sig { params(original: T.untyped).returns(Proc) }
373
373
  def _privacy_eager_block(original)
374
- wrapped = proc do |ds|
375
- ds = original.call(ds) if original
374
+ wrapped = proc do |dataset|
375
+ dataset = original.call(dataset) if original
376
376
  vc = Thread.current[DatasetMethods::EAGER_VC_KEY]
377
- if vc && T.unsafe(ds).model.respond_to?(:privacy_vc_key)
378
- T.unsafe(ds).for_vc(vc)
377
+ if vc && T.unsafe(dataset).model.respond_to?(:privacy_vc_key)
378
+ T.unsafe(dataset).for_vc(vc).clone(privacy_eager_load: true)
379
379
  else
380
- ds
380
+ dataset
381
381
  end
382
382
  end
383
383
  wrapped
@@ -385,6 +385,16 @@ module Sequel
385
385
 
386
386
  private
387
387
 
388
+ sig { returns(T::Module[T.anything]) }
389
+ def privacy_association_wrapper
390
+ wrapper = @privacy_association_wrapper ||= T.let(
391
+ Module.new,
392
+ T.nilable(T::Module[T.anything])
393
+ )
394
+ prepend(wrapper) unless ancestors.include?(wrapper)
395
+ wrapper
396
+ end
397
+
388
398
  sig { params(type: Symbol, name: Symbol, inject_eager_block: T::Boolean).void }
389
399
  def _setup_privacy_association_readers(type, name, inject_eager_block)
390
400
  reflection = association_reflection(name)
@@ -419,12 +429,11 @@ module Sequel
419
429
  dataset_method = :"#{name}_dataset"
420
430
  return unless method_defined?(dataset_method)
421
431
 
422
- original = instance_method(dataset_method)
423
432
  assoc_reflection = association_reflection(name)
424
433
  assoc_class = T.let(nil, T.nilable(T.class_of(Sequel::Model)))
425
434
 
426
- define_method(dataset_method) do |*args|
427
- ds = original.bind(self).(*args)
435
+ privacy_association_wrapper.define_method(dataset_method) do |*args, &block|
436
+ ds = super(*args, &block)
428
437
  vc = instance_variable_get(:@viewer_context)
429
438
  return ds unless vc
430
439
 
@@ -439,12 +448,11 @@ module Sequel
439
448
 
440
449
  sig { params(name: Symbol).void }
441
450
  def _override_singular_association(name)
442
- original = instance_method(name)
443
451
  assoc_reflection = association_reflection(name)
444
452
  # Resolve lazily to handle forward references between models.
445
453
  assoc_class = T.let(nil, T.nilable(T.class_of(Sequel::Model)))
446
454
 
447
- define_method(name) do
455
+ privacy_association_wrapper.define_method(name) do |*args, &block|
448
456
  vc = instance_variable_get(:@viewer_context)
449
457
 
450
458
  if vc.nil? && !T.unsafe(self.class).allow_unsafe_access?(name)
@@ -459,12 +467,12 @@ module Sequel
459
467
  old_vc = Thread.current[vc_key]
460
468
  Thread.current[vc_key] = vc
461
469
  begin
462
- original.bind(self).()
470
+ super(*args, &block)
463
471
  ensure
464
472
  Thread.current[vc_key] = old_vc
465
473
  end
466
474
  else
467
- original.bind(self).()
475
+ super(*args, &block)
468
476
  end
469
477
 
470
478
  return nil unless obj
@@ -484,11 +492,10 @@ module Sequel
484
492
 
485
493
  sig { params(name: Symbol).void }
486
494
  def _override_plural_association(name)
487
- original = instance_method(name)
488
495
  assoc_reflection = association_reflection(name)
489
496
  assoc_class = T.let(nil, T.nilable(T.class_of(Sequel::Model)))
490
497
 
491
- define_method(name) do
498
+ privacy_association_wrapper.define_method(name) do |*args, &block|
492
499
  vc = instance_variable_get(:@viewer_context)
493
500
 
494
501
  if vc.nil? && !T.unsafe(self.class).allow_unsafe_access?(name)
@@ -503,12 +510,12 @@ module Sequel
503
510
  old_vc = Thread.current[vc_key]
504
511
  Thread.current[vc_key] = vc
505
512
  begin
506
- original.bind(self).()
513
+ super(*args, &block)
507
514
  ensure
508
515
  Thread.current[vc_key] = old_vc
509
516
  end
510
517
  else
511
- original.bind(self).()
518
+ super(*args, &block)
512
519
  end
513
520
 
514
521
  return objs unless vc
@@ -530,13 +537,13 @@ module Sequel
530
537
  sig { params(assoc_name: Symbol, singular_name: Symbol, policies: T::Array[T.untyped]).void }
531
538
  def _wrap_association_add(assoc_name, singular_name, policies)
532
539
  method_name = :"add_#{singular_name}"
533
- original = instance_method(method_name)
534
540
 
535
- define_method(method_name) do |obj|
541
+ privacy_association_wrapper.define_method(method_name) do |*args, &block|
542
+ obj = args.first
536
543
  vc = instance_variable_get(:@viewer_context)
537
544
 
538
545
  unless vc
539
- return original.bind(self).(obj) if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
546
+ return super(*args, &block) if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
540
547
 
541
548
  Kernel.raise Sequel::Privacy::MissingViewerContext,
542
549
  "Cannot #{method_name} without a viewer context"
@@ -554,20 +561,20 @@ module Sequel
554
561
  "Cannot #{method_name} on #{self.class}"
555
562
  end
556
563
 
557
- original.bind(self).(obj)
564
+ super(*args, &block)
558
565
  end
559
566
  end
560
567
 
561
568
  sig { params(assoc_name: Symbol, singular_name: Symbol, policies: T::Array[T.untyped]).void }
562
569
  def _wrap_association_remove(assoc_name, singular_name, policies)
563
570
  method_name = :"remove_#{singular_name}"
564
- original = instance_method(method_name)
565
571
 
566
- define_method(method_name) do |obj|
572
+ privacy_association_wrapper.define_method(method_name) do |*args, &block|
573
+ obj = args.first
567
574
  vc = instance_variable_get(:@viewer_context)
568
575
 
569
576
  unless vc
570
- return original.bind(self).(obj) if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
577
+ return super(*args, &block) if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
571
578
 
572
579
  Kernel.raise Sequel::Privacy::MissingViewerContext,
573
580
  "Cannot #{method_name} without a viewer context"
@@ -585,20 +592,19 @@ module Sequel
585
592
  "Cannot #{method_name} on #{self.class}"
586
593
  end
587
594
 
588
- original.bind(self).(obj)
595
+ super(*args, &block)
589
596
  end
590
597
  end
591
598
 
592
599
  sig { params(assoc_name: Symbol, plural_name: Symbol, policies: T::Array[T.untyped]).void }
593
600
  def _wrap_association_remove_all(assoc_name, plural_name, policies)
594
601
  method_name = :"remove_all_#{plural_name}"
595
- original = instance_method(method_name)
596
602
 
597
- define_method(method_name) do
603
+ privacy_association_wrapper.define_method(method_name) do |*args, &block|
598
604
  vc = instance_variable_get(:@viewer_context)
599
605
 
600
606
  unless vc
601
- return original.bind(self).() if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
607
+ return super(*args, &block) if T.unsafe(self.class).allow_unsafe_access?(assoc_name)
602
608
 
603
609
  Kernel.raise Sequel::Privacy::MissingViewerContext,
604
610
  "Cannot #{method_name} without a viewer context"
@@ -616,7 +622,7 @@ module Sequel
616
622
  "Cannot #{method_name} on #{self.class}"
617
623
  end
618
624
 
619
- original.bind(self).()
625
+ super(*args, &block)
620
626
  end
621
627
  end
622
628
  end
@@ -747,8 +753,9 @@ module Sequel
747
753
  # can retreive. Materializes the model, and then checks the view
748
754
  # policy. If the model is being materialized within the context of
749
755
  # checking a policy this is bypassed, because policies often need to
750
- # check data that a VC might not have permission to see. The check is also
751
- # bypassed for eager loads, and checked on the association.
756
+ # check data that a VC might not have permission to see. For eager
757
+ # association loads, enforcement is deferred until association access so
758
+ # Sequel can finish attachment and populate reciprocal caches first.
752
759
  sig { returns(T.untyped) }
753
760
  def row_proc
754
761
  vc = opts[:viewer_context]
@@ -768,8 +775,8 @@ module Sequel
768
775
  next nil if instance.nil?
769
776
 
770
777
  instance.instance_variable_set(:@viewer_context, vc)
778
+ next instance if opts[:privacy_eager_load]
771
779
  next instance if Sequel::Privacy::Enforcer.in_policy_eval?
772
- next instance if Thread.current[EAGER_VC_KEY]
773
780
 
774
781
  if T.cast(instance, InstanceMethods).allow?(vc, :view)
775
782
  instance
@@ -3,6 +3,6 @@
3
3
 
4
4
  module Sequel
5
5
  module Privacy
6
- VERSION = '0.6'
6
+ VERSION = '0.6.1'
7
7
  end
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-privacy
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.6'
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Austin Bales