Class: Locomotive::Steam::Middlewares::ImpersonatedEntry

Inherits:
ThreadSafe
  • Object
show all
Includes:
Concerns::AuthHelpers, Concerns::Helpers
Defined in:
lib/locomotive/steam/middlewares/impersonated_entry.rb

Overview

If an account defined by a content type was impersonated from the back-office, load the entry and sign her/him in automatically.

Besides, display a small banner at the top of the page in order to let the administrator stop the impersonation of the account.

Constant Summary

Constants included from Concerns::Helpers

Concerns::Helpers::CACHE_HEADERS, Concerns::Helpers::HTML_CONTENT_TYPE, Concerns::Helpers::HTML_MIME_TYPES

Instance Attribute Summary

Attributes inherited from ThreadSafe

#env

Instance Method Summary collapse

Methods included from Concerns::AuthHelpers

#authenticated?, #authenticated_entry_type, #store_authenticated

Methods included from Concerns::Helpers

#html?, #inject_cookies, #json?, #log, #make_local_path, #modify_path, #mounted_on, #redirect_to, #render_response

Methods inherited from ThreadSafe

#call, #decorate_entry, #default_liquid_context, #default_locale, #liquid_assigns, #live_editing?, #locale, #locales, #merge_with_params, #page, #params, #path, #repositories, #request, #services, #session, #site

Instance Method Details

#_callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/locomotive/steam/middlewares/impersonated_entry.rb', line 15

def _call
  if params[:impersonating] == 'stop'

    # notify sign out
    services.auth.notify(:signed_out, request.env['steam.authenticated_entry'], request)

    # sign out the current user
    store_authenticated(nil)

    # leave the impersonating mode
    request.session[:authenticated_impersonation] = '0'

    # redirect the user to the same page
    redirect_to path, 302
  elsif authenticated? && is_impersonating?
    # useful if other middlewares need this information
    env['steam.impersonating_authenticated_entry'] = true

    # again, useful if the developer needs to add a "impersonate" button
    # directly in her/his website in any Liquid template
    liquid_assigns["is_impersonating_current_#{authenticated_entry_type.singularize}"] = true
  end
end

#nextObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locomotive/steam/middlewares/impersonated_entry.rb', line 39

def next
  response = super

  if authenticated? && is_impersonating? && html?
    # get the authenticated entry from the Auth middleware
    entry = env['steam.authenticated_entry']

    # modify the HTML body by adding a banner at the bottom of the page
    status, headers, body = response
    [status, headers, [body.first.gsub(/(<body[^>]*>)/, '\1' +  banner_html(entry))]]
  else
    response
  end
end