eco-helpers 1.5.13 → 1.5.14

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
2
  SHA256:
3
- metadata.gz: f83b4bc20ff8289dbc743531ceaf75350fefbefa687c3f88ae09325b762e1fd1
4
- data.tar.gz: b6ecd929fc1494ba657755020eac2cd92bdfe1ad4b4e5beb454c2bcaea165b77
3
+ metadata.gz: 2627cbc4d42e91e131e891cc4248895660a62bab2c331b43ac8ad3cd28b9cdee
4
+ data.tar.gz: fb2c29b13a67114ce8bbfbd16b271519896c201ec58cb9fd37b48db238abb014
5
5
  SHA512:
6
- metadata.gz: 0fbb3710424b72248a2abefb0a0c8682a6fa4e97c69b50af951298c867582e4e857b5db25fd0afe1ba0a6379f6ba85e751a48c1bed1b091ad0a180f1d52f73df
7
- data.tar.gz: 16157a6f8f6118574bec5ca59716b9c52b860b89acab5a7d56bc720b34339de80b747543da684ddd68b23c694f0a67e6af82396f95c1b3811bcf203e7cadb7ee
6
+ metadata.gz: 6da008ea99e2e6e323afcfbc0be117dd56402b95be100a68d808d3b4e81537def757fb66418fc0028492ea2e12ac74def8d4c6c47a7d05589bac3814ce9e10d9
7
+ data.tar.gz: 78af32b3b9f4725211baf6e458db91c064f49f25814bbda7177fe61af1b453597d658f1d734e155c8ebeb863aabdd36a0b6b81a9fcd972c6d92f2911f05ac11f
data/CHANGELOG.md CHANGED
@@ -1,6 +1,26 @@
1
1
  # Change Log
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [1.5.14] - 2021-02-xx
5
+
6
+ ### Added
7
+ - `Eco::API::Policies::DefaultPolicies` policies that are run always (after the custom policies)
8
+ - `Eco::API::Policies::DefaultPolicies::UserAccess`:
9
+ - remove account if there's no `email`
10
+ - provision min user level to those with account (if no usergroup and no abilities)
11
+ - `Eco::API::Session#policies` which merges the default policies to be added/run after the custom policies
12
+ - `Eco::API::Common::Session::Logger`: add caching of logs
13
+ - `Eco::API::Common::Session::Logger::Cache` new class to manage cached logs
14
+ - `Eco::API::Common::Session::Logger::Log` new class to have a trace on logs
15
+ - `Eco::API::Session::Batch::Job#summary` added subjobs (error handlers) summary
16
+
17
+ ### Changed
18
+ - `Eco::API::Session::Batch::Job#launch` it will also run the default api policies before feedback and query launch
19
+ - `Eco::API::Common::People::PersonEntry` add error log when wrong email error is detected
20
+ - previously it would have crashed
21
+
22
+ ### Fixed
23
+
4
24
  ## [1.5.13] - 2021-02-01
5
25
 
6
26
  ### Added
@@ -170,14 +170,25 @@ module Eco
170
170
  end
171
171
 
172
172
  # Setter to fill in all the `core` properties of the `Person` that are present in the `Entry`.
173
- # @note it only sets those core properties defined in the entry.
174
- # Meaning that if an core property is not present in the entry, this will not be set on the target person.
173
+ # @note
174
+ # 1. it only sets those core properties defined in the entry.
175
+ # Meaning that if an core property is not present in the entry, this will not be set on the target person.
176
+ # 2. if there's an incorrect email exception, it blanks the email and logs a warning message
175
177
  # @param person [Ecoportal::API::V1::Person] the person we want to set the core values to.
176
178
  # @param exclude [String, Array<String>] core attributes that should not be set/changed to the person.
177
179
  def set_core(person, exclude: nil)
178
180
  scoped_attrs = @emap.core_attrs - into_a(exclude)
179
181
  @final_entry.slice(*scoped_attrs).each do |attr, value|
180
- set_part(person, attr, value)
182
+ begin
183
+ set_part(person, attr, value)
184
+ rescue Exception => e
185
+ if attr == "email"
186
+ logger.error(e.to_s + " - setting blank email instead.")
187
+ set_part(person, attr, nil)
188
+ else
189
+ raise
190
+ end
191
+ end
181
192
  end
182
193
  end
183
194
 
@@ -460,6 +471,7 @@ module Eco
460
471
  obj.send("#{attr}=", value)
461
472
  end
462
473
  rescue Exception => e
474
+ # add more info to the error
463
475
  raise e.append_message " -- Entry #{to_s(:identify)}"
464
476
  end
465
477
  end
@@ -5,9 +5,12 @@ module Eco
5
5
  class Logger
6
6
  DEFAULT_TIMESTAMP_PATTERN = '%Y-%m-%dT%H:%M:%S'
7
7
 
8
+ attr_reader :cache
9
+
8
10
  def initialize(console_level: nil, file_level: ::Logger::DEBUG, log_file: nil, timestamp_console: false, enviro: nil)
9
11
  raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment)
10
12
  @enviro = enviro
13
+ @cache = Logger::Cache.new
11
14
 
12
15
  timestamp_console = fetch_timestamp_console(timestamp_console)
13
16
  @console_logger = ::Logger.new(STDOUT).tap do |_logger|
@@ -21,7 +24,9 @@ module Eco
21
24
  if log_file = fetch_log_file(log_file)
22
25
  @file_logger = ::Logger.new(log_file).tap do |_logger|
23
26
  _logger.formatter = proc do |severity, datetime, progname, msg|
24
- "#{severity.to_s[0]}: #{datetime.strftime(DEFAULT_TIMESTAMP_PATTERN)} > #{msg}\n"
27
+ "#{severity.to_s[0]}: #{datetime.strftime(DEFAULT_TIMESTAMP_PATTERN)} > #{msg}\n".tap do |formatted|
28
+ cache.add(severity, datetime, msg, formatted)
29
+ end
25
30
  end
26
31
  _logger.level = fetch_file_level(file_level)
27
32
  end
@@ -107,3 +112,6 @@ module Eco
107
112
  end
108
113
  end
109
114
  end
115
+
116
+ require_relative "logger/cache"
117
+ require_relative "logger/log"
@@ -0,0 +1,91 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module Session
5
+ class Logger
6
+ class Cache
7
+ LEVELS = ["UNKNOWN", "FATAL", "ERROR", "WARN", "INFO", "DEBUG"]
8
+
9
+ def initialize
10
+ reset
11
+ end
12
+
13
+ def level(level)
14
+ cache[to_level(level)] ||= []
15
+ end
16
+
17
+ def add(level, datetime, message, formatted)
18
+ Logger::Log.new(level, datetime, message, formatted).tap do |log|
19
+ self.level(level).push(log)
20
+ end
21
+ end
22
+
23
+ def cache
24
+ @cache ||= {}
25
+ end
26
+
27
+ def reset(level: nil, start_time: nil, end_time: nil)
28
+ where(start_time, end_time) do |cond|
29
+ to_levels(level).map do |lev|
30
+ self.level(lev).reject(&cond)
31
+ end
32
+ end
33
+ self
34
+ end
35
+
36
+ def logs(level: nil, start_time: nil, end_time: nil)
37
+ where(start_time, end_time) do |cond|
38
+ to_levels(level).map do |lev|
39
+ self.level(lev).select(&cond)
40
+ end.flatten
41
+ end.sort
42
+ end
43
+
44
+ private
45
+
46
+ def where(start_time, end_time)
47
+ tstart = to_datetime(start_time)
48
+ tend = to_datetime(end_time)
49
+ condition = Proc.new do |log|
50
+ next true unless tstart || tend
51
+ log.after?(tstart) && log.before?(tend)
52
+ end
53
+
54
+ yield(condition)
55
+ end
56
+
57
+ def to_datetime(value)
58
+ return nil unless value
59
+ Time.parse(value)
60
+ end
61
+
62
+ def to_levels(value)
63
+ levels = [value].flatten.map {|v| to_level(v)}.compact
64
+ levels = levels.empty?? LEVELS : levels
65
+ end
66
+
67
+ def to_level(value)
68
+ nil_or_upcase(value).tap do |out|
69
+ valid_level!(out)
70
+ end
71
+ end
72
+
73
+ def valid_level!(str)
74
+ return true if !str
75
+ unless LEVELS.any? {|lev| str == lev}
76
+ raise "Unknown level #{str}. Should be one of #{LEVELS}"
77
+ end
78
+ end
79
+
80
+ def nil_or_upcase(value)
81
+ value = value.to_s.upcase if value
82
+ return yield(value) if block_given?
83
+ value
84
+ end
85
+
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,48 @@
1
+ module Eco
2
+ module API
3
+ module Common
4
+ module Session
5
+ class Logger
6
+ class Log
7
+
8
+ attr_accessor :level, :datetime, :message, :formatted
9
+
10
+ def initialize(level, datetime, message, formatted)
11
+ @level = level
12
+ @datetime = datetime
13
+ @message = message
14
+ @formatted = formatted
15
+ end
16
+
17
+ def to_s
18
+ self.formatted
19
+ end
20
+
21
+ def <=>(other)
22
+ self.datetime <=> other.datetime
23
+ end
24
+
25
+ def before?(value)
26
+ return true if !value
27
+ datetime <= to_datetime(value)
28
+ end
29
+
30
+ def after?(value)
31
+ return true if !value
32
+ datetime >= to_datetime(value)
33
+ end
34
+
35
+ private
36
+
37
+ def to_datetime(value)
38
+ return value if value.is_a?(Time)
39
+ return nil unless value
40
+ Time.parse(value)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -36,3 +36,4 @@ module Eco
36
36
  end
