From d180f404009fce050a83a40bb70ce08e33c93baa Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 21 May 2015 18:49:24 +0200 Subject: [PATCH 1/5] Validate maximum active user count up until license issue date. --- app/models/historical_data.rb | 2 + app/models/license.rb | 21 ++++------ app/views/admin/licenses/show.html.haml | 2 +- spec/models/historical_data_spec.rb | 10 ++++- spec/models/license_spec.rb | 55 ++++++++++--------------- 5 files changed, 40 insertions(+), 50 deletions(-) diff --git a/app/models/historical_data.rb b/app/models/historical_data.rb index f573282b25..a62ae8780b 100644 --- a/app/models/historical_data.rb +++ b/app/models/historical_data.rb @@ -3,6 +3,8 @@ class HistoricalData < ActiveRecord::Base # HistoricalData.during((Date.today - 1.year)..Date.today).average(:active_user_count) scope :during, ->(range) { where(date: range) } + # HistoricalData.up_until(Date.today - 1.month).average(:active_user_count) + scope :up_until, ->(date) { where("date <= :date", date: date) } class << self def track! diff --git a/app/models/license.rb b/app/models/license.rb index f272b965f5..6d4ed3f2ba 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -96,27 +96,20 @@ class License < ActiveRecord::Base return unless self.license? && self.restricted?(:active_user_count) restricted_user_count = self.restrictions[:active_user_count] - active_user_count = User.active.count - historical_active_user_count = HistoricalData.maximum(:active_user_count) || 0 + + active_user_count = HistoricalData.up_until(self.issued_at).maximum(:active_user_count) || 0 - max_active_user_count = [active_user_count, historical_active_user_count].max + return if active_user_count < restricted_user_count - return if max_active_user_count < restricted_user_count - - overage = max_active_user_count - restricted_user_count + overage = active_user_count - restricted_user_count message = "" - message << - if historical_active_user_count > active_user_count - "At one point, this GitLab installation had " - else - "This GitLab installation has " - end - message << "#{number_with_delimiter max_active_user_count} active #{"user".pluralize(max_active_user_count)}, " + message << "At one point, this GitLab installation had " + message << "#{number_with_delimiter active_user_count} active #{"user".pluralize(active_user_count)}, " message << "exceeding this license's limit of #{number_with_delimiter restricted_user_count} by " message << "#{number_with_delimiter overage} #{"user".pluralize(overage)}. " message << "Please upload a license for at least " - message << "#{number_with_delimiter max_active_user_count} #{"user".pluralize(max_active_user_count)}." + message << "#{number_with_delimiter active_user_count} #{"user".pluralize(active_user_count)}." self.errors.add(:base, message) end diff --git a/app/views/admin/licenses/show.html.haml b/app/views/admin/licenses/show.html.haml index fa07fc463d..3b4c960de7 100644 --- a/app/views/admin/licenses/show.html.haml +++ b/app/views/admin/licenses/show.html.haml @@ -65,7 +65,7 @@ %strong Exceeds license limit - - historical = HistoricalData.maximum(:active_user_count) + - historical = HistoricalData.up_until(@license.issued_at).maximum(:active_user_count) - if historical %li %span.light Maximum active users: diff --git a/spec/models/historical_data_spec.rb b/spec/models/historical_data_spec.rb index 20d8a5f61f..015838ec01 100644 --- a/spec/models/historical_data_spec.rb +++ b/spec/models/historical_data_spec.rb @@ -8,13 +8,19 @@ describe HistoricalData do end describe ".during" do - it "returns the historical data during the given period" do + it "returns the historical data during the specified period" do expect(HistoricalData.during(Date.new(2014, 1, 1)..Date.new(2014, 12, 31)).average(:active_user_count)).to eq(650) end end + describe ".up_until" do + it "returns the historical data up until the specified date" do + expect(HistoricalData.up_until(Date.new(2014, 6, 1)).average(:active_user_count)).to eq(350) + end + end + describe ".at" do - it "returns the historical data at the given date" do + it "returns the historical data at the specified date" do expect(HistoricalData.at(Date.new(2014, 8, 1)).active_user_count).to eq(800) end end diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index f8998b0dfd..94707f36dd 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -23,39 +23,10 @@ describe License do end 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) } + let(:date) { License.current.issued_at } + let!(:historical_data) { HistoricalData.create!(date: date, active_user_count: active_user_count) } context "when there is no active user count restriction" do it "is valid" do @@ -68,8 +39,26 @@ describe License do gl_license.restrictions = { active_user_count: active_user_count - 1 } end - it "is invalid" do - expect(license).to_not be_valid + context "when the license was issued" do + it "is invalid" do + expect(license).to_not be_valid + end + end + + context "after the license was issued" do + let(:date) { Date.today } + + it "is valid" do + expect(license).to be_valid + end + end + + context "before the license was issued" do + let(:date) { License.current.issued_at - 6.months } + + it "is invalid" do + expect(license).to_not be_valid + end end end From fda2b5adae857ccf34344c5f51016c1aaa3819ad Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 21 May 2015 19:15:12 +0200 Subject: [PATCH 2/5] Only look at year before license was issued. --- app/models/license.rb | 7 +++++-- app/views/admin/licenses/show.html.haml | 3 ++- spec/models/license_spec.rb | 10 +++++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/app/models/license.rb b/app/models/license.rb index 6d4ed3f2ba..fee3cfe11e 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -96,15 +96,18 @@ class License < ActiveRecord::Base return unless self.license? && self.restricted?(:active_user_count) restricted_user_count = self.restrictions[:active_user_count] + + date_range = (self.issued_at - 1.year)..self.issued_at + active_user_count = HistoricalData.during(date_range).maximum(:active_user_count) || 0 - active_user_count = HistoricalData.up_until(self.issued_at).maximum(:active_user_count) || 0 + return unless active_user_count return if active_user_count < restricted_user_count overage = active_user_count - restricted_user_count message = "" - message << "At one point, this GitLab installation had " + message << "During the year before this license was issued, this GitLab installation had " message << "#{number_with_delimiter active_user_count} active #{"user".pluralize(active_user_count)}, " message << "exceeding this license's limit of #{number_with_delimiter restricted_user_count} by " message << "#{number_with_delimiter overage} #{"user".pluralize(overage)}. " diff --git a/app/views/admin/licenses/show.html.haml b/app/views/admin/licenses/show.html.haml index 3b4c960de7..021d4d65c6 100644 --- a/app/views/admin/licenses/show.html.haml +++ b/app/views/admin/licenses/show.html.haml @@ -65,7 +65,8 @@ %strong Exceeds license limit - - historical = HistoricalData.up_until(@license.issued_at).maximum(:active_user_count) + - date_range = (Date.today - 1.year)..Date.today + - historical = HistoricalData.during(date_range).maximum(:active_user_count) - if historical %li %span.light Maximum active users: diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index 94707f36dd..165c9b16f9 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -53,13 +53,21 @@ describe License do end end - context "before the license was issued" do + context "in the year before the license was issued" do let(:date) { License.current.issued_at - 6.months } it "is invalid" do expect(license).to_not be_valid end end + + context "earlier than a year before the license was issued" do + let(:date) { License.current.issued_at - 2.years } + + it "is valid" do + expect(license).to be_valid + end + end end context "when the active user count restriction is not exceeded" do From bf29e33d5dab92a4121ab1a40d4a3251280f95b9 Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Thu, 21 May 2015 19:50:57 +0200 Subject: [PATCH 3/5] Rename issued_at to starts_at. --- Gemfile.lock | 2 +- app/models/license.rb | 4 ++-- app/views/admin/licenses/show.html.haml | 12 ++++++------ spec/factories.rb | 2 +- spec/models/license_spec.rb | 14 +++++++------- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 52fa2e4b9f..760ee0a87d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -219,7 +219,7 @@ GEM diff-lcs (~> 1.1) mime-types (~> 1.15) posix-spawn (~> 0.3) - gitlab-license (0.0.2) + gitlab-license (0.0.3) gitlab-linguist (3.0.1) charlock_holmes (~> 0.6.6) escape_utils (~> 0.2.4) diff --git a/app/models/license.rb b/app/models/license.rb index fee3cfe11e..81b02593d9 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -97,7 +97,7 @@ class License < ActiveRecord::Base restricted_user_count = self.restrictions[:active_user_count] - date_range = (self.issued_at - 1.year)..self.issued_at + date_range = (self.starts_at - 1.year)..self.starts_at active_user_count = HistoricalData.during(date_range).maximum(:active_user_count) || 0 return unless active_user_count @@ -107,7 +107,7 @@ class License < ActiveRecord::Base overage = active_user_count - restricted_user_count message = "" - message << "During the year before this license was issued, this GitLab installation had " + message << "During the year before this license started, this GitLab installation had " message << "#{number_with_delimiter active_user_count} active #{"user".pluralize(active_user_count)}, " message << "exceeding this license's limit of #{number_with_delimiter restricted_user_count} by " message << "#{number_with_delimiter overage} #{"user".pluralize(overage)}. " diff --git a/app/views/admin/licenses/show.html.haml b/app/views/admin/licenses/show.html.haml index 021d4d65c6..6e28fbc22f 100644 --- a/app/views/admin/licenses/show.html.haml +++ b/app/views/admin/licenses/show.html.haml @@ -20,12 +20,12 @@ .panel-heading Details %ul.well-list - %li - %span.light Issued: - %strong= time_ago_with_tooltip @license.issued_at %li %span.light Uploaded: %strong= time_ago_with_tooltip @license.created_at + %li + %span.light Started: + %strong= time_ago_with_tooltip @license.starts_at %li %span.light - if @license.expired? @@ -107,8 +107,8 @@ %tr - @license.licensee.keys.each do |label| %th= label - %th Issued at %th Uploaded at + %th Started at %th Expired at %th Active users %tbody @@ -118,10 +118,10 @@ %td= license.licensee[label] %td %span - = license.issued_at + = license.created_at %td %span - = license.created_at + = license.starts_at %td %span = license.expires_at || "Never" diff --git a/spec/factories.rb b/spec/factories.rb index 4149e8d30e..35b199e66a 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -211,7 +211,7 @@ FactoryGirl.define do end factory :gitlab_license, class: "Gitlab::License" do - issued_at { Date.today - 1.month } + starts_at { Date.today - 1.month } licensee do { "Name" => Faker::Name.name } end diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index 165c9b16f9..d4465cfd30 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -25,7 +25,7 @@ describe License do describe "Historical active user count" do let(:active_user_count) { User.active.count + 10 } - let(:date) { License.current.issued_at } + let(:date) { License.current.starts_at } let!(:historical_data) { HistoricalData.create!(date: date, active_user_count: active_user_count) } context "when there is no active user count restriction" do @@ -39,13 +39,13 @@ describe License do gl_license.restrictions = { active_user_count: active_user_count - 1 } end - context "when the license was issued" do + context "when the license started" do it "is invalid" do expect(license).to_not be_valid end end - context "after the license was issued" do + context "after the license started" do let(:date) { Date.today } it "is valid" do @@ -53,16 +53,16 @@ describe License do end end - context "in the year before the license was issued" do - let(:date) { License.current.issued_at - 6.months } + context "in the year before the license started" do + let(:date) { License.current.starts_at - 6.months } it "is invalid" do expect(license).to_not be_valid end end - context "earlier than a year before the license was issued" do - let(:date) { License.current.issued_at - 2.years } + context "earlier than a year before the license started" do + let(:date) { License.current.starts_at - 2.years } it "is valid" do expect(license).to be_valid From 51d6aa1351ef872d7ec878a4d52cc1b232b04f5a Mon Sep 17 00:00:00 2001 From: Karen Carias Date: Thu, 21 May 2015 22:48:20 +0000 Subject: [PATCH 4/5] Added info about active users --- doc/api/users.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/doc/api/users.md b/doc/api/users.md index cd141daadc..f0293bab5e 100644 --- a/doc/api/users.md +++ b/doc/api/users.md @@ -2,6 +2,8 @@ ## List users +Active users = Total accounts - Blocked users + Get a list of users. This function takes pagination parameters `page` and `per_page` to restrict the list of users. @@ -393,4 +395,4 @@ Parameters: - `uid` (required) - id of specified user - `id` (required) - SSH key ID -Will return `200 OK` on success, or `404 Not found` if either user or key cannot be found. +Will return `200 OK` on success, or `404 Not found` if either user or key cannot be found. \ No newline at end of file From 91b0980a1c8c7725588f1f89002f6eee2006c345 Mon Sep 17 00:00:00 2001 From: Patricio Cano Date: Fri, 22 May 2015 18:07:36 -0500 Subject: [PATCH 5/5] Added 7.11.2 to the CHANGELOG --- CHANGELOG-EE | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG-EE b/CHANGELOG-EE index 70872e900e..37f99c41a6 100644 --- a/CHANGELOG-EE +++ b/CHANGELOG-EE @@ -1,3 +1,6 @@ +v 7.11.2 + - Fixed license upload and verification mechanism + v 7.11.0 - Skip git hooks commit validation when pushing new tag. - Add Two-factor authentication (2FA) for LDAP logins