Commit 45ab4d10 authored by Douwe Maan's avatar Douwe Maan

Validate historical active user count when uploading license.

parent 85c04a8a
......@@ -5,6 +5,7 @@ class Admin::LicensesController < Admin::ApplicationController
respond_to :html
def show
@historical_active_user_count = HistoricalData.during(license.issued_at..Date.today).maximum(:active_user_count)
@previous_licenses = License.previous
end
......
......@@ -96,12 +96,26 @@ class License < ActiveRecord::Base
restricted_user_count = self.restrictions[:active_user_count]
active_user_count = User.active.count
return if active_user_count <= restricted_user_count
if active_user_count > restricted_user_count
message = "This license allows #{restricted_user_count} active users. "
message << "This GitLab installation currently has #{active_user_count}, "
message << "i.e. #{active_user_count - restricted_user_count} too many."
message = "This license allows #{restricted_user_count} active users. "
message << "This GitLab installation currently has #{active_user_count}, "
message << "i.e. #{active_user_count - restricted_user_count} too many."
self.errors.add(:base, message)
self.errors.add(:base, message)
return
end
if License.current
historical_active_user_count = HistoricalData.during(License.current.issued_at..Date.today).maximum(:active_user_count)
if historical_active_user_count && historical_active_user_count > restricted_user_count
message = "This license allows #{restricted_user_count} active users. "
message << "At one point during the life of the previous license, this GitLab installation had "
message << "#{historical_active_user_count} active users, "
message << "i.e. #{historical_active_user_count - restricted_user_count} too many."
self.errors.add(:base, message)
end
end
end
def not_expired
......
......@@ -49,19 +49,21 @@
%span.light Active users:
%strong
- if @license.restricted?(:active_user_count)
- restricted_user_count = @license.restrictions[:active_user_count]
- active_user_count = User.active.count
#{restricted_user_count} users
- restricted = @license.restrictions[:active_user_count]
- current = User.active.count
- historical = @historical_active_user_count
- if active_user_count > restricted_user_count
%span.label.label-danger.pull-right
%strong Exceeded by #{active_user_count - restricted_user_count} users
- elsif restricted_user_count > active_user_count
%span.label.label-success.pull-right
%strong #{restricted_user_count - active_user_count} more allowed
- else
%span.label.label-info.pull-right
%strong Right at the limit
#{restricted} users
- if historical && historical > restricted
%span.label.label-danger.pull-right.prepend-left-10
%strong
Maximum during license life: #{historical} users
- label_class = current > restricted ? "danger" : restricted > current ? "success" : "info"
%span.label.pull-right{class: "label-#{label_class}"}
%strong
Current: #{current} users
- else
Unlimited
......
......@@ -204,7 +204,7 @@ FactoryGirl.define do
end
factory :gitlab_license, class: "Gitlab::License" do
issued_at { Date.today }
issued_at { Date.today - 1.month }
licensee do
{ "Name" => Faker::Name.name }
end
......
......@@ -24,6 +24,39 @@ describe License do
end
describe "Active user count" do
let(:active_user_count) { User.active.count }
context "when there is no active user count restriction" do
it "is valid" do
expect(license).to be_valid
end
end
context "when the active user count restriction is exceeded" do
before do
gl_license.restrictions = { active_user_count: active_user_count - 1 }
end
it "is invalid" do
expect(license).to_not be_valid
end
end
context "when the active user count restriction is not exceeded" do
before do
gl_license.restrictions = { active_user_count: active_user_count + 1 }
end
it "is valid" do
expect(license).to be_valid
end
end
end
describe "Historical active user count" do
let(:active_user_count) { User.active.count + 10 }
let!(:historical_data) { HistoricalData.create!(date: License.current.issued_at, active_user_count: active_user_count) }
context "when there is no active user count restriction" do
it "is valid" do
expect(license).to be_valid
......@@ -32,7 +65,7 @@ describe License do
context "when the active user count restriction is exceeded" do
before do
gl_license.restrictions = { active_user_count: User.active.count - 1 }
gl_license.restrictions = { active_user_count: active_user_count - 1 }
end
it "is invalid" do
......@@ -42,7 +75,7 @@ describe License do
context "when the active user count restriction is not exceeded" do
before do
gl_license.restrictions = { active_user_count: User.active.count + 1 }
gl_license.restrictions = { active_user_count: active_user_count + 1 }
end
it "is valid" do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment