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 diff --git a/Pearlception/app/controllers/application_controller.rb b/Pearlception/app/controllers/application_controller.rb index d620036..e32a2a1 100644 --- a/Pearlception/app/controllers/application_controller.rb +++ b/Pearlception/app/controllers/application_controller.rb @@ -1,6 +1,7 @@ class ApplicationController < ActionController::Base 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 +12,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 +21,5 @@ class ApplicationController < ActionController::Base return true end end + end diff --git a/Pearlception/app/controllers/registrations_controller.rb b/Pearlception/app/controllers/registrations_controller.rb index bc9c912..3b8ced9 100644 --- a/Pearlception/app/controllers/registrations_controller.rb +++ b/Pearlception/app/controllers/registrations_controller.rb @@ -1,19 +1,22 @@ class RegistrationsController < Devise::RegistrationsController include ApplicationHelper + #protected def new Apartment::Tenant.switch! end - + #Method to create a new User def create - @user = User.new(sign_up_params) - if params[:password] == nil || params[:password_confirmation] == nil + user_params = sign_up_params + @user = User.new(user_params) + if user_params[:password] == "" || user_params[:password_confirmation] == "" + puts "NO PASSWORD ERROR" flash[:error] = "Need a password to sign up" redirect_to '/signin' + 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 @@ -21,18 +24,38 @@ class RegistrationsController < Devise::RegistrationsController @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 + 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 + redirect_to "/" and return end - redirect_to "/" + 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..b19f09c 100644 --- a/Pearlception/app/controllers/sessions_controller.rb +++ b/Pearlception/app/controllers/sessions_controller.rb @@ -1,10 +1,20 @@ class SessionsController < Devise::RegistrationsController include ApplicationHelper - #def new - # Apartment::Tenant.switch! - #end + + def new + super + end def create + 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 + 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 +22,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/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/app/views/registrations/new.html.erb b/Pearlception/app/views/registrations/new.html.erb index 7450e9a..1b5405c 100644 --- a/Pearlception/app/views/registrations/new.html.erb +++ b/Pearlception/app/views/registrations/new.html.erb @@ -39,6 +39,7 @@ <%= render "shared/links" %>-->
<%= flash[:error] %>