Skip to content

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

[#56780] Avoid crashing the page on access token create

Judith Roth22f88d0989c0
diff --git a/app/components/my/access_token/access_token_created_dialog_component.html.erb b/app/components/my/access_token/access_token_created_dialog_component.html.erb
new file mode 100644
index 00000000000..d2f739a075f
--- /dev/null
+++ b/app/components/my/access_token/access_token_created_dialog_component.html.erb
@@ -0,0 +1,52 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 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.
+
+++#%>
+
+<%=
+ render(Primer::Alpha::Dialog.new(
+ id:,
+ title: nil,
+ size: :large
+ )) do |dialog|
+ dialog.with_body do
+ render(Primer::Beta::Blankslate.new) do |component|
+ component.with_visual_icon(icon: :"check-circle", color: :success)
+ component.with_heading(tag: :h1).with_content(heading)
+ component.with_description { text }
+ end
+ end
+ dialog.with_footer do
+ component_collection do |footer|
+ footer.with_component(Primer::ButtonComponent.new(data: { 'close-dialog-id': id })) do
+ I18n.t("button_close")
+ end
+ end
+ end
+ end
+%>
+
diff --git a/app/components/my/access_token/access_token_created_dialog_component.rb b/app/components/my/access_token/access_token_created_dialog_component.rb
new file mode 100644
index 00000000000..6bf6bcd80b2
--- /dev/null
+++ b/app/components/my/access_token/access_token_created_dialog_component.rb
@@ -0,0 +1,65 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+# This component renders a dialog with the new access token which the user has to copy.
+module My
+ module AccessToken
+ class AccessTokenCreatedDialogComponent < ApplicationComponent
+ include OpTurbo::Streamable
+ include OpPrimer::ComponentHelpers
+
+ def initialize(token_value:)
+ super
+
+ @token_value = token_value
+ end
+
+ def id
+ "access-token-created-dialog"
+ end
+
+ def heading
+ I18n.t("my.access_token.create_dialog.header", type: "API")
+ end
+
+ def text
+ options = {
+ tag: :div,
+ color: :subtle
+ }
+ safe_join(
+ [
+ render(Primer::Beta::Text.new(**options)) { I18n.t("my.access_token.create_dialog.text") },
+ render(Primer::Beta::Text.new(**options.merge(font_weight: :bold))) { @token_value },
+ render(Primer::Beta::Text.new(**options)) { I18n.t("my.access_token.create_dialog.warning") }
+ ]
+ )
+ end
+ end
+ end
+end
diff --git a/app/components/my/access_token/api/row_component.rb b/app/components/my/access_token/api/row_component.rb
new file mode 100644
index 00000000000..6e89dd97804
--- /dev/null
+++ b/app/components/my/access_token/api/row_component.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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
+ module AccessToken
+ module API
+ class RowComponent < ::RowComponent
+ def api_token
+ model
+ end
+
+ def token_name
+ if api_token.token_name.nil?
+ t("my_account.access_tokens.api.static_token_name")
+ else
+ api_token.token_name
+ end
+ end
+
+ def created_at
+ helpers.format_time(api_token.created_at)
+ end
+
+ def expires_on
+ I18n.t("my_account.access_tokens.indefinite_expiration")
+ end
+
+ def button_links
+ [delete_link].compact
+ end
+
+ def delete_link
+ link_to "",
+ {
+ controller: :my,
+ action: "revoke_api_key",
+ token_id: api_token.id
+ },
+ method: :delete,
+ data: { confirm: t("my_account.access_tokens.simple_revoke_confirmation"), test_selector: "api-token-revoke" },
+ class: "icon icon-delete"
+ end
+ end
+ end
+ end
+end
diff --git a/app/components/my/access_token/api/table_component.rb b/app/components/my/access_token/api/table_component.rb
new file mode 100644
index 00000000000..69d4d9fe5b7
--- /dev/null
+++ b/app/components/my/access_token/api/table_component.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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
+ module AccessToken
+ module API
+ class TableComponent < ::TableComponent
+ def initial_sort
+ %i[id asc]
+ end
+
+ def sortable?
+ false
+ end
+
+ def headers
+ [
+ ["token_name", { caption: I18n.t("attributes.name") }],
+ ["created_at", { caption: User.human_attribute_name(:created_at) }],
+ ["expires_on", { caption: I18n.t("my_account.access_tokens.headers.expiration") }]
+ ]
+ end
+
+ def columns
+ headers.map(&:first)
+ end
+ end
+ end
+ end
+end
diff --git a/app/components/my/access_token/api_tokens_section_component.html.erb b/app/components/my/access_token/api_tokens_section_component.html.erb
new file mode 100644
index 00000000000..0e89fa59181
--- /dev/null
+++ b/app/components/my/access_token/api_tokens_section_component.html.erb
@@ -0,0 +1,57 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 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.
+
+++#%>
+
+<%= component_wrapper do %>
+ <div class="attributes-group" id="api-token-section">
+ <div class="attributes-group--header">
+ <div class="attributes-group--header-container">
+ <h3 class="attributes-group--header-text"><%= t("my_account.access_tokens.api.title") %></h3>
+ </div>
+ </div>
+ <div>
+ <p>
+ <%= t("my_account.access_tokens.api.text_hint") %>
+ </p>
+ <% if Setting.rest_api_enabled? %>
+ <% if @api_tokens.any? %>
+ <%= render My::AccessToken::API::TableComponent.new(rows: @api_tokens) %>
+ <% end %>
+ <%= render My::AccessToken::NewAccessTokenComponent.new %>
+ <% else %>
+ <div tabindex="0" class="-info op-toast">
+ <div role="alert" aria-atomic="true" class="op-toast--content">
+ <p>
+ <span><%= t('my_account.access_tokens.api.disabled_text') %></span>
+ </p>
+ </div>
+ </div>
+ <% end %>
+ </div>
+ </div>
+<% end %>
diff --git a/app/components/my/access_token/api_tokens_section_component.rb b/app/components/my/access_token/api_tokens_section_component.rb
new file mode 100644
index 00000000000..b5faee9bd59
--- /dev/null
+++ b/app/components/my/access_token/api_tokens_section_component.rb
@@ -0,0 +1,47 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+# This components renders a dialog to confirm the deletion of a project from a storage.
+module My
+ module AccessToken
+ class APITokensSectionComponent < ::ApplicationComponent
+ include OpTurbo::Streamable
+ include Redmine::I18n
+
+ def initialize(api_tokens:)
+ super
+
+ @api_tokens = api_tokens
+ end
+
+ def id
+ "api-tokens-section-component"
+ end
+ end
+ end
+end
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 368fff61f5f..99c515bb664 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -164,18 +164,12 @@ class MyController < ApplicationController
result = APITokens::CreateService.new(user: current_user).call(token_name: params[:token_api][:token_name])
result.on_success do |r|
- flash[:primer_banner] = {
- scheme: :success,
- message: join_flash_messages(
- [
- t("my.access_token.notice_reset_token", type: "API"),
- Primer::Beta::Text.new(font_weight: :bold).render_in(view_context) { r.result.plain_value },
- t("my.access_token.token_value_warning")
- ]
- )
- }
-
- redirect_to action: "access_token"
+ 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|
@@ -183,9 +177,9 @@ class MyController < ApplicationController
component: My::AccessToken::NewAccessTokenFormComponent.new(token: r.result),
status: :bad_request
)
-
- respond_with_turbo_streams
end
+
+ respond_with_turbo_streams
end
# rubocop:enable Metrics/AbcSize
diff --git a/app/views/my/access_token.html.erb b/app/views/my/access_token.html.erb
index 6adc3b8c953..63ae2e71bad 100644
--- a/app/views/my/access_token.html.erb
+++ b/app/views/my/access_token.html.erb
@@ -34,9 +34,7 @@ See COPYRIGHT and LICENSE files for more details.
<div class="access-token-page--container">
<%= toolbar title: I18n.t('my_account.access_tokens.access_tokens') %>
- <%= render partial: 'my/access_token_partials/api_tokens_section',
- locals: { api_tokens: @user.api_tokens }
- %>
+ <%= 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 }
%>
diff --git a/app/views/my/access_token_partials/_api_tokens_section.html.erb b/app/views/my/access_token_partials/_api_tokens_section.html.erb
deleted file mode 100644
index e3e0770f0dd..00000000000
--- a/app/views/my/access_token_partials/_api_tokens_section.html.erb
+++ /dev/null
@@ -1,94 +0,0 @@
-<%#-- copyright
-OpenProject is an open source project management software.
-Copyright (C) 2012-2024 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.
-
-++#%>
-<div class="attributes-group" id="api-token-section">
- <div class="attributes-group--header">
- <div class="attributes-group--header-container">
- <h3 class="attributes-group--header-text"><%= t("my_account.access_tokens.api.title") %></h3>
- </div>
- </div>
- <div>
- <p>
- <%= t("my_account.access_tokens.api.text_hint") %>
- </p>
- <% if Setting.rest_api_enabled? %>
- <% if api_tokens.any? %>
- <div class="generic-table--container">
- <div class="generic-table--results-container">
- <table class="generic-table">
- <%= render partial: "my/access_token_partials/token_table_header",
- locals: {
- column_headers: [
- t('attributes.name'),
- User.human_attribute_name(:created_at),
- t('my_account.access_tokens.headers.expiration')
- ]
- }
- %>
- <tbody>
- <% api_tokens.each do |api_token| %>
- <tr>
- <td class="-w-rel-60" data-test-selector="api-token-row-name">
- <%= api_token.token_name.nil? ? t("my_account.access_tokens.api.static_token_name") : api_token.token_name %>
- </td>
- <td>
- <span title="<%= format_time(api_token.created_at) %>">
- <%= format_time(api_token.created_at.to_s) %>
- </span>
- </td>
- <td><%= t('my_account.access_tokens.indefinite_expiration') %></td>
- <td class="buttons">
- <%= link_to "",
- {
- controller: :my,
- action: 'revoke_api_key',
- token_id: api_token.id
- },
- method: :delete,
- data: { confirm: t('my_account.access_tokens.simple_revoke_confirmation'), test_selector: 'api-token-revoke' },
- class: 'icon icon-delete' %>
- </td>
- </tr>
- <% end %>
- </tbody>
- </table>
- </div>
- </div>
- <% end %>
- <%= render My::AccessToken::NewAccessTokenComponent.new %>
- <% else %>
- <div tabindex="0" class="-info op-toast">
- <div role="alert" aria-atomic="true" class="op-toast--content">
- <p>
- <span><%= t('my_account.access_tokens.api.disabled_text') %></span>
- </p>
- </div>
- </div>
- <% end %>
- </div>
-</div>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b5f640165cb..84c15c596bf 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -423,6 +423,10 @@ en:
my:
access_token:
+ create_dialog:
+ header: The %{type} token has been generated
+ text: "Your access token is:"
+ warning: Note that this is the only time you will see this token, make sure to copy it now.
errors:
token_name_blank: "Please provide an API token name"
token_name_in_use: "This API token name is already in use, please select a different one"
diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rb
index 3e82458cc57..7597dcaa353 100644
--- a/spec/controllers/my_controller_spec.rb
+++ b/spec/controllers/my_controller_spec.rb
@@ -305,14 +305,12 @@ RSpec.describe MyController do
it "creates a key" do
expect(user.api_tokens).to be_empty
- post :generate_api_key, params: { token_api: { token_name: "One heck of a token" } }
+ post :generate_api_key, params: { token_api: { token_name: "One heck of a token" } }, format: :turbo_stream
new_token = user.reload.api_tokens.last
expect(new_token).to be_present
- expect(flash[:primer_banner]).to be_present
- expect(flash[:primer_banner]).to include(scheme: :success)
-
- expect(response).to redirect_to action: :access_token
+ expect(response).to be_successful
+ expect(response.body).to include(new_token.token_name)
end
end
@@ -322,15 +320,14 @@ RSpec.describe MyController do
it "must add the new key" do
expect(user.reload.api_tokens.last).to eq(key)
- post :generate_api_key, params: { token_api: { token_name: "Two heck of a token" } }
+ post :generate_api_key, params: { token_api: { token_name: "Two heck of a token" } }, format: :turbo_stream
new_token = user.reload.api_tokens.last
expect(new_token).not_to eq(key)
expect(new_token.value).not_to eq(key.value)
- expect(flash[:primer_banner]).to be_present
- expect(flash[:primer_banner]).to include(scheme: :success)
- expect(response).to redirect_to action: :access_token
+ expect(response).to be_successful
+ expect(response.body).to include("Two heck of a token")
end
end
end
diff --git a/spec/features/users/my_spec.rb b/spec/features/users/my_spec.rb
index 0e309461ed2..a4ae872d63c 100644
--- a/spec/features/users/my_spec.rb
+++ b/spec/features/users/my_spec.rb
@@ -142,7 +142,7 @@ RSpec.describe "my", :js, :with_cuprite do
end
context "when API access is enabled via global settings", with_settings: { rest_api_enabled: true } do
- it "API tokens can generated and revoked" do
+ it "API tokens can be generated and revoked" do
visit my_access_token_path
expect(page).to have_no_content("API tokens are not enabled by the administrator.")
@@ -158,7 +158,10 @@ RSpec.describe "my", :js, :with_cuprite do
fill_in "token_api[token_name]", with: "Testing Token"
find_test_selector("create-api-token-button").click
- expect(page).to have_content "A new API token has been generated. Your access token is"
+ within("dialog#access-token-created-dialog") do
+ expect(page).to have_content "The API token has been generated"
+ end
+ expect(page).to have_content("Testing Token")
User.current.reload
visit my_access_token_path