strongmind-auth 1.1.100 → 1.1.104

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: 557c4edfda589ee28700958fd64124446c9baa854f8e64d938ef84ebf4349c27
4
- data.tar.gz: 7a4fda48941151d352c683c774c267fab93fd21133798e70443c2dca848c75d3
3
+ metadata.gz: e28c44ee15358c2cd005c765701a0bae728a8f731a26bd983cc89b98f22f93a0
4
+ data.tar.gz: f9002365819bfc3cb943d6b432406d4aaf8c0874bf661abff9f86ffda05727a7
5
5
  SHA512:
6
- metadata.gz: 4bb2fb0f665c02d3e2108bb9f5c9bd5b895ca5d1b92821357d5d37de67aa8271d5c8bca8f94e8d8c7d348dc8a9d5795793ecb21f5ae78f78fedbf46563620792
7
- data.tar.gz: cc8b758f4b1144b203d16ab61e469c4d165e1a7c3e1e89109dcf3372715084bfb1ca9149a6612118fa0df65d163658872975f2ae4af16e9c104fba9273624fd0
6
+ metadata.gz: 4105b0026dab50a2394d3bdbd788bb4bb921fe268670b2a4020688568dbb34d02a47c4fc32ddea6c7aea8600226a3dc6fd1c7090d3e11f9b80730caf4f6c1036
7
+ data.tar.gz: 0dbc0726df726ff5ae0baf5e1c6e3548d55b239f9da13ef48a248f4ffc16e24b267656a973a2572954d92bc982b09ae452a3ae8b9cde0ce5afe2370f33e02a8d
@@ -6,7 +6,12 @@ module StrongMindNav
6
6
 
7
7
  def fetch_common_nav
8
8
  begin
9
- navbar = Strongmind::CommonNavFetcher.new(current_user, request).retrieve(common_nav_options)
9
+ nav_items = build_nav_items
10
+
11
+ user_cache_key_encoded = generate_user_cache_key(nav_items)
12
+ navbar = Rails.cache.fetch(user_cache_key_encoded, expires_in: 30.seconds) do
13
+ Strongmind::CommonNavFetcher.new(current_user).retrieve(nav_items, common_nav_options)
14
+ end
10
15
 
11
16
  @top_navbar_html = navbar[:top_navbar_html]
12
17
  @bottom_navbar_html = navbar[:bottom_navbar_html]
@@ -33,9 +38,55 @@ module StrongMindNav
33
38
 
34
39
  def common_nav_options
35
40
  {
36
- menu_items: menu_items,
37
41
  authenticity_token: form_authenticity_token,
38
42
  show_role_switcher: false
39
43
  }
40
44
  end
45
+
46
+ def build_nav_items
47
+ items = { nav_items: menu_items.map { |item| nav_item_data(item) } }
48
+ items[:nav_items].first[:is_active] = true unless items[:nav_items].any? { |item| item[:is_active] }
49
+ items
50
+ end
51
+
52
+ def nav_item_data(item)
53
+ url = item[:url]
54
+ {
55
+ name: item[:name],
56
+ icon: item[:icon],
57
+ url: url,
58
+ is_active: current_page?(url)
59
+ }
60
+ end
61
+
62
+ private
63
+
64
+ def current_page?(url)
65
+ request.fullpath == URI.parse(url).path || path_in_request?(url)
66
+ end
67
+
68
+ def path_in_request?(path)
69
+ sanitized_path = path.gsub('/', '')
70
+
71
+ path_to_compare = request.fullpath.split('?').first
72
+
73
+ return path_to_compare == '/' if sanitized_path.empty?
74
+ path_to_compare.include?(sanitized_path) || path_to_compare.include?(sanitized_path.pluralize)
75
+ end
76
+
77
+ def generate_user_cache_key(nav_items)
78
+ navbar_options_cache_key = nav_items.to_json
79
+ session_token = session[:_csrf_token]
80
+ user = "user_#{current_user.uid}"
81
+
82
+ user_cache_key = if current_user.respond_to?(:selected_role_id)
83
+ role = "role_#{current_user.selected_role_id}"
84
+
85
+ [session_token, user, role, navbar_options_cache_key, 'navbar'].join('_')
86
+ else
87
+ [session_token, user, navbar_options_cache_key, 'navbar'].join('_')
88
+ end
89
+
90
+ Digest::SHA256.hexdigest(user_cache_key)
91
+ end
41
92
  end
