You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_helper.rb 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. module UsersHelper
  19. include ApplicationHelper
  20. def users_status_options_for_select(selected)
  21. user_count_by_status = User.group('status').count.to_hash
  22. options_for_select([[l(:label_all), '']] + (User.valid_statuses.map {|c| ["#{l('status_' + User::LABEL_BY_STATUS[c])} (#{user_count_by_status[c].to_i})", c]}), selected.to_s)
  23. end
  24. def user_mail_notification_options(user)
  25. user.valid_notification_options.collect {|o| [l(o.last), o.first]}
  26. end
  27. def default_issue_query_options(user)
  28. global_queries = IssueQuery.for_all_projects
  29. global_public_queries = global_queries.only_public
  30. global_user_queries = global_queries.where(user_id: user.id).where.not(id: global_public_queries.pluck(:id))
  31. label = user == User.current ? 'label_my_queries' : 'label_default_queries.for_this_user'
  32. grouped = {
  33. l('label_default_queries.for_all_users') => global_public_queries.pluck(:name, :id),
  34. l(".#{label}") => global_user_queries.pluck(:name, :id),
  35. }
  36. grouped_options_for_select(grouped, user.pref.default_issue_query)
  37. end
  38. def default_project_query_options(user)
  39. global_queries = ProjectQuery
  40. global_public_queries = global_queries.only_public
  41. global_user_queries = global_queries.where(user_id: user.id).where.not(id: global_public_queries.ids)
  42. label = user == User.current ? 'label_my_queries' : 'label_default_queries.for_this_user'
  43. grouped = {
  44. l('label_default_queries.for_all_users') => global_public_queries.pluck(:name, :id),
  45. l(".#{label}") => global_user_queries.pluck(:name, :id),
  46. }
  47. grouped_options_for_select(grouped, user.pref.default_project_query)
  48. end
  49. def textarea_font_options
  50. [[l(:label_font_default), '']] + UserPreference::TEXTAREA_FONT_OPTIONS.map {|o| [l("label_font_#{o}"), o]}
  51. end
  52. def history_default_tab_options
  53. [[l('label_issue_history_notes'), 'notes'],
  54. [l('label_history'), 'history'],
  55. [l('label_issue_history_properties'), 'properties'],
  56. [l('label_time_entry_plural'), 'time_entries'],
  57. [l('label_associated_revisions'), 'changesets'],
  58. [l('label_last_tab_visited'), 'last_tab_visited']]
  59. end
  60. def auto_watch_on_options
  61. UserPreference::AUTO_WATCH_ON_OPTIONS.index_by {|o| l("label_auto_watch_on_#{o}")}
  62. end
  63. def change_status_link(user)
  64. url = {:controller => 'users', :action => 'update', :id => user, :page => params[:page], :status => params[:status], :tab => nil}
  65. if user.locked?
  66. link_to l(:button_unlock), url.merge(:user => {:status => User::STATUS_ACTIVE}), :method => :put, :class => 'icon icon-unlock'
  67. elsif user.registered?
  68. link_to l(:button_activate), url.merge(:user => {:status => User::STATUS_ACTIVE}), :method => :put, :class => 'icon icon-unlock'
  69. elsif user != User.current
  70. link_to l(:button_lock), url.merge(:user => {:status => User::STATUS_LOCKED}), :method => :put, :class => 'icon icon-lock'
  71. end
  72. end
  73. def additional_emails_link(user)
  74. if user.email_addresses.count > 1 || Setting.max_additional_emails.to_i > 0
  75. link_to l(:label_email_address_plural), user_email_addresses_path(@user), :class => 'icon icon-email-add', :remote => true
  76. end
  77. end
  78. def user_emails(user)
  79. emails = [user.mail]
  80. emails += user.email_addresses.order(:id).where(:is_default => false).pluck(:address)
  81. emails.map {|email| mail_to(email, nil)}.join(', ').html_safe
  82. end
  83. def user_settings_tabs
  84. tabs =
  85. [
  86. {:name => 'general', :partial => 'users/general', :label => :label_general},
  87. {:name => 'memberships', :partial => 'users/memberships', :label => :label_project_plural}
  88. ]
  89. if Group.givable.any?
  90. tabs.insert 1, {:name => 'groups', :partial => 'users/groups', :label => :label_group_plural}
  91. end
  92. tabs
  93. end
  94. def csv_content(column_name, user)
  95. case column_name
  96. when 'status'
  97. l("status_#{User::LABEL_BY_STATUS[user.status]}")
  98. when 'twofa_scheme'
  99. if user.twofa_active?
  100. l("twofa__#{user.twofa_scheme}__name")
  101. else
  102. l(:label_disabled)
  103. end
  104. else
  105. user.send(column_name)
  106. end
  107. end
  108. def users_to_csv(users)
  109. Redmine::Export::CSV.generate(:encoding => params[:encoding]) do |csv|
  110. columns = [
  111. 'login',
  112. 'firstname',
  113. 'lastname',
  114. 'mail',
  115. 'admin',
  116. 'status',
  117. 'twofa_scheme',
  118. 'created_on',
  119. 'updated_on',
  120. 'last_login_on',
  121. 'passwd_changed_on'
  122. ]
  123. user_custom_fields = UserCustomField.sorted
  124. # csv header fields
  125. csv << columns.map {|column| l('field_' + column)} + user_custom_fields.pluck(:name)
  126. # csv lines
  127. users = users.preload(:custom_values)
  128. users.each do |user|
  129. values = columns.map {|c| csv_content(c, user)} +
  130. user_custom_fields.map {|custom_field| user.custom_value_for(custom_field)}
  131. csv << values.map do |value|
  132. format_object(value, false) do |v|
  133. case v.class.name
  134. when 'Float'
  135. sprintf('%.2f', v).gsub('.', l(:general_csv_decimal_separator))
  136. else
  137. v
  138. end
  139. end
  140. end
  141. end
  142. end
  143. end
  144. end