blacklight-access_controls 0.6.2 → 0.7.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: c08621f2787c992b840763f78bfbbd0da777c238
4
- data.tar.gz: 9a0333f2b7a1f34835afeb65e24e08465b595845
2
+ SHA256:
3
+ metadata.gz: ad6262064035e0af1ebfcae414a52f18073874ba0d4ef695fd01cf22e3ef2c06
4
+ data.tar.gz: 2f4f590ed542157b3d0e09335cb66a9ac90132908494a3d797ca041fb037ecb4
5
5
  SHA512:
6
- metadata.gz: 894505d49a44f7dfa2ae0e993314f086da84add4dd7aa1bcb578fedd2333ee01c9a55f783b1a682cb840f1cb450a19e6ffffc03bfab4052f3c195471961a5d55
7
- data.tar.gz: 5bbe9d38b52bf2d19e5da972a8a895bc985a2761ee1b263205198239d03c2294413e10b64d92b5978a14f3942b6f3ef553955911661ec36555d6611fb0b859fa
6
+ metadata.gz: bf9cd42d89e6315c6e8c65d46341f995e4d0d668a24fa62e98c06d5bcee80c45c49deb81a47a18625cae26574e3d0f6f0ee34b95278a6420a5b6f4b40b126a6a
7
+ data.tar.gz: 11de766eb48a9d94ed7aa6c1b8483a7e78d7bb4166ebbba8cf549b1b4b66171e633e418e3482b29e46245acb7c47b5db634e3b1520c0e29583128ea23bde75c5
@@ -8,28 +8,29 @@ AllCops:
8
8
  Exclude:
9
9
  - '.internal_test_app/**/*'
10
10
 
11
+
11
12
  Bundler/DuplicatedGem:
12
13
  Enabled: false
13
14
 
14
- Style/FileName:
15
+ Metrics/BlockLength:
16
+ Exclude:
17
+ - 'spec/**/*'
18
+
19
+ Metrics/LineLength:
20
+ Max: 185
21
+
22
+ Naming/FileName:
15
23
  Exclude:
16
24
  - 'Gemfile'
17
25
  - 'blacklight-access_controls.gemspec'
18
26
  - 'lib/blacklight-access_controls.rb'
19
27
 
20
- Style/MixinGrouping:
21
- Enabled: false # pending fix of https://github.com/bbatsov/rubocop/issues/4172
22
-
23
28
  Rails:
24
29
  Enabled: true
25
30
 
26
- Metrics/BlockLength:
27
- Exclude:
28
- - 'spec/**/*'
29
-
30
- Metrics/LineLength:
31
- Max: 185
32
-
33
31
  RSpec/MessageSpies:
34
32
  Enabled: false
35
-
33
+
34
+ RSpec/NestedGroups:
35
+ Exclude:
36
+ - 'spec/**/*_spec.rb'
@@ -1,28 +1,25 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2016-08-24 16:11:34 -0700 using RuboCop version 0.42.0.
3
+ # on 2017-09-06 09:54:51 -0700 using RuboCop version 0.49.1.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Manually added to prevent `unrecognized cop` warnings during execution.
10
- require: rubocop-rspec
11
-
12
- # Offense count: 4
9
+ # Offense count: 3
13
10
  Metrics/AbcSize:
14
- Max: 18
11
+ Max: 16
15
12
 
16
13
  # Offense count: 1
17
14
  # Configuration parameters: CountComments.
18
15
  Metrics/ModuleLength:
19
16
  Max: 120
20
17
 
21
- # Offense count: 7
22
- # Configuration parameters: SkipBlocks.
23
- RSpec/DescribedClass:
18
+ RSpec/ContextWording:
24
19
  Exclude:
25
20
  - 'spec/unit/ability_spec.rb'
21
+ - 'spec/unit/enforcement_spec.rb'
22
+ - 'spec/unit/blacklight/access_controls/search_builder_spec.rb'
26
23
 
27
24
  # Offense count: 1
28
25
  # Configuration parameters: Max.
@@ -31,26 +28,14 @@ RSpec/ExampleLength:
31
28
  - 'spec/unit/ability_spec.rb'
32
29
 
33
30
  # Offense count: 3
34
- # Configuration parameters: CustomTransform.
31
+ # Configuration parameters: CustomTransform, IgnoreMethods.
35
32
  RSpec/FilePath:
36
33
  Exclude:
37
34
  - 'spec/unit/catalog_spec.rb'
