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 for a single trial token, rename to active_trial_token

Oliver Günther06d78fe8f0e1
diff --git a/app/components/enterprise_edition/trial_teaser_component.rb b/app/components/enterprise_edition/trial_teaser_component.rb
index 8962cf8d69d..933374769ed 100644
--- a/app/components/enterprise_edition/trial_teaser_component.rb
+++ b/app/components/enterprise_edition/trial_teaser_component.rb
@@ -59,7 +59,7 @@ module EnterpriseEdition
end
def token
- @token ||= EnterpriseToken.active_trial_tokens.last
+ @token ||= EnterpriseToken.active_trial_token
end
def title
diff --git a/app/models/enterprise_token.rb b/app/models/enterprise_token.rb
index c7b6af9c801..809cbc579ab 100644
--- a/app/models/enterprise_token.rb
+++ b/app/models/enterprise_token.rb
@@ -45,8 +45,8 @@ class EnterpriseToken < ApplicationRecord
active_tokens.reject(&:trial?)
end
- def active_trial_tokens
- active_tokens.select(&:trial?)
+ def active_trial_token
+ active_tokens.find(&:trial?)
end
def table_exists?
@@ -62,7 +62,7 @@ class EnterpriseToken < ApplicationRecord
end
def trial_only?
- active_non_trial_tokens.empty? && active_trial_tokens.any?
+ active_non_trial_tokens.empty? && active_trial_token.present?
end
def available_features
@@ -88,8 +88,8 @@ class EnterpriseToken < ApplicationRecord
def user_limit
if active_non_trial_tokens.any?
get_user_limit_of(active_non_trial_tokens)
- else
- get_user_limit_of(active_trial_tokens)
+ elsif active_trial_token
+ get_user_limit_of([active_trial_token])
end
end
@@ -122,6 +122,7 @@ class EnterpriseToken < ApplicationRecord
uniqueness: { message: I18n.t("activerecord.errors.models.enterprise_token.already_added") }
validate :valid_token_object
validate :valid_domain
+ validate :one_trial_token
before_validation :strip_encoded_token
before_save :extract_validity_from_token
@@ -247,6 +248,12 @@ class EnterpriseToken < ApplicationRecord
errors.add :domain, :invalid if invalid_domain?
end
+ def one_trial_token
+ if self.class.active_trial_token.present?
+ errors.add :base, :only_one_trial
+ end
+ end
+
def extract_validity_from_token
return unless token_object
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 46e1c8d486c..fe507172c70 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -1351,6 +1351,7 @@ en:
failed_to_create: "Trial could not be created (%{status})"
general_consent: "Please accept the terms and conditions."
enterprise_token:
+ only_one_trial: "Only one trial token can be active. Please delete the previous trial token before adding another."
unreadable: "can't be read. Are you sure it is a support token?"
already_added: "This token has already been added."
grids/grid:
diff --git a/spec/components/enterprise_edition/trial_teaser_component_spec.rb b/spec/components/enterprise_edition/trial_teaser_component_spec.rb
index 28d10d0e9c7..56cf2683b70 100644
--- a/spec/components/enterprise_edition/trial_teaser_component_spec.rb
+++ b/spec/components/enterprise_edition/trial_teaser_component_spec.rb
@@ -40,7 +40,7 @@ RSpec.describe EnterpriseEdition::TrialTeaserComponent, type: :component do
end
before do
- allow(EnterpriseToken).to receive(:active_trial_tokens).and_return([mock_token])
+ allow(EnterpriseToken).to receive(:active_trial_token).and_return(mock_token)
end
context "for an admin user" do