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
Ensure token_name is unique per token class, and has a proper error message
Oliver Güntherf4eb39f79f28
diff --git a/app/contracts/api_tokens/create_contract.rb b/app/contracts/api_tokens/create_contract.rbindex b4835e55eb6..f642ba9543a 100644--- a/app/contracts/api_tokens/create_contract.rb+++ b/app/contracts/api_tokens/create_contract.rb@@ -32,7 +32,7 @@ module APITokensclass CreateContract < BaseContractattribute :token_name- validates :token_name, presence: { message: proc { I18n.t("my.access_token.errors.token_name_blank") } }+ validates :token_name, presence: truevalidate :token_name_is_unique, unless: :token_name_is_blank?private@@ -42,8 +42,8 @@ module APITokensenddef token_name_is_unique- if Token::API.where(user: model.user).any? { |t| t.token_name == model.token_name }- errors.add(:token_name, :taken, message: I18n.t("my.access_token.errors.token_name_in_use"))+ if model.class.where(user: model.user).any? { |t| t.token_name == model.token_name }+ errors.add(:token_name, :in_use)endendenddiff --git a/app/models/token/api.rb b/app/models/token/api.rbindex be34d36ed9c..2a28819405c 100644--- a/app/models/token/api.rb+++ b/app/models/token/api.rb@@ -29,13 +29,6 @@#++module Token- class API < HashedToken- store_attribute :data, :token_name, :string-- private-- def single_value?- false- end+ class API < Namedendenddiff --git a/app/models/token/named.rb b/app/models/token/named.rbnew file mode 100644index 00000000000..d03791cfa61--- /dev/null+++ b/app/models/token/named.rb@@ -0,0 +1,41 @@+# 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 Token+ class Named < HashedToken+ store_attribute :data, :token_name, :string++ protected++ def single_value?+ false+ end+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 11338ca52e0..220772dbc75 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -637,9 +637,6 @@ en:token/api:title: "The API token has been generated"warning: "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"failed_to_reset_token: "Failed to reset access token: %{error}"failed_to_create_token: "Failed to create access token: %{error}"failed_to_revoke_token: "Failed to revoke access token: %{error}"@@ -1221,6 +1218,8 @@ en:is_readonly: "Work package read-only"excluded_from_totals: "Exclude from calculation of totals in hierarchy"default_done_ratio: "% Complete"+ token/named:+ token_name: "Token name"token/ical:calendar: "Calendar"ical_token_query_assignment: "Query assignment"@@ -1704,9 +1703,11 @@ en:version:undeletable_archived_projects: "The version cannot be deleted as it has work packages attached to it."undeletable_work_packages_attached: "The version cannot be deleted as it has work packages attached to it."- token/api:+ token/named:attributes:token_name:+ blank: "Please provide a token name"+ in_use: "This token name is already in use, please select a different one"format: "%{message}"template:body: "Please check the following fields:"diff --git a/modules/meeting/app/models/token/ical_meeting.rb b/modules/meeting/app/models/token/ical_meeting.rbindex e433662cfd2..37d2bce78ad 100644--- a/modules/meeting/app/models/token/ical_meeting.rb+++ b/modules/meeting/app/models/token/ical_meeting.rb@@ -29,12 +29,10 @@#++module Token- class ICalMeeting < HashedToken+ class ICalMeeting < Token::Namedinclude OpenProject::StaticRouting::UrlHelpersinclude ActionView::Helpers::UrlHelper- store_attribute :data, :token_name, :string-def display_valueif plain_value.present?url_helpers.ical_feed_meetings_url(token: plain_value, format: :ics)@@ -45,10 +43,6 @@ module Tokenprivate- def single_value?- false- end-def url_helpers@url_helpers ||= OpenProject::StaticRouting::StaticUrlHelpers.newenddiff --git a/spec/contracts/api_tokens/create_contract_spec.rb b/spec/contracts/api_tokens/create_contract_spec.rbindex 032ee882d92..1492bc2b1e6 100644--- a/spec/contracts/api_tokens/create_contract_spec.rb+++ b/spec/contracts/api_tokens/create_contract_spec.rb@@ -66,7 +66,7 @@ RSpec.describe APITokens::CreateContract doendit "is invalid" do- expect_valid(false, token_name: %i(taken))+ expect_valid(false, token_name: %i(in_use))endendend