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 ...@@ -5,6 +5,7 @@ class Admin::LicensesController < Admin::ApplicationController
respond_to :html respond_to :html
def show def show
@historical_active_user_count = HistoricalData.during(license.issued_at..Date.today).maximum(:active_user_count)
@previous_licenses = License.previous @previous_licenses = License.previous
end end
......
...@@ -96,12 +96,26 @@ class License < ActiveRecord::Base ...@@ -96,12 +96,26 @@ class License < ActiveRecord::Base
restricted_user_count = self.restrictions[:active_user_count] restricted_user_count = self.restrictions[:active_user_count]
active_user_count = User.active.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. " self.errors.add(:base, message)
message << "This GitLab installation currently has #{active_user_count}, " return
message << "i.e. #{active_user_count - restricted_user_count} too many." end
self.errors.add(:base, message)
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 end
def not_expired def not_expired
......
...@@ -49,19 +49,21 @@ ...@@ -49,19 +49,21 @@
%span.light Active users: %span.light Active users:
%strong %strong
- if @license.restricted?(:active_user_count) - if @license.restricted?(:active_user_count)
- restricted_user_count = @license.restrictions[:active_user_count] - restricted = @license.restrictions[:active_user_count]
- active_user_count = User.active.count - current = User.active.count
#{restricted_user_count} users - historical = @historical_active_user_count
- if active_user_count > restricted_user_count #{restricted} users
%span.label.label-danger.pull-right
%strong Exceeded by #{active_user_count - restricted_user_count} users - if historical && historical > restricted
- elsif restricted_user_count > active_user_count %span.label.label-danger.pull-right.prepend-left-10
%span.label.label-success.pull-right %strong
%strong #{restricted_user_count - active_user_count} more allowed Maximum during license life: #{historical} users
- else
%span.label.label-info.pull-right - label_class = current > restricted ? "danger" : restricted > current ? "success" : "info"
%strong Right at the limit %span.label.pull-right{class: "label-#{label_class}"}
%strong
Current: #{current} users
- else - else
Unlimited Unlimited
......
...@@ -204,7 +204,7 @@ FactoryGirl.define do ...@@ -204,7 +204,7 @@ FactoryGirl.define do
end end
factory :gitlab_license, class: "Gitlab::License" do factory :gitlab_license, class: "Gitlab::License" do
issued_at { Date.today } issued_at { Date.today - 1.month }
licensee do licensee do
{ "Name" => Faker::Name.name } { "Name" => Faker::Name.name }
end end
......
...@@ -24,6 +24,39 @@ describe License do ...@@ -24,6 +24,39 @@ describe License do
end end
describe "Active user count" do 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 context "when there is no active user count restriction" do
it "is valid" do it "is valid" do
expect(license).to be_valid expect(license).to be_valid
...@@ -32,7 +65,7 @@ describe License do ...@@ -32,7 +65,7 @@ describe License do
context "when the active user count restriction is exceeded" do context "when the active user count restriction is exceeded" do
before do before do
gl_license.restrictions = { active_user_count: User.active.count - 1 } gl_license.restrictions = { active_user_count: active_user_count - 1 }
end end
it "is invalid" do it "is invalid" do
...@@ -42,7 +75,7 @@ describe License do ...@@ -42,7 +75,7 @@ describe License do
context "when the active user count restriction is not exceeded" do context "when the active user count restriction is not exceeded" do
before do before do
gl_license.restrictions = { active_user_count: User.active.count + 1 } gl_license.restrictions = { active_user_count: active_user_count + 1 }
end end
it "is valid" do 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