Commit 13e38dad authored by Pawel Chojnacki's avatar Pawel Chojnacki

Ensure License helpers have access to view methods when called from GitLab API context

- Allow the message to be returned from git_http controller
- Strip html chars from message returned via API
parent 49f7a449
...@@ -76,7 +76,7 @@ class Projects::GitHttpController < Projects::GitHttpClientController ...@@ -76,7 +76,7 @@ class Projects::GitHttpController < Projects::GitHttpClientController
end end
def access_denied_message def access_denied_message
project.above_size_limit? ? access_check.message : 'Access denied' access_check.message || 'Access denied'
end end
def upload_pack_allowed? def upload_pack_allowed?
......
module LicenseHelper module LicenseHelper
include ActionView::Helpers::AssetTagHelper
include ActionView::Helpers::UrlHelper
delegate :new_admin_license_path, to: 'Gitlab::Routing.url_helpers'
def current_active_user_count def current_active_user_count
User.active.count User.active.count
end end
...@@ -12,28 +17,24 @@ module LicenseHelper ...@@ -12,28 +17,24 @@ module LicenseHelper
if License.current if License.current
yes_license_message(signed_in, is_admin) yes_license_message(signed_in, is_admin)
else else
no_license_message(signed_in, is_admin) no_license_message(is_admin)
end end
end end
private private
def no_license_message(signed_in, is_admin) def no_license_message(is_admin)
message = [] message = []
message << 'No GitLab Enterprise Edition license has been provided yet.'
message << "No GitLab Enterprise Edition license has been provided yet." message << 'Pushing code and creation of issues and merge requests has been disabled.'
message << "Pushing code and creation of issues and merge requests has been disabled."
message << message <<
if is_admin if is_admin
"#{link_to('Upload a license', new_admin_license_path)} in the admin area" "#{link_to('Upload a license', new_admin_license_path)} in the admin area to activate this functionality."
else else
"Ask an admin to upload a license" 'Ask an admin to upload a license to activate this functionality.'
end end
message << "to activate this functionality." content_tag(:p, message.join(' ').html_safe)
content_tag(:p, message.join(" ").html_safe)
end end
def yes_license_message(signed_in, is_admin) def yes_license_message(signed_in, is_admin)
...@@ -45,33 +46,33 @@ module LicenseHelper ...@@ -45,33 +46,33 @@ module LicenseHelper
message = [] message = []
message << "The GitLab Enterprise Edition license" message << 'The GitLab Enterprise Edition license'
message << (license.expired? ? "expired" : "will expire") message << (license.expired? ? 'expired' : 'will expire')
message << "on #{license.expires_at}." message << "on #{license.expires_at}."
if license.expired? && license.will_block_changes? if license.expired? && license.will_block_changes?
message << "Pushing code and creation of issues and merge requests" message << 'Pushing code and creation of issues and merge requests'
message << message <<
if license.block_changes? if license.block_changes?
"has been disabled." 'has been disabled.'
else else
"will be disabled on #{license.block_changes_at}." "will be disabled on #{license.block_changes_at}."
end end
message << message <<
if is_admin if is_admin
"Upload a new license in the admin area" 'Upload a new license in the admin area'
else else
"Ask an admin to upload a new license" 'Ask an admin to upload a new license'
end end
message << "to" message << 'to'
message << (license.block_changes? ? "restore" : "ensure uninterrupted") message << (license.block_changes? ? 'restore' : 'ensure uninterrupted')
message << "service." message << 'service.'
end end
message.join(" ") message.join(' ')
end end
extend self extend self
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
# class return an instance of `GitlabAccessStatus` # class return an instance of `GitlabAccessStatus`
module Gitlab module Gitlab
class GitAccess class GitAccess
include ActionView::Helpers::SanitizeHelper
include PathLocksHelper include PathLocksHelper
UnauthorizedError = Class.new(StandardError) UnauthorizedError = Class.new(StandardError)
...@@ -148,7 +149,7 @@ module Gitlab ...@@ -148,7 +149,7 @@ module Gitlab
if ::License.block_changes? if ::License.block_changes?
message = ::LicenseHelper.license_message(signed_in: true, is_admin: (user && user.is_admin?)) message = ::LicenseHelper.license_message(signed_in: true, is_admin: (user && user.is_admin?))
raise UnauthorizedError, message raise UnauthorizedError, strip_tags(message)
end end
check_change_access!(changes) check_change_access!(changes)
......
...@@ -4,19 +4,26 @@ describe LicenseHelper do ...@@ -4,19 +4,26 @@ describe LicenseHelper do
describe '#license_message' do describe '#license_message' do
context 'no license installed' do context 'no license installed' do
before do before do
expect(License).to receive(:current).and_return(nil) allow(License).to receive(:current).and_return(nil)
end end
it 'admin user' do context 'admin user' do
let(:is_admin) { true }
it 'displays correct error message for admin user' do
admin_msg = '<p>No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. <a href="/admin/license/new">Upload a license</a> in the admin area to activate this functionality.</p>' admin_msg = '<p>No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. <a href="/admin/license/new">Upload a license</a> in the admin area to activate this functionality.</p>'
expect(license_message(signed_in: true, is_admin: true)).to eq(admin_msg) expect(license_message(signed_in: true, is_admin: is_admin)).to eq(admin_msg)
end
end end
it 'normal user' do context 'normal user' do
let(:is_admin) { false }
it 'displays correct error message for normal user' do
user_msg = '<p>No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Ask an admin to upload a license to activate this functionality.</p>' user_msg = '<p>No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Ask an admin to upload a license to activate this functionality.</p>'
expect(license_message(signed_in: true, is_admin: false)).to eq(user_msg) expect(license_message(signed_in: true, is_admin: is_admin)).to eq(user_msg)
end
end end
end end
end end
......
...@@ -283,6 +283,24 @@ describe 'Git HTTP requests', lib: true do ...@@ -283,6 +283,24 @@ describe 'Git HTTP requests', lib: true do
end end
end end
context 'when license is not provided' do
let(:env) { { user: user.username, password: user.password } }
before do
project.team << [user, :master]
end
it 'responds with status 403' do
msg = 'No GitLab Enterprise Edition license has been provided yet. Pushing code and creation of issues and merge requests has been disabled. Ask an admin to upload a license to activate this functionality.'
allow(License).to receive(:current).and_return(false)
upload(path, env) do |response|
expect(response).to have_http_status(403)
expect(response.body).to eq(msg)
end
end
end
context "when the project is private" do context "when the project is private" do
before do before do
project.update_attribute(:visibility_level, Project::PRIVATE) project.update_attribute(:visibility_level, Project::PRIVATE)
......
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