From 08244444b00faa9d105e7876e23dbf6202cdfc3d Mon Sep 17 00:00:00 2001 From: Godwin Date: Wed, 5 Oct 2016 21:00:51 -0700 Subject: [PATCH] Required that all accounts that can be used to log in have an email address --- .../stylesheets/bumbleberry-settings.json | 4 +- app/controllers/oauths_controller.rb | 72 +++++++++++++++++-- app/views/application/update_user.html.haml | 9 +++ config/locales/en.yml | 3 + config/routes.rb | 6 +- .../20161006021205_add_fb_id_to_user.rb | 5 ++ db/schema.rb | 5 +- 7 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 app/views/application/update_user.html.haml create mode 100644 db/migrate/20161006021205_add_fb_id_to_user.rb diff --git a/app/assets/stylesheets/bumbleberry-settings.json b/app/assets/stylesheets/bumbleberry-settings.json index 04a7c8b..0897d6f 100644 --- a/app/assets/stylesheets/bumbleberry-settings.json +++ b/app/assets/stylesheets/bumbleberry-settings.json @@ -5,8 +5,8 @@ "chrome": ["51"] }, "development": { - "and_chr": ["51"], - "chrome": ["52"], + "and_chr": ["53"], + "chrome": ["53"], "edge": ["13"], "firefox": ["44"], "ie": ["11"], diff --git a/app/controllers/oauths_controller.rb b/app/controllers/oauths_controller.rb index 8e1fedf..ea1834a 100644 --- a/app/controllers/oauths_controller.rb +++ b/app/controllers/oauths_controller.rb @@ -13,18 +13,76 @@ class OauthsController < ApplicationController set_callback user_info = (sorcery_fetch_user_hash auth_params[:provider] || {})[:user_info] - user = User.find_by_email(user_info['email']) - + + email = user_info['email'] + fb_id = user_info['id'] + + # try to find the user by facebook id + user = User.find_by_fb_id(fb_id) + + # otherwise find the user by email + unless user.present? + # only look if the email address is present + user = User.find_by_email(email) if email.present? + end + # create the user if the email is not recognized - unless user - user = User.new(email: user_info['email'], firstname: user_info['name']) + if user.nil? + if email.present? + user = User.new(email: email, firstname: user_info['name'], fb_id: fb_id) + user.save! + else + session[:oauth_update_user_info] = user_info + return redirect_to oauth_update_path + end + elsif user.fb_id.blank? || user.email.blank? + user.email = email + user.fb_id = fb_id user.save! end - # log in the user - auto_login(user) if user + if user.present? && user.email.present? + # log in the user + auto_login(user) + end + + oauth_last_url = (session[:oauth_last_url] || home_path) + session.delete(:oauth_last_url) + redirect_to oauth_last_url + end + + def update + @main_title = @page_title = 'articles.conference_registration.headings.email_confirm' + @errors = { email: flash[:error] } if flash[:error].present? + render 'application/update_user' + end + + def save + unless params[:email].present? + return redirect_to oauth_update_path + end + + user = User.find_by_email(params[:email]) + + if user.present? + flash[:error] = :exists + return redirect_to oauth_update_path + end - redirect_to (session[:oauth_last_url] || home_path) + # create the user + user = User.new(email: params[:email], firstname: session[:oauth_update_user_info]['name'], fb_id: session[:oauth_update_user_info]['id']) + user.save! + + # log in + auto_login(user) + + # clear out the session + oauth_last_url = (session[:oauth_last_url] || home_path) + session.delete(:oauth_last_url) + session.delete(:oauth_update_user_info) + + # go to our final destination + redirect_to oauth_last_url end private diff --git a/app/views/application/update_user.html.haml b/app/views/application/update_user.html.haml new file mode 100644 index 0000000..d107d68 --- /dev/null +++ b/app/views/application/update_user.html.haml @@ -0,0 +1,9 @@ += render :partial => 'application/header', :locals => {:image_file => 'grafitti.jpg'} +%article + = row do + = columns do + %h2=_'articles.conference_registration.headings.Enter_Your_Email' + %p=_'articles.conference_registration.paragraphs.provide_email', :p + = form_tag oauth_save_path, class: 'flex-form' do + = emailfield :email, nil, required: true, big: true + = button_tag :save, value: :save diff --git a/config/locales/en.yml b/config/locales/en.yml index 15b2eb0..2c333c3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -169,6 +169,8 @@ en: empty: 'Please enter an address' space: empty: 'Please select a space' + email: + exists: An account with this email address already exists housing: space: companions: This host wishes to be housed with %{name} @@ -1026,6 +1028,7 @@ en: info: Info companion: Companion paragraphs: + provide_email: Bike!Bike! uses email to communicate between you and your conference hosts, however your Facebook account does not provide us an email address. Before proceeding, you must provide us an email address. Policy_Agreement: Ensuring that all attendees feel welcome, safe, and respected at all times is especially important to us all. Please ensure that you have fully read and understand our safer spaces policy below, if you have any questions or concerns you can reach out to the organizers at any time. Confirm_Agreement: By clicking the "I Agree" button, you are pledging to do your best to uphold Bike!Bike!'s safer space agreement. Thank you! Registration_Info: Please fill in this registration form to help us prepare for your arrival to %{city}. If you wish to ask questions or tell us information we did not ask, please fill in the preferences field at the bottom of the page or use the contact us link. diff --git a/config/routes.rb b/config/routes.rb index 451c211..b3ff187 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -47,7 +47,7 @@ BikeBike::Application.routes.draw do get '/confirm/:token' => 'application#confirm', :as => :confirm match '/doconfirm' => 'application#do_confirm', :as => :do_confirm, via: [:get, :post] - #post '/doconfirm' => 'application#do_confirm', :as => :do_confirm + match '/user/logout' => 'application#user_logout', :as => :logout, :via => [:get, :post] get '/contact' => 'application#contact', :as => :contact post '/contact/send' => 'application#contact_send', :as => :contact_send @@ -55,10 +55,10 @@ BikeBike::Application.routes.draw do get '/user' => 'application#user_settings', :as => :settings post '/user/update' => 'application#update_user_settings', :as => :update_settings match '/oauth/callback' => 'oauths#callback', :via => [:get, :post] + get '/oauth/update' => 'oauths#update', :as => :oauth_update + post '/oauth/save' => 'oauths#save', :as => :oauth_save get '/oauth/:provider' => 'oauths#oauth', :as => :auth_at_provider - # post '/translator-request' => 'application#translator_request', :as => :translator_request - # patch '/capture_view' => 'application#capture_view' post '/js_error' => 'application#js_error' get '/error_403' => 'application#do_403' get '/error_404' => 'application#error_404' diff --git a/db/migrate/20161006021205_add_fb_id_to_user.rb b/db/migrate/20161006021205_add_fb_id_to_user.rb new file mode 100644 index 0000000..603e75d --- /dev/null +++ b/db/migrate/20161006021205_add_fb_id_to_user.rb @@ -0,0 +1,5 @@ +class AddFbIdToUser < ActiveRecord::Migration + def change + add_column :users, :fb_id, :bigint + end +end diff --git a/db/schema.rb b/db/schema.rb index bfd84b6..5c5b82e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160814000940) do +ActiveRecord::Schema.define(version: 20161006021205) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -328,7 +328,7 @@ ActiveRecord::Schema.define(version: 20160814000940) do t.string "activation_state" t.string "activation_token" t.datetime "activation_token_expires_at" - t.integer "failed_logins_count", default: 0 + t.integer "failed_logins_count", default: 0 t.datetime "lock_expires_at" t.string "unlock_token" t.string "avatar" @@ -340,6 +340,7 @@ ActiveRecord::Schema.define(version: 20160814000940) do t.json "languages" t.string "locale" t.boolean "is_subscribed" + t.integer "fb_id", limit: 8 end add_index "users", ["activation_token"], name: "index_users_on_activation_token", using: :btree