@@ -1,5 +1,5 @@
1
1
  module Strongmind
2
2
  module Auth
3
- VERSION = "1.1.100"
3
+ VERSION = "1.1.104"
4
4
  end
5
5
  end
@@ -5,7 +5,7 @@ require 'platform_sdk'
5
5
  module Strongmind
6
6
  # This class is responsible for fetching the common navbar data from SM Central
7
7
  class CommonNavFetcher
8
- attr_reader :user, :request, :auth_client
8
+ attr_reader :user, :auth_client
9
9
 
10
10
  include Rails.application.routes.url_helpers
11
11
 
@@ -13,40 +13,25 @@ module Strongmind
13
13
 
14
14
  class UserNotFoundError < StandardError; end
15
15
 
16
- def initialize(user, request)
16
+ def initialize(user)
17
17
  raise Strongmind::Exceptions::UserNotFoundError, 'User not found' unless user.present?
18
- raise ArgumentError, 'Request not found' unless request.present?
19
18
  raise ArgumentError, 'Identity base URL not found at IDENTITY_BASE_URL in environment' unless ENV['IDENTITY_BASE_URL'].present?
20
19
  raise ArgumentError, 'Identity client ID not found at IDENTITY_CLIENT_ID in environment' unless ENV['IDENTITY_CLIENT_ID'].present?
21
20
  raise ArgumentError, 'Identity client secret not found at IDENTITY_CLIENT_SECRET in environment' unless ENV['IDENTITY_CLIENT_SECRET'].present?
22
21
 
23
22
  @user = user
24
- @request = request
25
23
  @auth_client = PlatformSdk::Identity::AuthClient.new(
26
24
  ENV.fetch('IDENTITY_BASE_URL'), ENV.fetch('IDENTITY_CLIENT_ID'), ENV.fetch('IDENTITY_CLIENT_SECRET')
27
25
  )
28
26
  end
29
27
 
30
- def retrieve(nav_options)
31
- response = fetch_navbar_data(build_nav_items(nav_options[:menu_items]), nav_options)
28
+ def retrieve(nav_items, nav_options)
29
+ response = fetch_navbar_data(nav_items, nav_options)
32
30
  parse_navbar(response)
33
31
  end
34
32
 
35
33
  private
36
34
 
37
- def current_page?(url)
38
- request.fullpath == URI.parse(url).path || path_in_request?(url)
39
- end
40
-
41
- def path_in_request?(path)
42
- sanitized_path = path.gsub('/', '')
43
-
44
- path_to_compare = request.fullpath.split('?').first
45
-
46
- return path_to_compare == '/' if sanitized_path.empty?
47
- path_to_compare.include?(sanitized_path) || path_to_compare.include?(sanitized_path.pluralize)
48
- end
49
-
50
35
  def fetch_navbar_data(nav_items, nav_options)
51
36
  session = refresh_session
52
37
  access_token = session[:access_token]
@@ -95,21 +80,5 @@ module Strongmind
95
80
  def parse_navbar(response)
96
81
  JSON.parse(response.body, symbolize_names: true) if response
97
82
  end
98
-
99
- def build_nav_items(menu_items)
100
- items = { nav_items: menu_items.map { |item| nav_item_data(item) } }
101
- items[:nav_items].first[:is_active] = true unless items[:nav_items].any? { |item| item[:is_active] }
102
- items
103
- end
104
-
105
- def nav_item_data(item)
106
- url = item[:url]
107
- {
108
- name: item[:name],
109
- icon: item[:icon],
110
- url: url,
111
- is_active: current_page?(url)
112
- }
113
- end
114
83
  end
115
84
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strongmind-auth
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.100
4
+ version: 1.1.104
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Belding
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-26 00:00:00.000000000 Z
11
+ date: 2024-08-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails