Skip to content

Commit

Permalink
Move logged_in? to ApplicationController
Browse files Browse the repository at this point in the history
Centralizes the definition of the `logged_in?` method by moving it from ApplicationHelper and various controller concerns into ApplicationController.
  • Loading branch information
onk committed May 3, 2024
1 parent ddaf419 commit 149b94a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 24 deletions.
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ def current_user
end
helper_method :current_user

def logged_in?
!!session[:userinfo]
end
helper_method :logged_in?

def user_not_authorized
render(template: 'errors/error_403', status: 403, layout: 'application', content_type: 'text/html')
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/concerns/secured.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ def redirect_to_registration
redirect_to("/#{params[:event]}/registration") unless ['profiles'].include?(controller_name)
end

def logged_in?
session[:userinfo].present?
end

def new_user?
logged_in? && !Profile.find_by(email: current_user[:info][:email], conference_id: set_conference.id)
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/concerns/secured_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ def logged_in_using_omniauth?
end
end

def logged_in?
session[:userinfo].present?
end

def admin?
current_user[:extra][:raw_info]['https://cloudnativedays.jp/roles'].include?("#{conference.abbr.upcase}-Admin")
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/concerns/secured_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ def logged_in_using_omniauth?

private

def logged_in?
session[:userinfo].present?
end

def set_current_user
current_user
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/concerns/secured_speaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def set_current_user
current_user
end

def logged_in?
session[:userinfo].present?
end

def admin?
current_user[:extra][:raw_info]['https://cloudnativedays.jp/roles'].include?("#{@conference.abbr.upcase}-Admin")
end
Expand Down
4 changes: 0 additions & 4 deletions app/controllers/concerns/secured_sponsor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ def set_current_user
current_user
end

def logged_in?
session[:userinfo].present?
end

def admin?
current_user[:extra][:raw_info]['https://cloudnativedays.jp/roles'].include?("#{@conference.abbr.upcase}-Admin")
end
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module ApplicationHelper
def logged_in?
!!session[:userinfo]
end

def authenticate
return if logged_in?
redirect_to(root_path, alert: 'ログインしてください')
Expand Down
18 changes: 18 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,22 @@
it { is_expected.to(eq(@userinfo)) }
end
end

describe '#logged_in?' do
subject { controller.logged_in? }
context 'when logged in' 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(true)) }
end

context 'when not logged in' do
it { is_expected.to(eq(false)) }
end
end
end

0 comments on commit 149b94a

Please sign in to comment.