root/platform/redmine/contrib/add_users_from_csv.rb @ 31394

Revision 31394, 1.1 kB (checked in by oinume, 5 years ago)

Added documentation

Line 
1#!/usr/bin/env ruby
2
3# Import user information from CSV.
4# Usage: ruby script/runner contrib/add_users_from_csv.rb CSV_FILE
5#
6# CSV layout (comma separated, in Shift-JIS code)
7# 1: account name for logging in
8# 2: Password (plain)
9# 3: Lastname
10# 4: Firstname
11# 5: Email
12# 6: Is administrator? (1 or 0)
13#
14# example) oinume,hoge,oi,nume,oinume@cpan.org,1
15
16require 'csv'
17require 'kconv'
18
19csv_path = ARGV.shift
20unless csv_path
21  STDERR.puts "usage: ruby script/runner contrib/add_users_from_csv.rb CSV_FILE"
22  exit 1
23end
24
25rows = []
26reader = CSV.open(csv_path, "r")
27reader.shift
28reader.each do |row|
29  if row.size != 6
30    next
31  end
32
33  columns = row.collect do |column|
34    column.kconv(Kconv::UTF8,  Kconv::SJIS)
35  end
36
37  user = User.find_by_login(columns[0])
38  unless user
39    user = User.new(:status => 1, :mail_notification => 0, :language => 'ja')
40    user.login = columns[0]
41    user.password = columns[1]
42    user.password_confirmation = columns[1]
43    user.lastname = columns[2]
44    user.firstname = columns[3]
45    user.mail = columns[4]
46    user.admin = columns[5]
47  end
48
49#  p user
50  user.save_without_validation!
51end
Note: See TracBrowser for help on using the browser.