Class: JwtAuthCognito::UserDataService
- Inherits:
-
Object
- Object
- JwtAuthCognito::UserDataService
- Defined in:
- lib/jwt_auth_cognito/user_data_service.rb
Constant Summary collapse
- DEFAULT_CACHE_TTL =
5 minutes
300
Instance Method Summary collapse
- #clear_all_cache ⇒ Object
- #clear_user_cache(user_id) ⇒ Object
- #get_app_roles(app_id, organization_id) ⇒ Object
- #get_app_schema(app_id) ⇒ Object
- #get_application(app_id) ⇒ Object
- #get_comprehensive_user_data(user_id) ⇒ Object
- #get_effective_permissions(user_id, app_id, organization_id) ⇒ Object
- #get_organization(app_id, organization_id) ⇒ Object
- #get_user_applications(user_id) ⇒ Object
- #get_user_organizations(user_id) ⇒ Object
- #get_user_permissions(user_id) ⇒ Object
-
#initialize(redis_service = nil, config = {}) ⇒ UserDataService
constructor
A new instance of UserDataService.
- #initialize! ⇒ Object
- #initialized? ⇒ Boolean
- #shutdown ⇒ Object
- #stats ⇒ Object
Constructor Details
#initialize(redis_service = nil, config = {}) ⇒ UserDataService
Returns a new instance of UserDataService.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 9 def initialize(redis_service = nil, config = {}) @redis_service = redis_service || RedisService.new @config = { enable_user_data_retrieval: true, include_applications: true, include_organizations: true, include_roles: true, include_effective_permissions: false, cache_timeout: DEFAULT_CACHE_TTL }.merge(config) @cache = {} = {} @stats = { service: 'UserDataService', connection_status: 'not-initialized', initialized: false, cache_hits: 0, cache_misses: 0 } end |
Instance Method Details
#clear_all_cache ⇒ Object
289 290 291 292 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 289 def clear_all_cache @cache.clear .clear end |
#clear_user_cache(user_id) ⇒ Object
281 282 283 284 285 286 287 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 281 def clear_user_cache(user_id) keys_to_remove = @cache.keys.select { |key| key.include?(user_id) } keys_to_remove.each do |key| @cache.delete(key) .delete(key) end end |
#get_app_roles(app_id, organization_id) ⇒ Object
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 134 def get_app_roles(app_id, organization_id) return nil unless @config[:include_roles] cache_key = "app_roles:#{app_id}:#{organization_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = "app:roles:#{app_id}:#{organization_id}" data = @redis_service.get(redis_key) return nil unless data roles = JSON.parse(data) set_in_cache(cache_key, roles) roles rescue StandardError => e puts "Error fetching app roles #{app_id}:#{organization_id}: #{e.message}" nil end end |
#get_app_schema(app_id) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 164 def get_app_schema(app_id) cache_key = "app_schema:#{app_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = 'app-schemas' data = @redis_service.get(redis_key) return nil unless data schemas = JSON.parse(data) schema = schemas[app_id] return nil unless schema set_in_cache(cache_key, schema) schema rescue StandardError => e puts "Error fetching app schema for #{app_id}: #{e.message}" nil end end |
#get_application(app_id) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 74 def get_application(app_id) return nil unless @config[:include_applications] cache_key = "app:#{app_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = "app:#{app_id}" data = @redis_service.get(redis_key) return nil unless data application = JSON.parse(data) set_in_cache(cache_key, application) application rescue StandardError => e puts "Error fetching application #{app_id}: #{e.message}" nil end end |
#get_comprehensive_user_data(user_id) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 269 def get_comprehensive_user_data(user_id) = (user_id) organizations = get_user_organizations(user_id) applications = get_user_applications(user_id) { 'permissions' => , 'organizations' => organizations, 'applications' => applications } end |
#get_effective_permissions(user_id, app_id, organization_id) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 195 def (user_id, app_id, organization_id) return nil unless @config[:include_effective_permissions] cache_key = "effective_permissions:#{user_id}:#{app_id}:#{organization_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = "permissions:cache:#{user_id}:#{app_id}:#{organization_id}" data = @redis_service.get(redis_key) return nil unless data = JSON.parse(data) # Cache with shorter TTL for permission cache set_in_cache(cache_key, , (@config[:cache_timeout] / 2).to_i) rescue StandardError => e puts "Error fetching effective permissions #{user_id}:#{app_id}:#{organization_id}: #{e.message}" nil end end |
#get_organization(app_id, organization_id) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 104 def get_organization(app_id, organization_id) return nil unless @config[:include_organizations] cache_key = "org:#{app_id}:#{organization_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = "org:#{app_id}:#{organization_id}" data = @redis_service.get(redis_key) return nil unless data organization = JSON.parse(data) set_in_cache(cache_key, organization) organization rescue StandardError => e puts "Error fetching organization #{app_id}:#{organization_id}: #{e.message}" nil end end |
#get_user_applications(user_id) ⇒ Object
256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 256 def get_user_applications(user_id) user_organizations = get_user_organizations(user_id) unique_app_ids = user_organizations.map { |org| org['appId'] }.uniq applications = [] unique_app_ids.each do |app_id| app = get_application(app_id) applications << app if app && app['isActive'] end applications end |
#get_user_organizations(user_id) ⇒ Object
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 227 def get_user_organizations(user_id) = (user_id) return [] unless &.dig('permissions') user_organizations = [] ['permissions'].each do |app_id, orgs| orgs.each do |organization_id, org_data| next unless org_data['status'] == 'active' = nil = (user_id, app_id, organization_id) if @config[:include_effective_permissions] user_org = { 'appId' => app_id, 'organizationId' => organization_id, 'roles' => org_data['roles'], 'status' => org_data['status'] } user_org['effectivePermissions'] = if user_organizations << user_org end end user_organizations end |
#get_user_permissions(user_id) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 44 def (user_id) return nil unless @config[:enable_user_data_retrieval] cache_key = "user_permissions:#{user_id}" # Check cache first cached_data = get_from_cache(cache_key) if cached_data @stats[:cache_hits] += 1 return cached_data end @stats[:cache_misses] += 1 begin redis_key = "user:permissions:#{user_id}" data = @redis_service.get(redis_key) return nil unless data = JSON.parse(data) set_in_cache(cache_key, ) rescue StandardError => e puts "Error fetching user permissions for #{user_id}: #{e.message}" nil end end |
#initialize! ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 31 def initialize! @redis_service.initialize! unless @redis_service.connected? @stats[:initialized] = true @stats[:connection_status] = 'connected' puts '✅ UserDataService initialized successfully' rescue StandardError => e @stats[:initialized] = false @stats[:connection_status] = 'error' @stats[:error] = e. puts "❌ UserDataService initialization failed: #{e.message}" raise e end |
#initialized? ⇒ Boolean
298 299 300 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 298 def initialized? @stats[:initialized] && @redis_service.connected? end |
#shutdown ⇒ Object
302 303 304 305 306 307 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 302 def shutdown clear_all_cache @redis_service.disconnect if @redis_service.respond_to?(:disconnect) @stats[:initialized] = false @stats[:connection_status] = 'disconnected' end |
#stats ⇒ Object
294 295 296 |
# File 'lib/jwt_auth_cognito/user_data_service.rb', line 294 def stats @stats.dup end |