37
37
 
38
38
  require_relative 'policies/policy'
39
+ require_relative 'policies/default_policies'
@@ -0,0 +1,12 @@
1
+ module Eco
2
+ module API
3
+ class Policies
4
+ class DefaultPolicies < Eco::API::Policies
5
+ autoloads_children_of "Eco::API::Common::Loaders::Policy"
6
+ autoload_namespace "Eco::API::Policies::DefaultPolicies"
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ require_relative 'default_policies/99_user_access_policy'
@@ -0,0 +1,100 @@
1
+ class Eco::API::Policies::DefaultPolicies::UserAccess < Eco::API::Common::Loaders::Policy
2
+ name "default-user-access"
3
+
4
+ attr_reader :session, :options
5
+ attr_accessor :account_removed_count
6
+
7
+ def main(people, session, options, policy, job)
8
+ @session = session; @options = options
9
+
10
+ self.account_removed_count = 0
11
+
12
+ people.each do |person|
13
+ remove_account_when_no_email!(person) if person.email.to_s.empty?
14
+ account.policy_group_ids = [defid] if no_policy_group_ids?(person)
15
+ refresh_abilities!(person.account)
16
+ end
17
+
18
+ warn_account_removal!
19
+ end
20
+
21
+ private
22
+
23
+ def warn_account_removal!
24
+ if account_removed_count > 0
25
+ msg = "Removed account to #{account_removed_count} people"
26
+ session.logger.warn(msg)
27
+ end
28
+ end
29
+
30
+ def remove_account_when_no_email!(person)
31
+ if person.account
32
+ account_removed_count += 1 if had_account?(person)
33
+ person.account = nil
34
+ end
35
+ end
36
+
37
+ def had_account?(person)
38
+ return false if person.new?
39
+ return false if person.account_added?
40
+ return !!person.original_doc["account"]
41
+ end
42
+
43
+ def provision_basic_level!(person)
44
+ if account = person.account
45
+
46
+ unless options.dig(:exclude, :abilities)
47
+ account.permissions_custom = session.new_preset(person)
48
+
49
+ if no_abilities?(person)
50
+ account.permissions_custom = min_abilities
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+
57
+ def refresh_abilities!(account)
58
+ return nil unless account
59
+
60
+ end
61
+
62
+ def no_policy_group_ids?(person)
63
+ (account = person.account) && account.policy_group_ids.empty?
64
+ end
65
+
66
+ def no_abilities?(person)
67
+ person.account.permissions_custom &&
68
+ person.account.permissions_custom.values.all?(&:nil?)
69
+ end
70
+
71
+ def min_abilities
72
+ {
73
+ "files" => "upload",
74
+ "data" => nil,
75
+ "reports" => nil,
76
+ "pages" => "create",
77
+ "page_editor" => "basic",
78
+ "registers" => "view",
79
+ "organization" => nil,
80
+ "person_core" => "attach",
81
+ "person_core_edit" => nil,
82
+ "person_core_create" => nil,
83
+ "person_details" => "view",
84
+ "person_account" => nil
85
+ }
86
+ end
87
+
88
+ def defid
89
+ @defid ||= policy_groups.to_id(default_group)
90
+ end
91
+
92
+ def default_group
93
+ session.config.people.default_usergroup
94
+ end
95
+
96
+ def pgs
97
+ session.policy_groups
98
+ end
99
+
100
+ end
@@ -187,6 +187,8 @@ module Eco
187
187
  end
