-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from JuzerShakir/rails-sessions-generator
Rails Sessions Generator
- Loading branch information
Showing
47 changed files
with
203 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,17 @@ | ||
module ApplicationCable | ||
class Connection < ActionCable::Connection::Base | ||
identified_by :current_user | ||
|
||
def connect | ||
set_current_user || reject_unauthorized_connection | ||
end | ||
|
||
private | ||
|
||
def set_current_user | ||
if session ||= Session.find_by(id: cookies.signed[:session_id]) | ||
self.current_user = session.user | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,12 @@ | ||
class ApplicationController < ActionController::Base | ||
include Authentication | ||
# Only allow modern browsers supporting webp images, web push, badges, import maps, CSS nesting, and CSS :has. | ||
allow_browser versions: :modern | ||
|
||
include Pagy::Backend | ||
add_flash_types :success, :notice | ||
helper_method :current_user | ||
|
||
rescue_from CanCan::AccessDenied do | ||
flash[:alert] = t("flash.un_authorize") | ||
|
||
if current_user.nil? | ||
redirect_to login_path | ||
else | ||
redirect_to request.referer || thaalis_all_path(CURR_YR) | ||
end | ||
end | ||
|
||
private | ||
|
||
def current_user | ||
@current_user ||= User.select(:id, :slug).find(session[:user_id]) if session[:user_id] | ||
end | ||
|
||
def logged_in? | ||
redirect_to thaalis_all_path(CURR_YR), notice: t("flash.active_session") if session[:user_id] | ||
return_to_default_path(type: :alert, msg: "flash.un_authorize") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module Authentication | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :require_authentication | ||
helper_method :authenticated?, :current_user | ||
end | ||
|
||
class_methods do | ||
def allow_unauthenticated_access(**options) | ||
skip_before_action :require_authentication, **options | ||
end | ||
end | ||
|
||
private | ||
|
||
def authenticated? | ||
resume_session | ||
end | ||
|
||
def require_authentication | ||
resume_session || request_authentication | ||
end | ||
|
||
def resume_session | ||
Current.session ||= find_session_by_cookie | ||
end | ||
|
||
def find_session_by_cookie | ||
Session.find_by(id: cookies.signed[:session_id]) if cookies.signed[:session_id] | ||
end | ||
|
||
def request_authentication | ||
session[:return_to_after_authenticating] = request.url | ||
redirect_to login_path, alert: t("flash.un_authorize") | ||
end | ||
|
||
def after_authentication_url | ||
return_to_url = session.delete(:return_to_after_authenticating) | ||
return_to_url.present? ? "#{return_to_url}.html" : thaalis_all_path(CURR_YR, format: :html) | ||
end | ||
|
||
def start_new_session_for(user) | ||
user.sessions.create!(user_agent: request.user_agent, ip_address: request.remote_ip).tap do |session| | ||
Current.session = session | ||
cookies.signed.permanent[:session_id] = {value: session.id, httponly: true, same_site: :lax} | ||
end | ||
end | ||
|
||
def terminate_session | ||
Rails.cache.delete("user_#{current_user.id}_role") | ||
Current.session.destroy | ||
cookies.delete(:session_id) | ||
end | ||
|
||
def return_to_default_path(type: :notice, msg: "flash.active_session") | ||
flash[type] = t(msg) | ||
redirect_to thaalis_all_path(CURR_YR) | ||
end | ||
|
||
def current_user | ||
@current_user ||= Current.user | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class Current < ActiveSupport::CurrentAttributes | ||
attribute :session | ||
delegate :user, to: :session, allow_nil: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Session < ApplicationRecord | ||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class CreateSessions < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :sessions do |t| | ||
t.references :user, null: false, foreign_key: true | ||
t.string :ip_address | ||
t.string :user_agent | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
FactoryBot.define do | ||
factory :session do | ||
user | ||
ip_address { Faker::Internet.public_ip_v4_address } | ||
user_agent { Faker::Internet.user_agent } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.