Importing users from an external database

I have a list of users from an external database which i need to import into discourse and activate them. How should i do this?

You could feed the emails into the invites tab on your user page.

And if you don’t want a one-off import, but a continuous “sync”, search for SSO :slight_smile:

2 Likes

For those interested, here’s how I did it:

desc "Import users from a CSV file"
task :import, [:csv_file] => [:environment] do |_, args|
  cnt = 0
  CSV.foreach(args[:csv_file], col_sep: ';', headers: true) do |new_user|
    cnt = cnt + 1
    new_user = define_user(new_user, cnt)
    u = User.new({
      username: new_user['username']
      email: new_user['email'],
      password: SecureRandom.hex,
      name: new_user['name'],
      title: new_user['title'],
      approved: true,
      approved_by_id: -1,
      trust_level: 1
    })
    u.import_mode = true
    u.groups = parse_user_groups new_user['groups']

    begin
      u.save!
    rescue => e
      puts "NOK|#{cnt}|#{new_user['name']}|#{new_user['username']}|#{new_user['email']}|#{new_user['groups']} => #{e.message}"
    else
      token = ActiveRecord::Base.connection.execute("select * from email_tokens where user_id=#{u.id}")
      puts "OK |#{cnt}|#{new_user['name']}|#{new_user['username']}|#{new_user['email']}|#{new_user['groups']}|#{token[0]['token']}"
    end
end
2 Likes