Module: UcbRailsUser::Concerns::ControllerMethods

Extended by:
ActiveSupport::Concern
Included in:
HomeController
Defined in:
app/controllers/ucb_rails_user/concerns/controller_methods.rb

Instance Method Summary collapse

Instance Method Details

#current_ldap_personUCB::LDAP::Person?

Returns an instance of UCB::LDAP::Person if there is a logged in user

Returns:

  • (UCB::LDAP::Person)

    if user logged in

  • (nil)

    if user not logged in



53
54
55
56
57
58
59
60
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 53

def current_ldap_person
  if logged_in?
    @current_ldap_person ||= begin
      logger.debug 'recalc of current_ldap_person'
      user_session_manager.people_ou_entry(session[:uid])
    end
  end
end

#current_userObject



28
29
30
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 28

def current_user
  logged_in_user&.impersonation_target || logged_in_user
end

#ensure_admin_userObject



62
63
64
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 62

def ensure_admin_user
  superuser? or not_authorized!
end

#ensure_authenticated_usernil

Before filter that redirects redirects to login_url unless user is logged in

Returns:

  • (nil)


69
70
71
72
73
74
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 69

def ensure_authenticated_user
  unless session.has_key?(:uid)
    session[:original_url] = request.env['REQUEST_URI']
    redirect_to 
  end
end

#log_requestObject



40
41
42
43
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 40

def log_request
  UcbRailsUser::UserSessionManager::Base.current_user = current_user
  user_session_manager.log_request(current_user)
end

#logged_in?true, false

Returns true if there is a logged in user

Returns:

  • (true)

    if user logged in

  • (false)

    if user not logged in



36
37
38
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 36

def logged_in?
  logged_in_user.present?
end

#logged_in_userObject



21
22
23
24
25
26
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 21

def logged_in_user
  @logged_in_user ||= begin
    logger.debug 'recalc of logged_in_user'
    user_session_manager&.current_user(session[:uid])
  end
end

#not_authorized!Object



86
87
88
89
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 86

def not_authorized!
  render plain: "Not Authorized", status: 401
  return false
end

#not_authorized_unless(condition) ⇒ Object



91
92
93
94
95
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 91

def not_authorized_unless(condition)
  unless condition
    not_authorized!
  end
end

#remove_user_settingsObject



45
46
47
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 45

def 
  UcbRailsUser::UserSessionManager::Base.current_user = nil
end

#superuser?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 17

def superuser?
  current_user.try(:superuser?)
end

#user_session_managerObject



76
77
78
79
80
81
82
83
84
# File 'app/controllers/ucb_rails_user/concerns/controller_methods.rb', line 76

def user_session_manager
  @user_session_manager ||= begin
    logger.debug "creating new user_session_manager"
    klass = UcbRailsUser[:user_session_manager] || UcbRailsUser::UserSessionManager::ActiveInUserTable
    klass.to_s.classify.constantize.new
  end
rescue NameError
  raise "Could not find UcbRailsUser user_session_manager: #{klass}"
end