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

[48619] Fixed access token name validation in dialog

Andreas Pfohl6eeca161c656
diff --git a/app/components/my/access_token/new_access_token_component.html.erb b/app/components/my/access_token/new_access_token_component.html.erb
index 2c181aca15e..0104f24f92f 100644
--- a/app/components/my/access_token/new_access_token_component.html.erb
+++ b/app/components/my/access_token/new_access_token_component.html.erb
@@ -28,18 +28,17 @@ See COPYRIGHT and LICENSE files for more details.
++#%>
<%=
- component_wrapper do
render(
Primer::Alpha::Dialog.new(
- id: dialog_id,
- title:,
- test_selector: dialog_id,
+ id: "new-access-token-dialog",
+ title: I18n.t("my.access_token.new_access_token_dialog_title"),
+ test_selector: "new-access-token-dialog",
size: :large
)
) do |dialog|
dialog.with_show_button(scheme: :primary, mt: 3) do |button|
button.with_leading_visual_icon(icon: :plus)
- show_button_text
+ I18n.t("my.access_token.new_access_token_dialog_show_button_text")
end
dialog.with_header(
@@ -47,24 +46,6 @@ See COPYRIGHT and LICENSE files for more details.
visually_hide_title: false
)
- primer_form_with(
- url: my_generate_api_key_path,
- data: { turbo: true },
- method: :post
- ) do |form|
- concat(render(Primer::Alpha::Dialog::Body.new(
- id: dialog_body_id, test_selector: dialog_body_id, aria: { label: title }
- )) do
- concat(render(Primer::Alpha::Banner.new(scheme: :warning, mt: 3)) { attention_text })
- concat(render(Primer::Beta::Text.new(tag: :p, mt: 3, mb: 3)) { text })
- concat(render(My::AccessToken::NewAccessTokenForm.new(form)))
- end)
-
- concat(render(Primer::Alpha::Dialog::Footer.new(show_divider: false)) do
- concat(render(Primer::ButtonComponent.new(data: { 'close-dialog-id': dialog_id })) { cancel_button_text })
- concat(render(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) { submit_button_text })
- end)
- end
+ render(My::AccessToken::NewAccessTokenFormComponent.new(token: Token::API.new))
end
- end
%>
diff --git a/app/components/my/access_token/new_access_token_component.rb b/app/components/my/access_token/new_access_token_component.rb
index e812bf1bd57..3c80550c9c5 100644
--- a/app/components/my/access_token/new_access_token_component.rb
+++ b/app/components/my/access_token/new_access_token_component.rb
@@ -31,36 +31,6 @@
module My
module AccessToken
class NewAccessTokenComponent < ApplicationComponent
- include OpTurbo::Streamable
-
- options dialog_id: "my--new-access-token-component",
- dialog_body_id: "my--new-access-token-body-component"
-
- private
-
- def title
- I18n.t("my.access_token.new_access_token_dialog_title")
- end
-
- def text
- I18n.t("my.access_token.new_access_token_dialog_text")
- end
-
- def attention_text
- I18n.t("my.access_token.new_access_token_dialog_attention_text")
- end
-
- def show_button_text
- I18n.t("my.access_token.new_access_token_dialog_show_button_text")
- end
-
- def cancel_button_text
- I18n.t("button_cancel")
- end
-
- def submit_button_text
- I18n.t("my.access_token.new_access_token_dialog_submit_button_text")
- end
end
end
end
diff --git a/app/components/my/access_token/new_access_token_form.rb b/app/components/my/access_token/new_access_token_form.rb
index 284f8798244..93d78649488 100644
--- a/app/components/my/access_token/new_access_token_form.rb
+++ b/app/components/my/access_token/new_access_token_form.rb
@@ -31,14 +31,14 @@
module My
module AccessToken
class NewAccessTokenForm < ApplicationForm
- form do |f|
- f.group(layout: :horizontal) do |f_group|
- f_group.text_field(
- name: :token_name,
- label: I18n.t("my.access_token.new_access_token_dialog_text_field_label"),
- placeholder: I18n.t("my.access_token.new_access_token_dialog_text_field_placeholder_text")
- )
- end
+ form do |new_access_token_form|
+ new_access_token_form.text_field(
+ name: :token_name,
+ label: I18n.t("my.access_token.new_access_token_dialog_text_field_label"),
+ placeholder: I18n.t("my.access_token.new_access_token_dialog_text_field_placeholder_text"),
+ visually_hide_label: false,
+ required: true
+ )
end
end
end
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.erb
new file mode 100644
index 00000000000..d889ca9d157
--- /dev/null
+++ b/app/components/my/access_token/new_access_token_form_component.html.erb
@@ -0,0 +1,75 @@
+<%#-- 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
+ primer_form_with(
+ model: @token,
+ url: my_generate_api_key_path,
+ data: { turbo: true },
+ method: :post
+ ) do |form|
+ component_collection do |collection|
+ collection.with_component(Primer::Alpha::Dialog::Body.new(
+ aria: { label: I18n.t("my.access_token.new_access_token_dialog_title") }
+ )) do
+ flex_layout(my: 3) do |body|
+ body.with_row do
+ render(Primer::Alpha::Banner.new(scheme: :warning)) do
+ I18n.t("my.access_token.new_access_token_dialog_attention_text")
+ end
+ end
+
+ body.with_row(mt: 3) do
+ render(Primer::Beta::Text.new(tag: :p)) do
+ I18n.t("my.access_token.new_access_token_dialog_text")
+ end
+ end
+
+ body.with_row do
+ render(My::AccessToken::NewAccessTokenForm.new(form))
+ end
+ end
+ end
+
+ collection.with_component(Primer::Alpha::Dialog::Footer.new) do
+ component_collection do |footer|
+ footer.with_component(Primer::ButtonComponent.new(data: { 'close-dialog-id': "new-access-token-dialog" })) do
+ I18n.t("button_cancel")
+ end
+
+ footer.with_component(Primer::ButtonComponent.new(scheme: :primary, type: :submit)) do
+ I18n.t("my.access_token.new_access_token_dialog_submit_button_text")
+ end
+ end
+ end
+ end
+ end
+ end
+%>
diff --git a/app/components/my/access_token/new_access_token_form_component.rb b/app/components/my/access_token/new_access_token_form_component.rb
new file mode 100644
index 00000000000..d6b8a91ebd6
--- /dev/null
+++ b/app/components/my/access_token/new_access_token_form_component.rb
@@ -0,0 +1,44 @@
+# 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
+ class NewAccessTokenFormComponent < ApplicationComponent
+ include OpTurbo::Streamable
+ include OpPrimer::ComponentHelpers
+
+ def initialize(token:)
+ super
+
+ @token = token
+ end
+ end
+ end
+end
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index 482dab08cf0..b4514b63628 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -159,17 +159,24 @@ class MyController < ApplicationController
# Create a new API key
def generate_api_key
- token = Token::API.create!(user: current_user, data: { name: params[:token_name] })
- flash[:info] = [
- t("my.access_token.notice_reset_token", type: "API").html_safe,
- content_tag(:strong, token.plain_value),
- t("my.access_token.token_value_warning")
- ]
- rescue StandardError => e
- Rails.logger.error "Failed to create API key for user ##{current_user.id}: #{e}"
- flash[:error] = t("my.access_token.failed_to_create_token", error: e.message)
- ensure
- redirect_to action: "access_token"
+ token = Token::API.create(user: current_user, data: { name: params[:token_api][:token_name] })
+
+ if token.valid?
+ flash[:info] = [
+ t("my.access_token.notice_reset_token", type: "API").html_safe,
+ content_tag(:strong, token.plain_value),
+ t("my.access_token.token_value_warning")
+ ]
+
+ redirect_to action: "access_token"
+ else
+ update_via_turbo_stream(
+ component: My::AccessToken::NewAccessTokenFormComponent.new(token:),
+ status: :bad_request
+ )
+
+ respond_with_turbo_streams
+ end
end
def revoke_api_key