Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Versions OAuth #42

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions app/controllers/api/v1/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class BaseController < ActionController::API

private


def decoded_token
return @decoded_token if defined?(@decoded_token)

Expand Down Expand Up @@ -117,8 +116,6 @@ def require_user
false
end

private

def find_user_from_token
return unless decoded_token
user_data = decoded_token[:user]
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/api/v1/saved_scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ def saved_scenario_params
end

def engine_client
MyEtm::Auth.engine_client(current_user)#, scopes: doorkeeper_token.scopes)
MyEtm::Auth.engine_client(current_user, active_version_tag, scopes: doorkeeper_token.scopes)
end

def active_version_tag
if Version.tags.include?(saved_scenario_params[:version].to_s)
saved_scenario_params[:version]
else
Version::DEFAULT_TAG
end
end
end
end
Expand Down
25 changes: 16 additions & 9 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class ApplicationController < ActionController::Base
before_action :set_locale
before_action :configure_sentry
before_action :store_user_location!, if: :storable_location?
before_action :store_redirect_url
before_action :set_active_version_tag

helper_method :active_version_tag

rescue_from CanCan::AccessDenied do |_exception|
if current_user
Expand Down Expand Up @@ -36,8 +38,8 @@ def set_locale
session[:locale] || http_accept_language.preferred_language_from(I18n.available_locales)
end

def last_visited_page
redirect_to cookies[:last_visited_page] || root_path
def active_version_tag
session[:active_version_tag] || Version::DEFAULT_TAG
end

private
Expand Down Expand Up @@ -74,8 +76,8 @@ def configure_sentry
end
end

def engine_client
MyEtm::Auth.engine_client(current_user)
def engine_client(version_tag)
MyEtm::Auth.engine_client(current_user, version_tag)
end

# Internal: Renders a 404 page.
Expand Down Expand Up @@ -136,9 +138,14 @@ def turbo_alert(message = nil)
)
end

def store_redirect_url
if params[:redirect_url].present?
session[:redirect_url] = params[:redirect_url]
end
# Validates the version tag passed from the latest request and sets it in the
# session, so we can redirect back to that version later.
#
# TODO: somebody has to set this!
def set_active_version_tag
return unless params[:active_version]
return unless Version.tags.include?(params[:active_version].to_s)

session[:active_version_tag] = params[:active_version]
end
end
8 changes: 4 additions & 4 deletions app/controllers/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def show
# POST /collections/create_transition
def create_transition
result = CreateInterpolatedCollection.call(
engine_client,
engine_client(create_transition_params[:version]),
current_user.saved_scenarios.find(create_transition_params[:saved_scenario_ids]),
current_user
)
Expand Down Expand Up @@ -120,8 +120,8 @@ def confirm_destroy
# DELETE /collections/:id
def destroy
DeleteCollection.call(
engine_client,
current_user.collections.find(params.require(:id))
engine_client(@collection.version),
@collection
)

redirect_to collections_path
Expand Down Expand Up @@ -178,7 +178,7 @@ def create_collection_params
end

def create_transition_params
params.require(:collection).permit(:saved_scenario_ids)
params.require(:collection).permit(:version, :saved_scenario_ids)
end

def filter_params
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/passthru_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PassthruController < ApplicationController
def last
redirect_to cookies[:etm_last_visited_page] || root_path
end
end
7 changes: 5 additions & 2 deletions app/controllers/saved_scenario_history_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class SavedScenarioHistoryController < ApplicationController

# GET /saved_scenarios/:id/history
def index
version_tags_result = ApiScenario::VersionTags::FetchAll.call(engine_client, @saved_scenario)
version_tags_result = ApiScenario::VersionTags::FetchAll.call(
engine_client(@saved_scenario.version),
@saved_scenario
)

if version_tags_result.successful?
@history = SavedScenarioHistoryPresenter.present(@saved_scenario, version_tags_result.value)
Expand All @@ -32,7 +35,7 @@ def index
# PUT /saved_scenarios/:id/history/:scenario_id
def update
result = ApiScenario::VersionTags::Update.call(
engine_client,
engine_client(@saved_scenario.version),
params[:scenario_id],
update_params[:description]
)
Expand Down
13 changes: 10 additions & 3 deletions app/controllers/saved_scenario_users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ def new
# POST /saved_scenarios/:saved_scenario_id/users
def create
result = CreateSavedScenarioUser.call(
engine_client, @saved_scenario, current_user.name, scenario_user_params
engine_client(@saved_scenario.version),
@saved_scenario,
current_user.name,
scenario_user_params
)

if result.successful?
Expand Down Expand Up @@ -71,7 +74,7 @@ def create
# PUT /saved_scenarios/:saved_scenario_id/users/:id
def update
result = UpdateSavedScenarioUser.call(
engine_client,
engine_client(@saved_scenario.version),
@saved_scenario,
@saved_scenario_user,
scenario_user_params[:role_id]&.to_i
Expand Down Expand Up @@ -110,7 +113,11 @@ def confirm_destroy
#
# PUT /saved_scenarios/:saved_scenario_id/users/:id
def destroy
result = DestroySavedScenarioUser.call(engine_client, @saved_scenario, @saved_scenario_user)
result = DestroySavedScenarioUser.call(
engine_client(@saved_scenario.version),
@saved_scenario,
@saved_scenario_user
)

if result.successful?
@saved_scenario.reload
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/saved_scenarios_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def publish
@saved_scenario.update(private: false)

ApiScenario::UpdatePrivacy.call_with_ids(
engine_client,
engine_client(@saved_scenario.version),
@saved_scenario.all_scenario_ids,
private: false
)
Expand All @@ -127,7 +127,7 @@ def unpublish
@saved_scenario.update(private: true)

ApiScenario::UpdatePrivacy.call_with_ids(
engine_client,
engine_client(@saved_scenario.version),
@saved_scenario.all_scenario_ids,
private: true
)
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def configure_account_update_params
# Check if the user is already signed in and redirect back to client or to root.
def check_already_authenticated
return unless user_signed_in?
redirect_uri = session[:redirect_url].chomp('/')
token = MyEtm::Auth.user_jwt(current_user, client_id: params[:client_id])
redirect_url = URI(params[:redirect_url] || root_path)
redirect_url.query = URI.encode_www_form(token: token)
Expand All @@ -71,7 +70,7 @@ def stats_for_destroy
personal_access_tokens: current_user.personal_access_tokens.not_expired.count,
oauth_applications: current_user.oauth_applications.count,
collections: 0
# collections: current_user.collections.count
# collections: current_user.collections.count # TODO: Re-Implement
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!!

}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ def format_staff_config(config, app)
etengine_url = Settings.etengine.uri || "http://YOUR_ETENGINE_URL"

