rails_base 0.51.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +32 -0
- data/Rakefile +32 -0
- data/app/assets/config/rails_base/manifest.js +3 -0
- data/app/assets/images/rails_base/favicon.ico +0 -0
- data/app/assets/javascripts/rails_base/admin.js +2 -0
- data/app/assets/javascripts/rails_base/application.js +22 -0
- data/app/assets/javascripts/rails_base/cable.js +13 -0
- data/app/assets/javascripts/rails_base/mfa_auth.coffee +3 -0
- data/app/assets/javascripts/rails_base/secondary_authentication.coffee +3 -0
- data/app/assets/javascripts/rails_base/sessions.js +152 -0
- data/app/assets/javascripts/rails_base/user_settings.coffee +3 -0
- data/app/assets/stylesheets/rails_base/admin.css +4 -0
- data/app/assets/stylesheets/rails_base/application.scss +15 -0
- data/app/assets/stylesheets/rails_base/mfa_auth.scss +3 -0
- data/app/assets/stylesheets/rails_base/scaffolds.scss +84 -0
- data/app/assets/stylesheets/rails_base/secondary_authentication.scss +3 -0
- data/app/assets/stylesheets/rails_base/user_settings.scss +3 -0
- data/app/controllers/rails_base/admin_controller.rb +315 -0
- data/app/controllers/rails_base/application_controller.rb +153 -0
- data/app/controllers/rails_base/errors_controller.rb +29 -0
- data/app/controllers/rails_base/mfa_auth_controller.rb +50 -0
- data/app/controllers/rails_base/secondary_authentication_controller.rb +224 -0
- data/app/controllers/rails_base/switch_user_controller.rb +29 -0
- data/app/controllers/rails_base/user_settings_controller.rb +81 -0
- data/app/controllers/rails_base/users/passwords_controller.rb +19 -0
- data/app/controllers/rails_base/users/registrations_controller.rb +80 -0
- data/app/controllers/rails_base/users/sessions_controller.rb +108 -0
- data/app/helpers/rails_base/admin_helper.rb +107 -0
- data/app/helpers/rails_base/appearance_helper.rb +58 -0
- data/app/helpers/rails_base/application_helper.rb +26 -0
- data/app/helpers/rails_base/capture_reference_helper.rb +57 -0
- data/app/helpers/rails_base/mfa_auth_helper.rb +2 -0
- data/app/helpers/rails_base/secondary_authentication_helper.rb +2 -0
- data/app/helpers/rails_base/user_field_validators.rb +108 -0
- data/app/helpers/rails_base/user_settings_helper.rb +22 -0
- data/app/jobs/rails_base/application_job.rb +10 -0
- data/app/jobs/twilio_job.rb +9 -0
- data/app/mailers/rails_base/application_mailer.rb +9 -0
- data/app/mailers/rails_base/email_verification_mailer.rb +22 -0
- data/app/mailers/rails_base/event_mailer.rb +16 -0
- data/app/models/admin_action.rb +119 -0
- data/app/models/rails_base/application_record.rb +22 -0
- data/app/models/rails_base/user_constants.rb +28 -0
- data/app/models/secret.rb +37 -0
- data/app/models/short_lived_data.rb +132 -0
- data/app/models/user.rb +143 -0
- data/app/services/rails_base/admin_risky_mfa_send.rb +80 -0
- data/app/services/rails_base/admin_update_attribute.rb +100 -0
- data/app/services/rails_base/authentication/authenticate_user.rb +28 -0
- data/app/services/rails_base/authentication/constants.rb +60 -0
- data/app/services/rails_base/authentication/decision_twofa_type.rb +76 -0
- data/app/services/rails_base/authentication/destroy_user.rb +45 -0
- data/app/services/rails_base/authentication/mfa_set_encrypt_token.rb +32 -0
- data/app/services/rails_base/authentication/mfa_validator.rb +88 -0
- data/app/services/rails_base/authentication/modify_password.rb +67 -0
- data/app/services/rails_base/authentication/send_forgot_password.rb +26 -0
- data/app/services/rails_base/authentication/send_login_mfa_to_user.rb +77 -0
- data/app/services/rails_base/authentication/send_verification_email.rb +103 -0
- data/app/services/rails_base/authentication/session_token_verifier.rb +31 -0
- data/app/services/rails_base/authentication/single_sign_on_create.rb +44 -0
- data/app/services/rails_base/authentication/single_sign_on_send.rb +101 -0
- data/app/services/rails_base/authentication/single_sign_on_verify.rb +42 -0
- data/app/services/rails_base/authentication/sso_verify_email.rb +43 -0
- data/app/services/rails_base/authentication/update_phone_send_verification.rb +46 -0
- data/app/services/rails_base/authentication/verify_forgot_password.rb +46 -0
- data/app/services/rails_base/email_change.rb +20 -0
- data/app/services/rails_base/encryption.rb +87 -0
- data/app/services/rails_base/name_change.rb +71 -0
- data/app/services/rails_base/service_base.rb +65 -0
- data/app/services/rails_base/service_logging.rb +23 -0
- data/app/views/layouts/rails_base/application.html.erb +185 -0
- data/app/views/layouts/rails_base/mailer.html.erb +13 -0
- data/app/views/layouts/rails_base/mailer.text.erb +1 -0
- data/app/views/new.html.erb +4 -0
- data/app/views/rails_base/admin/history.html.erb +26 -0
- data/app/views/rails_base/admin/index.html.erb +149 -0
- data/app/views/rails_base/admin/show_config.html.erb +18 -0
- data/app/views/rails_base/devise/confirmations/new.html.erb +16 -0
- data/app/views/rails_base/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/rails_base/devise/mailer/email_changed.html.erb +7 -0
- data/app/views/rails_base/devise/mailer/password_change.html.erb +3 -0
- data/app/views/rails_base/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/rails_base/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/rails_base/devise/passwords/edit.html.erb +25 -0
- data/app/views/rails_base/devise/passwords/new.html.erb +27 -0
- data/app/views/rails_base/devise/registrations/edit.html.erb +43 -0
- data/app/views/rails_base/devise/registrations/new.html.erb +123 -0
- data/app/views/rails_base/devise/sessions/new.html.erb +4 -0
- data/app/views/rails_base/devise/shared/_error_messages.html.erb +15 -0
- data/app/views/rails_base/devise/shared/_links.html.erb +25 -0
- data/app/views/rails_base/devise/unlocks/new.html.erb +16 -0
- data/app/views/rails_base/email_verification_mailer/email_verification.html.erb +25 -0
- data/app/views/rails_base/email_verification_mailer/event.html.erb +20 -0
- data/app/views/rails_base/email_verification_mailer/forgot_password.html.erb +22 -0
- data/app/views/rails_base/errors/internal_error.html.erb +1 -0
- data/app/views/rails_base/errors/not_found.html.erb +1 -0
- data/app/views/rails_base/errors/unacceptable.html.erb +1 -0
- data/app/views/rails_base/event_mailer/event.html.erb +10 -0
- data/app/views/rails_base/mfa_auth/mfa_code.html.erb +10 -0
- data/app/views/rails_base/secondary_authentication/after_email_login_session_new.html.erb +3 -0
- data/app/views/rails_base/secondary_authentication/forgot_password.html.erb +9 -0
- data/app/views/rails_base/secondary_authentication/remove_me.html.erb +1 -0
- data/app/views/rails_base/secondary_authentication/static.html.erb +5 -0
- data/app/views/rails_base/shared/_admin_actions_modal.html.erb +65 -0
- data/app/views/rails_base/shared/_admin_config_class.html.erb +52 -0
- data/app/views/rails_base/shared/_admin_history.html.erb +86 -0
- data/app/views/rails_base/shared/_admin_modify_email.html.erb +78 -0
- data/app/views/rails_base/shared/_admin_modify_name.html.erb +107 -0
- data/app/views/rails_base/shared/_admin_modify_phone.html.erb +87 -0
- data/app/views/rails_base/shared/_admin_modify_text.html.erb +35 -0
- data/app/views/rails_base/shared/_admin_risky_change.html.erb +57 -0
- data/app/views/rails_base/shared/_admin_risky_mfa.html.erb +74 -0
- data/app/views/rails_base/shared/_admin_selector_dropdown.html.erb +70 -0
- data/app/views/rails_base/shared/_admin_toggle_button.html.erb +72 -0
- data/app/views/rails_base/shared/_admin_warning_alert.html.erb +7 -0
- data/app/views/rails_base/shared/_appearance_mode_selector.html.erb +183 -0
- data/app/views/rails_base/shared/_custom_form_validation_javascript.html.erb +129 -0
- data/app/views/rails_base/shared/_enable_mfa_auth_modal.html.erb +105 -0
- data/app/views/rails_base/shared/_error_pages.html.erb +123 -0
- data/app/views/rails_base/shared/_logged_in_header.html.erb +123 -0
- data/app/views/rails_base/shared/_logged_out_header.html.erb +14 -0
- data/app/views/rails_base/shared/_mfa_input_layout.html.erb +5 -0
- data/app/views/rails_base/shared/_mfa_input_layout_default.html.erb +97 -0
- data/app/views/rails_base/shared/_mfa_input_layout_fallback.html.erb +55 -0
- data/app/views/rails_base/shared/_modify_mfa_auth_modal.html.erb +20 -0
- data/app/views/rails_base/shared/_password_confirm_javascript.html.erb +71 -0
- data/app/views/rails_base/shared/_reset_password_form.html.erb +111 -0
- data/app/views/rails_base/shared/_session_create_form.html.erb +32 -0
- data/app/views/rails_base/shared/_session_timeout_modal.html.erb +76 -0
- data/app/views/rails_base/switch_user/_widget.html.erb +5 -0
- data/app/views/rails_base/user_settings/_confirm_destroy_user.html.erb +42 -0
- data/app/views/rails_base/user_settings/_destroy_user.html.erb +106 -0
- data/app/views/rails_base/user_settings/_modify_name.html.erb +71 -0
- data/app/views/rails_base/user_settings/_modify_password.html.erb +101 -0
- data/app/views/rails_base/user_settings/_modify_password_update_password.html.erb +2 -0
- data/app/views/rails_base/user_settings/index.html.erb +54 -0
- data/config/initializers/01_rails_config.rb +19 -0
- data/config/initializers/admin_action_helper.rb +88 -0
- data/config/initializers/browser.rb +4 -0
- data/config/initializers/default_logged_in_headers.rb +23 -0
- data/config/initializers/devise.rb +314 -0
- data/config/initializers/encryption.rb +2 -0
- data/config/initializers/switch_user.rb +58 -0
- data/config/initializers/switch_user_helper.rb +29 -0
- data/config/locales/devise.en.yml +65 -0
- data/config/locales/en.yml +58 -0
- data/config/routes.rb +114 -0
- data/db/migrate/20210212175453_devise_create_rails_base_users.rb +56 -0
- data/db/migrate/20210212190537_create_rails_base_short_lived_data.rb +19 -0
- data/db/migrate/20210212192645_create_rails_base_secrets.rb +11 -0
- data/db/migrate/20210406015744_create_rails_base_admin_actions.rb +17 -0
- data/db/seeds.rb +23 -0
- data/lib/link_decision_helper.rb +71 -0
- data/lib/rails_base.rb +50 -0
- data/lib/rails_base/admin/action_cache.rb +99 -0
- data/lib/rails_base/admin/action_helper.rb +134 -0
- data/lib/rails_base/admin/default_index_tile.rb +176 -0
- data/lib/rails_base/admin/index_tile.rb +186 -0
- data/lib/rails_base/config.rb +52 -0
- data/lib/rails_base/configuration/active_job.rb +38 -0
- data/lib/rails_base/configuration/admin.rb +231 -0
- data/lib/rails_base/configuration/app.rb +52 -0
- data/lib/rails_base/configuration/appearance.rb +131 -0
- data/lib/rails_base/configuration/authentication.rb +37 -0
- data/lib/rails_base/configuration/base.rb +209 -0
- data/lib/rails_base/configuration/display/background_color.rb +25 -0
- data/lib/rails_base/configuration/display/btn_danger.rb +25 -0
- data/lib/rails_base/configuration/display/btn_dark.rb +25 -0
- data/lib/rails_base/configuration/display/btn_info.rb +25 -0
- data/lib/rails_base/configuration/display/btn_light.rb +25 -0
- data/lib/rails_base/configuration/display/btn_primary.rb +25 -0
- data/lib/rails_base/configuration/display/btn_secondary.rb +25 -0
- data/lib/rails_base/configuration/display/btn_success.rb +25 -0
- data/lib/rails_base/configuration/display/btn_warning.rb +25 -0
- data/lib/rails_base/configuration/display/footer.rb +54 -0
- data/lib/rails_base/configuration/display/navbar.rb +25 -0
- data/lib/rails_base/configuration/display/table_body.rb +25 -0
- data/lib/rails_base/configuration/display/table_header.rb +25 -0
- data/lib/rails_base/configuration/display/text.rb +26 -0
- data/lib/rails_base/configuration/exceptions_app.rb +25 -0
- data/lib/rails_base/configuration/login_behavior.rb +17 -0
- data/lib/rails_base/configuration/mailer.rb +116 -0
- data/lib/rails_base/configuration/mfa.rb +84 -0
- data/lib/rails_base/configuration/owner.rb +17 -0
- data/lib/rails_base/configuration/redis.rb +29 -0
- data/lib/rails_base/configuration/user.rb +43 -0
- data/lib/rails_base/engine.rb +51 -0
- data/lib/rails_base/version.rb +10 -0
- data/lib/tasks/rails_base_tasks.rake +4 -0
- data/lib/twilio_helper.rb +26 -0
- data/lib/velocity_limiter.rb +91 -0
- metadata +619 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module RailsBase
|
3
|
+
class SwitchUserController < ::SwitchUserController
|
4
|
+
before_action :admin_user?
|
5
|
+
before_action :admin_user
|
6
|
+
before_action :can_impersonate?
|
7
|
+
after_action :admin_set_impersonation_session!, only: [:set_current_user]
|
8
|
+
|
9
|
+
def admin_set_impersonation_session!
|
10
|
+
admin_set_token_on_session(admin_user: admin_user, other_user: provider.current_user)
|
11
|
+
session[RailsBase::Authentication::Constants::ADMIN_REMEMBER_USERID_KEY] = admin_user.id
|
12
|
+
end
|
13
|
+
|
14
|
+
def can_impersonate?
|
15
|
+
return if RailsBase.config.admin.impersonate_tile_users.call(admin_user)
|
16
|
+
|
17
|
+
flash[:alert] = "You do not have correct permissions to impersonate users"
|
18
|
+
redirect_to RailsBase.url_routes.admin_base
|
19
|
+
end
|
20
|
+
|
21
|
+
def admin_user
|
22
|
+
if session[RailsBase::Authentication::Constants::ADMIN_REMEMBER_USERID_KEY]
|
23
|
+
User.find session[RailsBase::Authentication::Constants::ADMIN_REMEMBER_USERID_KEY]
|
24
|
+
else
|
25
|
+
current_user
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module RailsBase
|
2
|
+
class UserSettingsController < ApplicationController
|
3
|
+
before_action :authenticate_user!
|
4
|
+
before_action :confirm_password_flow, only: :confirm_password
|
5
|
+
|
6
|
+
include RailsBase::UserSettingsHelper
|
7
|
+
|
8
|
+
# GET user/settings
|
9
|
+
def index
|
10
|
+
end
|
11
|
+
|
12
|
+
# POST user/settings/edit/name
|
13
|
+
def edit_name
|
14
|
+
result = NameChange.call(
|
15
|
+
first_name: params[:user][:first_name],
|
16
|
+
last_name: params[:user][:last_name],
|
17
|
+
current_user: current_user
|
18
|
+
)
|
19
|
+
|
20
|
+
if result.failure?
|
21
|
+
flash[:alert] = result.message
|
22
|
+
else
|
23
|
+
@_admin_action_struct = RailsBase::AdminStruct.new(result.original_name, result.name_change)
|
24
|
+
flash[:notice] = "Name change succesful to #{result.name_change}"
|
25
|
+
end
|
26
|
+
|
27
|
+
redirect_to RailsBase.url_routes.user_settings_path
|
28
|
+
end
|
29
|
+
|
30
|
+
# POST user/settings/edit/password
|
31
|
+
# expected ajax POST
|
32
|
+
def edit_password
|
33
|
+
# current user is method and we will loose context if we are succesful in changing password
|
34
|
+
# store current user in current context
|
35
|
+
user = current_user
|
36
|
+
result = RailsBase::Authentication::ModifyPassword.call(password: params[:user][:password], password_confirmation: params[:user][:password_confirmation], current_user: current_user, flow: :user_settings)
|
37
|
+
|
38
|
+
if result.failure?
|
39
|
+
redirect_to RailsBase.url_routes.user_settings_path, alert: result.message
|
40
|
+
return
|
41
|
+
end
|
42
|
+
|
43
|
+
# password was changed so authentication will fail. Re-signin user
|
44
|
+
sign_out(current_user)
|
45
|
+
sign_in(user.reload)
|
46
|
+
redirect_to RailsBase.url_routes.user_settings_path, notice: 'Succesfully changed password'
|
47
|
+
end
|
48
|
+
|
49
|
+
# POST user/settings/confirm/password/:reason
|
50
|
+
def confirm_password
|
51
|
+
authenticate = RailsBase::Authentication::AuthenticateUser.call(email: current_user.email, current_user: current_user, password: params[:user][:password])
|
52
|
+
|
53
|
+
if authenticate.failure?
|
54
|
+
render json: { msg: authenticate.message }, status: 418
|
55
|
+
else
|
56
|
+
html = render_to_string partial: CONFIRM_PASSWORD_FLOW[params[:reason].to_sym]
|
57
|
+
render json: { html: html, datum: datum.data }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# POST user/settings/destroy
|
62
|
+
def destroy_user
|
63
|
+
destroy = RailsBase::Authentication::DestroyUser.call(data: params[:data], current_user: current_user)
|
64
|
+
|
65
|
+
if destroy.failure?
|
66
|
+
redirect_to RailsBase.url_routes.user_settings_path, alert: destroy.message
|
67
|
+
else
|
68
|
+
redirect_to RailsBase.url_routes.authenticated_root_path, notice: I18n.t('user_setting.destroy_user.soft')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def confirm_password_flow
|
75
|
+
return true if CONFIRM_PASSWORD_FLOW.keys.include?(params[:reason].to_sym)
|
76
|
+
|
77
|
+
render json: { msg: 'invalid parameter' }, status: 418
|
78
|
+
false
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class RailsBase::Users::PasswordsController < Devise::PasswordsController
|
4
|
+
# GET /resource/password/new
|
5
|
+
def new
|
6
|
+
self.resource = User.new
|
7
|
+
render template: 'rails_base/devise/passwords/new'
|
8
|
+
end
|
9
|
+
|
10
|
+
# POST /resource/password
|
11
|
+
def create
|
12
|
+
result = RailsBase::Authentication::SendForgotPassword.call(email: params[:user][:email])
|
13
|
+
if result.failure?
|
14
|
+
redirect_to RailsBase.url_routes.new_user_password_path, alert: result.message
|
15
|
+
return
|
16
|
+
end
|
17
|
+
redirect_to RailsBase.url_routes.new_user_password_path, notice: result.message
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class RailsBase::Users::RegistrationsController < Devise::RegistrationsController
|
4
|
+
include RailsBase::UserFieldValidators
|
5
|
+
before_action :configure_sign_up_params, only: [:create]
|
6
|
+
# before_action :configure_account_update_params, only: [:update]
|
7
|
+
|
8
|
+
# GET /resource/sign_up
|
9
|
+
def new
|
10
|
+
# super
|
11
|
+
@resource = User.new
|
12
|
+
@resource_name = :user
|
13
|
+
render template: 'rails_base/devise/registrations/new'
|
14
|
+
end
|
15
|
+
|
16
|
+
# POST /users/create
|
17
|
+
def create
|
18
|
+
# Finds the resource by email and updates tha attributes ... or creates new resource
|
19
|
+
resource = (User.find_by_email(params[:user][:email])) || User.new
|
20
|
+
if resource.encrypted_password.present?
|
21
|
+
@email = params[:user][:email]
|
22
|
+
redirect_to RailsBase.url_routes.new_user_session_path, notice: 'Credentials exist. Please login or click forgot Password'
|
23
|
+
return
|
24
|
+
end
|
25
|
+
|
26
|
+
user_form_validation = validate_complement?(user_params: params[:user])
|
27
|
+
|
28
|
+
unless user_form_validation[:status]
|
29
|
+
resource.assign_attributes(sign_up_params.except(:password, :password_confirmation))
|
30
|
+
@resource = resource
|
31
|
+
@resource_name = resource_name
|
32
|
+
@alert_errors = user_form_validation[:errors]
|
33
|
+
flash[:error] = @alert_errors.values.join('</br>')
|
34
|
+
render :new, notice: "Failure shit"
|
35
|
+
return
|
36
|
+
end
|
37
|
+
|
38
|
+
resource.admin = RailsBase.config.admin.default_admin_type
|
39
|
+
resource.assign_attributes(sign_up_params)
|
40
|
+
|
41
|
+
if resource.save
|
42
|
+
resource.reload
|
43
|
+
sign_up(resource_name, resource)
|
44
|
+
sign_out(resource)
|
45
|
+
|
46
|
+
email_verification = RailsBase::Authentication::SendVerificationEmail.call(user: resource, reason: RailsBase::Authentication::Constants::SVE_LOGIN_REASON)
|
47
|
+
if email_verification.failure?
|
48
|
+
render RailsBase.url_routes.auth_static_path, alert: email_verification.message
|
49
|
+
return
|
50
|
+
end
|
51
|
+
session[:mfa_randomized_token] = nil
|
52
|
+
session[:mfa_randomized_token] =
|
53
|
+
RailsBase::Authentication::MfaSetEncryptToken.call(purpose: RailsBase::Authentication::Constants::SSOVE_PURPOSE, user: resource, expires_at: Time.zone.now + 20.minutes).encrypted_val
|
54
|
+
redirect_to RailsBase.url_routes.auth_static_path, notice: "Check email for verification email."
|
55
|
+
else
|
56
|
+
flash[:error] = resource.errors.messages
|
57
|
+
email = params[:email] if !resource.errors.details.keys.include?(:email)
|
58
|
+
@resource = resource
|
59
|
+
@resource_name = resource_name
|
60
|
+
render :new, notice: 'Unknown failure. Please try again'
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
# GET /resource/edit
|
65
|
+
def edit
|
66
|
+
raise
|
67
|
+
end
|
68
|
+
|
69
|
+
# PUT /resource
|
70
|
+
def update
|
71
|
+
raise
|
72
|
+
end
|
73
|
+
|
74
|
+
protected
|
75
|
+
|
76
|
+
# If you have extra params to permit, append them to the sanitizer.
|
77
|
+
def configure_sign_up_params
|
78
|
+
devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name])
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class RailsBase::Users::SessionsController < Devise::SessionsController
|
4
|
+
prepend_before_action :protect_heartbeat, only: [:hearbeat_without_auth, :hearbeat_with_auth]
|
5
|
+
prepend_before_action :skip_timeout, only: [:hearbeat_without_auth]
|
6
|
+
|
7
|
+
# GET /user/sign_in
|
8
|
+
def new
|
9
|
+
@user = User.new
|
10
|
+
render template: 'rails_base/devise/sessions/new'
|
11
|
+
end
|
12
|
+
|
13
|
+
# POST /user/sign_in
|
14
|
+
def create
|
15
|
+
# Warden/Devise will try to sign the user in before we explicitly do
|
16
|
+
# Sign ou the user when this happens so we can sign them back in later
|
17
|
+
sign_out(current_user) if current_user
|
18
|
+
|
19
|
+
authenticate = RailsBase::Authentication::AuthenticateUser.call(email: params[:user][:email], password: params[:user][:password])
|
20
|
+
|
21
|
+
if authenticate.failure?
|
22
|
+
@user = User.new(email: params[:user][:email])
|
23
|
+
flash[:alert] = authenticate.message
|
24
|
+
render template: 'rails_base/devise/sessions/new'
|
25
|
+
return
|
26
|
+
end
|
27
|
+
|
28
|
+
mfa_decision = RailsBase::Authentication::DecisionTwofaType.call(user: authenticate.user)
|
29
|
+
if mfa_decision.failure?
|
30
|
+
redirect_to RailsBase.url_routes.new_user_session_path, email: params[:user][:email], alert: mfa_decision.message
|
31
|
+
return
|
32
|
+
end
|
33
|
+
|
34
|
+
if mfa_decision.set_mfa_randomized_token
|
35
|
+
session[:mfa_randomized_token] =
|
36
|
+
RailsBase::Authentication::MfaSetEncryptToken.call(
|
37
|
+
user: authenticate.user,
|
38
|
+
expires_at: mfa_decision.token_ttl,
|
39
|
+
purpose: mfa_decision.mfa_purpose,
|
40
|
+
).encrypted_val
|
41
|
+
end
|
42
|
+
|
43
|
+
redirect =
|
44
|
+
if mfa_decision.sign_in_user
|
45
|
+
sign_in(authenticate.user)
|
46
|
+
# only referentially redirect when we know the user should sign in
|
47
|
+
redirect_from_reference
|
48
|
+
end
|
49
|
+
|
50
|
+
redirect ||= mfa_decision.redirect_url
|
51
|
+
|
52
|
+
logger.info { "Successful sign in: Redirecting to #{redirect}" }
|
53
|
+
|
54
|
+
redirect_to(redirect, mfa_decision.flash)
|
55
|
+
end
|
56
|
+
|
57
|
+
# DELETE /user/sign_out
|
58
|
+
def destroy
|
59
|
+
session[:mfa_randomized_token] = nil
|
60
|
+
|
61
|
+
# force the user to sign out
|
62
|
+
sign_out(current_user)
|
63
|
+
reset_session
|
64
|
+
|
65
|
+
admin_reset_session!
|
66
|
+
|
67
|
+
flash[:notice] = 'You have been succesfully signed out'
|
68
|
+
redirect_to RailsBase.url_routes.unauthenticated_root_path
|
69
|
+
end
|
70
|
+
|
71
|
+
# GET /heartbeat
|
72
|
+
def hearbeat_without_auth
|
73
|
+
skip_capture_reference!
|
74
|
+
heartbeat
|
75
|
+
end
|
76
|
+
|
77
|
+
# POST /heartbeat
|
78
|
+
def hearbeat_with_auth
|
79
|
+
heartbeat
|
80
|
+
end
|
81
|
+
|
82
|
+
private
|
83
|
+
|
84
|
+
def heartbeat
|
85
|
+
if current_user
|
86
|
+
last_request = session['warden.user.user.session']['last_request_at']
|
87
|
+
ttd = last_request + Devise.timeout_in.to_i
|
88
|
+
ttl = ttd - Time.zone.now.to_i
|
89
|
+
render json: { success: true, ttd: ttd, ttl: ttl, last_request: last_request }
|
90
|
+
else
|
91
|
+
render json: { success: false }, status: 401
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# ensure session is present before we try and do anything
|
96
|
+
# proects the site by not querying for current_user
|
97
|
+
# would be better to do this at edge layer
|
98
|
+
def protect_heartbeat
|
99
|
+
return true if session.keys.present? || browser.bot?
|
100
|
+
|
101
|
+
logger.warn { "Hack attempt. Checking the heartbeat bad user agent. bot?:[#{browser.bot}]" }
|
102
|
+
render json: { success: false }, status: 401
|
103
|
+
end
|
104
|
+
|
105
|
+
def skip_timeout
|
106
|
+
request.env["devise.skip_trackable"] = true
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module RailsBase
|
2
|
+
module AdminHelper
|
3
|
+
|
4
|
+
SESSION_REASON_BASE = 'admin_random'
|
5
|
+
SESSION_REASON_KEY = 'admin_2fa_verify'
|
6
|
+
|
7
|
+
SECOND_MODAL_MAPPING = {
|
8
|
+
phone_number: 'rails_base/shared/admin_modify_phone',
|
9
|
+
email: 'rails_base/shared/admin_modify_email'
|
10
|
+
}
|
11
|
+
|
12
|
+
def filtered_classes(user, admin)
|
13
|
+
RailsBase.config.admin.admin_page_filter.map do |object|
|
14
|
+
object[:id] if object[:proc].call(user, admin)
|
15
|
+
end.compact.join(' ')
|
16
|
+
end
|
17
|
+
|
18
|
+
def users_for_proc(proc)
|
19
|
+
User.all.select do |user|
|
20
|
+
proc.call(user)
|
21
|
+
end.map(&:inspect_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
def array_for_proc(proc, array)
|
25
|
+
array.map do |instance|
|
26
|
+
proc.call(instance)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def paginated_records
|
31
|
+
AdminAction.paginate_records(page: @starting_page, count_on_page: @count_on_page, user_id: @starting_user[1], admin_id: @starting_admin[1])
|
32
|
+
end
|
33
|
+
|
34
|
+
def fullsized_paginated_count
|
35
|
+
AdminAction.paginate_records(page: @starting_page, count_on_page: @count_on_page, user_id: @starting_user[1], admin_id: @starting_admin[1], count: true)
|
36
|
+
end
|
37
|
+
|
38
|
+
def paginate_admin_what_page
|
39
|
+
return params[:page].to_i if params[:pagination_count].to_i == params[:prev_count].to_i
|
40
|
+
|
41
|
+
prev_page = params[:prev_page].to_i > 0 ? (params[:prev_page].to_i - 1) : 1
|
42
|
+
start_prev = prev_page * params[:prev_count].to_i
|
43
|
+
page = (start_prev / params[:pagination_count].to_i)
|
44
|
+
page > 0 ? page : 1
|
45
|
+
end
|
46
|
+
|
47
|
+
def paginate_admin_history_range(start:)
|
48
|
+
((start-AdminAction::DEFAULT_PAGE_RANGE)..(start+AdminAction::DEFAULT_PAGE_RANGE))
|
49
|
+
end
|
50
|
+
|
51
|
+
def paginante_class_names(curr_page:, page_number:, count_on_page:)
|
52
|
+
bootstrap_class = 'disabled' unless paginate_can_view_page?(page_number: page_number, count_on_page: count_on_page)
|
53
|
+
bootstrap_class = 'active' if curr_page == page_number
|
54
|
+
bootstrap_class || ''
|
55
|
+
end
|
56
|
+
|
57
|
+
def paginate_can_view_page?(page_number:, count_on_page:)
|
58
|
+
min_size = (page_number-1) * count_on_page
|
59
|
+
fullsized_paginated_count >= min_size
|
60
|
+
end
|
61
|
+
|
62
|
+
def paginate_admin_can_next?(page_number:, count_on_page:)
|
63
|
+
min_size = (page_number) * count_on_page
|
64
|
+
fullsized_paginated_count >= min_size
|
65
|
+
end
|
66
|
+
|
67
|
+
def paginate_admin_can_prev?(page_number:, count_on_page:)
|
68
|
+
return false if (page_number - 1) < 1
|
69
|
+
|
70
|
+
min_size = (page_number - 1) * count_on_page
|
71
|
+
fullsized_paginated_count > min_size
|
72
|
+
end
|
73
|
+
|
74
|
+
def paginate_get_users_array
|
75
|
+
@paginate_get_users_array ||= User.where(active: true).map { |u| [u.full_name, u.id] } << ['All Users', -1]
|
76
|
+
end
|
77
|
+
|
78
|
+
def paginate_get_admins_array
|
79
|
+
@paginate_get_admins_array ||= User.where.not(admin: :none).map { |u| [u.full_name, u.id] } << ['All Admins', -1]
|
80
|
+
end
|
81
|
+
|
82
|
+
def paginate_diff_id?(type:)
|
83
|
+
session[:"rails_base_paginate_start_#{type}"] != params[type].to_i
|
84
|
+
end
|
85
|
+
|
86
|
+
def accurate_admin_user
|
87
|
+
session[RailsBase::Authentication::Constants::ADMIN_REMEMBER_USERID_KEY] ||
|
88
|
+
current_user.id
|
89
|
+
end
|
90
|
+
|
91
|
+
def admin_user
|
92
|
+
@admin_user ||= User.find(accurate_admin_user)
|
93
|
+
end
|
94
|
+
|
95
|
+
def session_reason
|
96
|
+
session[SESSION_REASON_KEY]
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_mfa_to_obj
|
100
|
+
arr = []
|
101
|
+
params[:mfa_input].split('').each_with_index do |code, i|
|
102
|
+
arr << ["#{RailsBase::Authentication::Constants::MV_BASE_NAME}#{i}", code]
|
103
|
+
end
|
104
|
+
{ mfa: arr.to_h.symbolize_keys }
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module RailsBase::AppearanceHelper
|
2
|
+
APPEARANCE_MODE_COOKIE = "_#{Rails.application.class.parent_name}_appearance_mode".gsub(' ', '-').downcase
|
3
|
+
APPEARANCE_MODE_ACTUAL_COOKIE = "_#{Rails.application.class.parent_name}_appearance_actual_mode".gsub(' ', '-').downcase
|
4
|
+
APPEARANCE_TEXT_CLASS = RailsBase::Configuration::Display::Text::APPEARANCE_TEXT_CLASS
|
5
|
+
|
6
|
+
VIEWPORT_EXTRA_SMALL = 'xs'.freeze
|
7
|
+
VIEWPORT_SMALL = 'sm'.freeze
|
8
|
+
VIEWPORT_MEDIUM = 'md'.freeze
|
9
|
+
VIEWPORT_LARGE = 'lg'.freeze
|
10
|
+
VIEWPORT_EXTRA_LARGE = 'xl'.freeze
|
11
|
+
|
12
|
+
VIEWPORT_MOBILE_MAX = VIEWPORT_MEDIUM
|
13
|
+
|
14
|
+
VIEWPORT_SIZES = {
|
15
|
+
VIEWPORT_EXTRA_SMALL => 576,
|
16
|
+
VIEWPORT_SMALL => 768,
|
17
|
+
VIEWPORT_MEDIUM => 992,
|
18
|
+
VIEWPORT_LARGE => 1200,
|
19
|
+
VIEWPORT_EXTRA_LARGE => nil,
|
20
|
+
}
|
21
|
+
|
22
|
+
def footer_mode_case
|
23
|
+
return :sticky if @_sticky_mode
|
24
|
+
|
25
|
+
return :bottom if RailsBase.appearance.footer.content_bottom_or_sticky
|
26
|
+
|
27
|
+
sticky_pages = RailsBase.appearance.footer.sticky_pages
|
28
|
+
return if sticky_pages.empty?
|
29
|
+
|
30
|
+
full_controller_path = "#{controller_path.camelize}Controller"
|
31
|
+
return unless pages = sticky_pages[full_controller_path]
|
32
|
+
|
33
|
+
return :sticky if pages.include?(action_name.to_sym)
|
34
|
+
nil
|
35
|
+
end
|
36
|
+
|
37
|
+
def force_sticky_mode!
|
38
|
+
@_sticky_mode = true
|
39
|
+
end
|
40
|
+
|
41
|
+
def appearance_text_class
|
42
|
+
APPEARANCE_TEXT_CLASS
|
43
|
+
end
|
44
|
+
|
45
|
+
def appearance_mode_drop_down
|
46
|
+
@appearance_mode_drop_down ||= begin
|
47
|
+
current = cookies[APPEARANCE_MODE_COOKIE]
|
48
|
+
actual = cookies[APPEARANCE_MODE_ACTUAL_COOKIE]
|
49
|
+
raw_types = RailsBase::Configuration::Appearance::APPEARANCE_TYPES
|
50
|
+
types_a = raw_types.map(&:to_a).map(&:flatten).map(&:reverse)
|
51
|
+
types = raw_types.keys
|
52
|
+
unless types.include?(current&.to_sym)
|
53
|
+
cookies[APPEARANCE_MODE_COOKIE] = current = RailsBase.appearance.default_mode
|
54
|
+
end
|
55
|
+
{ types: types, current: current, types_a: types_a, actual: actual }
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|