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



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

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)


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

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

#current_adminObject



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

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



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

def current_impersonation
  return @current_impersonation if defined?(@current_impersonation)
  @current_impersonation = User.find(session[:user_id]) rescue nil
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)
  update_tracking!(email: user.email)
end

#impersonating?(user = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


65
66
67
# File 'app/controllers/workarea/impersonation.rb', line 65

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

#stop_admin_guest_browsingObject



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

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

#stop_impersonationObject



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

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