Sha256: ec2d5302eeb750c521cc2c873515067ecf30f66b19d448630863c26108a910be

Contents?: true

Size: 1.05 KB

Versions: 10

Compression:

Stored size: 1.05 KB

Contents

module Devise
  module Models
    module Masqueradable
      def self.included(base)
        base.class_eval do
          attr_reader :masquerade_key

          include InstanceMethods
          extend ClassMethods
        end
      end

      module InstanceMethods
        def masquerade!
          @masquerade_key = SecureRandom.urlsafe_base64(Devise.masquerade_key_size)
          Rails.cache.write("#{self.class.name.pluralize.underscore}:#{@masquerade_key}:masquerade", id, :expires_in => Devise.masquerade_expires_in)
        end
      end

      module ClassMethods
        def cache_masquerade_key_by(key)
          "#{self.name.pluralize.underscore}:#{key}:masquerade"
        end

        def remove_masquerade_key!(key)
          Rails.cache.delete(cache_masquerade_key_by(key))
        end

        def find_by_masquerade_key(key)
          id = Rails.cache.read(cache_masquerade_key_by(key))

          # clean up the cached masquerade key value
          remove_masquerade_key!(key)

          find_by_id(id)
        end
      end # ClassMethods
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
devise_masquerade-0.5.0 lib/devise_masquerade/model.rb
devise_masquerade-0.4.0 lib/devise_masquerade/model.rb
devise_masquerade-0.3.1 lib/devise_masquerade/model.rb
devise_masquerade-0.3.0 lib/devise_masquerade/model.rb
devise_masquerade-0.2.0 lib/devise_masquerade/model.rb
devise_masquerade-0.1.8 lib/devise_masquerade/model.rb
devise_masquerade-0.1.7 lib/devise_masquerade/model.rb
devise_masquerade-0.1.6 lib/devise_masquerade/model.rb
devise_masquerade-0.1.5 lib/devise_masquerade/model.rb
devise_masquerade-0.1.4 lib/devise_masquerade/model.rb