Module: CamaleonCms::SessionHelper
- Included in:
- CamaleonController
- Defined in:
- app/helpers/camaleon_cms/session_helper.rb
Instance Method Summary collapse
-
#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.
-
#cama_current_role ⇒ Object
return the role for current user if not logged in, then return ‘public’.
-
#cama_current_user ⇒ Object
return current user logged in.
-
#cama_get_session_id ⇒ Object
return the session id.
-
#cama_logout_user ⇒ Object
logout current user.
-
#cama_on_heroku? ⇒ Boolean
check if current host is heroku.
-
#cama_register_user(user_data, meta) ⇒ Object
User registration.
-
#cama_sign_in? ⇒ Boolean
(also: #signin?)
check if current user is already signed.
- #cookie_auth_token_complete? ⇒ Boolean
- #cookie_split_auth_token ⇒ Object
-
#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) redirect_url (default nil): after initialized the session, this will be redirected to “redirect_url” if defined it doesn’t redirect if redirect_url === false return to previous page if defined the cookie or login url received extra param: return_to=mysite.com.
-
#login_user_with_password(username, password) ⇒ Object
login a user using username and password return boolean: true => authenticated, false => authentication failed.
-
#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.
-
#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.
- #user_auth_token_from_cookie ⇒ Object
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
147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 147 def cama_authenticate(redirect_uri = nil) params[:return_to] = redirect_uri return if cama_sign_in? flash[:error] = t('camaleon_cms.admin.login.please_login') [:return_to] = if params[:return_to].present? params[:return_to] else (request.get? && params[:controller] != 'admin/sessions' ? request.original_url : nil) end redirect_to cama_admin_login_path end |
#cama_current_role ⇒ Object
return the role for current user if not logged in, then return ‘public’
116 117 118 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 116 def cama_current_role current_site.visitor_role end |
#cama_current_user ⇒ Object
return current user logged in
121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 121 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 if @cama_current_user return nil unless @cama_current_user = current_site.users_include_admins.find_by_auth_token().try(:decorate) end |
#cama_get_session_id ⇒ Object
return the session id
161 162 163 164 165 166 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 161 def cama_get_session_id session[:autor] = 'Owen Peredo Diaz' unless request.[:id].present? id = request.[:id] id = id.public_id if id.instance_of?(::Rack::Session::SessionId) id end |
#cama_logout_user ⇒ Object
logout current user
97 98 99 100 101 102 103 104 105 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 97 def cama_logout_user .delete(:auth_token, domain: :all) .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 [:auth_token] = c_data redirect_to params[:return_to].present? ? params[:return_to] : cama_admin_login_path, notice: t('camaleon_cms.admin.logout.message.closed') end |
#cama_on_heroku? ⇒ Boolean
check if current host is heroku
73 74 75 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 73 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:
-
first_name
-
email
-
username
-
password
-
password_confirmation
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 54 def cama_register_user(user_data, ) @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') } elsif @user.save @user.() = 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: , redirect_url: cama_admin_login_path } hooks_run('user_after_register', r) { result: true, message: r[:message], redirect_url: r[:redirect_url] } else { result: false, type: :no_saved } end end |
#cama_sign_in? ⇒ Boolean Also known as: signin?
check if current user is already signed
108 109 110 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 108 def cama_sign_in? !cama_current_user.nil? end |
#cookie_auth_token_complete? ⇒ Boolean
133 134 135 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 133 def &.size == 3 end |
#cookie_split_auth_token ⇒ Object
137 138 139 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 137 def [:auth_token]&.split('&') 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) redirect_url (default nil): after initialized the session, this will be redirected to
"redirect_url" if defined
it doesn't redirect if redirect_url === false
return to previous page if defined the cookie['return_to'] or login url received extra param: return_to=https://mysite.com
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 10 def login_user(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 # fix to overwrite a cookie .delete(:auth_token, domain: :all) .delete(:auth_token) user.update({ last_login_at: Time.zone.now }) [:auth_token] = c # user redirection flash[:notice] = t('camaleon_cms.admin.login.message.success', locale: current_site.get_admin_language) return if redirect_url == false if redirect_url.present? redirect_to redirect_url elsif (return_to = .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
37 38 39 40 41 42 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 37 def login_user_with_password(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&.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
88 89 90 91 92 93 94 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 88 def session_back_to_parent(redirect_url = nil) return unless cama_sign_in? && session[:parent_auth_token].present? [:auth_token] = session[:parent_auth_token] session.delete(:parent_auth_token) redirect_to (redirect_url || cama_admin_dashboard_path), notice: 'Welcome back!' 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
79 80 81 82 83 84 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 79 def session_switch_user(user, redirect_url = nil) return unless cama_sign_in? session[:parent_auth_token] = [:auth_token] login_user(user, false, redirect_url) end |
#user_auth_token_from_cookie ⇒ Object
141 142 143 |
# File 'app/helpers/camaleon_cms/session_helper.rb', line 141 def .first end |