summaryrefslogtreecommitdiff
path: root/lib/tasks/csv_user_import.rake
blob: 81959d55501eec9ec3e3441f3f223c1f5941e177 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
namespace :user_import do
  desc "Import users from a CSV file."
  task :csv => :environment do
    require 'csv'
    require 'digest/md5'

    # Example CSV format in the file:
    # UserName,LastName,FirstName,AcademicTitel,PhoneOffice,VoipNr1,VoipNr2,VoipNr3,VoipTelco,CellPhone,HardFax,SoftFax,Email,PIN
    # 123456,Mustermann,Max,Dr.,+49 123 1001234,20,21,22,23,+49 160 12345678,+49 123 1001238,29,max.mustermann@example.com,1324

    # A generic hook to manipulate each given phone number with Ruby tools.
    #
    def regex_convert_phone_numbers(phone_number = nil)
      if phone_number.class == String && !phone_number.blank?
        # 123 Example
        #
        # if !phone_number.match(/^123(..)$/).nil?
        #   phone_number = phone_number.gsub(/^123(..)$/,'123'+$1)
        # end
      end

      phone_number
    end

    # Read the CSV data and store them in the new_users hash.
    #
    csv_data    = CSV.read(GsParameter.get('IMPORT_CSV_FILE'), encoding: GsParameter.get('IMPORT_CSV_ENCODING'))
    headers     = csv_data.shift.map {|i| i.to_s }
    string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
    new_users   = string_data.map {|row| Hash[*headers.zip(row).flatten] }
    gs_node_id  = GsNode.where(:ip_address => GsParameter.get('HOMEBASE_IP_ADDRESS')).first.try(:id)

    if File.exists?(GsParameter.get('DOUBLE_CHECK_POSITIVE_USERS_CSV'))
      csv_data    = CSV.read(GsParameter.get('DOUBLE_CHECK_POSITIVE_USERS_CSV'), encoding: GsParameter.get('IMPORT_CSV_ENCODING'))
      if csv_data.blank?
        double_checked_user_names = []
      else
        headers     = csv_data.shift.map {|i| i.to_s }
        string_data = csv_data.map {|row| row.map {|cell| cell.to_s } }
        double_check_positiv_users = string_data.map {|row| Hash[*headers.zip(row).flatten] }

        double_checked_user_names = double_check_positiv_users.map{|user| user['UserName']}
      end
    else
      double_checked_user_names = new_users.map{|user| user['UserName']}
    end

    tenant = Tenant.find(GsParameter.get('DEFAULT_API_TENANT_ID'))

    # Destroy deleted user by making a diff of where(:importer_checksum)
    # and users in the CSV file.
    #
    if defined?(GsParameter.get('USER_NAME_PREFIX')) && !GsParameter.get('USER_NAME_PREFIX').blank?
      new_users_user_names = new_users.map{|x| GsParameter.get('USER_NAME_PREFIX').to_s + x['UserName'].to_s}
    else
      new_users_user_names = new_users.map{|x| x['UserName']}
    end
    csv_imported_user_names_in_the_database = User.where('importer_checksum != ?', '').pluck(:user_name)


    to_be_destroyed_user_names = csv_imported_user_names_in_the_database - new_users_user_names

    User.where(:user_name => to_be_destroyed_user_names, :gs_node_id => gs_node_id).destroy_all

    # Loop through all entries in the CSV file.
    #
    new_users.each do |csv_user|
      if !(csv_user['UserName'].blank? || csv_user['Email'].blank?) && double_checked_user_names.include?(csv_user['UserName'])
        csv_user['Email'] = csv_user['Email'].downcase
        if defined?(GsParameter.get('USER_NAME_PREFIX')) && !GsParameter.get('USER_NAME_PREFIX').blank?
          csv_user['UserName'] = GsParameter.get('USER_NAME_PREFIX').to_s + csv_user['UserName']
        end

        md5_sum = Digest::MD5.hexdigest(csv_user.to_yaml)
        user = nil

        # Check if this user already exists and has a changed checksum.
        #
        if tenant.users.where(:user_name => csv_user['UserName']).first.try(:importer_checksum).to_s != md5_sum.to_s
          # Find or create the user
          #
          if tenant.users.where(:user_name => csv_user['UserName']).count > 0
            user = tenant.users.where(:user_name => csv_user['UserName']).first
            if defined? IMPORT_IGNORE_PIN_ON_UPDATE && IMPORT_IGNORE_PIN_ON_UPDATE == true
              user.update_attributes(
                :last_name             => csv_user['LastName'],
                :first_name            => csv_user['FirstName'],
                :email                 => csv_user['Email'],
                :importer_checksum     => md5_sum,
                :gs_node_id            => gs_node_id,
              )
            else
              user.update_attributes(              
                :last_name             => csv_user['LastName'],
                :first_name            => csv_user['FirstName'],
                :email                 => csv_user['Email'],
                :new_pin               => csv_user['PIN'],
                :new_pin_confirmation  => csv_user['PIN'],
                :password              => csv_user['PIN'],
                :password_confirmation => csv_user['PIN'],
                :importer_checksum     => md5_sum,
                :gs_node_id            => gs_node_id,
              )
            end
          else
            if csv_user['PIN'].blank?
              csv_user['PIN'] = (1..6).map{|i| (0 .. 9).to_a.sample}.join
            end
            user = tenant.users.create(
              :user_name                          => csv_user['UserName'],
              :last_name                          => csv_user['LastName'],
              :first_name                         => csv_user['FirstName'],
              :email                              => csv_user['Email'],
              :new_pin                            => csv_user['PIN'],
              :new_pin_confirmation               => csv_user['PIN'],
              :password                           => csv_user['PIN'],
              :password_confirmation              => csv_user['PIN'],
              :language_id                        => tenant.language_id,
              :importer_checksum                  => md5_sum,
              :gs_node_id                         => gs_node_id,
              :send_voicemail_as_email_attachment => true,
            )
          end

          # Create group membership
          if tenant.user_groups.exists?(:name => 'Users')
            tenant.user_groups.where(:name => 'Users').first.user_group_memberships.create(:user => user)
          end
        end

        if user
          # Find or create a sip_accounts
          ['VoipNr1', 'VoipNr2', 'VoipNr3'].each_with_index do |phone_number_type, index|
            
            if index > 0 
              auth_name = "#{csv_user['UserName']}_#{index}"
            else
              auth_name = csv_user['UserName'].to_s
            end

            if !phone_number_type.blank? && !csv_user[phone_number_type].blank?
              sip_account = user.sip_accounts.where(:auth_name => auth_name).first
              if sip_account
                if sip_account.caller_name.to_s != user.to_s
                  sip_account.update_attributes(:caller_name => user.to_s)
                end
              else
                sip_account = user.sip_accounts.create(
                  :caller_name   => user.to_s,
                  :voicemail_pin => csv_user['PIN'],
                  :auth_name     => auth_name,
                  :password      => csv_user['PIN'],
                  :clip          => true,
                  :hotdeskable   => true,
                  :callforward_rules_act_per_sip_account => true,
                  :gs_node_id    => gs_node_id,
                  )
              end

              phone_numbers = Array.new()
              phone_numbers.push(csv_user[phone_number_type].to_s.gsub(/[^0-9\+]/, ''))

              # Find or create phone numbers
              converted_phone_number = regex_convert_phone_numbers(csv_user[phone_number_type])
              if converted_phone_number != csv_user[phone_number_type]
                phone_numbers.push(converted_phone_number.gsub(/[^0-9\+]/, ''))
              end
              phone_numbers_count = sip_account.phone_numbers.count
              phone_numbers.each do |phone_number_number|
                phone_number = sip_account.phone_numbers.where(:number => phone_number_number).first
                if !phone_number
                  phone_number = sip_account.phone_numbers.create(:number => phone_number_number, :gs_node_id => gs_node_id)
                end
              end

              # Create default call forwarding entries
              if phone_numbers_count < sip_account.phone_numbers.count
                call_forward_case_offline = CallForwardCase.find_by_value('offline')
                if call_forward_case_offline
                  sip_account.phone_numbers.first.call_forwards.create(:call_forward_case_id => call_forward_case_offline.id, :call_forwardable_type => 'Voicemail', :active => true, :depth => GsParameter.get('DEFAULT_CALL_FORWARD_DEPTH'))
                end
              end
            else
              user.sip_accounts.where(:auth_name => auth_name).destroy_all
            end
          end

          if !csv_user['SoftFax'].blank?
            phone_numbers = Array.new()
            phone_numbers.push(csv_user['SoftFax'].to_s.gsub(/[^0-9\+]/, ''))
            converted_phone_number = regex_convert_phone_numbers(csv_user['SoftFax'])
            if converted_phone_number != csv_user['SoftFax']
              phone_numbers.push(converted_phone_number.gsub(/[^0-9\+]/, ''))
            end

            fax_account = user.fax_accounts.first
            if fax_account
              if fax_account.name != user.to_s || fax_account.email != user.email
                fax_account.update_attributes(:name => user.to_s, :email => user.email)
              end
            else
              fax_account = user.fax_accounts.create(
                :name => user.to_s,
                :station_id => converted_phone_number.gsub(/[^0-9\+]/,''),
                :email => user.email,
                :days_till_auto_delete => 90,
                :retries => 3,
              )
            end

            if fax_account
              fax_account.phone_numbers.each do |phone_number|
                if !phone_numbers.include?(phone_number.number)
                  phone_number.delete
                end
              end
              phone_numbers.each do |phone_number_number|
                phone_number = fax_account.phone_numbers.where(:number => phone_number_number).first
                if !phone_number
                  phone_number = fax_account.phone_numbers.create(:number => phone_number_number)
                end
              end
            end
          else
            user.fax_accounts.destroy_all
          end

          if !csv_user['HardFax'].blank?
            phone_numbers = Array.new()
            phone_numbers.push(csv_user['HardFax'].to_s.gsub(/[^0-9\+]/, ''))
            converted_phone_number = regex_convert_phone_numbers(csv_user['HardFax'])
            if converted_phone_number != csv_user['HardFax']
              phone_numbers.push(converted_phone_number.gsub(/[^0-9\+]/, ''))
            end

            # Create a sip_account for a hardware fax.
            #
            fax_sip_account = user.sip_accounts.where(:description => 'Hardware-Fax').first
            if fax_sip_account
              if fax_sip_account.caller_name != "Hardware Fax of #{user.to_s}"
                fax_sip_account.update_attributes(:caller_name => "Hardware Fax of #{user.to_s}")
              end
            else
              fax_sip_account = user.sip_accounts.create(
                :caller_name => "Hardware Fax of #{user.to_s}",
                :description => 'Hardware-Fax',
                :auth_name     => 'HARDFAX' + csv_user['UserName'],
                :password      => csv_user['PIN'],
                :clip          => true,
                :hotdeskable   => false,
                :callforward_rules_act_per_sip_account => true,
                :gs_node_id    => gs_node_id,
              )
            end

            if fax_sip_account
              fax_sip_account.phone_numbers.each do |phone_number|
                if !phone_numbers.include?(phone_number.number)
                  phone_number.delete
                end
              end
              phone_numbers.each do |phone_number_number|
                phone_number = fax_sip_account.phone_numbers.where(:number => phone_number_number).first
                if !phone_number
                  phone_number = fax_sip_account.phone_numbers.create(:number => phone_number_number)
                end
              end
            end
          else
            user.sip_accounts.where(:description => 'Hardware-Fax').destroy_all
          end

          if !csv_user['VoipTelco'].blank?
            conference = user.conferences.first
            if conference.nil?
              # Create a conference room for this user.
              #
              conference = user.conferences.build
              conference.name = "Default Conference for #{user.to_s}"
              conference.start = nil
              conference.end = nil
              conference.open_for_anybody = true
              conference.max_members = GsParameter.get('DEFAULT_MAX_CONFERENCE_MEMBERS')
              conference.pin = (1..GsParameter.get('MINIMUM_PIN_LENGTH')).map{|i| (0 .. 9).to_a.sample}.join
              conference.save
            end

            phone_numbers = Array.new()
            phone_numbers.push(csv_user['VoipTelco'].to_s.gsub(/[^0-9\+]/, ''))
            converted_phone_number = regex_convert_phone_numbers(csv_user['VoipTelco'])
            if converted_phone_number != csv_user['VoipTelco']
              phone_numbers.push(converted_phone_number.gsub(/[^0-9\+]/, ''))
            end

            if conference
              conference.phone_numbers.each do |phone_number|
                if !phone_numbers.include?(phone_number.number)
                  phone_number.delete
                end
              end
              phone_numbers.each do |phone_number_number|
                phone_number = conference.phone_numbers.where(:number => phone_number_number).first
                if !phone_number
                  phone_number = conference.phone_numbers.create(:number => phone_number_number)
                end
              end
            end
          else
            user.conferences.destroy_all
          end

          # Create Whitelist Entry for default Callthrough
          #
          cell_phone_number = csv_user['CellPhone'].to_s.gsub(/[^0-9\+]/, '')

          if !cell_phone_number.blank?
            callthrough = tenant.callthroughs.find_or_create_by_name(GsParameter.get('CALLTHROUGH_NAME_TO_BE_USED_FOR_DEFAULT_ACTIVATION'))

            access_authorization = callthrough.access_authorizations.find_or_create_by_name('Cellphones')

            new_phone_number = access_authorization.phone_numbers.find_or_create_by_number(cell_phone_number, :name => user.to_s, :access_authorization_user_id => user.id)
          end
        end
      else
        # puts "#{csv_user['UserName']} (#{csv_user['Email']}) has not changed."
      end
    end


  end
end