From 9e9c471b35d64eeab08e5317d3c0db55466e086f Mon Sep 17 00:00:00 2001 From: cole m alban Date: Wed, 29 Mar 2017 16:03:45 -0400 Subject: [PATCH 1/2] Add exception handling in registrations#create method --- Pearlception/app/controllers/application_controller.rb | 9 --------- .../app/controllers/registrations_controller.rb | 10 +++++++--- Pearlception/app/controllers/sessions_controller.rb | 7 ++++--- Pearlception/app/models/user.rb | 3 +++ .../db/migrate/20170329193000_make_user_email_uniq.rb | 5 +++++ Pearlception/db/schema.rb | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 Pearlception/db/migrate/20170329193000_make_user_email_uniq.rb diff --git a/Pearlception/app/controllers/application_controller.rb b/Pearlception/app/controllers/application_controller.rb index 503e8f2..e32a2a1 100644 --- a/Pearlception/app/controllers/application_controller.rb +++ b/Pearlception/app/controllers/application_controller.rb @@ -1,6 +1,4 @@ class ApplicationController < ActionController::Base - before_filter :configure_permitted_parameters, if: :devise_controller? - protect_from_forgery with: :exception protected @@ -24,11 +22,4 @@ protected end end - def configure_permitted_parameters - puts "CONFIG PARAMS" - devise_parameter_sanitizer.permit(:sign_in) do |user_params| - user_params.permit(:email, :password, :remember_me) - end - end - end diff --git a/Pearlception/app/controllers/registrations_controller.rb b/Pearlception/app/controllers/registrations_controller.rb index 96af38b..8810c09 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -1,5 +1,6 @@ class RegistrationsController < Devise::RegistrationsController include ApplicationHelper + #protected def new Apartment::Tenant.switch! @@ -9,8 +10,6 @@ class RegistrationsController < Devise::RegistrationsController def create user_params = sign_up_params @user = User.new(user_params) - #if either the password or password confirmation is missing, redirect to sign in again - puts params if user_params[:password] == nil || user_params[:password_confirmation] == nil flash[:error] = "Need a password to sign up" redirect_to '/signin' and return @@ -25,7 +24,12 @@ class RegistrationsController < Devise::RegistrationsController end end @user.save - sign_in @user + begin + sign_in @user + rescue + flash[:error] = "Email already in use" + redirect_to '/signin' and return + end if !@user.admin Apartment::Tenant.switch(Company.find(@user.company_id).company_name.gsub(/'/,'').gsub(/\s/,'')) end diff --git a/Pearlception/app/controllers/sessions_controller.rb b/Pearlception/app/controllers/sessions_controller.rb index fdd9dfc..b19f09c 100644 --- a/Pearlception/app/controllers/sessions_controller.rb +++ b/Pearlception/app/controllers/sessions_controller.rb @@ -6,9 +6,10 @@ class SessionsController < Devise::RegistrationsController end def create - user_parameters = sign_in_params - @user = User.find_by(email: user_parameters[:email]) - if @user == nil || !@user.valid_password?(user_parameters[:password]) + puts "NOW IN CREATE" + params = sign_in_params + @user = User.find_by(email: params[:email]) + if @user == nil || !@user.valid_password?(params[:password]) redirect_to "/signin" return end diff --git a/Pearlception/app/models/user.rb b/Pearlception/app/models/user.rb index 1c27f7e..b04846f 100644 --- a/Pearlception/app/models/user.rb +++ b/Pearlception/app/models/user.rb @@ -3,6 +3,9 @@ class User < ApplicationRecord has_many :runs # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable + + validates :email, presence: true, uniqueness: true + devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable diff --git a/Pearlception/db/migrate/20170329193000_make_user_email_uniq.rb b/Pearlception/db/migrate/20170329193000_make_user_email_uniq.rb new file mode 100644 index 0000000..485f788 --- /dev/null +++ b/Pearlception/db/migrate/20170329193000_make_user_email_uniq.rb @@ -0,0 +1,5 @@ +class MakeUserEmailUniq < ActiveRecord::Migration[5.0] + def change + change_column :users, :email, :string, {unique: true, null: false , default: ""} + end +end diff --git a/Pearlception/db/schema.rb b/Pearlception/db/schema.rb index a2a9051..dcdd10a 100644 --- a/Pearlception/db/schema.rb +++ b/Pearlception/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170210202948) do +ActiveRecord::Schema.define(version: 20170329193000) do create_table "companies", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t| t.string "company_name" From 19d19b5908970920c682a2b3b8930070cee9d86b Mon Sep 17 00:00:00 2001 From: cole m alban Date: Wed, 29 Mar 2017 16:22:56 -0400 Subject: [PATCH 2/2] Fix missing password sign up bug --- Pearlception/app/controllers/registrations_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Pearlception/app/controllers/registrations_controller.rb b/Pearlception/app/controllers/registrations_controller.rb index 8810c09..afcc8ee 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -10,9 +10,11 @@ class RegistrationsController < Devise::RegistrationsController def create user_params = sign_up_params @user = User.new(user_params) - if user_params[:password] == nil || user_params[:password_confirmation] == nil + if user_params[:password] == "" || user_params[:password_confirmation] == "" + puts "NO PASSWORD ERROR" flash[:error] = "Need a password to sign up" - redirect_to '/signin' and return + redirect_to '/signin' + return end if params[:company_serial] company = Company.find_by(company_token: params[:company_serial])