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] Added naming of new API tokens

Andreas Pfohlaa4213f166fb
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
new file mode 100644
index 00000000000..2c181aca15e
--- /dev/null
+++ b/app/components/my/access_token/new_access_token_component.html.erb
@@ -0,0 +1,70 @@
+<%#-- 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
+ render(
+ Primer::Alpha::Dialog.new(
+ id: dialog_id,
+ title:,
+ test_selector: dialog_id,
+ size: :large
+ )
+ ) do |dialog|
+ dialog.with_show_button(scheme: :primary, mt: 3) do |button|
+ button.with_leading_visual_icon(icon: :plus)
+ show_button_text
+ end
+
+ dialog.with_header(
+ show_divider: false,
+ 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
+ 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
new file mode 100644
index 00000000000..e812bf1bd57
--- /dev/null
+++ b/app/components/my/access_token/new_access_token_component.rb
@@ -0,0 +1,66 @@
+# 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 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
new file mode 100644
index 00000000000..284f8798244
--- /dev/null
+++ b/app/components/my/access_token/new_access_token_form.rb
@@ -0,0 +1,45 @@
+# 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 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
+ end
+ end
+ end
+end
diff --git a/app/controllers/my_controller.rb b/app/controllers/my_controller.rb
index a01c556c161..3f047b58a93 100644
--- a/app/controllers/my_controller.rb
+++ b/app/controllers/my_controller.rb
@@ -55,12 +55,12 @@ class MyController < ApplicationController
:revoke_api_key,
:revoke_ical_token
- menu_item :account, only: [:account]
- menu_item :settings, only: [:settings]
- menu_item :password, only: [:password]
- menu_item :access_token, only: [:access_token]
- menu_item :notifications, only: [:notifications]
- menu_item :reminders, only: [:reminders]
+ menu_item :account, only: [:account]
+ menu_item :settings, only: [:settings]
+ menu_item :password, only: [:password]
+ menu_item :access_token, only: [:access_token]
+ menu_item :notifications, only: [:notifications]
+ menu_item :reminders, only: [:reminders]
def account; end
@@ -97,9 +97,9 @@ class MyController < ApplicationController
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])
+ .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")
@@ -158,7 +158,7 @@ class MyController < ApplicationController
# Create a new API key
def generate_api_key
- token = Token::API.create!(user: current_user)
+ 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),
@@ -213,8 +213,8 @@ class MyController < ApplicationController
def write_settings
result = Users::UpdateService
- .new(user: current_user, model: current_user)
- .call(user_params)
+ .new(user: current_user, model: current_user)
+ .call(user_params)
if result&.success
flash[:notice] = notice_account_updated
@@ -272,9 +272,9 @@ class MyController < ApplicationController
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)
+ .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
diff --git a/app/models/token/api.rb b/app/models/token/api.rb
index 860019700d4..c6e4244d0ca 100644
--- a/app/models/token/api.rb
+++ b/app/models/token/api.rb
@@ -28,6 +28,10 @@
module Token
class API < HashedToken
+ def name
+ data[:name]
+ end
+
private
def single_value?
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
index b4ca48681e6..49bf5a04afb 100644
--- a/app/views/my/access_token_partials/_api_tokens_section.html.erb
+++ b/app/views/my/access_token_partials/_api_tokens_section.html.erb
@@ -53,7 +53,9 @@ See COPYRIGHT and LICENSE files for more details.
<tbody>
<% api_tokens.each do |api_token| %>
<tr>
- <td class="-w-rel-60" data-test-selector="api-token-row-name"><%= t("my_account.access_tokens.api.static_token_name") %></td>
+ <td class="-w-rel-60" data-test-selector="api-token-row-name">
+ <%= api_token.name.nil? ? t("my_account.access_tokens.api.static_token_name") : api_token.name %>
+ </td>
<td>
<span title="<%= format_time(api_token.created_at) %>">
<%= format_time(api_token.created_at.to_s) %>
@@ -78,10 +80,11 @@ See COPYRIGHT and LICENSE files for more details.
</div>
</div>
<% end %>
- <%= link_to({ action: 'generate_api_key' }, method: :post, class: 'spot-link', data: { test_selector: 'api-token-add' }) do %>
- <span class="spot-icon spot-icon_add"></span>
- <span><%= t('my_account.access_tokens.api.static_token_name') %></span>
- <% end %>
+ <%#= link_to({ action: 'generate_api_key' }, method: :post, class: 'spot-link', data: { test_selector: 'api-token-add' }) do %>
+<!-- <span class="spot-icon spot-icon_add"></span>-->
+<!-- <span><%#= t('my_account.access_tokens.api.static_token_name') %></span>-->
+ <%# end %>
+ <%= render My::AccessToken::NewAccessTokenComponent.new %>
<% else %>
<div tabindex="0" class="-info op-toast">
<div role="alert" aria-atomic="true" class="op-toast--content">
diff --git a/config/locales/en.yml b/config/locales/en.yml
index d3ed79f9a6a..eb54fc06699 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -428,6 +428,13 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}
my:
access_token:
+ new_access_token_dialog_title: "Create new API token"
+ new_access_token_dialog_show_button_text: "API Token"
+ new_access_token_dialog_text_field_placeholder_text: "My API token"
+ new_access_token_dialog_text_field_label: "Name"
+ new_access_token_dialog_submit_button_text: "Create"
+ new_access_token_dialog_text: "This token will allow third-party applications to communicate with your instance. To generate a new API token please introduce a token name."
+ new_access_token_dialog_attention_text: "Treat API tokens like passwords. Anyone with this link will have access to information from this instance, share it only with trusted users."
failed_to_reset_token: "Failed to reset access token: %{error}"
notice_reset_token: "A new %{type} token has been generated. Your access token is:"
token_value_warning: "Note: This is the only time you will see this token, make sure to copy it now."
diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rb
index 3b2fc53d539..da6e9fadc75 100644
--- a/spec/controllers/my_controller_spec.rb
+++ b/spec/controllers/my_controller_spec.rb
@@ -303,10 +303,10 @@ RSpec.describe MyController do
describe "api" do
context "with no existing key" do
it "creates a key" do
- expect(user.api_token).to be_nil
+ expect(user.api_tokens).to be_empty
post :generate_api_key
- new_token = user.reload.api_token
+ new_token = user.reload.api_tokens.last
expect(new_token).to be_present
expect(flash[:info]).to be_present
@@ -319,12 +319,12 @@ RSpec.describe MyController do
context "with existing key" do
let!(:key) { Token::API.create user: }
- it "replaces the key" do
- expect(user.reload.api_token).to eq(key)
+ it "must add the new key" do
+ expect(user.reload.api_tokens.last).to eq(key)
post :generate_api_key
- new_token = user.reload.api_token
+ 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[:info]).to be_present