Repository URL to install this package:
|
Version:
1.44.1 ▾
|
class BaseController < ActionController::Base
before_action :login_required, :set_token
add_flash_types :error, :alert, :success, :info
rescue_from Faraday::Error::ClientError, with: :handle_server_error
rescue_from Faraday::Error::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_FEEDS_HOST + j_platform.tickets_path
@APP_PATHS_HASH["new_ticket_path"] = J_FEEDS_HOST + j_platform.new_ticket_path
@APP_PATHS_HASH["send_jpost_path"] = (J_FEEDS_HOST + 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
@_current_project = Rails.cache.fetch("#{session[:user_token]}_projects_#{project_id}_#{find_params.keys.join}", expires_in: 5.minutes) do
JPlatform::Project.where(find_params).find(project_id)
end
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 current_client_membership
Rails.cache.fetch("#{session[:user_token]}_client_membership_#{current_user.id}_#{current_client.id}", expires_in: 10.minutes) do
JPlatform::ClientMembership.where(user_id: current_user.id, client_id: current_client.id).first
end
end
def current_project_membership
Rails.cache.fetch("#{session[:user_token]}_project_membership_#{current_client_membership.id}_#{current_project.id}", expires_in: 10.minutes) do
JPlatform::ProjectMembership.where(client_membership_id: current_client_membership.id, project_id: current_project.id).first
end
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 :current_client_membership
helper_method :current_project_membership
helper_method :s3_service
private
def set_current_user
Rails.cache.fetch("#{session[:user_token]}_user_me", expires_in: 5.minutes) do
JPlatform::User.me
end
end
def expire_current_user
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 handle_server_error(exception)
case exception.response[:status]
when 401
invalid_or_expired_session_redirect
when 403
incorrect_permission_redirect
else
raise exception
end
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
RequestStore.store[:j_platform_user_secret] = session[:user_token]
end
end