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

Add service accounts

Jan Sandbrinkda9aa9b6a5a0
diff --git a/app/models/service_account.rb b/app/models/service_account.rb
new file mode 100644
index 00000000000..2048e2b318b
--- /dev/null
+++ b/app/models/service_account.rb
@@ -0,0 +1,55 @@
+# 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.
+#++
+
+class ServiceAccount < User
+ alias_attribute(:name, :lastname)
+ validates :name, presence: true
+ validates :name, length: { maximum: 256 }
+
+ has_one :service_account_association, dependent: :destroy
+
+ def to_s
+ name
+ end
+
+ def available_custom_fields = []
+
+ def logged? = false
+
+ def builtin? = true
+
+ def mail = ""
+
+ def time_zone
+ ActiveSupport::TimeZone[Setting.user_default_timezone.presence || "Etc/UTC"]
+ end
+
+ def rss_key = nil
+end
diff --git a/app/models/service_account_association.rb b/app/models/service_account_association.rb
new file mode 100644
index 00000000000..9675e0d07b5
--- /dev/null
+++ b/app/models/service_account_association.rb
@@ -0,0 +1,34 @@
+# 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.
+#++
+
+class ServiceAccountAssociation < ApplicationRecord
+ belongs_to :service_account
+ belongs_to :service, polymorphic: true
+end
diff --git a/db/migrate/20250324133701_create_service_account_associations.rb b/db/migrate/20250324133701_create_service_account_associations.rb
new file mode 100644
index 00000000000..ade2f5fa30a
--- /dev/null
+++ b/db/migrate/20250324133701_create_service_account_associations.rb
@@ -0,0 +1,43 @@
+# 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.
+#++
+
+class CreateServiceAccountAssociations < ActiveRecord::Migration[7.1]
+ def change
+ create_table :service_account_associations do |t|
+ t.belongs_to :service_account, null: false, index: { unique: true }
+ t.belongs_to :service, null: false, index: false # necessary index covered by composite
+ t.string :service_type, null: false
+
+ t.timestamps null: false
+
+ t.index %i[service_type service_id], unique: true
+ end
+ end
+end
diff --git a/spec/factories/service_account_factory.rb b/spec/factories/service_account_factory.rb
new file mode 100644
index 00000000000..b0d104b07d3
--- /dev/null
+++ b/spec/factories/service_account_factory.rb
@@ -0,0 +1,43 @@
+# 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.
+#++
+
+FactoryBot.define do
+ factory :service_account, parent: :user, class: "ServiceAccount" do
+ transient do
+ service { nil }
+ end
+
+ after(:create) do |instance, evaluator|
+ if evaluator.service.present?
+ instance.create_service_account_association!(service: evaluator.service)
+ end
+ end
+ end
+end
diff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rb
index f5b6f56d360..3cd98037d30 100644
--- a/spec/workers/principals/delete_job_integration_spec.rb
+++ b/spec/workers/principals/delete_job_integration_spec.rb
@@ -1,3 +1,5 @@
+# frozen_string_literal: true
+
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
@@ -516,6 +518,25 @@ RSpec.describe Principals::DeleteJob, type: :model do
end
end
+ context "with a service account" do
+ describe "service account association" do
+ let(:principal) { create(:service_account, service:) }
+ let(:service) { create(:oauth_application) }
+
+ before do
+ principal.save!
+ end
+
+ it "deletes the service account association" do
+ expect { job }.to change(ServiceAccountAssociation, :count).from(1).to(0)
+ end
+
+ it "does not delete the service associated to the service account" do
+ expect { job }.not_to change(Doorkeeper::Application, :count)
+ end
+ end
+ end
+
context "with a placeholder user" do
let(:principal) { create(:placeholder_user) }