Commit 1bf622c2 authored by Stan Hu's avatar Stan Hu

Merge branch...

Merge branch 'ee-1159-allow-permission-check-bypass-in-approve-access-request-service' into 'master'

Allow Members::ApproveAccessRequestService to accept a new `:force` option

## What does this MR do?

See the commit message.

This is a backport of the EE fix for https://gitlab.com/gitlab-org/gitlab-ee/issues/1159: gitlab-org/gitlab-ee!830

See merge request !7168
parent bcb6b995
...@@ -4,17 +4,25 @@ module Members ...@@ -4,17 +4,25 @@ module Members
attr_accessor :source attr_accessor :source
# source - The source object that respond to `#requesters` (i.g. project or group)
# current_user - The user that performs the access request approval
# params - A hash of parameters
# :user_id - User ID used to retrieve the access requester
# :id - Member ID used to retrieve the access requester
# :access_level - Optional access level set when the request is accepted
def initialize(source, current_user, params = {}) def initialize(source, current_user, params = {})
@source = source @source = source
@current_user = current_user @current_user = current_user
@params = params @params = params.slice(:user_id, :id, :access_level)
end end
def execute # opts - A hash of options
# :force - Bypass permission check: current_user can be nil in that case
def execute(opts = {})
condition = params[:user_id] ? { user_id: params[:user_id] } : { id: params[:id] } condition = params[:user_id] ? { user_id: params[:user_id] } : { id: params[:id] }
access_requester = source.requesters.find_by!(condition) access_requester = source.requesters.find_by!(condition)
raise Gitlab::Access::AccessDeniedError unless can_update_access_requester?(access_requester) raise Gitlab::Access::AccessDeniedError unless can_update_access_requester?(access_requester, opts)
access_requester.access_level = params[:access_level] if params[:access_level] access_requester.access_level = params[:access_level] if params[:access_level]
access_requester.accept_request access_requester.accept_request
...@@ -24,8 +32,11 @@ module Members ...@@ -24,8 +32,11 @@ module Members
private private
def can_update_access_requester?(access_requester) def can_update_access_requester?(access_requester, opts = {})
access_requester && can?(current_user, action_member_permission(:update, access_requester), access_requester) access_requester && (
opts[:force] ||
can?(current_user, action_member_permission(:update, access_requester), access_requester)
)
end end
end end
end end
...@@ -5,36 +5,37 @@ describe Members::ApproveAccessRequestService, services: true do ...@@ -5,36 +5,37 @@ describe Members::ApproveAccessRequestService, services: true do
let(:access_requester) { create(:user) } let(:access_requester) { create(:user) }
let(:project) { create(:project, :public) } let(:project) { create(:project, :public) }
let(:group) { create(:group, :public) } let(:group) { create(:group, :public) }
let(:opts) { {} }
shared_examples 'a service raising ActiveRecord::RecordNotFound' do shared_examples 'a service raising ActiveRecord::RecordNotFound' do
it 'raises ActiveRecord::RecordNotFound' do it 'raises ActiveRecord::RecordNotFound' do
expect { described_class.new(source, user, params).execute }.to raise_error(ActiveRecord::RecordNotFound) expect { described_class.new(source, user, params).execute(opts) }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end
shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do shared_examples 'a service raising Gitlab::Access::AccessDeniedError' do
it 'raises Gitlab::Access::AccessDeniedError' do it 'raises Gitlab::Access::AccessDeniedError' do
expect { described_class.new(source, user, params).execute }.to raise_error(Gitlab::Access::AccessDeniedError) expect { described_class.new(source, user, params).execute(opts) }.to raise_error(Gitlab::Access::AccessDeniedError)
end end
end end
shared_examples 'a service approving an access request' do shared_examples 'a service approving an access request' do
it 'succeeds' do it 'succeeds' do
expect { described_class.new(source, user, params).execute }.to change { source.requesters.count }.by(-1) expect { described_class.new(source, user, params).execute(opts) }.to change { source.requesters.count }.by(-1)
end end
it 'returns a <Source>Member' do it 'returns a <Source>Member' do
member = described_class.new(source, user, params).execute member = described_class.new(source, user, params).execute(opts)
expect(member).to be_a "#{source.class}Member".constantize expect(member).to be_a "#{source.class}Member".constantize
expect(member.requested_at).to be_nil expect(member.requested_at).to be_nil
end end
context 'with a custom access level' do context 'with a custom access level' do
let(:params) { { user_id: access_requester.id, access_level: Gitlab::Access::MASTER } } let(:params2) { params.merge(user_id: access_requester.id, access_level: Gitlab::Access::MASTER) }
it 'returns a ProjectMember with the custom access level' do it 'returns a ProjectMember with the custom access level' do
member = described_class.new(source, user, params).execute member = described_class.new(source, user, params2).execute(opts)
expect(member.access_level).to eq Gitlab::Access::MASTER expect(member.access_level).to eq Gitlab::Access::MASTER
end end
...@@ -60,6 +61,56 @@ describe Members::ApproveAccessRequestService, services: true do ...@@ -60,6 +61,56 @@ describe Members::ApproveAccessRequestService, services: true do
end end
let(:params) { { user_id: access_requester.id } } let(:params) { { user_id: access_requester.id } }
context 'when current user is nil' do
let(:user) { nil }
context 'and :force option is not given' do
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { project }
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { group }
end
end
context 'and :force option is false' do
let(:opts) { { force: false } }
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { project }
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { group }
end
end
context 'and :force option is true' do
let(:opts) { { force: true } }
it_behaves_like 'a service approving an access request' do
let(:source) { project }
end
it_behaves_like 'a service approving an access request' do
let(:source) { group }
end
end
context 'and :force param is true' do
let(:params) { { user_id: access_requester.id, force: true } }
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { project }
end
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { group }
end
end
end
context 'when current user cannot approve access request to the project' do context 'when current user cannot approve access request to the project' do
it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do it_behaves_like 'a service raising Gitlab::Access::AccessDeniedError' do
let(:source) { project } let(:source) { project }
......
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