Skip to content

Commit

Permalink
Merge pull request #2278 from cloudnativedaysjp/current_user_1
Browse files Browse the repository at this point in the history
Refactor: Use `current_user` instead of `set_current_user` (1/3)
  • Loading branch information
takaishi authored May 3, 2024
2 parents 173157e + b64a7ae commit f3ad6b0
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 43 deletions.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ class ApplicationController < ActionController::Base

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

def current_user
unless defined?(@current_user)
@current_user = session[:userinfo]
end
@current_user
end
helper_method :current_user

def user_not_authorized
render(template: 'errors/error_403', status: 403, layout: 'application', content_type: 'text/html')
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/attendees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ class AttendeesController < ApplicationController
before_action :set_profile, :set_speaker

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

def index
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/secured.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def logged_in?
end

def new_user?
logged_in? && !Profile.find_by(email: set_current_user[:info][:email], conference_id: set_conference.id)
logged_in? && !Profile.find_by(email: current_user[:info][:email], conference_id: set_conference.id)
end

def admin?
Expand All @@ -71,7 +71,7 @@ def event_name
end

def set_current_user
@current_user ||= session[:userinfo]
current_user
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/secured_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def is_admin?
end

def set_current_user
@current_user ||= session[:userinfo]
current_user
end

def get_or_create_admin_profile
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/secured_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ def logged_in?
end

def set_current_user
@current_user ||= session[:userinfo]
current_user
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/secured_beta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def is_beta_user?
end

def set_current_user
@current_user ||= session[:userinfo]
current_user
end

def admin?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/secured_speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def logged_in_using_omniauth?
end

def set_current_user
@current_user = session[:userinfo]
current_user
end

def logged_in?
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/concerns/secured_sponsor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def logged_in_using_omniauth?
end

def set_current_user
@current_user = session[:userinfo]
current_user
end

def logged_in?
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/contents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ class ContentsController < ApplicationController
before_action :set_profile, :set_speaker

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

def index
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/event_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def show
end

def set_current_user
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

def privacy
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/profiles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class ProfilesController < ApplicationController
def new
@profile = Profile.new
@conference = Conference.find_by(abbr: params[:event])
if set_current_user && Profile.find_by(conference_id: @conference.id, email: @current_user[:info][:email])
if current_user && Profile.find_by(conference_id: @conference.id, email: @current_user[:info][:email])
redirect_to(dashboard_path)
end
@event = params[:event]
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/speaker_dashboard/speakers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def new
@conference = Conference.find_by(abbr: params[:event])
@sponsor = Sponsor.find(params[:sponsor_id]) if params[:sponsor_id]

if set_current_user
if current_user
if Speaker.find_by(conference_id: @conference.id, email: @current_user[:info][:email])
redirect_to(speaker_dashboard_path)
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/speaker_dashboards_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ def sponsor?
end

def logged_in_using_omniauth?
if logged_in?
@current_user = session[:userinfo]
end
current_user
end

def set_speaker
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/speakers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ class SpeakersController < ApplicationController
before_action :set_profile

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

# GET /speakers
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sponsor_dashboards/speakers_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def new
@conference = Conference.find_by(abbr: params[:event])
@sponsor = Sponsor.find(params[:sponsor_id]) if params[:sponsor_id]

if set_current_user
if current_user
if Speaker.find_by(conference_id: @conference.id, email: @current_user[:info][:email])
redirect_to(speaker_dashboard_path)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ def sponsor?
end

def logged_in_using_omniauth?
if logged_in?
@current_user = session[:userinfo]
end
current_user
end

def set_sponsor_profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def new
unless logged_in?
redirect_to(sponsor_dashboards_login_path)
else
if set_current_user
if current_user
if SponsorProfile.find_by(conference_id: @conference.id, email: @current_user[:info][:email])
redirect_to(sponsor_dashboards_path)
end
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/talks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ class TalksController < ApplicationController
helper_method :talk_start_to_end

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

# - プロポーザルの採択結果を表示する場合
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/teams_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ class TeamsController < ApplicationController
before_action :set_conference, :set_profile, :set_speaker

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

def show
Expand Down
4 changes: 1 addition & 3 deletions app/controllers/timetable_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ class TimetableController < ApplicationController
before_action :set_profile, :set_speaker

def logged_in_using_omniauth?
if session[:userinfo].present?
@current_user = session[:userinfo]
end
current_user
end

def index
Expand Down
5 changes: 0 additions & 5 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
module ApplicationHelper
def current_user
return unless session[:userinfo]
@current_user ||= session[:userinfo]
end

def logged_in?
!!session[:userinfo]
end
Expand Down
21 changes: 21 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'rails_helper'

RSpec.describe(ApplicationController, type: :controller) do
describe '#current_user' do
subject { controller.current_user }
context 'when omniauth session does not exist' do
it { is_expected.to(eq(nil)) }
end

context 'when omniauth session exists' do
before do
@userinfo = {
info: { email: '[email protected]' },
extra: { raw_info: { sub: 'mock', 'https://cloudnativedays.jp/roles' => '' } }
}
session[:userinfo] = @userinfo
end
it { is_expected.to(eq(@userinfo)) }
end
end
end

0 comments on commit f3ad6b0

Please sign in to comment.