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 validation to contract

Oliver Günthera842422482b2
diff --git a/app/contracts/users/create_contract.rb b/app/contracts/users/create_contract.rb
index 63b7abd48bc..1b4ddab137e 100644
--- a/app/contracts/users/create_contract.rb
+++ b/app/contracts/users/create_contract.rb
@@ -42,9 +42,16 @@ module Users
validate :user_allowed_to_add
validate :authentication_defined
validate :type_is_user
+ validate :user_limit_not_exceeded
private
+ def user_limit_not_exceeded
+ if OpenProject::Enterprise.user_limit_reached?
+ errors.add :base, :user_limit_reached
+ end
+ end
+
def authentication_defined
errors.add :password, :blank if model.active? && no_auth?
end
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 410ca6a2eb7..f1c2e3abbcb 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -812,6 +812,8 @@ en:
group_without_name: "Unnamed groups are not allowed."
user:
attributes:
+ base:
+ user_limit_reached: "User limit reached. No more accounts can be created on the current plan."
password_confirmation:
confirmation: "Password confirmation does not match password."
format: "%{message}"
diff --git a/spec/contracts/users/create_contract_spec.rb b/spec/contracts/users/create_contract_spec.rb
index 31911877335..15cf4d27875 100644
--- a/spec/contracts/users/create_contract_spec.rb
+++ b/spec/contracts/users/create_contract_spec.rb
@@ -71,6 +71,14 @@ describe Users::CreateContract do
it_behaves_like 'contract is valid'
end
end
+
+ context 'when user limit reached' do
+ before do
+ allow(OpenProject::Enterprise).to receive(:user_limit_reached?).and_return(true)
+ end
+
+ it_behaves_like 'contract is invalid', base: :user_limit_reached
+ end
end
end
end