OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Extract Access Tokens into their own controller
Klaus Zanders18ecb2ef8f2d
diff --git a/app/components/my/access_token/new_access_token_form_component.html.erb b/app/components/my/access_token/new_access_token_form_component.html.erbindex 06ad515f16b..412731d4286 100644--- a/app/components/my/access_token/new_access_token_form_component.html.erb+++ b/app/components/my/access_token/new_access_token_form_component.html.erb@@ -31,7 +31,7 @@ See COPYRIGHT and LICENSE files for more details.component_wrapper doprimer_form_with(model: @token,- url: my_generate_api_key_path,+ url: generate_api_key_my_access_tokens_path,data: { turbo: true },method: :post) do |form|diff --git a/app/controllers/my/access_tokens_controller.rb b/app/controllers/my/access_tokens_controller.rbnew file mode 100644index 00000000000..28fcb5d85b3--- /dev/null+++ b/app/controllers/my/access_tokens_controller.rb@@ -0,0 +1,186 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) the OpenProject GmbH+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License version 3.+#+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:+# Copyright (C) 2006-2013 Jean-Philippe Lang+# Copyright (C) 2010-2013 the ChiliProject Team+#+# This program is free software; you can redistribute it and/or+# modify it under the terms of the GNU General Public License+# as published by the Free Software Foundation; either version 2+# of the License, or (at your option) any later version.+#+# This program is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU General Public License+# along with this program; if not, write to the Free Software+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.+#+# See COPYRIGHT and LICENSE files for more details.+#++++module My+ class AccessTokensController < ::ApplicationController+ before_action :require_login++ before_action :set_current_user+ before_action :set_grouped_ical_tokens, only: %i[index]+ before_action :set_ical_token, only: %i[revoke_ical_token]+ before_action :set_api_token, only: %i[revoke_api_key]++ menu_item :access_tokens++ layout "my"++ no_authorization_required! :index,+ :generate_rss_key,+ :revoke_rss_key,+ :generate_api_key,+ :revoke_api_key,+ :revoke_ical_token,+ :delete_storage_token++ def index+ @ical_meeting_tokens = current_user.ical_meeting_tokens++ @storage_tokens = OAuthClientToken+ .preload(:oauth_client)+ .joins(:oauth_client)+ .where(user: @user, oauth_client: { integration_type: "Storages::Storage" })+ end++ def revoke_storage_token+ token = OAuthClientToken+ .preload(:oauth_client)+ .joins(:oauth_client)+ .where(user: @user, oauth_client: { integration_type: "Storages::Storage" }).find_by(id: params[:access_token_id])++ if token&.destroy+ flash[:info] = I18n.t("my_account.access_tokens.storages.removed")+ else+ flash[:error] = I18n.t("my_account.access_tokens.storages.failed")+ end+ redirect_to action: :index+ end++ def generate_rss_key # rubocop:disable Metrics/AbcSize+ token = Token::RSS.create!(user: current_user)+ flash[:info] = [+ t("my.access_token.notice_reset_token", type: "RSS").html_safe,+ helpers.content_tag(:strong, helpers.content_tag(:code, token.plain_value)),+ t("my.access_token.token_value_warning")+ ]+ rescue StandardError => e+ Rails.logger.error "Failed to reset user ##{current_user.id} RSS key: #{e}"+ flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)+ ensure+ redirect_to action: :index+ end++ def revoke_rss_key+ current_user.rss_token.destroy+ flash[:info] = t("my.access_token.notice_rss_token_revoked")+ rescue StandardError => e+ Rails.logger.error "Failed to revoke rss token ##{current_user.id}: #{e}"+ flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)+ ensure+ redirect_to action: :index+ end++ def generate_api_key # rubocop:disable Metrics/AbcSize+ result = APITokens::CreateService.new(user: current_user).call(token_name: params[:token_api][:token_name])++ result.on_success do |r|+ update_via_turbo_stream(+ component: My::AccessToken::APITokensSectionComponent.new(api_tokens: @user.api_tokens)+ )++ dialog = My::AccessToken::AccessTokenCreatedDialogComponent.new(token_value: r.result.plain_value)+ modify_via_turbo_stream(component: dialog, action: :dialog, status: :ok)+ end++ result.on_failure do |r|+ update_via_turbo_stream(+ component: My::AccessToken::NewAccessTokenFormComponent.new(token: r.result),+ status: :bad_request+ )+ end++ respond_with_turbo_streams+ end++ def revoke_api_key # rubocop:disable Metrics/AbcSize+ result = APITokens::DeleteService.new(user: current_user, model: @api_token).call++ # rubocop:disable Rails/ActionControllerFlashBeforeRender+ result.on_success do+ flash[:notice] = t("my.access_token.notice_api_token_revoked")+ end++ result.on_failure do |r|+ error = r.errors.map(&:message).join("; ")+ Rails.logger.error("Failed to revoke api token ##{current_user.id}: #{error}")+ flash[:error] = t("my.access_token.failed_to_revoke_token", error:)+ end+ # rubocop:enable Rails/ActionControllerFlashBeforeRender++ redirect_to action: :index+ end++ def revoke_ical_token+ message = ical_destroy_info_message+ @ical_token.destroy+ flash[:info] = message+ rescue StandardError => e+ Rails.logger.error "Failed to revoke all ical tokens for ##{current_user.id}: #{e}"+ flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)+ ensure+ redirect_to action: :index+ end++ private++ def set_current_user+ @user = User.current+ end++ helper_method :has_tokens?++ def has_tokens?+ Setting.feeds_enabled? || Setting.rest_api_enabled? || current_user.ical_tokens.any?+ end++ def set_api_token+ @api_token = current_user.api_tokens.find(params[:access_token_id])+ end++ def set_ical_token+ @ical_token = current_user.ical_tokens.find(params[:access_token_id])+ end++ def set_grouped_ical_tokens+ @ical_tokens_grouped_by_query = current_user.ical_tokens+ .joins(ical_token_query_assignment: { query: :project })+ .select("tokens.*, ical_token_query_assignments.query_id")+ .group_by(&:query_id)+ end++ def ical_destroy_info_message+ t(+ "my.access_token.notice_ical_token_revoked",+ token_name: @ical_token.ical_token_query_assignment.name,+ calendar_name: @ical_token.query.name,+ project_name: @ical_token.query.project.name+ )+ end+ end+enddiff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rbindex c3daf8bd48b..f1cef66d096 100644--- a/app/controllers/my_controller.rb+++ b/app/controllers/my_controller.rb@@ -40,9 +40,6 @@ class MyController < ApplicationControllerbefore_action :require_loginbefore_action :set_current_userbefore_action :check_password_confirmation, only: %i[update_account]- before_action :set_grouped_ical_tokens, only: %i[access_token]- before_action :set_ical_token, only: %i[revoke_ical_token]- before_action :set_api_token, only: %i[revoke_api_key]no_authorization_required! :account,:update_account,@@ -51,21 +48,13 @@ class MyController < ApplicationController:update_settings,:password,:change_password,- :access_token,- :delete_storage_token,:notifications,- :reminders,- :generate_rss_key,- :revoke_rss_key,- :generate_api_key,- :revoke_api_key,- :revoke_ical_token+ :remindersmenu_item :account, only: [:account]menu_item :settings, only: [:settings]menu_item :interface, only: [:interface]menu_item :password, only: [:password]- menu_item :access_token, only: [:access_token]menu_item :notifications, only: [:notifications]menu_item :reminders, only: [:reminders]@@ -96,118 +85,12 @@ class MyController < ApplicationControllerendend- # Administer access tokens- def access_token- @ical_meeting_tokens = current_user.ical_meeting_tokens-- @storage_tokens = OAuthClientToken- .preload(:oauth_client)- .joins(:oauth_client)- .where(user: @user, oauth_client: { integration_type: "Storages::Storage" })- end-- def delete_storage_token- token = OAuthClientToken- .preload(:oauth_client)- .joins(:oauth_client)- .where(user: @user, oauth_client: { integration_type: "Storages::Storage" }).find_by(id: params[:id])-- if token&.destroy- flash[:info] = I18n.t("my_account.access_tokens.storages.removed")- else- flash[:error] = I18n.t("my_account.access_tokens.storages.failed")- end- redirect_to action: :access_token- end-# Configure user's in app notificationsdef notifications; end# Configure user's mail remindersdef reminders; end- # Create a new feeds key- def generate_rss_key- token = Token::RSS.create!(user: current_user)- flash[:info] = [- t("my.access_token.notice_reset_token", type: "RSS").html_safe,- content_tag(:strong, token.plain_value),- t("my.access_token.token_value_warning")- ]- rescue StandardError => e- Rails.logger.error "Failed to reset user ##{current_user.id} RSS key: #{e}"- flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)- ensure- redirect_to action: "access_token"- end-- def revoke_rss_key- current_user.rss_token.destroy- flash[:info] = t("my.access_token.notice_rss_token_revoked")- rescue StandardError => e- Rails.logger.error "Failed to revoke rss token ##{current_user.id}: #{e}"- flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)- ensure- redirect_to action: "access_token"- end-- # rubocop:disable Metrics/AbcSize- def generate_api_key- result = APITokens::CreateService.new(user: current_user).call(token_name: params[:token_api][:token_name])-- result.on_success do |r|- update_via_turbo_stream(- component: My::AccessToken::APITokensSectionComponent.new(api_tokens: @user.api_tokens)- )-- dialog = My::AccessToken::AccessTokenCreatedDialogComponent.new(token_value: r.result.plain_value)- modify_via_turbo_stream(component: dialog, action: :dialog, status: :ok)- end-- result.on_failure do |r|- update_via_turbo_stream(- component: My::AccessToken::NewAccessTokenFormComponent.new(token: r.result),- status: :bad_request- )- end-- respond_with_turbo_streams- end-- # rubocop:enable Metrics/AbcSize-- # rubocop:disable Metrics/AbcSize- def revoke_api_key- result = APITokens::DeleteService.new(user: current_user, model: @api_token).call-- # rubocop:disable Rails/ActionControllerFlashBeforeRender- result.on_success do- flash[:notice] = t("my.access_token.notice_api_token_revoked")- end-- result.on_failure do |r|- error = r.errors.map(&:message).join("; ")- Rails.logger.error("Failed to revoke api token ##{current_user.id}: #{error}")- flash[:error] = t("my.access_token.failed_to_revoke_token", error:)- end- # rubocop:enable Rails/ActionControllerFlashBeforeRender-- redirect_to action: "access_token"- end-- # rubocop:enable Metrics/AbcSize-- def revoke_ical_token- message = ical_destroy_info_message- @ical_token.destroy- flash[:info] = message- rescue StandardError => e- Rails.logger.error "Failed to revoke all ical tokens for ##{current_user.id}: #{e}"- flash[:error] = t("my.access_token.failed_to_reset_token", error: e.message)- ensure- redirect_to action: "access_token"- end-privatedef redirect_if_password_change_not_allowed_for(user)@@ -234,8 +117,6 @@ class MyController < ApplicationControllerredirect_back(fallback_location: my_account_path)end- helper_method :has_tokens?-def handle_email_changes# If mail changed, expire all other sessionsif @user.previous_changes["mail"]@@ -247,10 +128,6 @@ class MyController < ApplicationControllerendend- def has_tokens?- Setting.feeds_enabled? || Setting.rest_api_enabled? || current_user.ical_tokens.any?- end-def user_paramspermitted_params.my_account_settings.to_hend@@ -273,28 +150,4 @@ class MyController < ApplicationControllerdef get_current_layout@user.pref[:my_page_layout] || DEFAULT_LAYOUT.dupend-- def set_api_token- @api_token = current_user.api_tokens.find(params[:token_id])- end-- def set_ical_token- @ical_token = current_user.ical_tokens.find(params[:id])- end-- def set_grouped_ical_tokens- @ical_tokens_grouped_by_query = current_user.ical_tokens- .joins(ical_token_query_assignment: { query: :project })- .select("tokens.*, ical_token_query_assignments.query_id")- .group_by(&:query_id)- end-- def ical_destroy_info_message- t(- "my.access_token.notice_ical_token_revoked",- token_name: @ical_token.ical_token_query_assignment.name,- calendar_name: @ical_token.query.name,- project_name: @ical_token.query.project.name- )- endenddiff --git a/app/controllers/oauth/grants_controller.rb b/app/controllers/oauth/grants_controller.rbindex 5f70ee5c40a..e61bdaf9f94 100644--- a/app/controllers/oauth/grants_controller.rb+++ b/app/controllers/oauth/grants_controller.rb@@ -34,7 +34,7 @@ module OAuthauthorization_checked! :index, :revoke_applicationlayout "my"- menu_item :access_token+ menu_item :access_tokensdef index@applications = ::Doorkeeper::Application.authorized_for(current_user)@@ -53,7 +53,7 @@ module OAuth)flash[:notice] = I18n.t("oauth.grants.successful_application_revocation", application_name: application.name)- redirect_to controller: "/my", action: :access_token+ redirect_to my_access_tokens_pathendprivatediff --git a/app/views/my/access_token_partials/_icalendar_meeting_tokens_section.html.erb b/app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erbsimilarity index 97%rename from app/views/my/access_token_partials/_icalendar_meeting_tokens_section.html.erbrename to app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erbindex bad7f22bb40..44808db477d 100644--- a/app/views/my/access_token_partials/_icalendar_meeting_tokens_section.html.erb+++ b/app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erb@@ -41,7 +41,7 @@ See COPYRIGHT and LICENSE files for more details.<div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">- <%= render partial: "my/access_token_partials/token_table_header",+ <%= render partial: "token_table_header",locals: {column_headers: [t("attributes.name"),diff --git a/app/views/my/access_token_partials/_icalendar_tokens_section.html.erb b/app/views/my/access_tokens/_icalendar_tokens_section.html.erbsimilarity index 97%rename from app/views/my/access_token_partials/_icalendar_tokens_section.html.erbrename to app/views/my/access_tokens/_icalendar_tokens_section.html.erbindex 583aa76beba..34bcdd6d8d9 100644--- a/app/views/my/access_token_partials/_icalendar_tokens_section.html.erb+++ b/app/views/my/access_tokens/_icalendar_tokens_section.html.erb@@ -41,7 +41,7 @@ See COPYRIGHT and LICENSE files for more details.<div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">- <%= render partial: "my/access_token_partials/token_table_header",+ <%= render partial: "token_table_header",locals: {column_headers: [t("attributes.name"),@@ -74,7 +74,7 @@ See COPYRIGHT and LICENSE files for more details.<td><%= t("my_account.access_tokens.indefinite_expiration") %></td><td class="buttons"><%= link_to "",- { action: "revoke_ical_token", id: token.id },+ { action: "revoke_ical_token", access_token_id: token.id },data: { confirm: t("my_account.access_tokens.simple_revoke_confirmation"), test_selector: "ical-token-row-#{token.id}-revoke" },method: :delete,class: "icon icon-delete" %>diff --git a/app/views/my/access_token_partials/_oauth_tokens_section.html.erb b/app/views/my/access_tokens/_oauth_tokens_section.html.erbsimilarity index 97%rename from app/views/my/access_token_partials/_oauth_tokens_section.html.erbrename to app/views/my/access_tokens/_oauth_tokens_section.html.erbindex caadcb1ca92..eb87e4742fb 100644--- a/app/views/my/access_token_partials/_oauth_tokens_section.html.erb+++ b/app/views/my/access_tokens/_oauth_tokens_section.html.erb@@ -40,7 +40,7 @@ See COPYRIGHT and LICENSE files for more details.<div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">- <%= render partial: "my/access_token_partials/token_table_header",+ <%= render partial: "token_table_header",locals: {column_headers: [t("attributes.name"),diff --git a/app/views/my/access_token_partials/_rss_tokens_section.html.erb b/app/views/my/access_tokens/_rss_tokens_section.html.erbsimilarity index 97%rename from app/views/my/access_token_partials/_rss_tokens_section.html.erbrename to app/views/my/access_tokens/_rss_tokens_section.html.erbindex 552d59c6837..a393254ea5b 100644--- a/app/views/my/access_token_partials/_rss_tokens_section.html.erb+++ b/app/views/my/access_tokens/_rss_tokens_section.html.erb@@ -41,7 +41,7 @@ See COPYRIGHT and LICENSE files for more details.<div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">- <%= render partial: "my/access_token_partials/token_table_header",+ <%= render partial: "token_table_header",locals: {column_headers: [t("attributes.name"),diff --git a/app/views/my/access_token_partials/_storage_tokens_section.html.erb b/app/views/my/access_tokens/_storage_tokens_section.html.erbsimilarity index 97%rename from app/views/my/access_token_partials/_storage_tokens_section.html.erbrename to app/views/my/access_tokens/_storage_tokens_section.html.erbindex 9672938489d..3c58c54a012 100644--- a/app/views/my/access_token_partials/_storage_tokens_section.html.erb+++ b/app/views/my/access_tokens/_storage_tokens_section.html.erb@@ -40,7 +40,7 @@ See COPYRIGHT and LICENSE files for more details.<div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">- <%= render partial: "my/access_token_partials/token_table_header",+ <%= render partial: "token_table_header",locals: {column_headers: [t("attributes.name"),diff --git a/app/views/my/access_token_partials/_token_table_header.html.erb b/app/views/my/access_tokens/_token_table_header.html.erbsimilarity index 100%rename from app/views/my/access_token_partials/_token_table_header.html.erbrename to app/views/my/access_tokens/_token_table_header.html.erbdiff --git a/app/views/my/access_token.html.erb b/app/views/my/access_tokens/index.html.erbsimilarity index 73%rename from app/views/my/access_token.html.erbrename to app/views/my/access_tokens/index.html.erbindex b0850c7aa96..c7f1ef7cd6d 100644--- a/app/views/my/access_token.html.erb+++ b/app/views/my/access_tokens/index.html.erb@@ -40,16 +40,11 @@ See COPYRIGHT and LICENSE files for more details.%><%= render ::My::AccessToken::APITokensSectionComponent.new(api_tokens: @user.api_tokens) %>-<%= render partial: "my/access_token_partials/icalendar_tokens_section",- locals: { ical_tokens_grouped_by_query: @ical_tokens_grouped_by_query } %>-<%= render partial: "my/access_token_partials/icalendar_meeting_tokens_section",- locals: { ical_tokens: @ical_meeting_tokens } %>-<%= render partial: "my/access_token_partials/oauth_tokens_section",- locals: { granted_applications: granted_applications } %>-<%= render partial: "my/access_token_partials/rss_tokens_section",- locals: { rss_token: @user.rss_token } %>-<%= render partial: "my/access_token_partials/storage_tokens_section",- locals: { storage_tokens: @storage_tokens } %>+<%= render partial: "icalendar_tokens_section", locals: { ical_tokens_grouped_by_query: @ical_tokens_grouped_by_query } %>+<%= render partial: "icalendar_meeting_tokens_section", locals: { ical_tokens: @ical_meeting_tokens } %>+<%= render partial: "oauth_tokens_section", locals: { granted_applications: granted_applications } %>+<%= render partial: "rss_tokens_section", locals: { rss_token: @user.rss_token } %>+<%= render partial: "storage_tokens_section", locals: { storage_tokens: @storage_tokens } %><div class="generic-table--container"><div class="generic-table--results-container"><table class="generic-table" data-controller="table-highlighting">diff --git a/config/initializers/menus.rb b/config/initializers/menus.rbindex c44000c21ce..53983ebcd85 100644--- a/config/initializers/menus.rb+++ b/config/initializers/menus.rb@@ -240,8 +240,8 @@ Redmine::MenuManager.map :my_menu do |menu|caption: :button_change_password,if: ->(_) { User.current.change_password_allowed? },icon: "lock"- menu.push :access_token,- { controller: "/my", action: "access_token" },+ menu.push :access_tokens,+ { controller: "/my/access_tokens", action: "index" },caption: I18n.t("my_account.access_tokens.access_tokens"),icon: "key"menu.push :sessions,diff --git a/config/routes.rb b/config/routes.rbindex 3547fed7f65..2db2f76d036 100644--- a/config/routes.rb+++ b/config/routes.rb@@ -851,7 +851,6 @@ Rails.application.routes.draw doscope "my" doget "/deletion_info" => "users#deletion_info", as: "delete_my_account_info"post "/oauth/revoke_application/:application_id" => "oauth/grants#revoke_application", as: "revoke_my_oauth_application"- delete "/storage_token/:id" => "my#delete_storage_token", as: "storage_token_delete"resources :sessions, controller: "my/sessions", as: "my_sessions", only: %i[index show destroy]resources :auto_login_tokens, controller: "my/auto_login_tokens", as: "my_auto_login_tokens", only: %i[destroy]@@ -860,6 +859,21 @@ Rails.application.routes.draw dopost "/dismiss_banner" => "my/enterprise_banners#dismiss", as: "dismiss_enterprise_banner"end+ namespace :my do+ resources :access_tokens, only: %i[index] do+ collection do+ post "generate_rss_key", action: "generate_rss_key"+ delete "revoke_rss_key", action: "revoke_rss_key"++ post "generate_api_key", action: "generate_api_key"+ end++ delete "revoke_api_key", action: "revoke_api_key"+ delete "revoke_ical_token", action: "revoke_ical_token"+ delete "revoke_storage_token", action: "revoke_storage_token"+ end+ end+scope controller: "my" doget "/my/password", action: "password"post "/my/change_password", action: "change_password"@@ -872,13 +886,6 @@ Rails.application.routes.draw dopatch "/my/account", action: "update_account"patch "/my/settings", action: "update_settings"-- post "/my/generate_rss_key", action: "generate_rss_key"- delete "/my/revoke_rss_key", action: "revoke_rss_key"- post "/my/generate_api_key", action: "generate_api_key"- delete "/my/revoke_api_key", action: "revoke_api_key"- delete "/my/revoke_ical_token", action: "revoke_ical_token"- get "/my/access_token", action: "access_token"endscope controller: "onboarding" do