2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
module GroupsHelper
2016-06-02 11:05:42 +05:30
def can_change_group_visibility_level? ( group )
can? ( current_user , :change_visibility_level , group )
end
2020-05-24 23:13:21 +05:30
def can_update_default_branch_protection? ( group )
can? ( current_user , :update_default_branch_protection , group )
end
2018-03-17 18:26:18 +05:30
def can_change_share_with_group_lock? ( group )
can? ( current_user , :change_share_with_group_lock , group )
end
2021-09-04 01:27:46 +05:30
def can_change_prevent_sharing_groups_outside_hierarchy? ( group )
can? ( current_user , :change_prevent_sharing_groups_outside_hierarchy , group )
end
2019-10-12 21:52:04 +05:30
def can_disable_group_emails? ( group )
2019-12-21 20:55:43 +05:30
can? ( current_user , :set_emails_disabled , group ) && ! group . parent & . emails_disabled?
2019-10-12 21:52:04 +05:30
end
2021-10-27 15:23:28 +05:30
def can_admin_group_member? ( group )
Ability . allowed? ( current_user , :admin_group_member , group )
end
2018-03-17 18:26:18 +05:30
def group_icon_url ( group , options = { } )
2015-04-26 12:48:37 +05:30
if group . is_a? ( String )
2017-08-17 22:00:37 +05:30
group = Group . find_by_full_path ( group )
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
group . try ( :avatar_url ) || ActionController :: Base . helpers . image_path ( 'no_group_avatar.png' )
2015-09-25 12:07:36 +05:30
end
def group_title ( group , name = nil , url = nil )
2017-08-17 22:00:37 +05:30
@has_group_title = true
2018-12-05 23:21:45 +05:30
full_title = [ ]
2015-09-25 12:07:36 +05:30
2021-06-08 01:23:25 +05:30
sorted_ancestors ( group ) . with_route . reverse_each . with_index do | parent , index |
2018-03-17 18:26:18 +05:30
if index > 0
2021-12-11 22:18:48 +05:30
add_to_breadcrumb_collapsed_links ( group_title_link ( parent ) , location : :before )
2018-03-17 18:26:18 +05:30
else
2018-12-05 23:21:45 +05:30
full_title << breadcrumb_list_item ( group_title_link ( parent , hidable : false ) )
2018-03-17 18:26:18 +05:30
end
2021-01-29 00:20:46 +05:30
push_to_schema_breadcrumb ( simple_sanitize ( parent . name ) , group_path ( parent ) )
2017-08-17 22:00:37 +05:30
end
2021-12-11 22:18:48 +05:30
full_title << render ( " layouts/nav/breadcrumbs/collapsed_inline_list " , location : :before , title : _ ( " Show all breadcrumbs " ) )
2017-08-17 22:00:37 +05:30
2018-12-05 23:21:45 +05:30
full_title << breadcrumb_list_item ( group_title_link ( group ) )
2021-01-29 00:20:46 +05:30
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
2018-03-17 18:26:18 +05:30
2018-12-05 23:21:45 +05:30
full_title . join . html_safe
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
def projects_lfs_status ( group )
lfs_status =
if group . lfs_enabled?
2021-06-08 01:23:25 +05:30
group . projects . count ( & :lfs_enabled? )
2016-09-29 09:46:39 +05:30
else
2021-06-08 01:23:25 +05:30
group . projects . count { | project | ! project . lfs_enabled? }
2016-09-29 09:46:39 +05:30
end
size = group . projects . size
if lfs_status == size
'for all projects'
else
" for #{ lfs_status } out of #{ pluralize ( size , 'project' ) } "
end
end
def group_lfs_status ( group )
status = group . lfs_enabled? ? 'enabled' : 'disabled'
content_tag ( :span , class : " lfs- #{ status } " ) do
" #{ status . humanize } #{ projects_lfs_status ( group ) } "
end
end
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
def remove_group_message ( group )
2021-10-27 15:23:28 +05:30
_ ( " 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-09-10 17:25:29 +05:30
{ group_name : group . name }
end
2018-03-17 18:26:18 +05:30
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
end
2021-09-04 01:27:46 +05:30
def link_to_group ( group )
link_to ( group . name , group_path ( group ) )
end
def prevent_sharing_groups_outside_hierarchy_help_text ( group )
2021-11-18 22:05:49 +05:30
s_ ( " GroupSettings|Available only on the top-level group. Applies to all subgroups. Groups already shared with a group outside %{group} are still shared unless removed manually. " ) . html_safe % { group : link_to_group ( group ) }
2021-09-04 01:27:46 +05:30
end
2018-03-17 18:26:18 +05:30
def parent_group_options ( current_group )
2019-07-07 11:18:12 +05:30
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-03-17 18:26:18 +05:30
{ id : group . id , text : group . human_name }
end
groups . to_json
end
2021-04-29 21:17:54 +05:30
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
2022-11-25 23:54:43 +05:30
def show_thanks_for_purchase_alert? ( quantity )
quantity . to_i > 0
2021-01-29 00:20:46 +05:30
end
2021-02-22 17:27:13 +05:30
def project_list_sort_by
@group_projects_sort || @sort || params [ :sort ] || sort_value_recently_created
end
2022-08-27 11:52:29 +05:30
def subgroup_creation_data ( group )
{
parent_group_name : group . parent & . name ,
import_existing_group_path : new_group_path ( parent_id : group . parent_id , anchor : 'import-group-pane' )
}
end
2022-03-02 08:16:31 +05:30
def verification_for_group_creation_data
# overridden in EE
{ }
end
2022-04-04 11:22:00 +05:30
def require_verification_for_namespace_creation_enabled?
2022-03-02 08:16:31 +05:30
# overridden in EE
false
end
2022-08-27 11:52:29 +05:30
def group_name_and_path_app_data
2022-07-23 23:45:48 +05:30
{
2022-08-27 11:52:29 +05:30
base_path : root_url ,
2022-07-23 23:45:48 +05:30
mattermost_enabled : Gitlab . config . mattermost . enabled . to_s
}
end
def subgroups_and_projects_list_app_data ( group )
{
show_schema_markup : 'true' ,
2022-08-27 11:52:29 +05:30
new_subgroup_path : new_group_path ( parent_id : group . id , anchor : 'create-group-pane' ) ,
2022-07-23 23:45:48 +05:30
new_project_path : new_project_path ( namespace_id : group . id ) ,
new_subgroup_illustration : image_path ( 'illustrations/subgroup-create-new-sm.svg' ) ,
new_project_illustration : image_path ( 'illustrations/project-create-new-sm.svg' ) ,
empty_subgroup_illustration : image_path ( 'illustrations/empty-state/empty-subgroup-md.svg' ) ,
render_empty_state : 'true' ,
can_create_subgroups : can? ( current_user , :create_subgroup , group ) . to_s ,
can_create_projects : can? ( current_user , :create_projects , group ) . to_s
}
end
2022-10-11 01:57:18 +05:30
def group_overview_tabs_app_data ( group )
{
subgroups_and_projects_endpoint : group_children_path ( group , format : :json ) ,
shared_projects_endpoint : group_shared_projects_path ( group , format : :json ) ,
archived_projects_endpoint : group_children_path ( group , format : :json , archived : 'only' ) ,
2022-11-25 23:54:43 +05:30
current_group_visibility : group . visibility ,
initial_sort : project_list_sort_by
2022-10-11 01:57:18 +05:30
} . merge ( subgroups_and_projects_list_app_data ( group ) )
end
2022-07-23 23:45:48 +05:30
def enabled_git_access_protocol_options_for_group
case :: Gitlab :: CurrentSettings . enabled_git_access_protocol
when nil , " "
[ [ _ ( " Both SSH and HTTP(S) " ) , " all " ] , [ _ ( " Only SSH " ) , " ssh " ] , [ _ ( " Only HTTP(S) " ) , " http " ] ]
when " ssh "
[ [ _ ( " Only SSH " ) , " ssh " ] ]
when " http "
[ [ _ ( " Only HTTP(S) " ) , " http " ] ]
end
end
2017-09-10 17:25:29 +05:30
private
2018-03-17 18:26:18 +05:30
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
2022-08-27 11:52:29 +05:30
icon = group_icon ( group , alt : group . name , class : " avatar-tile " , width : 15 , height : 15 ) if group . try ( :avatar_url ) || show_avatar
2018-12-05 23:21:45 +05:30
[ icon , simple_sanitize ( group . name ) ] . join . html_safe
2017-09-10 17:25:29 +05:30
end
end
2018-03-17 18:26:18 +05:30
def ancestor_group ( group )
ancestor = oldest_consecutively_locked_ancestor ( group )
if can? ( current_user , :read_group , ancestor )
link_to ancestor . name , group_path ( ancestor )
else
ancestor . name
end
end
def remove_the_share_with_group_lock_from_ancestor ( group )
ancestor = oldest_consecutively_locked_ancestor ( group )
text = s_ ( " GroupSettings|remove the share with group lock from %{ancestor_group_name} " ) % { ancestor_group_name : ancestor . name }
if can? ( current_user , :admin_group , ancestor )
link_to text , edit_group_path ( ancestor )
else
text
end
end
def oldest_consecutively_locked_ancestor ( group )
2021-06-08 01:23:25 +05:30
sorted_ancestors ( group ) . find do | group |
2018-03-17 18:26:18 +05:30
! group . has_parent? || ! group . parent . share_with_group_lock?
end
end
2021-06-08 01:23:25 +05:30
# 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
2018-03-17 18:26:18 +05:30
def default_help
2021-11-18 22:05:49 +05:30
s_ ( " GroupSettings|Applied to all subgroups unless overridden by a group owner. Groups already added to the project lose access. " )
2018-03-17 18:26:18 +05:30
end
def ancestor_locked_but_you_can_override ( group )
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 ) }
end
def ancestor_locked_so_ask_the_owner ( group )
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 ) }
end
def ancestor_locked_and_has_been_overridden ( group )
s_ ( " GroupSettings|This setting is applied on %{ancestor_group} and has been overridden on this subgroup. " ) . html_safe % { ancestor_group : ancestor_group ( group ) }
end
2021-04-29 21:17:54 +05:30
2021-10-27 15:23:28 +05:30
def group_url_error_message
2022-04-04 11:22:00 +05:30
s_ ( 'GroupSettings|Choose a group path that does not start with a dash or end with a period. It can also contain alphanumeric characters and underscores.' )
2021-04-29 21:17:54 +05:30
end
2021-11-11 11:23:49 +05:30
# 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
2014-09-02 18:07:02 +05:30
end
2019-12-04 20:38:33 +05:30
2021-06-08 01:23:25 +05:30
GroupsHelper . prepend_mod_with ( 'GroupsHelper' )