From a7aa6b776878852b55c8e99684a391ce2772d41c Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Fri, 17 Mar 2017 14:01:37 -0400 Subject: [PATCH 1/8] vs code launcher config files --- Pearlception/.vscode/launch.json | 61 ++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 Pearlception/.vscode/launch.json diff --git a/Pearlception/.vscode/launch.json b/Pearlception/.vscode/launch.json new file mode 100644 index 0000000..96d10d7 --- /dev/null +++ b/Pearlception/.vscode/launch.json @@ -0,0 +1,61 @@ +{ + "version": "0.2.0", + "configurations": [ + /*{ + "name": "Debug Local File", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${workspaceRoot}/main.rb" + }, + { + "name": "Listen for rdebug-ide", + "type": "Ruby", + "request": "attach", + "cwd": "${workspaceRoot}", + "remoteHost": "127.0.0.1", + "remotePort": "1234", + "remoteWorkspaceRoot": "${workspaceRoot}" + },*/ + { + "name": "Rails server", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${workspaceRoot}/bin/rails", + "args": [ + "server" + ] + }, + { + "name": "RSpec - all", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${workspaceRoot}/bin/rspec", + "args": [ + "-I", + "${workspaceRoot}" + ] + }, + { + "name": "RSpec - active spec file only", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${workspaceRoot}/bin/rspec", + "args": [ + "-I", + "${workspaceRoot}", + "${file}" + ] + }, + { + "name": "Cucumber", + "type": "Ruby", + "request": "launch", + "cwd": "${workspaceRoot}", + "program": "${workspaceRoot}/bin/cucumber" + } + ] +} \ No newline at end of file From c92bac542de25eb2de69cf35517300f2426e1648 Mon Sep 17 00:00:00 2001 From: cole m alban Date: Tue, 28 Mar 2017 13:05:17 -0400 Subject: [PATCH 2/8] Fix error with double render --- .../app/controllers/application_controller.rb | 14 ++++++++++- .../controllers/registrations_controller.rb | 23 +++++++++++-------- .../app/controllers/sessions_controller.rb | 22 +++++++++++++++--- Pearlception/config/routes.rb | 2 +- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/Pearlception/app/controllers/application_controller.rb b/Pearlception/app/controllers/application_controller.rb index d620036..503e8f2 100644 --- a/Pearlception/app/controllers/application_controller.rb +++ b/Pearlception/app/controllers/application_controller.rb @@ -1,6 +1,9 @@ class ApplicationController < ActionController::Base + before_filter :configure_permitted_parameters, if: :devise_controller? + protect_from_forgery with: :exception - protected +protected + def authenticate_user if session[:user_id] # set current user object to @current_user object variable @@ -11,6 +14,7 @@ class ApplicationController < ActionController::Base return false end end + def save_login_state if session[:user_id] redirect_to(:controller => 'sessions', :action => 'home') @@ -19,4 +23,12 @@ class ApplicationController < ActionController::Base return true 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 b17fba2..96af38b 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -5,33 +5,38 @@ class RegistrationsController < Devise::RegistrationsController Apartment::Tenant.switch! end - + #Method to create a new User def create - @user = User.new(sign_up_params) - + 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 + end if params[:company_serial] - params.require(:user).permit(:company_id) company = Company.find_by(company_token: params[:company_serial]) - if company @user.company_id = company.id if company.company_name == "IVA" @user.admin = true end - else end - - else end @user.save sign_in @user if !@user.admin Apartment::Tenant.switch(Company.find(@user.company_id).company_name.gsub(/'/,'').gsub(/\s/,'')) end - redirect_to "/" + redirect_to "/" and return end + private + + #Param checking method for creation of a new user def sign_up_params params.require(:user).permit(:email, :password, :password_confirmation) end + end diff --git a/Pearlception/app/controllers/sessions_controller.rb b/Pearlception/app/controllers/sessions_controller.rb index 2b4870e..fdd9dfc 100644 --- a/Pearlception/app/controllers/sessions_controller.rb +++ b/Pearlception/app/controllers/sessions_controller.rb @@ -1,10 +1,19 @@ class SessionsController < Devise::RegistrationsController include ApplicationHelper - #def new - # Apartment::Tenant.switch! - #end + + def new + super + 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]) + redirect_to "/signin" + return + end + super + #Do we need this code below? It was never running before resource = warden.authenticate!(:scope => :user) sign_in(:user, resource) if !current_user.admin @@ -12,4 +21,11 @@ class SessionsController < Devise::RegistrationsController end redirect_to "/" end + +private + + def sign_in_params + params.require(:user).permit(:email,:password,:remember_me) + end + end diff --git a/Pearlception/config/routes.rb b/Pearlception/config/routes.rb index d958c2f..47f42e1 100644 --- a/Pearlception/config/routes.rb +++ b/Pearlception/config/routes.rb @@ -8,7 +8,7 @@ Rails.application.routes.draw do resources :companies resources :grades - devise_for :users, :controllers => {:registrations => 'registrations'} + devise_for :users, :controllers => {:registrations => 'registrations', :sessions => "sessions"} devise_scope :users do get 'signin' => 'registrations#new' post 'signin' => 'registrations#create' From b1bb0f69ad752b995f7231f7b8b383ca3d22f706 Mon Sep 17 00:00:00 2001 From: cole m alban Date: Tue, 28 Mar 2017 13:06:17 -0400 Subject: [PATCH 3/8] Sign in back to original --- Pearlception/config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pearlception/config/routes.rb b/Pearlception/config/routes.rb index 47f42e1..d958c2f 100644 --- a/Pearlception/config/routes.rb +++ b/Pearlception/config/routes.rb @@ -8,7 +8,7 @@ Rails.application.routes.draw do resources :companies resources :grades - devise_for :users, :controllers => {:registrations => 'registrations', :sessions => "sessions"} + devise_for :users, :controllers => {:registrations => 'registrations'} devise_scope :users do get 'signin' => 'registrations#new' post 'signin' => 'registrations#create' From 9e9c471b35d64eeab08e5317d3c0db55466e086f Mon Sep 17 00:00:00 2001 From: cole m alban Date: Wed, 29 Mar 2017 16:03:45 -0400 Subject: [PATCH 4/8] 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 5/8] 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]) From 7f03001238a34beacb433242600d396cf7e3dc25 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Thu, 30 Mar 2017 12:18:07 -0400 Subject: [PATCH 6/8] Added flash messages for company sign up --- .../controllers/registrations_controller.rb | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/Pearlception/app/controllers/registrations_controller.rb b/Pearlception/app/controllers/registrations_controller.rb index 96af38b..49b4c31 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -22,14 +22,27 @@ class RegistrationsController < Devise::RegistrationsController if company.company_name == "IVA" @user.admin = true end + else + flash[:alert] = "The serial key provided was invalid." + redirect_to '/' end + else + flash[:alert] = "A company serial key must be provided to register." + redirect_to '/' end - @user.save - sign_in @user - if !@user.admin - Apartment::Tenant.switch(Company.find(@user.company_id).company_name.gsub(/'/,'').gsub(/\s/,'')) + + if @user == "" + flash[:alert] = "Please provide a password to register with." + redirect_to '/' + else + @user.save + sign_in @user + if !@user.admin + Apartment::Tenant.switch(Company.find(@user.company_id).company_name.gsub(/'/,'').gsub(/\s/,'')) + end + redirect_to "/" and return end - redirect_to "/" and return + end private From 730c399b7100ce0818a57c649bd69a96c920bfd1 Mon Sep 17 00:00:00 2001 From: Okechi Onyeje Date: Thu, 30 Mar 2017 12:32:36 -0400 Subject: [PATCH 7/8] adding a tab character --- .../app/controllers/registrations_controller.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Pearlception/app/controllers/registrations_controller.rb b/Pearlception/app/controllers/registrations_controller.rb index 51ca8db..3b8ced9 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -37,12 +37,12 @@ class RegistrationsController < Devise::RegistrationsController redirect_to '/' else @user.save - begin - sign_in @user - rescue - flash[:error] = "Email already in use" - redirect_to '/signin' and return - end + 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 From d1c705846c6e84f8df6fad02bb984fa9491dcd63 Mon Sep 17 00:00:00 2001 From: cole m alban Date: Thu, 30 Mar 2017 12:50:28 -0400 Subject: [PATCH 8/8] Add flash code to registrations new view --- Pearlception/app/views/registrations/new.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/Pearlception/app/views/registrations/new.html.erb b/Pearlception/app/views/registrations/new.html.erb index 2b78d5b..465d21b 100644 --- a/Pearlception/app/views/registrations/new.html.erb +++ b/Pearlception/app/views/registrations/new.html.erb @@ -36,6 +36,7 @@ <%= render "shared/links" %>-->
+

<%= flash[:error] %>