Implements iChain authentication
This commit is contained in:
parent
15c6ed127f
commit
49d565def2
21 changed files with 269 additions and 126 deletions
|
|
@ -8,24 +8,29 @@ class ApplicationController < ActionController::Base
|
|||
check_authorization unless: :devise_controller?
|
||||
|
||||
def store_location
|
||||
session[:return_to] = request.fullpath if request.get? && controller_name != 'user_sessions' && controller_name != 'sessions'
|
||||
# store last url - this is needed for post-login redirect to whatever the user last visited.
|
||||
return unless request.get?
|
||||
if (request.path != '/accounts/sign_in' &&
|
||||
request.path != '/accounts/sign_up' &&
|
||||
request.path != '/accounts/password/new' &&
|
||||
request.path != '/accounts/password/edit' &&
|
||||
request.path != '/accounts/confirmation' &&
|
||||
request.path != '/accounts/sign_out' &&
|
||||
request.path != '/users/ichain_registration/ichain_sign_up' &&
|
||||
!request.path.starts_with?(Devise.ichain_base_url) &&
|
||||
!request.xhr?) # don't store ajax calls
|
||||
session[:return_to] = request.fullpath
|
||||
end
|
||||
end
|
||||
|
||||
def after_sign_in_path_for(resource)
|
||||
def after_sign_in_path_for(_resource)
|
||||
if (can? :view, Conference) &&
|
||||
(!session[:return_to] ||
|
||||
session[:return_to] &&
|
||||
session[:return_to] == root_path)
|
||||
admin_conference_index_path
|
||||
else
|
||||
if session[:return_to] &&
|
||||
!session[:return_to].start_with?(user_registration_path)
|
||||
logger.debug "Returning to #{session[:return_to]}"
|
||||
session[:return_to]
|
||||
else
|
||||
logger.debug "Not returning to #{session[:return_to]} because it would loop"
|
||||
super
|
||||
end
|
||||
session[:return_to] || root_path
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -53,6 +58,13 @@ class ApplicationController < ActionController::Base
|
|||
redirect_to root_path, alert: exception.message
|
||||
end
|
||||
|
||||
rescue_from IChainRecordNotFound do
|
||||
Rails.logger.debug('IChain Record was not Unique!')
|
||||
sign_out(current_user)
|
||||
flash[:error] = 'Your E-Mail adress is already registered at OSEM. Please contact the admin if you want to attach your openSUSE Account to OSEM!'
|
||||
redirect_to root_path
|
||||
end
|
||||
|
||||
def not_found
|
||||
raise ActionController::RoutingError.new('Not Found')
|
||||
end
|
||||
|
|
|
|||
|
|
@ -3,55 +3,12 @@ class RegistrationsController < Devise::RegistrationsController
|
|||
|
||||
def edit
|
||||
@openids = Openid.where(user_id: current_user.id).order(:provider)
|
||||
super
|
||||
end
|
||||
|
||||
def update
|
||||
@openids = Openid.where(user_id: current_user.id).order(:provider)
|
||||
@user = User.find(current_user.id)
|
||||
email_changed = false
|
||||
|
||||
if !params[:user][:email].nil?
|
||||
if @user.email != params[:user][:email]
|
||||
email_changed = true
|
||||
else
|
||||
params[:user].delete :email
|
||||
end
|
||||
end
|
||||
|
||||
password_changed = false
|
||||
if !params[:user][:password].nil?
|
||||
if !params[:user][:password].empty?
|
||||
password_changed = true
|
||||
else
|
||||
params[:user].delete :password
|
||||
params[:user].delete :password_confirmation
|
||||
end
|
||||
end
|
||||
|
||||
if email_changed || password_changed
|
||||
successfully_updated = @user.update_with_password(account_update_params)
|
||||
else
|
||||
params[:user].delete :current_password
|
||||
successfully_updated = @user.update_without_password(account_update_params)
|
||||
end
|
||||
|
||||
if successfully_updated
|
||||
if email_changed
|
||||
unless @user.nil?
|
||||
@user.update_attribute('email', params[:user][:email])
|
||||
end
|
||||
set_flash_message :notice, :update_needs_confirmation
|
||||
else
|
||||
set_flash_message :notice, :updated
|
||||
end
|
||||
# Sign in the user bypassing validation in case his password changed
|
||||
sign_in @user, bypass: true
|
||||
redirect_to after_update_path_for(@user)
|
||||
else
|
||||
flash[:alert] = 'Updating account failed. ' \
|
||||
"#{@user.errors.full_messages.join('. ')}."
|
||||
render 'edit'
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
@ -67,8 +24,7 @@ class RegistrationsController < Devise::RegistrationsController
|
|||
def configure_permitted_parameters
|
||||
devise_parameter_sanitizer.for(:account_update) do |u|
|
||||
u.
|
||||
permit(:email, :password, :password_confirmation, :current_password, :name, :biography,
|
||||
:nickname, :affiliation, :username)
|
||||
permit(:email, :password, :password_confirmation, :current_password, :username)
|
||||
end
|
||||
devise_parameter_sanitizer.for(:sign_up) do |u|
|
||||
u.
|
||||
|
|
|
|||
|
|
@ -11,11 +11,13 @@ module Users
|
|||
|
||||
def handle(provider)
|
||||
auth_hash = request.env['omniauth.auth']
|
||||
uid = auth_hash[:uid]
|
||||
openid = Openid.find_for_oauth(auth_hash) # Get or create openid
|
||||
# If openid exists and is associated with a user, sign in with associated user,
|
||||
# even if the email of the associated user and the email of the provided openid are different
|
||||
unless (user = openid.user)
|
||||
user = User.find_for_auth(auth_hash, current_user) # Get or create users
|
||||
user.username = "#{uid}@#{provider}" if user.username.blank?
|
||||
end
|
||||
|
||||
begin
|
||||
|
|
@ -28,7 +30,8 @@ module Users
|
|||
sign_in user
|
||||
redirect_to root_path, notice: user.email + " signed in successfully with #{provider}"
|
||||
rescue => e
|
||||
redirect_back_or_to new_user_registration_path, alert: 'Failed' + e.message
|
||||
flash[:error] = e.message
|
||||
redirect_back_or_to new_user_registration_path
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
30
app/controllers/users_controller.rb
Normal file
30
app/controllers/users_controller.rb
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
class UsersController < ApplicationController
|
||||
before_filter :verify_user
|
||||
load_and_authorize_resource
|
||||
|
||||
# GET /users/1
|
||||
def show
|
||||
@events = @user.events.where(state: :confirmed)
|
||||
end
|
||||
|
||||
# GET /users/1/edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# PATCH/PUT /users/1
|
||||
def update
|
||||
if @user.update(user_params)
|
||||
redirect_to @user, notice: 'User was successfully updated.'
|
||||
else
|
||||
flash[:error] = "A error prohibited your Profile from being saved: #{@user.errors.full_messages.join('. ')}."
|
||||
render :edit
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Only allow a trusted parameter "white list" through.
|
||||
def user_params
|
||||
params.require(:user).permit(:user_id, :name, :biography, :nickname, :affiliation)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue