Guest User

Untitled

a guest
Jul 10th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. def import
  2. # login,name,surname,email,client,organisation,city,country,role,state,site_access
  3. if request.post?
  4. errors = []
  5. @parsed_file=CSV::Reader.parse(params[:file])
  6. @parsed_file.each_with_index do |row,index|
  7.        import_vars => {
  8. :first_name => row[0],
  9. :second_name => row[1],
  10. :email => row[2],
  11. :user_name => row[3],
  12. :password => row[4],
  13. :organisation => row[5],
  14. :location_city => row[6],
  15. :country => row[7],
  16. :site_access => row[8],
  17. :expiry => row[9]
  18. }
  19.  
  20. password = Digest::SHA1.hexdigest("#{Time.now.to_s.split(//).sort_by {rand}.join}#{row[3]}")[0,7]
  21.  
  22.        user = User.new(:login => import_vars[:user_name],
  23.                :name => import_vars[:first_name],
  24. :surname => import_vars[:second_name],
  25. :email => import_vars[:email],
  26. :organisation => import_vars[:organisation],
  27. :city => import_vars[:location_city],
  28. :site_access => import_vars[:site_access],
  29. :password => password,
  30. :password_confirmation => password,
  31. :role => "User",
  32. :state => "active",
  33. :country_id = country.id if ( country = Country.find_by_name(import_vars[:country]) ),
  34. :reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ),
  35. :activated_at = Time.now)
  36.  
  37. if user.save
  38. if client = Client.find_by_name([import_vars[:organisation]])
  39. client.add_user(user)
  40. end
  41. # UserMailer.deliver_admin_user_import(user)
  42. flash[:notice] = "Users have been imported successfully"
  43. redirect_to(admin_users_path)# and return
  44. else
  45. errors << "<div class='import-error'><span class='user-name'>#{row[3]} <span class='row'>row ##{index+1}</span></span><br />#{user.errors.map {|key,value| "#{key}: #{value}<br />"}}</div>"
  46. end
  47. end
  48. @errors = "<h3>I couldn't create the following users, please check your CSV file</h3>#{errors.join('')}" unless errors.empty?
  49. flash[:error] = "Something went wrong"
  50. end
  51. end
Add Comment
Please, Sign In to add comment