Module: CamaleonCms::SessionHelper

Included in:
CamaleonController
Defined in:
app/helpers/camaleon_cms/session_helper.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

Instance Method Details

#cama_authenticate(redirect_uri = nil) ⇒ Object

check if a visitor was logged in if the user was not logged in, then redirect to login url


137
138
139
140
141
142
143
144
# File 'app/helpers/camaleon_cms/session_helper.rb', line 137

def cama_authenticate(redirect_uri = nil)
  params[:return_to] = redirect_uri
  unless cama_sign_in?
    flash[:error] = t('camaleon_cms.admin.login.please_login')
    cookies[:return_to] = params[:return_to].present? ? params[:return_to] : ((request.get? && params[:controller] != "admin/sessions") ? request.original_url : nil)
    redirect_to 
  end
end

#cama_current_roleObject

return the role for current user if not logged in, then return 'public'


113
114
115
# File 'app/helpers/camaleon_cms/session_helper.rb', line 113

def cama_current_role
  (cama_sign_in?) ? cama_current_user.role : 'public'
end

#cama_current_userObject Also known as: current_user

return current user logged in


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/camaleon_cms/session_helper.rb', line 118

def cama_current_user
  return @cama_current_user if defined?(@cama_current_user)
  # api current user...
  @cama_current_user = cama_calc_api_current_user
  return @cama_current_user unless @cama_current_user.nil?

  return nil unless cookies[:auth_token].present?
  c = cookies[:auth_token].split("&")
  return nil unless c.size == 3

  if c[1] == request.user_agent && request.ip == c[2]
    @cama_current_user = (current_site.users_include_admins.find_by_auth_token(c[0]).decorate rescue nil)
  end
end

#cama_get_session_idObject

return the session id


147
148
149
150
# File 'app/helpers/camaleon_cms/session_helper.rb', line 147

def cama_get_session_id
  session[:autor] = "Owen Peredo Diaz" unless request.session_options[:id].present?
  request.session_options[:id]
end

#cama_logout_userObject

logout current user


95
96
97
98
99
100
101
102
# File 'app/helpers/camaleon_cms/session_helper.rb', line 95

def cama_logout_user
  cookies.delete(:auth_token, domain: :all)
  cookies.delete(:auth_token, domain: nil)
  c_data = {value: nil, expires: 24.hours.ago}
  c_data[:domain] = :all if PluginRoutes.system_info["users_share_sites"].present? && CamaleonCms::Site.count > 1
  cookies[:auth_token] = c_data
  redirect_to params[:return_to].present? ? params[:return_to] : , :notice => t('camaleon_cms.admin.logout.message.closed')
end

#cama_on_heroku?Boolean

check if current host is heroku

Returns:

  • (Boolean)

72
73
74
# File 'app/helpers/camaleon_cms/session_helper.rb', line 72

def cama_on_heroku?
  ENV.keys.any? { |var_name| var_name.match(/(heroku|dyno)/i) }
end

#cama_register_user(user_data, meta) ⇒ Object

User registration.

user_data must contain:

  • email

  • username

  • password

  • password_confirmation

meta must contain:

  • first_name

  • last_name


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/camaleon_cms/session_helper.rb', line 53

def cama_register_user(user_data, meta)
  @user = current_site.users.new(user_data)
  r = {user: @user, params: params}; hook_run('user_before_register', r)

  if current_site.security_user_register_captcha_enabled? && !cama_captcha_verified?
    {:result => false, :type => :captcha_error, :message => t('camaleon_cms.admin.users.message.error_captcha')}
  else
    if @user.save
      @user.set_meta_from_form(meta)
      message = current_site.need_validate_email? ? t('camaleon_cms.admin.users.message.created_pending_validate_email') : t('camaleon_cms.admin.users.message.created')
      r = {user: @user, message: message, redirect_url: }; hooks_run('user_after_register', r)
      {:result => true, :message => r[:message], :redirect_url => r[:redirect_url]}
    else
      {:result => false, :type => :no_saved}
    end
  end
end

#cama_sign_in?Boolean Also known as: signin?

check if current user is already signed

Returns:

  • (Boolean)

105
106
107
# File 'app/helpers/camaleon_cms/session_helper.rb', line 105

def cama_sign_in?
  !cama_current_user.nil?
end

#login_user(user, remember_me = false, redirect_url = nil) ⇒ Object

log in the user in to system user: User model remember_me: true/false (remember session permanently)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/camaleon_cms/session_helper.rb', line 13

def (user, remember_me = false, redirect_url = nil)
  c = {value: [user.auth_token, request.user_agent, request.ip], expires: 24.hours.from_now}
  c[:domain] = :all if PluginRoutes.system_info["users_share_sites"].present? && CamaleonCms::Site.count > 1
  c[:expires] = 1.month.from_now if remember_me

  user.update({last_login_at: Time.zone.now})
  cookies[:auth_token] = c

  # user redirection
  flash[:notice] = t('camaleon_cms.admin.login.message.success', locale: current_site.get_admin_language)
  if redirect_url.present?
    redirect_to redirect_url
  elsif (return_to = cookies.delete(:return_to)).present?
    redirect_to return_to
  else
    redirect_to cama_admin_dashboard_path
  end
end

#login_user_with_password(username, password) ⇒ Object

login a user using username and password return boolean: true => authenticated, false => authentication failed


34
35
36
37
38
# File 'app/helpers/camaleon_cms/session_helper.rb', line 34

def (username, password)
  @user = current_site.users.find_by_username(username)
  r = {user: @user, params: params, password: password, captcha_validate: true}; hooks_run('user_before_login', r)
  @user && @user.authenticate(password)
end

#session_back_to_parent(redirect_url = nil) ⇒ Object

switch current session into parent session called by session_switch_user after returned into parent session, this will be redirected to redirect_url or admin dashboard


87
88
89
90
91
92
# File 'app/helpers/camaleon_cms/session_helper.rb', line 87

def session_back_to_parent(redirect_url = nil)
  if cama_sign_in? && cookies[:parent_auth_token].present?
    cookies[:auth_token] = cookies[:parent_auth_token]
    redirect_to (redirect_url || cama_admin_dashboard_path), notice: "Welcome back!"
  end
end

#session_switch_user(user, redirect_url = nil) ⇒ Object

switch current session user into other (user) after switched, this will be redirected to redirect_url or admin dashboard


78
79
80
81
82
83
# File 'app/helpers/camaleon_cms/session_helper.rb', line 78

def session_switch_user(user, redirect_url = nil)
  if cama_sign_in?
    cookies[:parent_auth_token] = cookies[:auth_token]
    (user, false, redirect_url)
  end
end