From f5abb7c767c1fd983f4bd7543fb3b0273559c353 Mon Sep 17 00:00:00 2001 From: Oleksii Kriuchykhin Date: Wed, 27 Nov 2019 23:16:25 +0100 Subject: [PATCH] Add connected accounts section to settings [SCI-4106] --- .../account/connected_accounts_controller.rb | 19 ++++++++++++ app/helpers/left_menu_bar_helper.rb | 2 +- app/helpers/user_settings_helper.rb | 7 ++++- app/views/users/settings/_sidebar.html.erb | 14 +++++++++ .../connected_accounts/_azure_ad.html.erb | 24 ++++++++++++++ .../account/connected_accounts/index.html.erb | 31 +++++++++++++++++++ .../unlink_modals/_azure_ad_modal.html.erb | 23 ++++++++++++++ config/initializers/api.rb | 23 ++------------ config/initializers/azure_ad.rb | 24 ++++++++++++++ config/locales/en.yml | 14 +++++++++ config/routes.rb | 6 ++++ 11 files changed, 164 insertions(+), 23 deletions(-) create mode 100644 app/controllers/users/settings/account/connected_accounts_controller.rb create mode 100644 app/views/users/settings/account/connected_accounts/_azure_ad.html.erb create mode 100644 app/views/users/settings/account/connected_accounts/index.html.erb create mode 100644 app/views/users/settings/account/connected_accounts/unlink_modals/_azure_ad_modal.html.erb create mode 100644 config/initializers/azure_ad.rb diff --git a/app/controllers/users/settings/account/connected_accounts_controller.rb b/app/controllers/users/settings/account/connected_accounts_controller.rb new file mode 100644 index 000000000..2edee3954 --- /dev/null +++ b/app/controllers/users/settings/account/connected_accounts_controller.rb @@ -0,0 +1,19 @@ +module Users + module Settings + module Account + class ConnectedAccountsController < ApplicationController + layout 'fluid' + + def index + @linked_accounts = current_user.user_identities.pluck(:provider) + end + + def destroy + current_user.user_identities.where(provider: params.require(:provider)).take&.destroy! + @linked_accounts = current_user.user_identities.pluck(:provider) + render :index + end + end + end + end +end diff --git a/app/helpers/left_menu_bar_helper.rb b/app/helpers/left_menu_bar_helper.rb index 13c0260d8..536ef7391 100644 --- a/app/helpers/left_menu_bar_helper.rb +++ b/app/helpers/left_menu_bar_helper.rb @@ -18,7 +18,7 @@ module LeftMenuBarHelper end def settings_are_selected? - controller_name.in? %(registrations preferences addons teams) + controller_name.in? %(registrations preferences addons teams connected_accounts) end def activities_are_selected? diff --git a/app/helpers/user_settings_helper.rb b/app/helpers/user_settings_helper.rb index 448ef9b63..3332564e0 100644 --- a/app/helpers/user_settings_helper.rb +++ b/app/helpers/user_settings_helper.rb @@ -2,7 +2,8 @@ module UserSettingsHelper def on_settings_account_page? controller_name == 'registrations' && action_name == 'edit' || controller_name == 'preferences' && action_name == 'index' || - controller_name == 'addons' && action_name == 'index' + controller_name == 'addons' && action_name == 'index' || + controller_name == 'connected_accounts' end def on_settings_account_profile_page? @@ -21,4 +22,8 @@ module UserSettingsHelper controller_name.in?(%w(teams audits)) && action_name.in?(%w(index new create show audits_index)) end + + def on_settings_account_connected_accounts_page? + controller_name == 'connected_accounts' + end end diff --git a/app/views/users/settings/_sidebar.html.erb b/app/views/users/settings/_sidebar.html.erb index a68b5c6c9..82530afef 100644 --- a/app/views/users/settings/_sidebar.html.erb +++ b/app/views/users/settings/_sidebar.html.erb @@ -56,6 +56,20 @@ <% end %> + +
  • + + <% if on_settings_account_connected_accounts_page? %> + "> + <%= t("users.settings.sidebar.account_nav.connected_accounts") %> + + <% else %> + <%= link_to t("users.settings.sidebar.account_nav.connected_accounts"), + connected_accounts_path, + data: { 'no-turbolink' => 'true' } %> + <% end %> + +
  • diff --git a/app/views/users/settings/account/connected_accounts/_azure_ad.html.erb b/app/views/users/settings/account/connected_accounts/_azure_ad.html.erb new file mode 100644 index 000000000..119bd8d65 --- /dev/null +++ b/app/views/users/settings/account/connected_accounts/_azure_ad.html.erb @@ -0,0 +1,24 @@ +
    +
    +
    + <%= t('users.settings.account.connected_accounts.azure_ad.title') %>
    +

    <%= t('users.settings.account.connected_accounts.azure_ad.connect_hint') %>

    +
    +
    +
    + + <%= t('users.settings.account.connected_accounts.azure_ad.connected') %> + + +
    +
    + <%= link_to t('users.settings.account.connected_accounts.azure_ad.unlink_button'), + '#unlinkAzureADModal', + class: 'btn btn-danger', + data: { toggle: 'modal'} %> +
    +
    +
    +
    + +<%= render partial: 'users/settings/account/connected_accounts/unlink_modals/azure_ad_modal', locals: { provider: provider } %> diff --git a/app/views/users/settings/account/connected_accounts/index.html.erb b/app/views/users/settings/account/connected_accounts/index.html.erb new file mode 100644 index 000000000..a8c63b402 --- /dev/null +++ b/app/views/users/settings/account/connected_accounts/index.html.erb @@ -0,0 +1,31 @@ +<% provide(:head_title, t("users.settings.account.connected_accounts.head_title")) %> + +<%= render partial: "users/settings/sidebar.html.erb" %> +
    +
    + +
    +
    +

    <%= t('users.settings.account.connected_accounts.title') %>

    + + <% if @linked_accounts.any? %> + <% @linked_accounts.each do |provider| %> + <% if Rails.configuration.x.azure_ad_apps.find { |_,value| value[:provider] == provider } %> + <% if lookup_context.exists?(provider, 'users/settings/account/connected_accounts', true) %> + <%= render partial: provider %> + <% else %> + <%= render partial: 'azure_ad', locals: { provider: provider } %> + <% end %> + <% end %> + <% end %> + <% else %> +
    + <%= t('users.settings.account.connected_accounts.not_connected') %> +
    + <% end %> + +
    +
    +
    +
    +
    diff --git a/app/views/users/settings/account/connected_accounts/unlink_modals/_azure_ad_modal.html.erb b/app/views/users/settings/account/connected_accounts/unlink_modals/_azure_ad_modal.html.erb new file mode 100644 index 000000000..7cd417403 --- /dev/null +++ b/app/views/users/settings/account/connected_accounts/unlink_modals/_azure_ad_modal.html.erb @@ -0,0 +1,23 @@ + diff --git a/config/initializers/api.rb b/config/initializers/api.rb index d748f9924..5fcd444ab 100644 --- a/config/initializers/api.rb +++ b/config/initializers/api.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + Rails.application.configure do config.x.core_api_sign_alg = ENV['CORE_API_SIGN_ALG'] if ENV['CORE_API_SIGN_ALG'] @@ -8,25 +10,4 @@ Rails.application.configure do config.x.core_api_rate_limit = ENV['CORE_API_RATE_LIMIT'] ? ENV['CORE_API_RATE_LIMIT'].to_i : 1000 config.x.core_api_v1_enabled = true if ENV['CORE_API_V1_ENABLED'] - - vars = ENV.select { |name, _| name =~ /^[[:alnum:]]*_AZURE_AD_APP_ID/ } - vars.each do |name, value| - app_name = name.sub('_AZURE_AD_APP_ID', '') - config.x.azure_ad_apps[value] = {} - - iss = ENV["#{app_name}_AZURE_AD_ISS"] - raise StandardError, "No ISS for #{app_name} Azure app" unless iss - - config.x.azure_ad_apps[value][:iss] = iss - - conf_url = ENV["#{app_name}_AZURE_AD_CONF_URL"] - raise StandardError, "No CONF_URL for #{app_name} Azure app" unless conf_url - - config.x.azure_ad_apps[value][:conf_url] = conf_url - - provider = ENV["#{app_name}_AZURE_AD_PROVIDER_NAME"] - raise StandardError, "No PROVIDER_NAME for #{app_name} Azure app" unless provider - - config.x.azure_ad_apps[value][:provider] = provider - end end diff --git a/config/initializers/azure_ad.rb b/config/initializers/azure_ad.rb new file mode 100644 index 000000000..78cfc9b23 --- /dev/null +++ b/config/initializers/azure_ad.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +Rails.application.configure do + vars = ENV.select { |name, _| name =~ /^[[:alnum:]]*_AZURE_AD_APP_ID/ } + vars.each do |name, value| + app_name = name.sub('_AZURE_AD_APP_ID', '') + config.x.azure_ad_apps[value] = {} + + iss = ENV["#{app_name}_AZURE_AD_ISS"] + raise StandardError, "No ISS for #{app_name} Azure app" unless iss + + config.x.azure_ad_apps[value][:iss] = iss + + conf_url = ENV["#{app_name}_AZURE_AD_CONF_URL"] + raise StandardError, "No CONF_URL for #{app_name} Azure app" unless conf_url + + config.x.azure_ad_apps[value][:conf_url] = conf_url + + provider = ENV["#{app_name}_AZURE_AD_PROVIDER_NAME"] + raise StandardError, "No PROVIDER_NAME for #{app_name} Azure app" unless provider + + config.x.azure_ad_apps[value][:provider] = provider + end +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 28a37488e..3bbed6ecf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1463,6 +1463,7 @@ en: profile: "Profile" preferences: "Preferences" addons: "Add-ons" + connected_accounts: "Connected Accounts" account: preferences: head_title: "Settings | My preferences" @@ -1483,6 +1484,19 @@ en: head_title: "Settings | Add-ons" title: "Add-ons" no_addons: "You have no SciNote Add-ons." + connected_accounts: + head_title: "Settings | Connected Accounts" + title: "Connected Accounts" + not_connected: "You have no Connected accounts" + azure_ad: + title: "Your Azure AD Account" + connect_hint: "Allows you to sign in with your Azure AD account." + connected: "Connected" + unlink_button: "Unlink" + unlink_modal: + title: "Unlink Azure AD account?" + description_1: "Are you sure you would like unlink Azure AD and SciNote accounts?" + submit_button: "Submit" teams: head_title: "Settings | Teams" breadcrumbs: diff --git a/config/routes.rb b/config/routes.rb index 03c2cba5f..d878067ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -62,6 +62,12 @@ Rails.application.routes.draw do get 'users/settings/account/addons', to: 'users/settings/account/addons#index', as: 'addons' + get 'users/settings/account/connected_accounts', + to: 'users/settings/account/connected_accounts#index', + as: 'connected_accounts' + delete 'users/settings/account/connected_accounts', + to: 'users/settings/account/connected_accounts#destroy', + as: 'unlink_connected_account' put 'users/settings/account/preferences', to: 'users/settings/account/preferences#update', as: 'update_preferences'