Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

user_import.rb 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 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. class UserImport < Import
  19. AUTO_MAPPABLE_FIELDS = {
  20. 'login' => 'field_login',
  21. 'firstname' => 'field_firstname',
  22. 'lastname' => 'field_lastname',
  23. 'mail' => 'field_mail',
  24. 'language' => 'field_language',
  25. 'admin' => 'field_admin',
  26. 'auth_source' => 'field_auth_source',
  27. 'password' => 'field_password',
  28. 'must_change_passwd' => 'field_must_change_passwd',
  29. 'status' => 'field_status'
  30. }
  31. def self.menu_item
  32. :users
  33. end
  34. def self.layout
  35. 'admin'
  36. end
  37. def self.authorized?(user)
  38. user.admin?
  39. end
  40. # Returns the objects that were imported
  41. def saved_objects
  42. User.where(:id => saved_items.pluck(:obj_id)).order(:id)
  43. end
  44. def mappable_custom_fields
  45. UserCustomField.all
  46. end
  47. private
  48. def build_object(row, item)
  49. object = User.new
  50. attributes = {
  51. :login => row_value(row, 'login'),
  52. :firstname => row_value(row, 'firstname'),
  53. :lastname => row_value(row, 'lastname'),
  54. :mail => row_value(row, 'mail')
  55. }
  56. lang = nil
  57. if language = row_value(row, 'language')
  58. lang = find_language(language)
  59. end
  60. attributes[:language] = lang || Setting.default_language
  61. if admin = row_value(row, 'admin')
  62. if yes?(admin)
  63. attributes['admin'] = '1'
  64. end
  65. end
  66. if auth_source_name = row_value(row, 'auth_source')
  67. if auth_source = AuthSource.find_by(:name => auth_source_name)
  68. attributes[:auth_source_id] = auth_source.id
  69. end
  70. end
  71. if password = row_value(row, 'password')
  72. object.password = password
  73. object.password_confirmation = password
  74. end
  75. if must_change_passwd = row_value(row, 'must_change_passwd')
  76. if yes?(must_change_passwd)
  77. attributes[:must_change_passwd] = '1'
  78. end
  79. end
  80. if status_name = row_value(row, 'status')
  81. if status = User::LABEL_BY_STATUS.key(status_name)
  82. attributes[:status] = status
  83. end
  84. end
  85. attributes['custom_field_values'] = object.custom_field_values.each_with_object({}) do |v, h|
  86. value =
  87. case v.custom_field.field_format
  88. when 'date'
  89. row_date(row, "cf_#{v.custom_field.id}")
  90. else
  91. row_value(row, "cf_#{v.custom_field.id}")
  92. end
  93. if value
  94. h[v.custom_field.id.to_s] = v.custom_field.value_from_keyword(value, object)
  95. end
  96. end
  97. object.send(:safe_attributes=, attributes, user)
  98. object
  99. end
  100. def extend_object(row, item, object)
  101. Mailer.deliver_account_information(object, object.password) if yes?(settings['notifications'])
  102. end
  103. end