format(config, app.attributes.symbolize_keys.merge(
myetm_url: root_url.chomp("/root"),
myetm_url: root_url.chomp("/"),
etengine_url: etengine_url,
etmodel_url: Settings.etmodel.uri || "http://YOUR_ETMODEL_URL",
collections_url: Settings.collections.uri || "http://YOUR_COLLECTIONS_URL",
etengine_uid: Doorkeeper::Application.find_by(uri: etengine_url)&.uid || "YOUR_ETEngine_ID_HERE"
etengine_uid: Doorkeeper::Application.find_by(uri: etengine_url)&.uid || "YOUR_ETEngine_ID"
))
end

Expand Down
41 changes: 31 additions & 10 deletions app/models/version.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# frozen_string_literal: true

# TODO: port to db and hook into OAuth apps. This is a mess and not nice to keep up for beta and pro!
# A valid version of the ETM
class Version
URL = "energytransitionmodel.com".freeze
DEFAULT_TAG = "latest"

# Tag => prefix
LIST = {
Expand All @@ -11,6 +13,12 @@ class Version
"stable.02" => "stable2."
}.freeze

LOCAL_URLS = {
"collections" => Settings.collections.uri,
"model" => Settings.etmodel.uri,
"engine" => Settings.etengine.uri
}.freeze

# All available versions. Uses ActiveRecord syntax 'all' to
# make future porting to db easier
def self.all
Expand All @@ -21,26 +29,39 @@ def self.tags
LIST.keys
end

def self.model_url(tag)
"https://#{LIST[tag]}#{Version::URL}"
def self.collections_url(tag = nil)
build_url("collections", tag)
end

def self.engine_url(tag)
"https://#{LIST[tag]}engine.#{Version::URL}"
def self.model_url(tag = nil)
build_url("model", tag)
end

# TODO: Collections url

# TODO: urls for local development => Add a local version and
# exceptions for the urls
def self.engine_url(tag = nil)
build_url("engine", tag)
end

def self.as_json(*)
Version.tags.map do |tag|
{
tag: tag,
url: Version.model_url(tag),
engine_url: Version.engine_url(tag)
model_url: model_url(tag),
engine_url: engine_url(tag),
collections_url: collections_url(tag)
}
end
end

private

def self.build_url(context, tag)
tag ||= DEFAULT_TAG
raise ArgumentError, "Invalid version tag: #{tag}" unless LIST.key?(tag)

if Rails.env.development?
LOCAL_URLS[context]
else
"https://#{LIST[tag]}#{context == 'model' ? '' : "#{context}."}#{URL}"
end
end
end
2 changes: 1 addition & 1 deletion app/services/create_staff_application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.call(user, app_config, uri: nil)

# Update application attributes
app.attributes = app_config.to_model_attributes.merge(
owner: user,
owner_id: user.id,
uri: parsed_uri.to_s,
redirect_uri: redirect_uri.to_s
)
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_buttons.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.flex.py-5
.flex.basis-full{class: 'lg:basis-3/4'}
- if cookies[:last_visited_page].present?
- if cookies[:etm_last_visited_page].present?
.bg-gray-100.p-2.px-5.mr-0.ml-auto.rounded-md
= link_to t('continue_working'), last_visited_page_path, class: 'continue-button'
= link_to t('continue_working'), back_to_etm_path, class: 'continue-button'
3 changes: 1 addition & 2 deletions app/views/layouts/_sidebar.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.sidebar.fixed.top-0.bottom-0.overflow-y-auto.bg-midnight-300{class: 'lg:left-0 w-[300px]'}
-# Where does the logo refer to? REMEMBER LAST VERSION IN COOKIE
%a.logo.p-5.mb-3.inline-block.w-full.text-midnight-800
%a.logo.p-5.mb-3.inline-block.w-full.text-midnight-800{href: Version.model_url(active_version_tag)}
= image_tag 'header/logo-round.png', class: 'h-8 inline mb-1 mr-2 hover:animate-spin'
%span Energy Transition Model

Expand Down
2 changes: 1 addition & 1 deletion config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#
# access_token_class "Doorkeeper::AccessToken"
# access_grant_class "Doorkeeper::AccessGrant"
application_class 'OAuthApplication'
application_class "OAuthApplication"
#
# Don't forget to include Doorkeeper ORM mixins into your custom models:
#
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
mount Sidekiq::Web => '/sidekiq'
end

get '/last_visited_page', to: 'application#last_visited_page', as: :last_visited_page
get '/passthru/last', to: 'passthru#last', as: :back_to_etm

namespace :identity do
get '/', to: redirect('/identity/profile')
Expand Down
Loading
Loading