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

Ensure admins cannot be edited by anybody but active admins

Christophe Bliard74fe9e5e07ee
diff --git a/app/contracts/users/update_contract.rb b/app/contracts/users/update_contract.rb
index f24b67b99cf..e37d136ec1e 100644
--- a/app/contracts/users/update_contract.rb
+++ b/app/contracts/users/update_contract.rb
@@ -39,7 +39,6 @@ module Users
# - the user has the global manage_user permission and is not editing an admin
def user_allowed_to_update
return if editing_themself?
- return if user.admin?
return if can_manage_this_user?
errors.add :base, :error_unauthorized
@@ -49,8 +48,10 @@ module Users
user == model
end
+ # Only admins can edit other admins
+ # Only users with manage_user permission can edit other users
def can_manage_this_user?
- user.allowed_to_globally?(:manage_user) && !model.admin?
+ user.allowed_to_globally?(:manage_user) && (user.admin? || !model.admin?)
end
end
end
diff --git a/spec/contracts/users/update_contract_spec.rb b/spec/contracts/users/update_contract_spec.rb
index 5f45d0155b4..527b71d06cb 100644
--- a/spec/contracts/users/update_contract_spec.rb
+++ b/spec/contracts/users/update_contract_spec.rb
@@ -41,7 +41,9 @@ RSpec.describe Users::UpdateContract do
firstname: user_firstname,
lastname: user_lastname,
login: user_login,
- mail: user_mail
+ mail: user_mail,
+ password: nil,
+ password_confirmation: nil
}
end
@@ -50,14 +52,17 @@ RSpec.describe Users::UpdateContract do
describe 'can lock the user' do
before do
- # needed for the stub
- user.password = user.password_confirmation = nil
-
user.status = Principal.statuses[:locked]
end
it_behaves_like 'contract is valid'
end
+
+ describe 'cannot update an administrator' do
+ let(:user) { build_stubbed(:admin, attributes) }
+
+ it_behaves_like 'contract is invalid'
+ end
end
context "when updated user is current user" do
@@ -68,9 +73,6 @@ RSpec.describe Users::UpdateContract do
context 'when setting status' do
before do
- # needed for the stub
- user.password = user.password_confirmation = nil
-
user.status = Principal.statuses[:locked]
end