Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def import
- # login,name,surname,email,client,organisation,city,country,role,state,site_access
- if request.post?
- errors = []
- @parsed_file=CSV::Reader.parse(params[:file])
- @parsed_file.each_with_index do |row,index|
- import_vars => {
- :first_name => row[0],
- :second_name => row[1],
- :email => row[2],
- :user_name => row[3],
- :password => row[4],
- :organisation => row[5],
- :location_city => row[6],
- :country => row[7],
- :site_access => row[8],
- :expiry => row[9]
- }
- password = Digest::SHA1.hexdigest("#{Time.now.to_s.split(//).sort_by {rand}.join}#{row[3]}")[0,7]
- user = User.new(:login => import_vars[:user_name],
- :name => import_vars[:first_name],
- :surname => import_vars[:second_name],
- :email => import_vars[:email],
- :organisation => import_vars[:organisation],
- :city => import_vars[:location_city],
- :site_access => import_vars[:site_access],
- :password => password,
- :password_confirmation => password,
- :role => "User",
- :state => "active",
- :country_id = country.id if ( country = Country.find_by_name(import_vars[:country]) ),
- :reset_code = Digest::SHA1.hexdigest( Time.now.to_s.split(//).sort_by {rand}.join ),
- :activated_at = Time.now)
- if user.save
- if client = Client.find_by_name([import_vars[:organisation]])
- client.add_user(user)
- end
- # UserMailer.deliver_admin_user_import(user)
- flash[:notice] = "Users have been imported successfully"
- redirect_to(admin_users_path)# and return
- else
- 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>"
- end
- end
- @errors = "<h3>I couldn't create the following users, please check your CSV file</h3>#{errors.join('')}" unless errors.empty?
- flash[:error] = "Something went wrong"
- end
- end
Add Comment
Please, Sign In to add comment