2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-03-24 06:11:36 -04:00
|
|
|
class DeployKeyEntity < Grape::Entity
|
|
|
|
expose :id
|
|
|
|
expose :user_id
|
|
|
|
expose :title
|
|
|
|
expose :fingerprint
|
2020-01-13 19:08:14 -05:00
|
|
|
expose :fingerprint_sha256
|
2017-04-07 07:34:39 -04:00
|
|
|
expose :destroyed_when_orphaned?, as: :destroyed_when_orphaned
|
|
|
|
expose :almost_orphaned?, as: :almost_orphaned
|
2017-03-24 06:11:36 -04:00
|
|
|
expose :created_at
|
|
|
|
expose :updated_at
|
2018-01-05 10:23:44 -05:00
|
|
|
expose :deploy_keys_projects, using: DeployKeysProjectEntity do |deploy_key|
|
2019-08-01 12:18:17 -04:00
|
|
|
deploy_key.deploy_keys_projects.select do |deploy_key_project|
|
2019-12-11 19:07:43 -05:00
|
|
|
!deploy_key_project.project&.pending_delete? && (allowed_to_read_project?(deploy_key_project.project) || options[:user].admin?)
|
2019-08-01 12:18:17 -04:00
|
|
|
end
|
2017-03-24 06:11:36 -04:00
|
|
|
end
|
2017-03-31 08:54:38 -04:00
|
|
|
expose :can_edit
|
2020-07-15 08:09:26 -04:00
|
|
|
expose :user, as: :owner, using: ::API::Entities::UserBasic, if: -> (_, opts) { can_read_owner?(opts) }
|
2017-03-31 08:54:38 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def can_edit
|
2019-10-16 20:07:27 -04:00
|
|
|
Ability.allowed?(options[:user], :update_deploy_key, object) ||
|
|
|
|
Ability.allowed?(options[:user], :update_deploy_keys_project, object.deploy_keys_project_for(options[:project]))
|
2017-03-31 08:54:38 -04:00
|
|
|
end
|
2019-12-11 19:07:43 -05:00
|
|
|
|
2020-07-15 08:09:26 -04:00
|
|
|
def can_read_owner?(opts)
|
|
|
|
opts[:with_owner] && Ability.allowed?(options[:user], :read_user, object.user)
|
|
|
|
end
|
|
|
|
|
2019-12-11 19:07:43 -05:00
|
|
|
def allowed_to_read_project?(project)
|
|
|
|
if options[:readable_project_ids]
|
|
|
|
options[:readable_project_ids].include?(project.id)
|
|
|
|
else
|
|
|
|
Ability.allowed?(options[:user], :read_project, project)
|
|
|
|
end
|
|
|
|
end
|
2017-03-24 06:11:36 -04:00
|
|
|
end
|