Repository URL to install this package:
Version:
2.23.0 ▾
|
class BaseController < ActionController::Base
before_action :ajax_only, only: :change_working_project
before_action :login_required, :set_token
add_flash_types :error, :alert, :success, :info
rescue_from ActiveResource::ForbiddenAccess, with: :incorrect_permission_redirect
rescue_from ActiveResource::UnauthorizedAccess, with: :invalid_or_expired_session_redirect
rescue_from ActiveResource::ResourceNotFound, with: :active_resource_not_found
layout 'j_platform/application'
helper JPlatform::DateTimeHelper
def set_app_paths
@APP_PATHS_HASH ||= {}
@APP_PATHS_HASH["tickets_path"] = j_platform.tickets_path
@APP_PATHS_HASH["new_ticket_path"] = j_platform.new_ticket_path
@APP_PATHS_HASH["send_jpost_path"] = j_platform.project_posts_path(current_project) if current_project.present?
@APP_PATHS_HASH["platform_endpoint"] = JPlatform::Default.endpoint
end
def current_user
set_current_user
end
def current_organization
@_current_organization ||= (current_user.present? ? current_user.organization : nil)
end
def current_clients
@_current_clients ||= (current_user.present? ? current_user.clients.select {|c| !c.is_suspended } : [])
end
def current_projects
@_current_projects ||= (current_clients.empty? ? [] : current_user.projects.uniq.select {|p| !p.is_suspended && !p.is_closed })
end
def client_with_projects
if @_client_with_projects.blank?
grouped_projects = current_projects.group_by(&:client_id)
@_client_with_projects ||= grouped_projects.inject({}) do |x, (k,v)|
client = current_clients.find { |c| c.id == k }
v = v.sort_by { |project| project.name.downcase } if v.present?
x[client] = v unless client.nil?
x
end
end
@_client_with_projects
end
def current_project(options={})
find_params = { include_social_accounts: true, include_client: true }.merge!(options)
project_id = params[:project_id].present? ? params[:project_id] : session[:project_id].presence
project_id ||= current_projects.first.id rescue nil
@_current_project = Rails.cache.fetch("#{session[:cache_key]}_projects_#{project_id}_#{find_params.keys.join}", expires_in: 10.minutes) do
Project.find(project_id, params: find_params)
end if project_id
session[:project_id] = @_current_project.try(:id)
@_current_project
end
alias_method :current_team, :current_project
def current_project_id
current_project.try(:id)
end
alias_method :current_team_id, :current_project_id
def current_client
@_current_client ||= current_clients.select { |c| c.id == current_team.client_id }.first if current_team.present?
end
def s3_service(project_id=nil)
@_s3 ||= S3Service.new(project_id || current_team_id)
end
helper_method :current_user
helper_method :current_organization
helper_method :current_project
helper_method :current_project_id
helper_method :current_team
helper_method :current_client
helper_method :client_with_projects
helper_method :s3_service
def change_working_project
clean_session!
session[:project_id] = params[:working_project_id].to_i
render json: 'success'
end
protected
def clean_session!
old_session = { user_token: session[:user_token], user_id: session[:user_id], last_seen: session[:last_seen] }
session.clear
session.merge! old_session
clean_session_cache_key!
end
def clean_session_cache_key!
session[:cache_key] = SecureRandom.hex(10)
end
private
def set_current_user
Rails.cache.fetch("#{session[:cache_key]}_user_me", expires_in: 10.minutes) do
User.me
end
end
def ajax_only
raise ActionController::RoutingError.new('Not Found') unless (request.xhr?.nil? || request.xhr? == 0)
end
alias_method :active_resource_not_found, :ajax_only
def require_super_admin_permission
incorrect_permission_redirect unless current_user.present? && current_user.super_admin?
end
def incorrect_permission_redirect
redirect_to(j_platform.me_path, notice: t('messages.incorrect_permission'))
end
def invalid_or_expired_session_redirect
session.clear
login_required
end
def require_available_projects
if current_projects.empty?
if request[:controller] == 'j_platform/users' && request[:action] == 'show'
flash[:notice] ||= current_clients.empty? ? t('user.messages.no_client_error') : t('user.messages.no_project_error')
else
return redirect_to(j_platform.me_path, status: 303) if (request[:controller] =~ /admin/).nil?
end
end
end
def login_required
if session[:last_seen].nil? || (session[:last_seen] < 2.hours.ago)
session.clear
if request.xhr?
return render plain: 'Expired session', status: 412
else
return redirect_to '/auth/j_auth?origin=' + Rack::Utils.escape(request.fullpath)
end
end
session[:last_seen] = Time.now
end
def set_token
::PlatformResource.headers['authorization'] = "Bearer #{session[:user_token]}"
end
end