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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

114 lines
3 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
class GroupChildEntity < Grape::Entity
include ActionView::Helpers::NumberHelper
include RequestAwareEntity
include MarkupHelper
expose :id, :name, :description, :visibility, :full_name,
:created_at, :updated_at, :avatar_url
expose :type do |instance|
type
end
expose :can_edit do |instance|
2018-03-27 19:54:05 +05:30
can_edit?
2018-03-17 18:26:18 +05:30
end
expose :edit_path do |instance|
# 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)`
2021-03-11 19:13:27 +05:30
# while our methods are `edit_group_path` or `edit_project_path`
2018-03-17 18:26:18 +05:30
public_send("edit_#{type}_path", instance) # rubocop:disable GitlabSecurity/PublicSend
end
expose :relative_path do |instance|
polymorphic_path(instance)
end
expose :permission do |instance|
membership&.human_access
end
# Project only attributes
2022-03-02 08:16:31 +05:30
expose :last_activity_at, if: lambda { |instance| project? }
2018-11-08 19:23:39 +05:30
expose :star_count, :archived,
2018-03-17 18:26:18 +05:30
if: lambda { |_instance, _options| project? }
# Group only attributes
2021-11-11 11:23:49 +05:30
expose :children_count, :parent_id,
2018-03-17 18:26:18 +05:30
unless: lambda { |_instance, _options| project? }
2021-11-11 11:23:49 +05:30
expose :subgroup_count, if: lambda { |group| access_group_counts?(group) }
expose :project_count, if: lambda { |group| access_group_counts?(group) }
2018-03-17 18:26:18 +05:30
expose :leave_path, unless: lambda { |_instance, _options| project? } do |instance|
leave_group_members_path(instance)
end
expose :can_leave, unless: lambda { |_instance, _options| project? } do |instance|
if membership
can?(request.current_user, :destroy_group_member, membership)
else
false
end
end
2022-04-04 11:22:00 +05:30
expose :can_remove, unless: lambda { |_instance, _options| project? } do |group|
can?(request.current_user, :admin_group, group)
end
2018-03-17 18:26:18 +05:30
expose :number_users_with_delimiter, unless: lambda { |_instance, _options| project? } do |instance|
number_with_delimiter(instance.member_count)
end
expose :markdown_description do |instance|
markdown_description
end
private
2021-11-11 11:23:49 +05:30
def access_group_counts?(group)
!project? && can?(request.current_user, :read_counts, group)
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def membership
return unless request.current_user
@membership ||= request.current_user.members.find_by(source: object)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2018-03-17 18:26:18 +05:30
def project?
object.is_a?(Project)
end
def type
object.class.name.downcase
end
def markdown_description
markdown_field(object, :description)
end
2018-03-27 19:54:05 +05:30
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
2018-03-17 18:26:18 +05:30
end
2020-01-01 13:55:28 +05:30
2021-06-08 01:23:25 +05:30
GroupChildEntity.prepend_mod_with('GroupChildEntity')