debian-mirror-gitlab/app/serializers/deploy_key_entity.rb

40 lines
1.3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2017-08-17 22:00:37 +05:30
class DeployKeyEntity < Grape::Entity
expose :id
expose :user_id
expose :title
expose :fingerprint
2020-03-09 13:42:32 +05:30
expose :fingerprint_sha256
2017-08-17 22:00:37 +05:30
expose :destroyed_when_orphaned?, as: :destroyed_when_orphaned
expose :almost_orphaned?, as: :almost_orphaned
expose :created_at
expose :updated_at
2018-03-17 18:26:18 +05:30
expose :deploy_keys_projects, using: DeployKeysProjectEntity do |deploy_key|
2019-10-12 21:52:04 +05:30
deploy_key.deploy_keys_projects.select do |deploy_key_project|
2020-01-01 13:55:28 +05:30
!deploy_key_project.project&.pending_delete? && (allowed_to_read_project?(deploy_key_project.project) || options[:user].admin?)
2019-10-12 21:52:04 +05:30
end
2017-09-10 17:25:29 +05:30
end
expose :can_edit
2020-07-28 23:09:34 +05:30
expose :user, as: :owner, using: ::API::Entities::UserBasic, if: -> (_, opts) { can_read_owner?(opts) }
2017-09-10 17:25:29 +05:30
private
def can_edit
2019-12-21 20:55:43 +05:30
Ability.allowed?(options[:user], :update_deploy_key, object) ||
Ability.allowed?(options[:user], :update_deploy_keys_project, object.deploy_keys_project_for(options[:project]))
2017-08-17 22:00:37 +05:30
end
2020-01-01 13:55:28 +05:30
2020-07-28 23:09:34 +05:30
def can_read_owner?(opts)
opts[:with_owner] && Ability.allowed?(options[:user], :read_user, object.user)
end
2020-01-01 13:55:28 +05:30
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-08-17 22:00:37 +05:30
end