38
35
  - 'spec/unit/config_spec.rb'
39
36
  - 'spec/unit/enforcement_spec.rb'
40
37
 
41
- # Offense count: 17
42
- # Configuration parameters: AssignmentOnly.
43
- RSpec/InstanceVariable:
44
- Exclude:
45
- - 'spec/unit/enforcement_spec.rb'
46
-
47
- # Offense count: 23
48
- RSpec/LeadingSubject:
49
- Exclude:
50
- - 'spec/unit/ability_spec.rb'
51
- - 'spec/unit/enforcement_spec.rb'
52
-
53
- # Offense count: 7
38
+ # Offense count: 15
54
39
  RSpec/MultipleExpectations:
55
40
  Max: 6
56
41
 
@@ -62,53 +47,12 @@ RSpec/NamedSubject:
62
47
  - 'spec/unit/config_spec.rb'
63
48
  - 'spec/unit/enforcement_spec.rb'
64
49
 
65
- # Offense count: 30
66
- # Configuration parameters: MaxNesting.
67
- RSpec/NestedGroups:
68
- Exclude:
69
- - 'spec/unit/ability_spec.rb'
70
- - 'spec/unit/catalog_spec.rb'
71
- - 'spec/unit/enforcement_spec.rb'
72
-
73
- # Offense count: 2
74
- # Configuration parameters: EnforcedStyle, SupportedStyles.
75
- # SupportedStyles: not_to, to_not
76
- RSpec/NotToNot:
77
- Exclude:
78
- - 'spec/unit/catalog_spec.rb'
79
- - 'spec/unit/enforcement_spec.rb'
80
-
81
50
  # Offense count: 1
82
51
  # Configuration parameters: IgnoreSymbolicNames.
83
52
  RSpec/VerifiedDoubles:
84
53
  Exclude:
85
54
  - 'spec/unit/ability_spec.rb'
86
55
 
87
- # Offense count: 5
88
- # Cop supports --auto-correct.
89
- # Configuration parameters: EnforcedStyle, SupportedStyles, IndentationWidth.
90
- # SupportedStyles: with_first_parameter, with_fixed_indentation
91
- Style/AlignParameters:
92
- Exclude:
93
- - 'lib/generators/blacklight/access_controls_generator.rb'
94
-
95
- # Offense count: 6
96
- # Cop supports --auto-correct.
97
- # Configuration parameters: EnforcedStyle, SupportedStyles, ProceduralMethods, FunctionalMethods, IgnoredMethods.
98
- # SupportedStyles: line_count_based, semantic, braces_for_chaining
99
- # ProceduralMethods: benchmark, bm, bmbm, create, each_with_object, measure, new, realtime, tap, with_object
100
- # FunctionalMethods: let, let!, subject, watch
101
- # IgnoredMethods: lambda, proc, it
102
- Style/BlockDelimiters:
103
- Exclude:
104
- - 'spec/unit/ability_spec.rb'
105
-
106
- # Offense count: 3
107
- # Cop supports --auto-correct.
108
- Style/BlockEndNewline:
109
- Exclude:
110
- - 'spec/unit/ability_spec.rb'
111
-
112
56
  # Offense count: 3
113
57
  # Configuration parameters: EnforcedStyle, SupportedStyles.
114
58
  # SupportedStyles: nested, compact
@@ -118,7 +62,7 @@ Style/ClassAndModuleChildren:
118
62
  - 'lib/blacklight/access_controls/permissions_cache.rb'
119
63
  - 'lib/blacklight/access_controls/permissions_query.rb'
120
64
 
121
- # Offense count: 12
65
+ # Offense count: 10
122
66
  Style/Documentation:
123
67
  Exclude:
124
68
  - 'spec/**/*'
@@ -126,31 +70,9 @@ Style/Documentation:
126
70
  - 'lib/blacklight-access_controls.rb'
127
71
  - 'lib/blacklight/access_controls.rb'
128
72
  - 'lib/blacklight/access_controls/ability.rb'
129
- - 'lib/blacklight/access_controls/catalog.rb'
130
73
  - 'lib/blacklight/access_controls/config.rb'
131
- - 'lib/blacklight/access_controls/enforcement.rb'
132
74
  - 'lib/blacklight/access_controls/permissions_cache.rb'
133
75
  - 'lib/blacklight/access_controls/permissions_query.rb'
134
76
  - 'lib/blacklight/access_controls/user.rb'
