Fall back on ghost user when deploy key user is not set

This commit is contained in:
Douwe Maan 2018-03-28 18:54:15 +02:00
parent 59d910f2a1
commit 7bca902a23
3 changed files with 29 additions and 4 deletions

View file

@ -27,6 +27,10 @@ class DeployKey < Key
self.private?
end
def user
super || User.ghost
end
def has_access_to?(project)
deploy_keys_project_for(project).present?
end

View file

@ -99,8 +99,6 @@ module Gitlab
end
def check_active_user!
return if deploy_key?
if user && !user_access.allowed?
raise UnauthorizedError, ERROR_MESSAGES[:account_blocked]
end
@ -219,7 +217,7 @@ module Gitlab
raise UnauthorizedError, ERROR_MESSAGES[:read_only]
end
if deploy_key
if deploy_key?
unless deploy_key.can_push_to?(project)
raise UnauthorizedError, ERROR_MESSAGES[:deploy_key_upload]
end
@ -309,8 +307,10 @@ module Gitlab
case actor
when User
actor
when DeployKey
nil
when Key
actor.user unless actor.is_a?(DeployKey)
actor.user
when :ci
nil
end

View file

@ -17,4 +17,25 @@ describe DeployKey, :mailer do
should_not_email(user)
end
end
describe '#user' do
let(:deploy_key) { create(:deploy_key) }
let(:user) { create(:user) }
context 'when user is set' do
before do
deploy_key.user = user
end
it 'returns the user' do
expect(deploy_key.user).to be(user)
end
end
context 'when user is not set' do
it 'returns the ghost user' do
expect(deploy_key.user).to eq(User.ghost)
end
end
end
end