2018-08-18 07:19:57 -04:00
# frozen_string_literal: true
2013-01-07 13:48:57 -05:00
module GroupsHelper
2016-03-01 10:22:29 -05:00
def can_change_group_visibility_level? ( group )
can? ( current_user , :change_visibility_level , group )
end
2020-04-24 05:09:44 -04:00
def can_update_default_branch_protection? ( group )
can? ( current_user , :update_default_branch_protection , group )
end
2017-09-05 15:43:48 -04:00
def can_change_share_with_group_lock? ( group )
can? ( current_user , :change_share_with_group_lock , group )
end
2021-06-14 20:10:11 -04:00
def can_change_prevent_sharing_groups_outside_hierarchy? ( group )
can? ( current_user , :change_prevent_sharing_groups_outside_hierarchy , group )
end
2019-08-17 01:56:48 -04:00
def can_disable_group_emails? ( group )
2019-10-03 17:07:29 -04:00
can? ( current_user , :set_emails_disabled , group ) && ! group . parent & . emails_disabled?
2019-08-17 01:56:48 -04:00
end
2021-08-18 20:11:16 -04:00
def can_admin_group_member? ( group )
Ability . allowed? ( current_user , :admin_group_member , group )
end
2021-08-13 17:09:54 -04:00
def group_dependency_proxy_image_prefix ( group )
2021-03-12 07:09:33 -05:00
# The namespace path can include uppercase letters, which
# Docker doesn't allow. The proxy expects it to be downcased.
2021-08-13 17:09:54 -04:00
url = " #{ group_url ( group ) . downcase } #{ DependencyProxy :: URL_SUFFIX } "
# Docker images do not include the protocol
url . partition ( '//' ) . last
2021-03-12 07:09:33 -05:00
end
2017-10-02 07:35:01 -04:00
def group_icon_url ( group , options = { } )
2015-03-12 16:56:53 -04:00
if group . is_a? ( String )
2016-10-31 07:00:53 -04:00
group = Group . find_by_full_path ( group )
2015-03-12 16:56:53 -04:00
end
2017-10-10 04:59:07 -04:00
group . try ( :avatar_url ) || ActionController :: Base . helpers . image_path ( 'no_group_avatar.png' )
2015-03-12 16:56:53 -04:00
end
2015-09-07 07:18:11 -04:00
2015-09-17 06:16:24 -04:00
def group_title ( group , name = nil , url = nil )
2017-02-28 21:59:55 -05:00
@has_group_title = true
2018-08-18 07:19:57 -04:00
full_title = [ ]
2016-12-08 11:28:49 -05:00
2021-05-06 05:18:56 -04:00
sorted_ancestors ( group ) . with_route . reverse_each . with_index do | parent , index |
2017-09-06 07:19:03 -04:00
if index > 0
2017-09-12 04:54:01 -04:00
add_to_breadcrumb_dropdown ( group_title_link ( parent , hidable : false , show_avatar : true , for_dropdown : true ) , location : :before )
2017-08-16 08:13:34 -04:00
else
2018-08-18 07:19:57 -04:00
full_title << breadcrumb_list_item ( group_title_link ( parent , hidable : false ) )
2017-08-16 08:13:34 -04:00
end
2020-11-11 19:09:44 -05:00
push_to_schema_breadcrumb ( simple_sanitize ( parent . name ) , group_path ( parent ) )
2017-08-16 08:13:34 -04:00
end
2018-08-18 07:19:57 -04:00
full_title << render ( " layouts/nav/breadcrumbs/collapsed_dropdown " , location : :before , title : _ ( " Show parent subgroups " ) )
2016-12-08 11:28:49 -05:00
2018-08-18 07:19:57 -04:00
full_title << breadcrumb_list_item ( group_title_link ( group ) )
2020-11-11 19:09:44 -05:00
push_to_schema_breadcrumb ( simple_sanitize ( group . name ) , group_path ( group ) )
if name
full_title << ' · ' . html_safe + link_to ( simple_sanitize ( name ) , url , class : 'group-path breadcrumb-item-text js-breadcrumb-item-text' )
push_to_schema_breadcrumb ( simple_sanitize ( name ) , url )
end
2015-09-17 06:16:24 -04:00
2018-08-18 07:19:57 -04:00
full_title . join . html_safe
2015-09-07 07:18:11 -04:00
end
2016-09-01 19:49:48 -04:00
2016-09-06 12:48:00 -04:00
def projects_lfs_status ( group )
lfs_status =
if group . lfs_enabled?
2021-04-26 17:10:25 -04:00
group . projects . count ( & :lfs_enabled? )
2016-09-06 12:48:00 -04:00
else
2021-04-26 17:10:25 -04:00
group . projects . count { | project | ! project . lfs_enabled? }
2016-09-06 12:48:00 -04:00
end
2016-09-05 16:40:49 -04:00
2016-09-05 14:14:49 -04:00
size = group . projects . size
2016-09-06 12:48:00 -04:00
if lfs_status == size
'for all projects'
2016-09-05 14:14:49 -04:00
else
2016-09-06 12:48:00 -04:00
" for #{ lfs_status } out of #{ pluralize ( size , 'project' ) } "
2016-09-05 14:14:49 -04:00
end
end
def group_lfs_status ( group )
2016-09-05 16:40:49 -04:00
status = group . lfs_enabled? ? 'enabled' : 'disabled'
content_tag ( :span , class : " lfs- #{ status } " ) do
2016-09-06 12:48:00 -04:00
" #{ status . humanize } #{ projects_lfs_status ( group ) } "
2016-09-05 14:14:49 -04:00
end
2016-09-01 19:49:48 -04:00
end
2016-11-05 13:28:29 -04:00
2017-07-04 11:18:51 -04:00
def remove_group_message ( group )
2021-08-05 02:09:25 -04: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 11:18:51 -04:00
{ group_name : group . name }
end
2017-09-05 20:02:11 -04: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 01:58:05 -04:00
end
2021-06-14 20:10:11 -04:00
def link_to_group ( group )
link_to ( group . name , group_path ( group ) )
end
def prevent_sharing_groups_outside_hierarchy_help_text ( group )
s_ ( " GroupSettings|This setting is only available on the top-level group and it applies to all subgroups. Groups that have already been shared with a group outside %{group} will still be shared, and this access will have to be revoked manually. " ) . html_safe % { group : link_to_group ( group ) }
end
2018-02-05 19:10:58 -05:00
def parent_group_options ( current_group )
2019-04-02 02:49:11 -04: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-05 19:10:58 -05:00
{ id : group . id , text : group . human_name }
end
groups . to_json
end
2020-09-03 23:08:22 -04:00
def show_invite_banner? ( group )
2021-04-23 11:09:37 -04:00
can? ( current_user , :admin_group , group ) &&
! just_created? &&
! multiple_members? ( group )
2020-09-03 23:08:22 -04:00
end
2021-04-05 23:09:02 -04:00
def render_setting_to_allow_project_access_token_creation? ( group )
group . root? && current_user . can? ( :admin_setting_to_allow_project_access_token_creation , group )
end
2020-10-28 11:08:49 -04:00
def show_thanks_for_purchase_banner?
params . key? ( :purchased_quantity ) && params [ :purchased_quantity ] . to_i > 0
end
2020-11-23 10:09:37 -05:00
def project_list_sort_by
@group_projects_sort || @sort || params [ :sort ] || sort_value_recently_created
end
2017-06-30 12:52:11 -04:00
private
2020-09-03 23:08:22 -04:00
def just_created?
flash [ :notice ] =~ / successfully created /
end
def multiple_members? ( group )
2021-04-26 20:10:25 -04:00
group . member_count > 1 || group . members_with_parents . count > 1
2020-09-03 23:08:22 -04:00
end
2017-09-12 04:54:01 -04: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 07:19:57 -04: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 12:52:11 -04:00
end
end
2017-09-01 01:58:05 -04:00
2017-09-06 02:49:57 -04:00
def ancestor_group ( group )
2017-09-05 20:02:11 -04:00
ancestor = oldest_consecutively_locked_ancestor ( group )
2017-09-06 02:49:57 -04:00
if can? ( current_user , :read_group , ancestor )
link_to ancestor . name , group_path ( ancestor )
else
ancestor . name
end
2017-09-05 20:02:11 -04:00
end
2017-09-06 14:31:45 -04:00
def remove_the_share_with_group_lock_from_ancestor ( group )
2017-09-06 01:11:49 -04:00
ancestor = oldest_consecutively_locked_ancestor ( group )
2017-09-06 14:31:45 -04:00
text = s_ ( " GroupSettings|remove the share with group lock from %{ancestor_group_name} " ) % { ancestor_group_name : ancestor . name }
2017-09-06 02:49:57 -04:00
if can? ( current_user , :admin_group , ancestor )
link_to text , edit_group_path ( ancestor )
else
text
end
2017-09-06 01:11:49 -04:00
end
2017-09-05 20:02:11 -04:00
def oldest_consecutively_locked_ancestor ( group )
2021-05-06 05:18:56 -04:00
sorted_ancestors ( group ) . find do | group |
2017-09-05 20:02:11 -04:00
! group . has_parent? || ! group . parent . share_with_group_lock?
end
2017-09-05 17:53:32 -04:00
end
2021-05-06 05:18:56 -04:00
# Ancestors sorted by hierarchy depth in bottom-top order.
def sorted_ancestors ( group )
if group . root_ancestor . use_traversal_ids?
group . ancestors ( hierarchy_order : :asc )
else
group . ancestors
end
end
2017-09-01 01:58:05 -04:00
def default_help
2017-09-22 06:10:25 -04: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 01:58:05 -04:00
end
2017-09-05 20:02:11 -04:00
def ancestor_locked_but_you_can_override ( group )
2017-09-06 14:31:45 -04: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 01:58:05 -04:00
end
2017-09-05 20:02:11 -04:00
def ancestor_locked_so_ask_the_owner ( group )
2017-09-06 14:31:45 -04: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 01:58:05 -04:00
end
2017-09-05 20:02:11 -04:00
def ancestor_locked_and_has_been_overridden ( group )
2017-09-06 05:05:50 -04: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 01:58:05 -04:00
end
2021-03-29 17:09:08 -04:00
2021-07-21 08:09:35 -04:00
def group_url_error_message
s_ ( 'GroupSettings|Please choose a group URL with no special characters or spaces.' )
end
2021-08-26 08:10:28 -04:00
# Maps `jobs_to_be_done` values to option texts
def localized_jobs_to_be_done_choices
{
basics : _ ( 'I want to learn the basics of Git' ) ,
move_repository : _ ( 'I want to move my repository to GitLab from somewhere else' ) ,
code_storage : _ ( 'I want to store my code' ) ,
exploring : _ ( 'I want to explore GitLab to see if it’ s worth switching to' ) ,
ci : _ ( 'I want to use GitLab CI with my existing repository' ) ,
other : _ ( 'A different reason' )
} . with_indifferent_access . freeze
end
2013-01-07 13:48:57 -05:00
end
2019-09-13 09:26:31 -04:00
2021-05-11 17:10:21 -04:00
GroupsHelper . prepend_mod_with ( 'GroupsHelper' )