Module: UsersHelper

Included in:
API::Entities::User, Admin::UserEntity, UserStatusTooltip
Defined in:
app/helpers/users_helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_user_actions_data_attributes(user) ⇒ Object


168
169
170
171
172
173
# File 'app/helpers/users_helper.rb', line 168

def (user)
  {
    user: Admin::UserEntity.represent(user, { current_user: current_user }).to_json,
    paths: admin_users_paths.to_json
  }
end

#admin_users_data_attributes(users) ⇒ Object


4
5
6
7
8
9
# File 'app/helpers/users_helper.rb', line 4

def admin_users_data_attributes(users)
  {
    users: Admin::UserSerializer.new.represent(users, { current_user: current_user }).to_json,
    paths: admin_users_paths.to_json
  }
end

#ban_feature_available?Boolean

Returns:

  • (Boolean)

127
128
129
# File 'app/helpers/users_helper.rb', line 127

def ban_feature_available?
  Feature.enabled?(:ban_user_feature_flag)
end

#can_force_email_confirmation?(user) ⇒ Boolean

Returns:

  • (Boolean)

123
124
125
# File 'app/helpers/users_helper.rb', line 123

def can_force_email_confirmation?(user)
  !user.confirmed?
end

#confirm_user_data(user) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/users_helper.rb', line 131

def confirm_user_data(user)
  message = if user.unconfirmed_email.present?
              _('This user has an unconfirmed email address (%{email}). You may force a confirmation.') % { email: user.unconfirmed_email }
            else
              _('This user has an unconfirmed email address. You may force a confirmation.')
            end

  modal_attributes = Gitlab::Json.dump({
    title: s_('AdminUsers|Confirm user %{username}?') % { username: sanitize_name(user.name) },
    messageHtml: message,
    actionPrimary: {
      text: s_('AdminUsers|Confirm user'),
      attributes: [{ variant: 'info', 'data-qa-selector': 'confirm_user_confirm_button' }]
    },
    actionSecondary: {
      text: _('Cancel'),
      attributes: [{ variant: 'default' }]
    }
  })

  {
    path: confirm_admin_user_path(user),
    method: 'put',
    modal_attributes: modal_attributes,
    qa_selector: 'confirm_user_button'
  }
end

#current_user_menu?(item) ⇒ Boolean

Returns:

  • (Boolean)

50
51
52
# File 'app/helpers/users_helper.rb', line 50

def current_user_menu?(item)
  current_user_menu_items.include?(item)
end

#current_user_menu_itemsObject


46
47
48
# File 'app/helpers/users_helper.rb', line 46

def current_user_menu_items
  @current_user_menu_items ||= get_current_user_menu_items
end

#display_public_email?(user) ⇒ Boolean

Returns:

  • (Boolean)

175
176
177
# File 'app/helpers/users_helper.rb', line 175

def display_public_email?(user)
  user.public_email.present?
end

#impersonation_enabled?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/helpers/users_helper.rb', line 94

def impersonation_enabled?
  Gitlab.config.gitlab.impersonation_enabled
end

#load_max_project_member_accesses(projects) ⇒ Object

Used to preload when you are rendering many projects and checking access

rubocop: disable CodeReuse/ActiveRecord: `projects` can be array which also responds to pluck


57
58
59
# File 'app/helpers/users_helper.rb', line 57

def load_max_project_member_accesses(projects)
  current_user&.max_member_access_for_project_ids(projects.pluck(:id))
end

#max_project_member_access(project) ⇒ Object

rubocop: enable CodeReuse/ActiveRecord


62
63
64
# File 'app/helpers/users_helper.rb', line 62

def max_project_member_access(project)
  current_user&.max_member_access_for_project(project.id) || Gitlab::Access::NO_ACCESS
end

#max_project_member_access_cache_key(project) ⇒ Object


66
67
68
# File 'app/helpers/users_helper.rb', line 66

def max_project_member_access_cache_key(project)
  "access:#{max_project_member_access(project)}"
end

#profile_tab?(tab) ⇒ Boolean

