lib/aptible/rails/controller.rb in aptible-rails-0.6.13 vs lib/aptible/rails/controller.rb in aptible-rails-0.7.0
- old
+ new
@@ -25,12 +25,15 @@
end
def current_organization
return @current_organization if @current_organization
url = read_shared_cookie(:organization_url)
- @current_organization = Aptible::Auth::Organization.find_by_url(
- url, token: session_token) if url
+ if url
+ @current_organization = Aptible::Auth::Organization.find_by_url(
+ url, token: session_token
+ )
+ end
@current_organization ||= default_organization
rescue HyperResource::ClientError => e
raise e unless e.body['code'] == 403
@current_organization = default_organization
@@ -40,11 +43,11 @@
@organization_users ||= current_organization.users
end
def production_apps
return @production_apps if @production_apps
- accounts = Aptible::Api::Account.all(token: service_token)
+ accounts = Aptible::Api::Account.all(token: session_token)
accounts = accounts.select do |account|
next unless account.type == 'production'
next unless account.organization == current_organization
true
end
@@ -72,15 +75,10 @@
# before_action :authenticate_user
def authenticate_user
redirect_to Aptible::Rails.configuration.login_url unless current_user
end
- # before_action :ensure_service_token
- def ensure_service_token
- redirect_to aptible_login_url unless service_token
- end
-
# before_action :ensure_compliance_plan
def ensure_compliance_plan
unless current_organization &&
current_organization.can_manage_compliance?
redirect_to compliance_url
@@ -95,59 +93,22 @@
end
end
def criteria
@criteria ||= Aptible::Gridiron::Criterion.where(
- token: service_token,
+ token: session_token,
organization: current_organization
)
end
def compliance_alerts
return @compliance_alerts if @compliance_alerts
@compliance_alerts = ComplianceAlertCollection.new(
- criteria, production_apps, organization_users
- ).all
+ criteria, production_apps, organization_users
+ ).all
end
- def service_token
- return unless session_token && session_token.session
- return @service_token if @service_token
-
- @service_token = cached_service_token(session_token)
- if Fridge::AccessToken.new(@service_token).valid?
- @service_token
- else
- @service_token = cached_service_token(session_token,
- force: true) || session_token
- end
- end
-
- def cached_service_token(session_token, options = {})
- fail 'Token must be a service token' unless session_token.session
- cache_key = "service_token:#{session_token.session}"
- ::Rails.cache.fetch(cache_key, options) do
- swap_session_token(session_token)
- end
- end
-
- # rubocop:disable MethodLength
- def swap_session_token(session_token)
- Aptible::Auth::Token.create(
- client_id: Aptible::Rails.configuration.client_id,
- client_secret: Aptible::Rails.configuration.client_secret,
- subject: session_token.serialize
- ).access_token
- rescue OAuth2::Error => e
- if e.code == 'unauthorized'
- nil
- else
- raise 'Could not swap session token, check Client#privileged?'
- end
- end
- # rubocop:enable MethodLength
-
def organization_url(id)
"#{dashboard_url}/organizations/#{id}"
end
def user_url(id = current_user.id)
@@ -155,10 +116,10 @@
end
def criterion_by_handle(handle)
Aptible::Gridiron::Criterion.where(
handle: handle.to_s,
- token: service_token,
+ token: session_token,
organization: current_organization
).first
end
def auth_url(path = '/', params = {})