Sha256: f348d2965e2aecb926c4fcc90fbf716be57927b8cc9f83185367a5feb082f6bb

Contents?: true

Size: 1.65 KB

Versions: 10

Compression:

Stored size: 1.65 KB

Contents

module Bugsnag::Middleware
  class WardenUser
    SCOPE_PATTERN = /^warden\.user\.([^.]+)\.key$/
    COMMON_USER_FIELDS = [:email, :name, :first_name, :last_name, :created_at]

    def initialize(bugsnag)
      @bugsnag = bugsnag
    end

    def call(notification)
      if notification.request_data[:rack_env] && notification.request_data[:rack_env]["warden"]
        env = notification.request_data[:rack_env]
        session = env["rack.session"] || {}

        # Find all warden user scopes
        warden_scopes = session.keys.select {|k| k.match(SCOPE_PATTERN)}.map {|k| k.gsub(SCOPE_PATTERN, '\1')}
        unless warden_scopes.empty?
          # Pick the best scope for unique id (the default is "user")
          best_scope = warden_scopes.include?("user") ? "user" : warden_scopes.first

          # Set the user_id
          if best_scope
            user_id = session[best_scope][1][0] rescue nil
            notification.user_id = user_id unless user_id.nil?
          end

          # Extract useful user information
          warden_tab = {}
          warden_scopes.each do |scope|
            user_object = env["warden"].user({:scope => scope, :run_callbacks => false}) rescue nil
            if user_object
              # Build the user info for this scope
              scope_hash = warden_tab["Warden #{scope.capitalize}"] = {}
              COMMON_USER_FIELDS.each do |field|
                scope_hash[field] = user_object.send(field) if user_object.respond_to?(field)
              end
            end
          end

          notification.add_tab(:user, warden_tab) unless warden_tab.empty?
        end
      end

      @bugsnag.call(notification)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
bugsnag-1.2.8 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.7 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.6 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.5 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.4 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.3 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.2 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.1 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.0 lib/bugsnag/middleware/warden_user.rb
bugsnag-1.2.0.beta lib/bugsnag/middleware/warden_user.rb