Returns:

  • (Boolean)

31
32
33
# File 'app/helpers/users_helper.rb', line 31

def profile_tab?(tab)
  profile_tabs.include?(tab)
end

#profile_tabsObject


27
28
29
# File 'app/helpers/users_helper.rb', line 27

def profile_tabs
  @profile_tabs ||= get_profile_tabs
end

#show_status_emoji?(status) ⇒ Boolean

Returns:

  • (Boolean)

70
71
72
73
74
# File 'app/helpers/users_helper.rb', line 70

def show_status_emoji?(status)
  return false unless status

  status.message.present? || status.emoji != UserStatus::DEFAULT_EMOJI
end

#user_badges_in_admin_section(user) ⇒ Object


98
99
100
101
102
103
104
105
106
# File 'app/helpers/users_helper.rb', line 98

def user_badges_in_admin_section(user)
  [].tap do |badges|
    badges << blocked_user_badge(user) if user.blocked?
    badges << { text: s_('AdminUsers|Admin'), variant: 'success' } if user.admin?
    badges << { text: s_('AdminUsers|External'), variant: 'secondary' } if user.external?
    badges << { text: s_("AdminUsers|It's you!"), variant: 'muted' } if current_user == user
    badges << { text: s_("AdminUsers|Locked"), variant: 'warning' } if user.access_locked?
  end
end

#user_display_name(user) ⇒ Object


159
160
161
162
163
164
165
166
# File 'app/helpers/users_helper.rb', line 159

def user_display_name(user)
  return s_('UserProfile|Blocked user') if user.blocked?

  can_read_profile = can?(current_user, :read_user_profile, user)
  return s_('UserProfile|Unconfirmed user') unless user.confirmed? || can_read_profile

  user.name
end

#user_email_help_text(user) ⇒ Object


17
18
19
20
21
22
23
24
25
# File 'app/helpers/users_helper.rb', line 17

def user_email_help_text(user)
  return 'We also use email for avatar detection if no avatar is uploaded' unless user.unconfirmed_email.present?

  confirmation_link = link_to 'Resend confirmation e-mail', user_confirmation_path(user: { email: @user.unconfirmed_email }), method: :post

  h('Please click the link in the confirmation email before continuing. It was sent to ') +
    (:strong) { user.unconfirmed_email } + h('.') +
    (:p) { confirmation_link }
end

#user_internal_regex_dataObject


35
36
37
38
39
40
41
42
43
44
# File 'app/helpers/users_helper.rb', line 35

def user_internal_regex_data
  settings = Gitlab::CurrentSettings.current_application_settings

  pattern, options = if settings.user_default_internal_regex_enabled?
                       regex = settings.user_default_internal_regex_instance
                       JsRegex.new(regex).to_h.slice(:source, :options).values
                     end

  { user_internal_regex_pattern: pattern, user_internal_regex_options: options }
end

11
12
13
14
15
# File 'app/helpers/users_helper.rb', line 11

def user_link(user)
  link_to(user.name, user_path(user),
          title: user.email,
          class: 'has-tooltip commit-committer-link')
end

#user_status(user) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/users_helper.rb', line 76

def user_status(user)
  return unless user

  unless user.association(:status).loaded?
    exception = RuntimeError.new("Status was not preloaded")
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(exception, user: user.inspect)
  end

  return unless user.status

   :span,
              class: 'user-status-emoji has-tooltip',
              title: user.status.message_html,
              data: { html: true, placement: 'top' } do
    emoji_icon user.status.emoji
  end
end

#work_information(user, with_schema_markup: false) ⇒ Object


108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/helpers/users_helper.rb', line 108

def work_information(user, with_schema_markup: false)
  return unless user

  organization = user.organization
  job_title = user.job_title

  if organization.present? && job_title.present?
    render_job_title_and_organization(job_title, organization, with_schema_markup: with_schema_markup)
  elsif job_title.present?
    render_job_title(job_title, with_schema_markup: with_schema_markup)
  elsif organization.present?
    render_organization(organization, with_schema_markup: with_schema_markup)
  end
end