2018-07-19 14:43:13 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-05 05:30:16 -04:00
|
|
|
class GroupChildEntity < Grape::Entity
|
|
|
|
include ActionView::Helpers::NumberHelper
|
|
|
|
include RequestAwareEntity
|
2017-12-21 16:15:50 -05:00
|
|
|
include MarkupHelper
|
2017-09-05 05:30:16 -04:00
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :id, :name, :description, :visibility, :full_name,
|
|
|
|
:created_at, :updated_at, :avatar_url
|
2017-09-05 10:18:24 -04:00
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :type do |instance|
|
2017-10-11 08:39:23 -04:00
|
|
|
type
|
2017-09-05 05:30:16 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :can_edit do |instance|
|
2017-12-11 09:21:06 -05:00
|
|
|
can_edit?
|
2017-09-05 10:18:24 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :edit_path do |instance|
|
2017-10-11 08:39:23 -04:00
|
|
|
# We know `type` will be one either `project` or `group`.
|
|
|
|
# The `edit_polymorphic_path` helper would try to call the path helper
|
|
|
|
# with a plural: `edit_groups_path(instance)` or `edit_projects_path(instance)`
|
|
|
|
# while our methods are `edit_group_path` or `edit_group_path`
|
|
|
|
public_send("edit_#{type}_path", instance) # rubocop:disable GitlabSecurity/PublicSend
|
2017-09-05 10:18:24 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :relative_path do |instance|
|
2017-10-11 08:39:23 -04:00
|
|
|
polymorphic_path(instance)
|
2017-09-13 05:11:09 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :permission do |instance|
|
2017-10-05 04:38:05 -04:00
|
|
|
membership&.human_access
|
2017-09-05 10:18:24 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Project only attributes
|
2018-05-29 05:50:19 -04:00
|
|
|
expose :star_count, :archived,
|
2017-09-05 10:18:24 -04:00
|
|
|
if: lambda { |_instance, _options| project? }
|
|
|
|
|
|
|
|
# Group only attributes
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :children_count, :parent_id, :project_count, :subgroup_count,
|
2017-09-05 10:18:24 -04:00
|
|
|
unless: lambda { |_instance, _options| project? }
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :leave_path, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
leave_group_members_path(instance)
|
2017-09-05 10:18:24 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :can_leave, unless: lambda { |_instance, _options| project? } do |instance|
|
2017-10-05 04:38:05 -04:00
|
|
|
if membership
|
2017-09-12 04:51:34 -04:00
|
|
|
can?(request.current_user, :destroy_group_member, membership)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :number_projects_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
number_with_delimiter(instance.project_count)
|
2017-09-05 10:18:24 -04:00
|
|
|
end
|
|
|
|
|
2017-10-05 07:12:59 -04:00
|
|
|
expose :number_users_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance|
|
|
|
|
number_with_delimiter(instance.member_count)
|
2017-09-05 05:30:16 -04:00
|
|
|
end
|
2017-10-05 04:38:05 -04:00
|
|
|
|
2017-12-21 16:15:50 -05:00
|
|
|
expose :markdown_description do |instance|
|
|
|
|
markdown_description
|
|
|
|
end
|
|
|
|
|
2017-10-05 04:38:05 -04:00
|
|
|
private
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-10-05 04:38:05 -04:00
|
|
|
def membership
|
|
|
|
return unless request.current_user
|
|
|
|
|
2017-10-10 10:25:26 -04:00
|
|
|
@membership ||= request.current_user.members.find_by(source: object)
|
2017-10-05 04:38:05 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-10-05 07:12:59 -04:00
|
|
|
|
|
|
|
def project?
|
|
|
|
object.is_a?(Project)
|
|
|
|
end
|
2017-10-11 08:39:23 -04:00
|
|
|
|
|
|
|
def type
|
|
|
|
object.class.name.downcase
|
|
|
|
end
|
2017-12-21 16:15:50 -05:00
|
|
|
|
|
|
|
def markdown_description
|
|
|
|
markdown_field(object, :description)
|
|
|
|
end
|
2017-12-11 09:21:06 -05:00
|
|
|
|
|
|
|
def can_edit?
|
|
|
|
return false unless request.respond_to?(:current_user)
|
|
|
|
|
|
|
|
if project?
|
|
|
|
# Avoid checking rights for each project, as it might be expensive if the
|
|
|
|
# user cannot read cross project.
|
|
|
|
can?(request.current_user, :read_cross_project) &&
|
|
|
|
can?(request.current_user, :admin_project, object)
|
|
|
|
else
|
|
|
|
can?(request.current_user, :admin_group, object)
|
|
|
|
end
|
|
|
|
end
|
2017-09-05 05:30:16 -04:00
|
|
|
end
|
2019-12-17 10:08:15 -05:00
|
|
|
|
|
|
|
GroupChildEntity.prepend_if_ee('EE::GroupChildEntity')
|