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
[48619] Added create and delete service for Token::API
Andreas Pfohl6dbfb1e437ea
diff --git a/app/contracts/api_tokens/create_contract.rb b/app/contracts/api_tokens/create_contract.rbnew file mode 100644index 00000000000..48cc10c4c4a--- /dev/null+++ b/app/contracts/api_tokens/create_contract.rb@@ -0,0 +1,46 @@+# frozen_string_literal: true++#-- 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.+#++++module APITokens+ class CreateContract < BaseContract+ attribute :token_name++ validates :token_name, presence: { message: I18n.t("my.access_token.errors.token_name_blank") }+ validate :token_name_is_unique++ private++ def token_name_is_unique+ if Token::API.where(user: model.user).any? { |t| t.token_name == model.token_name }+ errors.add(:token_name, I18n.t("my.access_token.errors.token_name_in_use"))+ end+ end+ end+enddiff --git a/app/contracts/api_tokens/delete_contract.rb b/app/contracts/api_tokens/delete_contract.rbnew file mode 100644index 00000000000..170f812f917--- /dev/null+++ b/app/contracts/api_tokens/delete_contract.rb@@ -0,0 +1,6 @@+# frozen_string_literal: true++module APITokens+ class DeleteContract < BaseContract+ end+enddiff --git a/app/models/token/api.rb b/app/models/token/api.rbindex dd0c3ca147e..dca73edcac5 100644--- a/app/models/token/api.rb+++ b/app/models/token/api.rb@@ -28,18 +28,10 @@module Tokenclass API < HashedToken- validate :token_name_exist-- def name- data[:name]- end+ store_attribute :data, :token_name, :stringprivate- def token_name_exist- errors.add(:token_name, I18n.t("my.access_token.errors.token_name_blank")) if data.blank? || data[:name].blank?- end-def single_value?falseenddiff --git a/app/services/api_tokens/create_service.rb b/app/services/api_tokens/create_service.rbnew file mode 100644index 00000000000..1fd994048f2--- /dev/null+++ b/app/services/api_tokens/create_service.rb@@ -0,0 +1,11 @@+# frozen_string_literal: true++module APITokens+ class CreateService < BaseServices::Create+ private++ def instance_class+ Token::API+ end+ end+enddiff --git a/app/services/api_tokens/delete_service.rb b/app/services/api_tokens/delete_service.rbnew file mode 100644index 00000000000..5f1a12421de--- /dev/null+++ b/app/services/api_tokens/delete_service.rb@@ -0,0 +1,6 @@+# frozen_string_literal: true++module APITokens+ class DeleteService < BaseServices::Delete+ end+enddiff --git a/app/services/api_tokens/set_attributes_service.rb b/app/services/api_tokens/set_attributes_service.rbnew file mode 100644index 00000000000..6519c2558e9--- /dev/null+++ b/app/services/api_tokens/set_attributes_service.rb@@ -0,0 +1,13 @@+# frozen_string_literal: true++module APITokens+ class SetAttributesService < ::BaseServices::SetAttributes+ private++ def set_default_attributes(_params)+ model.change_by_system do+ model.user = user if model.user.nil?+ end+ end+ end+enddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex 0a4772d1608..820e337ee02 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -430,6 +430,7 @@ Project attributes and sections are defined in the <a href=%{admin_settings_url}access_token: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"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"