135
77
  - 'lib/generators/blacklight/ability.rb'
136
78
  - 'lib/generators/blacklight/access_controls_generator.rb'
137
-
138
- # Offense count: 1
139
- # Configuration parameters: ExpectMatchingDefinition, Regex, IgnoreExecutableScripts.
140
- Style/FileName:
141
- Exclude:
142
- - 'lib/blacklight-access_controls.rb'
143
-
144
- # Offense count: 3
145
- # Cop supports --auto-correct.
146
- Style/MultilineBlockLayout:
147
- Exclude:
148
- - 'spec/unit/ability_spec.rb'
149
-
150
- # Offense count: 6
151
- # Cop supports --auto-correct.
152
- # Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
153
- # SupportedStyles: slashes, percent_r, mixed
154
- Style/RegexpLiteral:
155
- Exclude:
156
- - 'spec/unit/enforcement_spec.rb'
@@ -8,7 +8,6 @@ rvm:
8
8
  - 2.3.1
9
9
 
10
10
  env:
11
- - "RAILS_VERSION=4.2.7.1"
12
11
  - "RAILS_VERSION=5.0.0.1"
13
12
 
14
13
  global_env:
data/Rakefile CHANGED
@@ -1,4 +1,3 @@
1
- #!/usr/bin/env rake
2
1
  begin
3
2
  require 'bundler/setup'
4
3
  rescue LoadError
@@ -18,7 +17,7 @@ RSpec::Core::RakeTask.new(:spec)
18
17
  task default: 'ci'
19
18
 
20
19
  def solr_config_dir
21
- File.join(File.expand_path(File.dirname(__FILE__)), 'solr_conf', 'conf')
20
+ File.join(__dir__, 'solr_conf', 'conf')
22
21
  end
23
22
 
24
23
  namespace :solr do
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.2
1
+ 0.7.0.rc1
@@ -15,18 +15,18 @@ Gem::Specification.new do |gem|
15
15
  gem.version = version
16
16
  gem.license = 'APACHE2'
17
17
 
18
- gem.required_ruby_version = '>= 1.9.3'
18
+ gem.required_ruby_version = '>= 2.1.0'
19
19
 
20
- gem.add_dependency 'cancancan', '~> 1.8'
21
20
  gem.add_dependency 'blacklight', '~> 6.0'
21
+ gem.add_dependency 'cancancan', '~> 1.8'
22
22
  gem.add_dependency 'deprecation', '~> 1.0'
23
23
 
24
- gem.add_development_dependency 'rake', '~> 11.3'
25
- gem.add_development_dependency 'rspec', '~> 3.1'
26
- gem.add_development_dependency 'engine_cart', '~> 1.0'
27
- gem.add_development_dependency 'solr_wrapper'
28
- gem.add_development_dependency 'factory_girl_rails', '~> 4.0'
29
24
  gem.add_development_dependency 'database_cleaner'
30
- gem.add_development_dependency 'rubocop'
25
+ gem.add_development_dependency 'engine_cart', '~> 1.0'
26
+ gem.add_development_dependency 'factory_bot_rails', '~> 4.8'
27
+ gem.add_development_dependency 'rake', '~> 12.3'
28
+ gem.add_development_dependency 'rspec', '~> 3.1'
29
+ gem.add_development_dependency 'rubocop', '~> 0.52.1'
31
30
  gem.add_development_dependency 'rubocop-rspec'
31
+ gem.add_development_dependency 'solr_wrapper'
32
32
  end
@@ -10,6 +10,7 @@ module Blacklight
10
10
  autoload :PermissionsCache
11
11
  autoload :Ability
12
12
  autoload :Enforcement
13
+ autoload :SearchBuilder
13
14
  autoload :Catalog
14
15
  end
15
16
  end
@@ -15,7 +15,7 @@ module Blacklight
15
15
  # permission methods to ability_logic, like so:
16
16
  # self.ability_logic += [:setup_my_permissions]
17
17
  class_attribute :ability_logic
18
- self.ability_logic = %i(discover_permissions read_permissions download_permissions)
18
+ self.ability_logic = %i[discover_permissions read_permissions download_permissions]
19
19
  end
20
20
 
21
21
  def initialize(user, options = {})
@@ -55,6 +55,7 @@ module Blacklight
55
55
  end
56
56
 
57
57
  def read_permissions
58
+ # Loading an object from your datastore might be slow (e.g. Fedora), so assume that if a string is passed, it's an object id
58
59
  can :read, String do |id|
