diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index fc52cd2f367f2e629d97985882799c3f9b748d4a..678b56b5d9bee61fbe826c8161bc3a4c68e6e5e2 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -309,7 +309,7 @@ class ProjectsController < Projects::ApplicationController :issues_tracker_id, :default_branch, :wiki_enabled, :visibility_level, :import_url, :last_activity_at, :namespace_id, :avatar, :builds_enabled, :build_allow_git_fetch, :build_timeout_in_minutes, :build_coverage_regex, - :public_builds, :only_allow_merge_if_build_succeeds, :request_access_enabled + :public_builds, :only_allow_merge_if_build_succeeds, :request_access_enabled, :enable_lfs ) end diff --git a/app/helpers/lfs_helper.rb b/app/helpers/lfs_helper.rb index eb651e3687eb64eb8d0eaf66e43faf7ad6e78128..5d82abfca79078df050e04a9132fef69774d4048 100644 --- a/app/helpers/lfs_helper.rb +++ b/app/helpers/lfs_helper.rb @@ -23,10 +23,14 @@ module LfsHelper end def lfs_download_access? + return false unless project.lfs_enabled? + project.public? || ci? || (user && user.can?(:download_code, project)) end def lfs_upload_access? + return false unless project.lfs_enabled? + user && user.can?(:push_code, project) end diff --git a/app/helpers/projects_helper.rb b/app/helpers/projects_helper.rb index 356f27f2d5dad79bbc5011261f4afb0431223a6b..a5ae9f8668ef2189b7fb01f998be314353cee5c6 100644 --- a/app/helpers/projects_helper.rb +++ b/app/helpers/projects_helper.rb @@ -187,6 +187,18 @@ module ProjectsHelper nav_tabs.flatten end + def project_lfs_status(project) + if project.lfs_enabled? + content_tag(:span, class: 'vs-private') do + 'Enabled' + end + else + content_tag(:span, class: 'vs-internal') do + 'Disabled' + end + end + end + def git_user_name if current_user current_user.name diff --git a/app/models/project.rb b/app/models/project.rb index c34064f96cebfb15ca101ef3fd1375113d9271b6..c271448946cb5dfe6eeaa60cc2a793278e239063 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -390,6 +390,10 @@ class Project < ActiveRecord::Base end end + def lfs_enabled? + (Gitlab.config.lfs.enabled && enable_lfs) || (enable_lfs.nil? && Gitlab.config.lfs.enabled) + end + def repository_storage_path Gitlab.config.repositories.storages[repository_storage] end diff --git a/app/views/admin/projects/show.html.haml b/app/views/admin/projects/show.html.haml index b2c607361b3d417e9b9755efc11893abcda1a69d..f65322cc12fe5f6f08147db28b8b5a6bca0fb956 100644 --- a/app/views/admin/projects/show.html.haml +++ b/app/views/admin/projects/show.html.haml @@ -73,6 +73,11 @@ %span.light last commit: %strong = last_commit(@project) + + %li + %span.light LFS status: + %strong + = project_lfs_status(@project) - else %li %span.light repository: diff --git a/app/views/projects/edit.html.haml b/app/views/projects/edit.html.haml index b282aa52b25d0a6eadcbd7b1ac1d05062f859f5c..8aa2db197a3ca143e4be8a04e4746ca55df5d1db 100644 --- a/app/views/projects/edit.html.haml +++ b/app/views/projects/edit.html.haml @@ -80,6 +80,14 @@ %strong Snippets %br %span.descr Share code pastes with others out of git repository + - if Gitlab.config.lfs.enabled && current_user.admin? + .form-group + .checkbox + = f.label :enable_lfs do + = f.check_box :enable_lfs, checked: (true if @project.enable_lfs || @project.enable_lfs.nil?) + %strong LFS + %br + %span.descr Git Large File Storage - if Gitlab.config.registry.enabled .form-group .checkbox diff --git a/db/migrate/20160823213309_add_enable_lfs_to_projects.rb b/db/migrate/20160823213309_add_enable_lfs_to_projects.rb new file mode 100644 index 0000000000000000000000000000000000000000..9df1a5078faad2f1677b412c10806dfcbd73beeb --- /dev/null +++ b/db/migrate/20160823213309_add_enable_lfs_to_projects.rb @@ -0,0 +1,29 @@ +# See http://doc.gitlab.com/ce/development/migration_style_guide.html +# for more information on how to write migrations for GitLab. + +class AddEnableLfsToProjects < ActiveRecord::Migration + include Gitlab::Database::MigrationHelpers + + # Set this constant to true if this migration requires downtime. + DOWNTIME = false + + # When a migration requires downtime you **must** uncomment the following + # constant and define a short and easy to understand explanation as to why the + # migration requires downtime. + # DOWNTIME_REASON = '' + + # When using the methods "add_concurrent_index" or "add_column_with_default" + # you must disable the use of transactions as these methods can not run in an + # existing transaction. When using "add_concurrent_index" make sure that this + # method is the _only_ method called in the migration, any other changes + # should go in a separate migration. This ensures that upon failure _only_ the + # index creation fails and can be retried or reverted easily. + # + # To disable transactions uncomment the following line and remove these + # comments: + # disable_ddl_transaction! + + def change + add_column :projects, :enable_lfs, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 227e10294e42be46b43777bc352f5537d370ef38..28711294746d383119130a8e9d56c263217a1100 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -825,6 +825,7 @@ ActiveRecord::Schema.define(version: 20160824103857) do t.string "repository_storage", default: "default", null: false t.boolean "request_access_enabled", default: true, null: false t.boolean "has_external_wiki" + t.boolean "enable_lfs" end add_index "projects", ["ci_id"], name: "index_projects_on_ci_id", using: :btree diff --git a/spec/requests/lfs_http_spec.rb b/spec/requests/lfs_http_spec.rb index 4c9b4a8ba422ef6f921b5130557ca82628bcdec2..2d39f3808d50c47d909d5e86095067052e8dad50 100644 --- a/spec/requests/lfs_http_spec.rb +++ b/spec/requests/lfs_http_spec.rb @@ -44,6 +44,113 @@ describe 'Git LFS API and storage' do end end + context 'project specific LFS settings' do + let(:project) { create(:empty_project) } + let(:body) do + { + 'objects' => [ + { 'oid' => '91eff75a492a3ed0dfcb544d7f31326bc4014c8551849c192fd1e48d4dd2c897', + 'size' => 1575078 + }, + { 'oid' => sample_oid, + 'size' => sample_size + } + ], + 'operation' => 'upload' + } + end + let(:authorization) { authorize_user } + + context 'with LFS disabled globally' do + before do + project.team << [user, :master] + allow(Gitlab.config.lfs).to receive(:enabled).and_return(false) + end + + describe 'LFS disabled in project' do + before do + project.update_attribute(:enable_lfs, false) + end + + it 'responds with a 501 message on upload' do + post_lfs_json "#{project.http_url_to_repo}/info/lfs/objects/batch", body, headers + + expect(response).to have_http_status(501) + end + + it 'responds with a 501 message on download' do + get "#{project.http_url_to_repo}/gitlab-lfs/objects/#{sample_oid}", nil, headers + + expect(response).to have_http_status(501) + end + end + + describe 'LFS enabled in project' do + before do + project.update_attribute(:enable_lfs, true) + end + + it 'responds with a 501 message on upload' do + post_lfs_json "#{project.http_url_to_repo}/info/lfs/objects/batch", body, headers + + expect(response).to have_http_status(501) + end + + it 'responds with a 501 message on download' do + get "#{project.http_url_to_repo}/gitlab-lfs/objects/#{sample_oid}", nil, headers + + expect(response).to have_http_status(501) + end + end + end + + context 'with LFS enabled globally' do + before do + project.team << [user, :master] + enable_lfs + end + + describe 'LFS disabled in project' do + before do + project.update_attribute(:enable_lfs, false) + end + + it 'responds with a 403 message on upload' do + post_lfs_json "#{project.http_url_to_repo}/info/lfs/objects/batch", body, headers + + expect(response).to have_http_status(403) + expect(json_response).to include('message' => 'Access forbidden. Check your access level.') + end + + it 'responds with a 403 message on download' do + get "#{project.http_url_to_repo}/gitlab-lfs/objects/#{sample_oid}", nil, headers + + expect(response).to have_http_status(403) + expect(json_response).to include('message' => 'Access forbidden. Check your access level.') + end + end + + describe 'LFS enabled in project' do + before do + project.update_attribute(:enable_lfs, true) + end + + it 'responds with a 200 message on upload' do + post_lfs_json "#{project.http_url_to_repo}/info/lfs/objects/batch", body, headers + + expect(response).to have_http_status(200) + expect(json_response['objects'].first['size']).to eq(1575078) + end + + it 'responds with a 200 message on download' do + get "#{project.http_url_to_repo}/gitlab-lfs/objects/#{sample_oid}", nil, headers + + expect(response).to have_http_status(200) + end + end + end + end + describe 'deprecated API' do let(:project) { create(:empty_project) }