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
Normalize API Token components to reuse them for other tokens
Klaus Zandersa6c1a7a658fa
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.erbindex 928b20ab901..70e15db5e96 100644--- 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@@ -36,7 +36,7 @@ See COPYRIGHT and LICENSE files for more details.)) do |dialog|dialog.with_feedback_message do |message|- message.with_heading(tag: :h2) { I18n.t("my.access_token.create_dialog.header", type: "API") }+ message.with_heading(tag: :h2) { I18n.t("my.access_token.create_dialog.header", type: token.model_name.human) }enddialog.with_additional_details do@@ -45,12 +45,12 @@ See COPYRIGHT and LICENSE files for more details.render(Primer::OpenProject::InputGroup.new) do |input_group|input_group.with_text_input(name: :openproject_api_access_token,- label: Token::API.model_name.human,+ label: token.model_name.human,visually_hide_label: false,- value: @token_value+ value: private_token_value)input_group.with_trailing_action_clipboard_copy_button(- value: @token_value,+ value: private_token_value,aria: {label: I18n.t("button_copy_to_clipboard")}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.rbindex 09633c112c7..7f38e9011b7 100644--- a/app/components/my/access_token/access_token_created_dialog_component.rb+++ b/app/components/my/access_token/access_token_created_dialog_component.rb@@ -35,14 +35,27 @@ module Myinclude OpTurbo::Streamableinclude OpPrimer::ComponentHelpers- def initialize(token_value:)+ attr_reader :token++ def initialize(token:)super- @token_value = token_value+ @token = tokenenddef id- "access-token-created-dialog"+ "#{token.model_name.element}-created-dialog"+ end++ def private_token_value+ case token+ when Token::API+ token.plain_value+ when Token::ICalMeeting+ ical_feed_meetings_url(token: token.plain_value, format: :ics)+ else+ raise ArgumentError, "Unknown token type: #{token.class}"+ endendendenddiff --git a/app/components/my/access_token/api_tokens_section_component.html.erb b/app/components/my/access_token/api_tokens_section_component.html.erbindex 3ca6d8559e2..7744e5ff1d8 100644--- a/app/components/my/access_token/api_tokens_section_component.html.erb+++ b/app/components/my/access_token/api_tokens_section_component.html.erb@@ -28,41 +28,39 @@ See COPYRIGHT and LICENSE files for more details.++#%><%= component_wrapper do %>- <div class="attributes-group" id="api-token-section">+ <div class="attributes-group"><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>+ <h3 class="attributes-group--header-text"><%= t(:title, scope: i18n_scope) %></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) %>+ <p> <%= t(:text_hint, scope: i18n_scope) %> </p>+ <% if token_available? %>+ <% if @tokens.any? %>+ <%= render My::AccessToken::API::TableComponent.new(rows: @tokens) %><% end %><%= render(Primer::Beta::Button.new(tag: :a,mt: 3,scheme: :secondary,- test_selector: "api-token-add",- href: dialog_my_access_tokens_path(token_type: "api"),+ test_selector: "#{token_type.model_name.element}-token-add",+ href: dialog_my_access_tokens_path(token_type: token_type.model_name.element),data: { turbo_stream: true },- aria: { label: I18n.t("my.access_token.new_access_token_dialog_show_button_text") },+ aria: { label: t(:add_button, scope: i18n_scope) },role: "button",- title: I18n.t("my.access_token.new_access_token_dialog_show_button_text")+ title: t(:add_button, scope: i18n_scope))) do |button|- button.with_leading_visual_icon(icon: :plus)- I18n.t("my.access_token.new_access_token_dialog_show_button_text")+ button.with_leading_visual_icon(icon: add_button_icon)+ t(:add_button, scope: i18n_scope)end %><% 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>+ <span><%= t(:disabled_text, scope: i18n_scope) %></span></p></div></div>diff --git a/app/components/my/access_token/api_tokens_section_component.rb b/app/components/my/access_token/api_tokens_section_component.rbindex 7d44dbc18bb..2893e7f3275 100644--- a/app/components/my/access_token/api_tokens_section_component.rb+++ b/app/components/my/access_token/api_tokens_section_component.rb@@ -35,14 +35,43 @@ module Myinclude OpTurbo::Streamableinclude Redmine::I18n- def initialize(api_tokens:)+ attr_reader :tokens, :token_type++ def initialize(tokens:, token_type:)super- @api_tokens = api_tokens+ @tokens = tokens+ @token_type = token_type+ end++ private++ def wrapper_key+ "#{token_type.model_name.element}-token-component"+ end++ def i18n_scope+ case token_type.to_s+ when "Token::API" then "my_account.access_tokens.api"+ when "Token::ICalMeeting" then "my_account.access_tokens.ical_meeting"+ else raise ArgumentError, "Unknown token type: #{token_type}"+ end+ end++ def token_available?+ case token_type.to_s+ when "Token::API" then Setting.rest_api_enabled?+ when "Token::ICalMeeting" then Setting.ical_enabled?+ else raise ArgumentError, "Unknown token type: #{token_type}"+ endend- def id- "api-tokens-section-component"+ def add_button_icon+ case token_type.to_s+ when "Token::API" then :plus+ when "Token::ICalMeeting" then :rss+ else raise ArgumentError, "Unknown token type: #{token_type}"+ endendendenddiff --git a/app/components/my/access_token/new_access_token_dialog_component.rb b/app/components/my/access_token/new_access_token_dialog_component.rbindex 59516e148c7..d5295f8dd94 100644--- a/app/components/my/access_token/new_access_token_dialog_component.rb+++ b/app/components/my/access_token/new_access_token_dialog_component.rb@@ -45,16 +45,16 @@ module Myprivatedef new_token- case token_type- when "api" then Token::API.new- when "ical-meeting" then Token::ICalMeeting.new- end+ @new_token ||= case token_type+ when "api" then Token::API.new+ when "ical_meeting" then Token::ICalMeeting.new+ endenddef i18n_scope- case token_type- when "api" then "my.access_token.api"- when "ical-meeting" then "my.access_token.ical_meeting"+ case new_token+ when Token::API then "my.access_token.dialog.api"+ when Token::ICalMeeting then "my.access_token.dialog.ical_meeting"else raise ArgumentError, "Unknown token type: #{token_type}"endenddiff --git a/app/components/my/access_token/new_access_token_form.rb b/app/components/my/access_token/new_access_token_form.rbindex 5f11eae44b7..6672e90bd20 100644--- a/app/components/my/access_token/new_access_token_form.rb+++ b/app/components/my/access_token/new_access_token_form.rb@@ -45,8 +45,8 @@ module Mydef i18n_scopecase model- when Token::API then "my.access_token.api"- when Token::ICalMeeting then "my.access_token.ical_meeting"+ when Token::API then "my.access_token.dialog.api"+ when Token::ICalMeeting then "my.access_token.dialog.ical_meeting"else raise ArgumentError, "Unknown token type: #{token_type}"endenddiff --git a/app/components/my/access_token/new_access_token_form_component.rb b/app/components/my/access_token/new_access_token_form_component.rbindex 71763b6a226..1ba8598c841 100644--- a/app/components/my/access_token/new_access_token_form_component.rb+++ b/app/components/my/access_token/new_access_token_form_component.rb@@ -46,8 +46,8 @@ module Mydef i18n_scopecase token- when Token::API then "my.access_token.api"- when Token::ICalMeeting then "my.access_token.ical_meeting"+ when Token::API then "my.access_token.dialog.api"+ when Token::ICalMeeting then "my.access_token.dialog.ical_meeting"else raise ArgumentError, "Unknown token type: #{token_type}"endenddiff --git a/app/controllers/my/access_tokens_controller.rb b/app/controllers/my/access_tokens_controller.rbindex 591bec72a7f..4b9f99be48c 100644--- a/app/controllers/my/access_tokens_controller.rb+++ b/app/controllers/my/access_tokens_controller.rb@@ -103,15 +103,13 @@ module Myredirect_to action: :indexend- def generate_api_key # rubocop:disable Metrics/AbcSize- result = APITokens::CreateService.new(user: current_user).call(token_name: params[:token_api][:token_name])+ def generate_api_key+ result = call_generate_token_serviceresult.on_success do |r|- update_via_turbo_stream(- component: My::AccessToken::APITokensSectionComponent.new(api_tokens: @user.api_tokens)- )+ update_via_turbo_stream(component: section_to_render(r.result))- dialog = My::AccessToken::AccessTokenCreatedDialogComponent.new(token_value: r.result.plain_value)+ dialog = My::AccessToken::AccessTokenCreatedDialogComponent.new(token: r.result)modify_via_turbo_stream(component: dialog, action: :dialog, status: :ok)end@@ -189,5 +187,30 @@ module Myproject_name: @ical_token.query.project.name)end++ def section_to_render(token)+ case token+ when Token::API+ My::AccessToken::APITokensSectionComponent.new(tokens: current_user.api_tokens, token_type: Token::API)+ when Token::ICalMeeting+ My::AccessToken::APITokensSectionComponent.new(tokens: current_user.ical_meeting_tokens, token_type: Token::ICalMeeting)+ else+ raise ArgumentError, "Unknown token type: #{token.class}"+ end+ end++ def call_generate_token_service # rubocop:disable Metrics/AbcSize+ if params[:token_api]+ APITokens::CreateService+ .new(user: current_user, model: Token::API.new)+ .call(token_name: params[:token_api][:token_name])+ elsif params[:token_ical_meeting]+ APITokens::CreateService+ .new(user: current_user, model: Token::ICalMeeting.new)+ .call(token_name: params[:token_ical_meeting][:token_name])+ else+ raise ArgumentError, "Unknown token type"+ end+ endendenddiff --git a/app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erb b/app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erbdeleted file mode 100644index 66207274159..00000000000--- a/app/views/my/access_tokens/_icalendar_meeting_tokens_section.html.erb+++ /dev/null@@ -1,103 +0,0 @@-<%#-- 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.--++#%>-<div class="attributes-group" id="icalendar-token-section">- <div class="attributes-group--header">- <div class="attributes-group--header-container">- <h3 class="attributes-group--header-text"><%= t("my_account.access_tokens.ical_meeting.title") %></h3>- </div>- </div>- <div>- <p>- <%= t("my_account.access_tokens.ical_meeting.text_hint") %>- </p>- <% if Setting.ical_enabled? %>- <% if ical_tokens.any? %>- <div class="generic-table--container">- <div class="generic-table--results-container">- <table class="generic-table" data-controller="table-highlighting">- <%= render partial: "token_table_header",- locals: {- column_headers: [- t("attributes.name"),- User.human_attribute_name(:created_at),- t("my_account.access_tokens.headers.expiration")- ]- } %>- <tbody>- <% ical_tokens.each do |token| %>- <tr>- <td class="-w-rel-20 -mw-abs-200" data-test-selector="ical-meeting-token-row-<%= token.id %>-name"><%= token.token_name %></td>- <td>- <span title="<%= format_time(token.created_at) %>">- <%= format_time(token.created_at.to_s) %>- </span>- </td>- <td><%= t("my_account.access_tokens.indefinite_expiration") %></td>- <td class="buttons">- <%= link_to "",- { action: "revoke_ical_meeting_token", id: token.id },- data: { confirm: t("my_account.access_tokens.simple_revoke_confirmation"), test_selector: "ical-meeting-token-row-#{token.id}-revoke" },- method: :delete,- class: "icon icon-delete" %>- </td>- </tr>- <% end %>- </tbody>- </table>- </div>- </div>- <% else %>- <% end %>- <%= render(- Primer::Beta::Button.new(- tag: :a,- mt: 3,- scheme: :secondary,- test_selector: "ical-meeting-token-add",- href: dialog_my_access_tokens_path(token_type: "ical-meeting"),- data: { turbo_stream: true },- aria: { label: I18n.t("my.access_token.new_ical_meeting_token_dialog_show_button_text") },- role: "button",- title: I18n.t("my.access_token.new_ical_meeting_token_dialog_show_button_text")- )- ) do |button|- button.with_leading_visual_icon(icon: :rss)- I18n.t("my.access_token.new_ical_meeting_token_dialog_show_button_text")- end %>- <% 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.ical_meeting.disabled_text") %></span>- </p>- </div>- </div>- <% end %>- </div>-</div>diff --git a/app/views/my/access_tokens/index.html.erb b/app/views/my/access_tokens/index.html.erbindex c7f1ef7cd6d..479fb6c3f7f 100644--- a/app/views/my/access_tokens/index.html.erb+++ b/app/views/my/access_tokens/index.html.erb@@ -39,9 +39,9 @@ See COPYRIGHT and LICENSE files for more details.end%>-<%= render ::My::AccessToken::APITokensSectionComponent.new(api_tokens: @user.api_tokens) %>+<%= render ::My::AccessToken::APITokensSectionComponent.new(tokens: @user.api_tokens, token_type: Token::API) %><%= 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 ::My::AccessToken::APITokensSectionComponent.new(tokens: @user.ical_meeting_tokens, token_type: Token::ICalMeeting) %><%= 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 } %>diff --git a/config/locales/en.yml b/config/locales/en.ymlindex 98ebcbdc605..d629f09d977 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -615,20 +615,21 @@ en:my:access_token:- api:- dialog_title: "Create new API token"- attention_text: "Treat API tokens like passwords. Anyone with this token will have access to information from this instance, share it only with trusted users."- dialog_body: "This token will allow third-party applications to communicate with your instance. To differentiate the new API token, please give it a name."- create_button: "Create"- name_label: "Name"- name_placeholder: "My API token"- ical_meeting:- dialog_title: "Create new iCalendar subscription token for meetings"- attention_text: "Treat the URL to the calendar scubscription like a password. Anyone with this link will have access to information from all meetings you are a participant of."- dialog_body: "This link will allow you to subscribe to all of your meetings in your calendar application. To differentiate the new iCalendar subscription, please tell us on what device you want to use this subscription."- create_button: "Create subscription"- name_label: "Where will you be using this?"- name_placeholder: 'Type a name, e.g. "Phone", "Work computer"'+ dialog:+ api:+ dialog_title: "Create new API token"+ attention_text: "Treat API tokens like passwords. Anyone with this token will have access to information from this instance, share it only with trusted users."+ dialog_body: "This token will allow third-party applications to communicate with your instance. To differentiate the new API token, please give it a name."+ create_button: "Create"+ name_label: "Name"+ name_placeholder: "My API token"+ ical_meeting:+ dialog_title: "Create new iCalendar subscription token for meetings"+ attention_text: "Treat the URL to the calendar scubscription like a password. Anyone with this link will have access to information from all meetings you are a participant of."+ dialog_body: "This link will allow you to subscribe to all of your meetings in your calendar application. To differentiate the new iCalendar subscription, please tell us on what device you want to use this subscription."+ create_button: "Create subscription"+ name_label: "Where will you be using this?"+ name_placeholder: 'Type a name, e.g. "Phone", "Work computer"'create_dialog:title: Token generatedheader: The %{type} token has been generated@@ -1749,6 +1750,9 @@ en:token/api:one: Access tokenother: Access tokens+ token/ical_meeting:+ one: "iCal Meeting subscription"+ other: "iCal Meeting subscription"type: "Type"user: "User"version: "Version"@@ -2723,6 +2727,7 @@ en:text_hint: "API tokens allow third-party applications to communicate with this OpenProject instance via REST APIs."static_token_name: "API token"disabled_text: "API tokens are not enabled by the administrator. Please contact your administrator to use this feature."+ add_button: "API Token"ical:title: "iCalendar"text_hint: 'iCalendar tokens allow users to <a href="%{path}" target="_blank">subscribe to OpenProject calendars</a> and view up-to-date work package information from external clients.'@@ -2733,6 +2738,7 @@ en:text_hint: "iCalendar meeting tokens allow users to subscribe to all their meetings and view up-to-date meeting information in external clients."disabled_text: "iCalendar meeting subscriptions are not enabled by the administrator. Please contact your administrator to use this feature."empty_text_hint: 'To add an iCalendar meeting token, go to the <a href="%{path}">meetings section</a> and subscribe from there.'+ add_button: "Subscribe to calendar"oauth:title: "OAuth"text_hint: "OAuth tokens allow third-party applications to connect with this OpenProject instance."