2018-08-18 11:19:57 +00:00
# frozen_string_literal: true
2013-01-07 18:48:57 +00:00
module GroupsHelper
2018-10-17 17:44:43 +00:00
def group_overview_nav_link_paths
%w[
groups #show
2019-03-14 12:55:46 +00:00
groups #details
2018-10-17 17:44:43 +00:00
groups #activity
groups #subgroups
]
end
2020-04-08 21:09:50 +00:00
def group_settings_nav_link_paths
2020-04-08 18:09:16 +00:00
%w[
groups #projects
groups #edit
badges #index
2020-04-21 15:21:10 +00:00
repository #show
2020-04-08 18:09:16 +00:00
ci_cd #show
integrations #index
integrations #edit
ldap_group_links #index
hooks #index
audit_events #index
pipeline_quota #index
]
2018-02-06 12:17:23 +00:00
end
2019-10-17 12:07:33 +00:00
def group_packages_nav_link_paths
%w[
2020-07-21 12:09:30 +00:00
groups / packages #index
2019-10-17 12:07:33 +00:00
groups / container_registries #index
]
end
def group_container_registry_nav?
2019-10-17 18:08:05 +00:00
Gitlab . config . registry . enabled &&
2020-03-31 18:07:42 +00:00
can? ( current_user , :read_container_image , @group )
2019-10-17 12:07:33 +00:00
end
2017-12-11 14:21:06 +00:00
def group_sidebar_links
@group_sidebar_links || = get_group_sidebar_links
end
def group_sidebar_link? ( link )
group_sidebar_links . include? ( link )
end
2016-03-01 15:22:29 +00:00
def can_change_group_visibility_level? ( group )
can? ( current_user , :change_visibility_level , group )
end
2020-04-24 09:09:44 +00:00
def can_update_default_branch_protection? ( group )
can? ( current_user , :update_default_branch_protection , group )
end
2017-09-05 19:43:48 +00:00
def can_change_share_with_group_lock? ( group )
can? ( current_user , :change_share_with_group_lock , group )
end
2019-08-17 05:56:48 +00:00
def can_disable_group_emails? ( group )
2019-10-03 21:07:29 +00:00
can? ( current_user , :set_emails_disabled , group ) && ! group . parent & . emails_disabled?
2019-08-17 05:56:48 +00:00
end
2018-02-23 12:10:57 +00:00
def group_issues_count ( state : )
IssuesFinder
. new ( current_user , group_id : @group . id , state : state , non_archived : true , include_subgroups : true )
. execute
. count
end
def group_merge_requests_count ( state : )
MergeRequestsFinder
. new ( current_user , group_id : @group . id , state : state , non_archived : true , include_subgroups : true )
. execute
. count
end
2017-10-02 11:35:01 +00:00
def group_icon_url ( group , options = { } )
2015-03-12 20:56:53 +00:00
if group . is_a? ( String )
2016-10-31 11:00:53 +00:00
group = Group . find_by_full_path ( group )
2015-03-12 20:56:53 +00:00
end
2017-10-10 08:59:07 +00:00
group . try ( :avatar_url ) || ActionController :: Base . helpers . image_path ( 'no_group_avatar.png' )
2015-03-12 20:56:53 +00:00
end
2015-09-07 11:18:11 +00:00
2015-09-17 10:16:24 +00:00
def group_title ( group , name = nil , url = nil )
2017-03-01 02:59:55 +00:00
@has_group_title = true
2018-08-18 11:19:57 +00:00
full_title = [ ]
2016-12-08 16:28:49 +00:00
2017-08-16 09:35:59 +00:00
group . ancestors . reverse . each_with_index do | parent , index |
2017-09-06 11:19:03 +00:00
if index > 0
2017-09-12 08:54:01 +00:00
add_to_breadcrumb_dropdown ( group_title_link ( parent , hidable : false , show_avatar : true , for_dropdown : true ) , location : :before )
2017-08-16 12:13:34 +00:00
else
2018-08-18 11:19:57 +00:00
full_title << breadcrumb_list_item ( group_title_link ( parent , hidable : false ) )
2017-08-16 12:13:34 +00:00
end
end
2018-08-18 11:19:57 +00:00
full_title << render ( " layouts/nav/breadcrumbs/collapsed_dropdown " , location : :before , title : _ ( " Show parent subgroups " ) )
2016-12-08 16:28:49 +00:00
2018-08-18 11:19:57 +00:00
full_title << breadcrumb_list_item ( group_title_link ( group ) )
full_title << ' · ' . html_safe + link_to ( simple_sanitize ( name ) , url , class : 'group-path breadcrumb-item-text js-breadcrumb-item-text' ) if name
2015-09-17 10:16:24 +00:00
2018-08-18 11:19:57 +00:00
full_title . join . html_safe
2015-09-07 11:18:11 +00:00
end
2016-09-01 23:49:48 +00:00
2016-09-06 16:48:00 +00:00
def projects_lfs_status ( group )
lfs_status =
if group . lfs_enabled?
group . projects . select ( & :lfs_enabled? ) . size
else
group . projects . reject ( & :lfs_enabled? ) . size
end
2016-09-05 20:40:49 +00:00
2016-09-05 18:14:49 +00:00
size = group . projects . size
2016-09-06 16:48:00 +00:00
if lfs_status == size
'for all projects'
2016-09-05 18:14:49 +00:00
else
2016-09-06 16:48:00 +00:00
" for #{ lfs_status } out of #{ pluralize ( size , 'project' ) } "
2016-09-05 18:14:49 +00:00
end
end
def group_lfs_status ( group )
2016-09-05 20:40:49 +00:00
status = group . lfs_enabled? ? 'enabled' : 'disabled'
content_tag ( :span , class : " lfs- #{ status } " ) do
2016-09-06 16:48:00 +00:00
" #{ status . humanize } #{ projects_lfs_status ( group ) } "
2016-09-05 18:14:49 +00:00
end
2016-09-01 23:49:48 +00:00
end
2016-11-05 17:28:29 +00:00
2017-07-04 15:18:51 +00:00
def remove_group_message ( group )
2020-08-12 21:09:54 +00:00
_ ( " You are going to remove %{group_name}, this will also delete all of its subgroups and projects. Removed groups CANNOT be restored! Are you ABSOLUTELY sure? " ) %
2017-07-04 15:18:51 +00:00
{ group_name : group . name }
end
2017-09-06 00:02:11 +00:00
def share_with_group_lock_help_text ( group )
return default_help unless group . parent & . share_with_group_lock?
if group . share_with_group_lock?
if can? ( current_user , :change_share_with_group_lock , group . parent )
ancestor_locked_but_you_can_override ( group )
else
ancestor_locked_so_ask_the_owner ( group )
end
else
ancestor_locked_and_has_been_overridden ( group )
end
2017-09-01 05:58:05 +00:00
end
2018-02-06 00:10:58 +00:00
def parent_group_options ( current_group )
2019-04-02 06:49:11 +00:00
exclude_groups = current_group . self_and_descendants . pluck_primary_key
exclude_groups << current_group . parent_id if current_group . parent_id
groups = GroupsFinder . new ( current_user , min_access_level : Gitlab :: Access :: OWNER , exclude_group_ids : exclude_groups ) . execute . sort_by ( & :human_name ) . map do | group |
2018-02-06 00:10:58 +00:00
{ id : group . id , text : group . human_name }
end
groups . to_json
end
2020-07-21 12:09:30 +00:00
def group_packages_nav?
group_packages_list_nav? ||
group_container_registry_nav?
end
def group_packages_list_nav?
@group . packages_feature_enabled?
end
2020-09-04 03:08:22 +00:00
def show_invite_banner? ( group )
Feature . enabled? ( :invite_your_teammates_banner_a , group ) &&
can? ( current_user , :admin_group , group ) &&
! just_created? &&
! multiple_members? ( group )
end
2020-10-28 15:08:49 +00:00
def show_thanks_for_purchase_banner?
params . key? ( :purchased_quantity ) && params [ :purchased_quantity ] . to_i > 0
end
2017-06-30 16:52:11 +00:00
private
2020-09-04 03:08:22 +00:00
def just_created?
flash [ :notice ] =~ / successfully created /
end
def multiple_members? ( group )
group . member_count > 1
end
2017-12-11 14:21:06 +00:00
def get_group_sidebar_links
links = [ :overview , :group_members ]
2018-07-03 12:08:46 +00:00
resources = [ :activity , :issues , :boards , :labels , :milestones ,
:merge_requests ]
links += resources . select do | resource |
can? ( current_user , " read_group_ #{ resource } " . to_sym , @group )
2017-12-11 14:21:06 +00:00
end
2019-06-27 12:17:26 +00:00
if can? ( current_user , :read_cluster , @group )
2018-10-24 12:55:16 +00:00
links << :kubernetes
end
2017-12-11 14:21:06 +00:00
if can? ( current_user , :admin_group , @group )
links << :settings
end
2020-07-16 18:09:35 +00:00
if can? ( current_user , :read_wiki , @group )
links << :wiki
end
2017-12-11 14:21:06 +00:00
links
end
2017-09-12 08:54:01 +00:00
def group_title_link ( group , hidable : false , show_avatar : false , for_dropdown : false )
link_to ( group_path ( group ) , class : " group-path #{ 'breadcrumb-item-text' unless for_dropdown } js-breadcrumb-item-text #{ 'hidable' if hidable } " ) do
2018-08-18 11:19:57 +00:00
icon = group_icon ( group , class : " avatar-tile " , width : 15 , height : 15 ) if ( group . try ( :avatar_url ) || show_avatar ) && ! Rails . env . test?
[ icon , simple_sanitize ( group . name ) ] . join . html_safe
2017-06-30 16:52:11 +00:00
end
end
2017-09-01 05:58:05 +00:00
2017-09-06 06:49:57 +00:00
def ancestor_group ( group )
2017-09-06 00:02:11 +00:00
ancestor = oldest_consecutively_locked_ancestor ( group )
2017-09-06 06:49:57 +00:00
if can? ( current_user , :read_group , ancestor )
link_to ancestor . name , group_path ( ancestor )
else
ancestor . name
end
2017-09-06 00:02:11 +00:00
end
2017-09-06 18:31:45 +00:00
def remove_the_share_with_group_lock_from_ancestor ( group )
2017-09-06 05:11:49 +00:00
ancestor = oldest_consecutively_locked_ancestor ( group )
2017-09-06 18:31:45 +00:00
text = s_ ( " GroupSettings|remove the share with group lock from %{ancestor_group_name} " ) % { ancestor_group_name : ancestor . name }
2017-09-06 06:49:57 +00:00
if can? ( current_user , :admin_group , ancestor )
link_to text , edit_group_path ( ancestor )
else
text
end
2017-09-06 05:11:49 +00:00
end
2017-09-06 00:02:11 +00:00
def oldest_consecutively_locked_ancestor ( group )
group . ancestors . find do | group |
! group . has_parent? || ! group . parent . share_with_group_lock?
end
2017-09-05 21:53:32 +00:00
end
2017-09-01 05:58:05 +00:00
def default_help
2017-09-22 10:10:25 +00:00
s_ ( " GroupSettings|This setting will be applied to all subgroups unless overridden by a group owner. Groups that already have access to the project will continue to have access unless removed manually. " )
2017-09-01 05:58:05 +00:00
end
2017-09-06 00:02:11 +00:00
def ancestor_locked_but_you_can_override ( group )
2017-09-06 18:31:45 +00:00
s_ ( " GroupSettings|This setting is applied on %{ancestor_group}. You can override the setting or %{remove_ancestor_share_with_group_lock}. " ) . html_safe % { ancestor_group : ancestor_group ( group ) , remove_ancestor_share_with_group_lock : remove_the_share_with_group_lock_from_ancestor ( group ) }
2017-09-01 05:58:05 +00:00
end
2017-09-06 00:02:11 +00:00
def ancestor_locked_so_ask_the_owner ( group )
2017-09-06 18:31:45 +00:00
s_ ( " GroupSettings|This setting is applied on %{ancestor_group}. To share projects in this group with another group, ask the owner to override the setting or %{remove_ancestor_share_with_group_lock}. " ) . html_safe % { ancestor_group : ancestor_group ( group ) , remove_ancestor_share_with_group_lock : remove_the_share_with_group_lock_from_ancestor ( group ) }
2017-09-01 05:58:05 +00:00
end
2017-09-06 00:02:11 +00:00
def ancestor_locked_and_has_been_overridden ( group )
2017-09-06 09:05:50 +00:00
s_ ( " GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup. " ) . html_safe % { ancestor_group : ancestor_group ( group ) }
2017-09-01 05:58:05 +00:00
end
2013-01-07 18:48:57 +00:00
end
2019-09-13 13:26:31 +00:00
GroupsHelper . prepend_if_ee ( 'EE::GroupsHelper' )