59
60
  test_read(id)
60
61
  end
@@ -15,6 +15,11 @@ module Blacklight
15
15
  end
16
16
  permissions
17
17
  end
18
+
19
+ # This will work for BL 6, but will need to move to SearchService in BL 7
20
+ def search_builder
21
+ Blacklight::AccessControls::SearchBuilder.new(self, current_ability)
22
+ end
18
23
  end
19
24
  end
20
25
  end
@@ -19,10 +19,11 @@ module Blacklight
19
19
  attr_writer :current_ability, :discovery_permissions
20
20
  deprecation_deprecate :current_ability=
21
21
 
22
+ Deprecation.warn(self, 'Blacklight::AccessControls::Enforcement is deprecated and will be removed in 1.0')
22
23
  class_attribute :solr_access_filters_logic
23
24
  alias_method :add_access_controls_to_solr_params, :apply_gated_discovery
24
25
 
25
- self.solr_access_filters_logic = %i(apply_group_permissions apply_user_permissions)
26
+ self.solr_access_filters_logic = %i[apply_group_permissions apply_user_permissions]
26
27
 
27
28
  # Apply appropriate access controls to all solr queries
28
29
  self.default_processor_chain += [:add_access_controls_to_solr_params] if respond_to?(:default_processor_chain)
@@ -33,7 +34,7 @@ module Blacklight
33
34
  # Which permission levels (logical OR) will grant you the ability to discover documents in a search.
34
35
  # Override this method if you want it to be something other than the default, or hit the setter
35
36
  def discovery_permissions
36
- @discovery_permissions ||= %w(discover read)
37
+ @discovery_permissions ||= %w[discover read]
37
38
  end
38
39
 
39
40
  protected
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Blacklight
4
+ module AccessControls
5
+ # SearchBuilder that restricts access via Solr.
6
+ #
7
+ # Note: solr_access_filters_logic is an Array of Symbols.
8
+ # It sets defaults. Each symbol identifies a _method_ that must be in
9
+ # this class, taking two parameters (permission_types, ability).
10
+ # Can be changed in local apps or by plugins, e.g.:
11
+ # Blacklight::AccessControls::SearchBuilder.solr_access_filters_logic += [:new_method]
12
+ # Blacklight::AccessControls::SearchBuilder.solr_access_filters_logic.delete(:we_dont_want)
13
+ class SearchBuilder < ::SearchBuilder
14
+ class_attribute :solr_access_filters_logic
15
+ self.solr_access_filters_logic = %i[apply_group_permissions apply_user_permissions]
16
+
17
+ # Apply appropriate access controls to all solr queries
18
+ self.default_processor_chain += [:apply_gated_discovery]
19
+
20
+ # @param scope [Object] typically the controller instance
21
+ # @param ability [Ability] the current user ability
22
+ # @param permission_types [Array<String>] Which permission levels (logical OR) will grant you the ability to discover documents in a search.
23
+ def initialize(scope, ability:, permission_types: default_permission_types)
24
+ if self.class.included_modules.include? Blacklight::AccessControls::Enforcement
25
+ raise 'You may not use Blacklight::AccessControls::SearchBuilder and ' \
26
+ 'include Blacklight::AccessControls::Enforcement on SearchBuilder at the same time'
27
+ end
28
+ super(scope)
29
+ @ability = ability
30
+ @permission_types = permission_types
31
+ end
32
+
33
+ attr_reader :ability, :permission_types
34
+
35
+ def default_permission_types
36
+ %w[discover read]
37
+ end
38
+
39
+ private
40
+
41
+ # Grant access based on user id & group
42
+ # @return [Array{Array{String}}]
43
+ def gated_discovery_filters
44
+ solr_access_filters_logic.map { |method| send(method).reject(&:blank?) }.reject(&:empty?)
45
+ end
46
+
47
+ ### Solr query modifications
48
+
49
+ # Controller before_filter that sets up access-controlled lucene query to provide gated discovery behavior.
50
+ # Set solr_parameters to enforce appropriate permissions.
51
+ # @param [Hash{Object}] solr_parameters the current solr parameters, to be modified herein!
52
+ # @note Applies a lucene filter query to the solr :fq parameter for gated discovery.
53
+ def apply_gated_discovery(solr_parameters)
54
+ solr_parameters[:fq] ||= []
55
+ solr_parameters[:fq] << gated_discovery_filters.reject(&:blank?).join(' OR ')
56
+ Rails.logger.debug("Solr parameters: #{solr_parameters.inspect}")
57
+ end
58
+
59
+ # For groups
60
+ # @return [Array{String}] values are lucence syntax term queries suitable for :fq
61
+ # @example
62
+ # [ "({!terms f=discover_access_group_ssim}public,faculty,africana-faculty,registered)",
63
+ # "({!terms f=read_access_group_ssim}public,faculty,africana-faculty,registered)" ]
64
+ def apply_group_permissions
65
+ groups = ability.user_groups
66
+ return [] if groups.empty?
67
+ permission_types.map do |type|
68
+ field = solr_field_for(type, 'group')
69
+ "({!terms f=#{field}}#{groups.join(',')})" # parens required to properly OR the clauses together.
70
+ end
71
+ end
72
+
73
+ # For individual user access
74
+ # @return [Array{String}] values are lucence syntax term queries suitable for :fq
75
+ # @example ['discover_access_person_ssim:user_1@abc.com', 'read_access_person_ssim:user_1@abc.com']
76
+ def apply_user_permissions
77
+ user = ability.current_user
78
+ return [] unless user && user.user_key.present?
79
+ permission_types.map do |type|
80
+ escape_filter(solr_field_for(type, 'user'), user.user_key)
81
+ end
82
+ end
83
+
84
+ # @param [#to_s] permission_type a single value, e.g. "read" or "discover"
85
+ # @param [#to_s] permission_category a single value, e.g. "group" or "person"
86
+ # @return [String] name of the solr field for this type of permission
87
+ # @example return values: "read_access_group_ssim" or "discover_access_person_ssim"
88
+ def solr_field_for(permission_type, permission_category)
89
+ method_name = "#{permission_type}_#{permission_category}_field".to_sym
90
+ Blacklight::AccessControls.config.send(method_name)
91
+ end
92
+
93
+ def escape_filter(key, value)
94
+ [key, escape_value(value)].join(':')
95
+ end
96
+
97
+ def escape_value(value)
98
+ RSolr.solr_escape(value).gsub(/ /, '\ ')
99
+ end
100
+ end
101
+ end
102
+ end
@@ -3,7 +3,6 @@
3
3
  module Blacklight