188
188
 
189
189
  # Does merge `Eco::API::UseCases::DefaultCases` with the custom cases.
190
+ # @note
191
+ # - the order matters, as a default usecase can be redefined by a custom one with same name
190
192
  # @return [Eco::API::UseCases]
191
193
  def usecases
192
194
  @usecases ||= config.usecases.dup.tap do |cases|
@@ -195,6 +197,17 @@ module Eco
195
197
  end
196
198
  end
197
199
 
200
+ # Does merge `Eco::API::Policies::DefaultPolicies` with the custom policies.
201
+ # @note
202
+ # - the default policies are added at the end (meaning they will run after the custom policies)
203
+ # @return [Eco::API::Policies]
204
+ def policies
205
+ @policies ||= config.policies.dup.tap do |policies|
206
+ default_policies = Eco::API::Policies::DefaultPolicies.new
207
+ policies.merge(default_policies)
208
+ end
209
+ end
210
+
198
211
  # Set of helpers to simplify your code
199
212
  # @see Eco::API::MicroCases
200
213
  # @return [Eco::API::MicroCases]
@@ -71,12 +71,6 @@ module Eco
71
71
  @subjobs ||= Eco::API::Session::Batch::Jobs.new(enviro, name: "childs-of:#{self.name}")
72
72
  end
73
73
 
74
- def subjobs_add(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase, &block)
75
- dup(name, usecase: usecase).tap do |subjob|
76
- subjobs.add(subjob, &block)
77
- end
78
- end
79
-
80
74
  # @return [Boolean] `true` if the current batch job is a result of an error_handler
81
75
  def error_handler?
82
76
  usecase? && usecase.is_a?(Eco::API::Error::Handler)
@@ -211,6 +205,7 @@ module Eco
211
205
  end
212
206
 
213
207
  msg << status.errors.message unless !status
208
+ msg << subjobs_summary
214
209
  end
215
210
  end.join("\n")
216
211
  end
@@ -238,7 +233,7 @@ module Eco
238
233
  # Applies the changes introduced by api policies
239
234
  def apply_policies(pre_queue)
240
235
  people(pre_queue).tap do |entries|
241
- policies = session.config.policies
236
+ policies = session.policies
242
237
  unless policies.empty? || options.dig(:skip, :api_policies)
243
238
  policies.launch(people: entries, session: session, options: options, job: self)
244
239
  end
@@ -307,6 +302,21 @@ module Eco
307
302
  file_manager.save_json(requests, file, :timestamp)
308
303
  end
309
304
 
305
+ # Adds a job tied to the current job
306
+ # Used with error handlers that need their own job to run
307
+ def subjobs_add(name = "ad-hoc:job-from:#{self.name}", usecase: self.usecase, &block)
308
+ dup(name, usecase: usecase).tap do |subjob|
309
+ subjobs.add(subjob, &block)
310
+ end
311
+ end
312
+
313
+ def subjobs_summary
314
+ return "" unless subjobs.count > 0
315
+ [].tap do |msg|
316
+ subjobs.map {|subjob| msg << subjob.summary}
317
+ end.join("\n")
318
+ end
319
+
310
320
  end
311
321
  end
312
322
  end