Module: Workarea::Impersonation

Extended by:
ActiveSupport::Concern
Includes:
AdminGuestBrowsing, Authorization
Defined in:
app/controllers/workarea/impersonation.rb

Instance Method Summary collapse

Methods included from AdminGuestBrowsing

#admin_browse_as_guest, #admin_browsing_as_guest?, #stop_admin_guest_browsing, #touch_admin_guest_browsing

Methods included from Authentication

#current_user, #login, #logout, #redirect_back_or, #remember_location, #require_login, #require_logout, #require_password_changes, #return_to, #touch_auth_cookie

Methods included from Authorization

#authorized?, #check_authorization, #require_admin, #required_permissions, #unauthorized_user

Instance Method Details

#current_adminObject



43
44
45
46
47
48
49
50
# File 'app/controllers/workarea/impersonation.rb', line 43

def current_admin
  @current_admin ||=
    if impersonating? || admin_browsing_as_guest?
      User.find(session[:admin_id])
    elsif current_user.try(:admin?)
      current_user
    end
end

#current_impersonationObject



52
53
54
# File 'app/controllers/workarea/impersonation.rb', line 52

def current_impersonation
  @current_impersonation ||= User.find(cookies.signed[:user_id])
end

#impersonate_user(user) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/workarea/impersonation.rb', line 15

def impersonate_user(user)
  session[:admin_id] = current_user.id

  cookies.signed[:user_id] = {
    value: user.id,
    expires: Workarea.config.customer_session_timeout.from_now
  }

  user.mark_impersonated_by!(current_user)
  @current_user = user
end

#impersonating?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
# File 'app/controllers/workarea/impersonation.rb', line 36

def impersonating?(user = nil)
  session[:admin_id].present? &&
    cookies.signed[:user_id].present? &&
    session[:admin_id] != cookies.signed[:user_id] &&
    (user.blank? || user.id.to_s == cookies.signed[:user_id])
end

#keep_auth_aliveObject



63
64
65
66
67
68
69
# File 'app/controllers/workarea/impersonation.rb', line 63

def keep_auth_alive
  if impersonating?
    touch_impersonation
  else
    super
  end
end

#logged_in?Boolean

Override when impersonating to prevent IP address and user agent validation.

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'app/controllers/workarea/impersonation.rb', line 75

def logged_in?
  if impersonating?
    current_user.present?
  else
    super
  end
end

#stop_impersonationObject



27
28
29
30
31
32
33
34
# File 'app/controllers/workarea/impersonation.rb', line 27

def stop_impersonation
  cookies.signed[:user_id] = {
    value: current_admin.id,
    expires: Workarea.config.admin_session_timeout.from_now
  }

  session.delete(:admin_id)
end

#touch_impersonationObject



56
57
58
59
60
61
# File 'app/controllers/workarea/impersonation.rb', line 56

def touch_impersonation
  cookies.signed[:user_id] = {
    value: cookies.signed[:user_id],
    expires: Workarea.config.customer_session_timeout.from_now
  }
end