diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3dbf503..ecf5dcf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -49,11 +49,20 @@ end def after_sign_in_path_for(resource) - (from_external_domain? ? root_path : (!request.referer.eql?(new_user_session_path) ? request.referer || root_path : root_path)) + if from_external_domain? || request.referer.eql?(new_user_session_url(:protocol => 'https')) || request.referer.eql?(new_user_registration_url(:protocol => 'https')) + root_path + else + return request.referer unless request.referer.nil? + root_path + end end def after_sign_up_path_for(resource) - (from_external_domain? ? root_path : (!request.referer.eql?(new_user_session_path) ? request.referer || root_path : root_path)) + if from_external_domain? or request.referer.eql?(new_user_session_url(:protocol => 'https')) + root_path + else + request.referer + end end def after_sign_in_error_path_for(resource) diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 3f53c49..7984224 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -26,14 +26,13 @@ unless oauth.nil? # The OAuth provider could not be determined or there was no unique UID! - if oauth[:provider].nil? || oauth[:uid].nil? - flash[:alert] = _('We were unable to verify your account. Please use the following form to create a new account. You will be able to link your new account afterward.') - + if oauth['provider'].nil? || oauth['uid'].nil? + # flash[:alert] = _('We were unable to verify your account. Please use the following form to create a new account. You will be able to link your new account afterward.') else # Connect the new user with the identifier sent back by the OAuth provider - flash[:notice] = _('It does not look like you have setup an account with us yet. Please fill in the following information to complete your registration.') - UserIdentifier.create(identifier_scheme: oauth[:provider].upcase, - identifier: oauth[:uid], + flash[:notice] = _('Please make a choice below. After linking your details to a %{application_name} account, you will be able to sign in directly with your institutional credentials.') % {application_name: Rails.configuration.branding[:application][:name]} + UserIdentifier.create(identifier_scheme: IdentifierScheme.find_by(name: oauth['provider'].downcase), + identifier: oauth['uid'], user: @user) end end @@ -41,6 +40,11 @@ # POST /resource def create + oauth = {provider: nil, uid: nil} + IdentifierScheme.all.each do |scheme| + oauth = session["devise.#{scheme.name.downcase}_data"] unless session["devise.#{scheme.name.downcase}_data"].nil? + end + if !sign_up_params[:accept_terms] redirect_to after_sign_up_error_path_for(resource), alert: _('You must accept the terms and conditions to register.') elsif params[:user][:org_id].blank? && params[:user][:other_organisation].blank? @@ -70,6 +74,19 @@ set_flash_message :notice, :signed_up if is_navigational_format? sign_up(resource_name, resource) UserMailer.welcome_notification(current_user).deliver + unless oauth.nil? + # The OAuth provider could not be determined or there was no unique UID! + unless oauth['provider'].nil? || oauth['uid'].nil? + prov = IdentifierScheme.find_by(name: oauth['provider'].downcase) + # Until we enable ORCID signups + if prov.name == 'shibboleth' + UserIdentifier.create(identifier_scheme: prov, + identifier: oauth['uid'], + user: @user) + flash[:notice] = _('Welcome! You have signed up successfully with your institutional credentials. You will now be able to access your account with them.') + end + end + end respond_with resource, location: after_sign_up_path_for(resource) else set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format? diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 0c9730d..dacd5a1 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -3,22 +3,29 @@ def new redirect_to(root_path) end + # Capture the user's shibboleth id if they're coming in from an IDP # --------------------------------------------------------------------- def create existing_user = User.find_by(email: params[:user][:email]) if !existing_user.nil? -# TODO: Not sure why we check for shib data in params and then use session value below. We should move this to the -# new user_identifiers table - if !params[:shibboleth_data].nil? - #after authentication verify if session[:shibboleth] exists - existing_user.update_attributes(shibboleth_id: session[:shibboleth_data][:uid]) + # Until ORCID login is supported + if !session['devise.shibboleth_data'].nil? + if u = UserIdentifier.create(identifier_scheme: IdentifierScheme.find_by(name: 'shibboleth'), + identifier: session['devise.shibboleth_data']['uid'], + user: existing_user) + success = _('Your account has been successfully linked to your institutional credentials. You will now be able to sign in with them.') + end + existing_user.update_attributes(shibboleth_id: session['devise.shibboleth_data'][:uid]) end session[:locale] = existing_user.get_locale unless existing_user.get_locale.nil? set_gettext_locale #Method defined at controllers/application_controller.rb end super + if success + flash[:notice] = success + end end def destroy @@ -26,4 +33,4 @@ session[:locale] = nil set_gettext_locale #Method defined at controllers/application_controller.rb end -end \ No newline at end of file +end diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb index 3cafb36..38719f9 100644 --- a/app/controllers/users/omniauth_callbacks_controller.rb +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -27,7 +27,6 @@ # If the uid didn't have a match in the system send them to register if user.nil? session["devise.#{scheme.name.downcase}_data"] = request.env["omniauth.auth"] - flash[:notice] = _('It does not look like you have setup an account with us yet. Please fill in the following information to complete your registration.') redirect_to new_user_registration_url # Otherwise sign them in diff --git a/app/views/devise/passwords/edit.html.erb b/app/views/devise/passwords/edit.html.erb index 92d1218..cabaa15 100644 --- a/app/views/devise/passwords/edit.html.erb +++ b/app/views/devise/passwords/edit.html.erb @@ -20,7 +20,7 @@
+
<%= _("This will link your existing account to your credentials.") %>
+<%= render :partial => 'shared/sign_in_form' %>
+
<%= _("This will create an account and link it to your credentials.") %>
+<%= render :partial => 'shared/create_account_form', locals: {extended: false} %>