Module: Workarea::Impersonation

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

Instance Method Summary collapse

Methods included from Authorization

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

Instance Method Details

#admin_browse_as_guestObject



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

def admin_browse_as_guest
  session[:admin_id] = current_user.id.to_s
  session.delete(:user_id)
end

#admin_browsing_as_guest?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/controllers/workarea/impersonation.rb', line 50

def admin_browsing_as_guest?
  session[:admin_id].present? && session[:user_id].blank?
end

#current_adminObject



32
33
34
35
36
37
38
39
# File 'app/controllers/workarea/impersonation.rb', line 32

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



41
42
43
# File 'app/controllers/workarea/impersonation.rb', line 41

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

#impersonate_user(user) ⇒ Object



13
14
15
16
17
18
19
# File 'app/controllers/workarea/impersonation.rb', line 13

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

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

#impersonating?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'app/controllers/workarea/impersonation.rb', line 26

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

#logged_in?Boolean

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

Returns:

  • (Boolean)


63
64
65
# File 'app/controllers/workarea/impersonation.rb', line 63

def logged_in?
  super || (impersonating? && !admin_browsing_as_guest? && current_admin.present?)
end

#stop_admin_guest_browsingObject



54
55
56
57
# File 'app/controllers/workarea/impersonation.rb', line 54

def stop_admin_guest_browsing
  session[:user_id] = current_admin.id.to_s
  session.delete(:admin_id)
end

#stop_impersonationObject



21
22
23
24
# File 'app/controllers/workarea/impersonation.rb', line 21

def stop_impersonation
  session[:user_id] = current_admin.id.to_s
  session.delete(:admin_id)
end