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

[#50208] Disabling the default admin user is possible via a system user (#14057)

Dombi Attilafbb2844069cd
diff --git a/app/contracts/users/update_contract.rb b/app/contracts/users/update_contract.rb
index c282d28afb3..78c6bc9096f 100644
--- a/app/contracts/users/update_contract.rb
+++ b/app/contracts/users/update_contract.rb
@@ -29,6 +29,7 @@
module Users
class UpdateContract < BaseContract
validate :user_allowed_to_update
+ validate :at_least_one_admin_is_active
##
# Users can only be updated when
@@ -47,6 +48,12 @@ module Users
end
end
+ def at_least_one_admin_is_active
+ if (model.locked? || !model.admin?) && User.active.admin.where.not(id: model).none?
+ errors.add :base, :one_must_be_active
+ end
+ end
+
def editing_themself?
user == model
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index b4edbd1044f..3dae6da1fb9 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -956,6 +956,7 @@ en:
attributes:
base:
user_limit_reached: "User limit reached. No more accounts can be created on the current plan."
+ one_must_be_active: "Admin User cannot be locked/removed. At least one admin must be active."
password_confirmation:
confirmation: "Password confirmation does not match password."
format: "%{message}"
diff --git a/modules/backlogs/spec/features/tasks_on_taskboard_spec.rb b/modules/backlogs/spec/features/tasks_on_taskboard_spec.rb
index 1215c6741af..15015a33937 100644
--- a/modules/backlogs/spec/features/tasks_on_taskboard_spec.rb
+++ b/modules/backlogs/spec/features/tasks_on_taskboard_spec.rb
@@ -32,6 +32,7 @@ require_relative '../support/pages/taskboard'
RSpec.describe 'Tasks on taskboard',
js: true,
with_cuprite: false do
+ include_context 'with default admin'
let!(:project) do
create(:project,
types: [story, task, other_story],
diff --git a/spec/contracts/users/update_contract_spec.rb b/spec/contracts/users/update_contract_spec.rb
index 97d876d266c..caf4916188d 100644
--- a/spec/contracts/users/update_contract_spec.rb
+++ b/spec/contracts/users/update_contract_spec.rb
@@ -30,7 +30,10 @@ require 'spec_helper'
require_relative 'shared_contract_examples'
RSpec.describe Users::UpdateContract do
+ let!(:default_admin) { create(:admin) }
+
it_behaves_like 'user contract' do
+ let(:current_user) { create(:admin) }
let(:user) { build_stubbed(:user, attributes) }
let(:contract) { described_class.new(user, current_user) }
let(:attributes) do
@@ -44,6 +47,39 @@ RSpec.describe Users::UpdateContract do
}
end
+ context 'with a system user' do
+ let(:current_user) { create(:system) }
+ let(:user) { create(:admin, attributes) }
+
+ context 'when admin flag is removed' do
+ before do
+ user.admin = false
+ end
+
+ it_behaves_like 'contract is valid'
+
+ context 'when no admins left' do
+ let(:default_admin) { nil }
+
+ it_behaves_like 'contract is invalid', base: :one_must_be_active
+ end
+ end
+
+ context 'when status is locked on an admin user' do
+ before do
+ user.status = :locked
+ end
+
+ it_behaves_like 'contract is valid'
+
+ context 'when no admins left' do
+ let(:default_admin) { nil }
+
+ it_behaves_like 'contract is invalid', base: :one_must_be_active
+ end
+ end
+ end
+
context 'when global user' do
let(:current_user) { create(:user, global_permissions: :manage_user) }
diff --git a/spec/controllers/my_controller_spec.rb b/spec/controllers/my_controller_spec.rb
index 572c2305ae7..29bd1967d50 100644
--- a/spec/controllers/my_controller_spec.rb
+++ b/spec/controllers/my_controller_spec.rb
@@ -29,6 +29,8 @@
require 'spec_helper'
RSpec.describe MyController do
+ include_context 'with default admin'
+
let(:user) { create(:user) }
before do
diff --git a/spec/features/users/my_spec.rb b/spec/features/users/my_spec.rb
index 13e26cde3bb..24abb694ff3 100644
--- a/spec/features/users/my_spec.rb
+++ b/spec/features/users/my_spec.rb
@@ -29,6 +29,7 @@
require 'spec_helper'
RSpec.describe 'my', js: true, with_cuprite: true do
+ include_context 'with default admin'
let(:user_password) { 'bob' * 4 }
let(:user) do
create(:user,
diff --git a/spec/requests/api/v3/user/update_form_resource_spec.rb b/spec/requests/api/v3/user/update_form_resource_spec.rb
index 3e04c6d4c50..0871872fdf7 100644
--- a/spec/requests/api/v3/user/update_form_resource_spec.rb
+++ b/spec/requests/api/v3/user/update_form_resource_spec.rb
@@ -32,6 +32,8 @@ RSpec.describe API::V3::Users::UpdateFormAPI, content_type: :json do
include Rack::Test::Methods
include API::V3::Utilities::PathHelper
+ include_context 'with default admin'
+
shared_let(:text_custom_field) do
create(:user_custom_field, :string)
end
diff --git a/spec/requests/api/v3/user/update_user_resource_spec.rb b/spec/requests/api/v3/user/update_user_resource_spec.rb
index 8d4b691d498..6354778b017 100644
--- a/spec/requests/api/v3/user/update_user_resource_spec.rb
+++ b/spec/requests/api/v3/user/update_user_resource_spec.rb
@@ -30,9 +30,9 @@ require 'rack/test'
RSpec.describe API::V3::Users::UsersAPI do
include API::V3::Utilities::PathHelper
+ include_context 'with default admin'
let(:path) { api_v3_paths.user(user.id) }
-
let(:user) { create(:user) }
let(:parameters) { {} }
diff --git a/spec/services/authentication/omniauth_service_spec.rb b/spec/services/authentication/omniauth_service_spec.rb
index 6242137ef2d..a4b3fa31c0d 100644
--- a/spec/services/authentication/omniauth_service_spec.rb
+++ b/spec/services/authentication/omniauth_service_spec.rb
@@ -28,6 +28,8 @@
require 'spec_helper'
RSpec.describe Authentication::OmniauthService do
+ include_context 'with default admin'
+
let(:strategy) { double('Omniauth Strategy', name: 'saml') }
let(:auth_hash) do
OmniAuth::AuthHash.new(
diff --git a/spec/services/ldap/synchronize_users_service_integration_spec.rb b/spec/services/ldap/synchronize_users_service_integration_spec.rb
index 8c64e4ace88..6535e134742 100644
--- a/spec/services/ldap/synchronize_users_service_integration_spec.rb
+++ b/spec/services/ldap/synchronize_users_service_integration_spec.rb
@@ -2,6 +2,7 @@ require 'spec_helper'
RSpec.describe Ldap::SynchronizeUsersService do
include_context 'with temporary LDAP'
+ include_context 'with default admin'
subject do
described_class.new(ldap_auth_source).call
diff --git a/spec/services/users/set_attributes_service_integration_spec.rb b/spec/services/users/set_attributes_service_integration_spec.rb
index 323b7c8d299..ce8b47b7977 100644
--- a/spec/services/users/set_attributes_service_integration_spec.rb
+++ b/spec/services/users/set_attributes_service_integration_spec.rb
@@ -29,7 +29,9 @@
require 'spec_helper'
RSpec.describe Users::SetAttributesService, 'Integration', type: :model do
- let(:input_user) { create(:user) }
+ include_context 'with default admin'
+
+ shared_let(:input_user) { create(:user) }
let(:actor) { build_stubbed(:admin) }
let(:instance) do
diff --git a/spec/services/users/update_service_spec.rb b/spec/services/users/update_service_spec.rb
index edfe87acf2c..7280211efd7 100644
--- a/spec/services/users/update_service_spec.rb
+++ b/spec/services/users/update_service_spec.rb
@@ -29,6 +29,8 @@ require 'spec_helper'
require 'services/base_services/behaves_like_update_service'
RSpec.describe Users::UpdateService do
+ include_context 'with default admin'
+
it_behaves_like 'BaseServices update service' do
# The user service also tries to save the preferences
before do
diff --git a/spec/support/shared/with_default_admin.rb b/spec/support/shared/with_default_admin.rb
new file mode 100644
index 00000000000..0cb2cd0fded
--- /dev/null
+++ b/spec/support/shared/with_default_admin.rb
@@ -0,0 +1,31 @@
+#-- 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.
+#++
+
+RSpec.shared_context 'with default admin' do
+ shared_let(:default_admin) { create(:admin) }
+end