Added users support
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
class ApplicationController < ActionController::Base
|
||||
before_action :set_locale
|
||||
before_action :require_login
|
||||
|
||||
private
|
||||
|
||||
@@ -8,27 +9,37 @@ class ApplicationController < ActionController::Base
|
||||
session[:locale] = I18n.locale
|
||||
end
|
||||
|
||||
# Optional: Make locale persist across requests via URL helpers
|
||||
def default_url_options
|
||||
{ locale: I18n.locale }
|
||||
end
|
||||
|
||||
def set_company
|
||||
# This should be handled by your authentication system
|
||||
# But for now, we'll use a placeholder
|
||||
company_id = session[:company_id]
|
||||
|
||||
unless company_id && Company.exists?(company_id)
|
||||
# If no company in session or it doesn't exist, use the first company
|
||||
company_id = Company.first&.id
|
||||
session[:company_id] = company_id
|
||||
end
|
||||
|
||||
@company = Company.find(company_id) if company_id
|
||||
def current_user
|
||||
@current_user ||= User.find(session[:user_id]) if session[:user_id]
|
||||
end
|
||||
|
||||
|
||||
def logged_in?
|
||||
!!current_user
|
||||
end
|
||||
|
||||
def require_login
|
||||
return if logged_in?
|
||||
|
||||
flash[:alert] = t('sessions.login_required')
|
||||
redirect_to login_path
|
||||
end
|
||||
|
||||
def set_company
|
||||
return unless logged_in?
|
||||
|
||||
@company = current_user.company
|
||||
|
||||
return if @company
|
||||
|
||||
redirect_to companies_path, alert: 'No company found. Please create a company first.'
|
||||
end
|
||||
|
||||
def current_company
|
||||
@company
|
||||
end
|
||||
helper_method :current_company
|
||||
helper_method :current_user, :logged_in?, :current_company
|
||||
end
|
||||
|
||||
@@ -219,5 +219,10 @@ class ReservationsController < ApplicationController
|
||||
end
|
||||
|
||||
@company = Company.includes(:teams).find(company_id) if company_id
|
||||
|
||||
unless @company
|
||||
redirect_to companies_path, alert: 'No company found. Please create a company first.'
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
25
app/controllers/sessions_controller.rb
Normal file
25
app/controllers/sessions_controller.rb
Normal file
@@ -0,0 +1,25 @@
|
||||
class SessionsController < ApplicationController
|
||||
skip_before_action :require_login, only: %i[new create]
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
user = User.find_by_login(params[:login])
|
||||
|
||||
if user&.authenticate(params[:password])
|
||||
session[:user_id] = user.id
|
||||
session[:company_id] = user.company_id
|
||||
redirect_to root_path, notice: t('sessions.login_successful')
|
||||
else
|
||||
flash.now[:alert] = t('sessions.invalid_credentials')
|
||||
render :new, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
session[:user_id] = nil
|
||||
session[:company_id] = nil
|
||||
redirect_to login_path, notice: t('sessions.logout_successful')
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user