4
4
  class AccessControlsGenerator < Rails::Generators::Base
5
5
  desc "This generator makes the following changes to your application:
6
-
7
6
  1. Includes Blacklight::AccessControls::User in the User class.
8
7
  2. Includes Blacklight::AccessControls::Enforcement in the SearchBuilder class.
9
8
  3. Adds access controls to CatalogController.
@@ -22,32 +21,23 @@ module Blacklight
22
21
  def add_access_controls_to_user
23
22
  say_status('status', 'ADDING ACCESS CONTROLS TO USER MODEL', :yellow)
24
23
  insert_into_file File.join('app', 'models', "#{options[:user_model].underscore}.rb"),
25
- " include Blacklight::AccessControls::User\n\n",
26
- after: "include Blacklight::User\n"
27
- end
28
-
29
- def add_access_controls_to_search_builder
30
- say_status('status', 'ADDING ACCESS CONTROLS TO SEARCH BUILDERS', :yellow)
31
- options[:search_builders].each do |file_path|
32
- insert_into_file file_path,
33
- " include Blacklight::AccessControls::Enforcement\n\n",
34
- after: "include Blacklight::Solr::SearchBuilderBehavior\n"
35
- end
24
+ " include Blacklight::AccessControls::User\n\n",
25
+ after: "include Blacklight::User\n"
36
26
  end
37
27
 
38
28
  def add_access_controls_to_catalog_controller
39
29
  say_status('status', 'ADDING ACCESS CONTROLS TO CATALOG CONTROLLER', :yellow)
40
30
 
41
- string_to_insert = <<-EOS
31
+ string_to_insert = <<-ADDITIONS
42
32
  include Blacklight::AccessControls::Catalog
43
33
 
44
34
  # Apply the blacklight-access_controls
45
35
  before_action :enforce_show_permissions, only: :show
46
36
 
47
- EOS
37
+ ADDITIONS
48
38
 
49
39
  insert_into_file 'app/controllers/catalog_controller.rb',
50
- string_to_insert, after: "include Blacklight::Catalog\n"
40
+ string_to_insert, after: "include Blacklight::Catalog\n"
51
41
  end
52
42
 
53
43
  def add_cancan_ability