Bulk Change Usernames


#1

Hi All,

I am running Discourse for my classes at a school. I have invited all the students (via the bulk CSV upload utility) but would now like to bulk change all their usernames to be their real names (rather than their chosen pseudonyms) so everyone knows who they are talking to on the forum.

Their email addresses are their school email addresses, so I can use those as a primary key (of sorts) to change their usernames. Is there a way to do this, I am guessing through SSH on the command line?

Also, is there a way to then stop the users from changing their usernames back to something else?

Thanks!


(Jeff Atwood) #2

Why not populate the full name field with the real names and keep the usernames as is?


#3

Nice idea. Any suggestions on how to do that in bulk (e.g. with a CSV file)? There’s a lot of students…


(Jeff Atwood) #4

Did you want to keep the existing usernames, are they good usernames that people actually selected vs. being autogenerated?


#5

Not really, they are nicknames and it’s difficult to tell who is who on the forum (a bit of a problem in a school) so I would prefer to get rid of them all and refresh them with their real names. It’s a private forum only used internally so it’s not a problem using real names.


(Jeff Atwood) #6

I still recommend having both and turning full name display on in the site settings. Usernames won’t “look” like real names as they are limited to 20 chars and no spaces.


#7

Makes sense. So, how would I go about doing this in bulk?


(Jeff Atwood) #8

I can’t think of any other way than writing a little Ruby code in the Docker container to loop across all users, look up email addresses, then change the fullname field for each one that has a matching lookup.


#9

Thanks, that was what I was thinking (fearing!!) too. Do you have any sample code to point me in the right direction? Ruby isn’t my forte, but I can understand/modify code if given something to work with.


(Dean Taylor) #10

It’s possible to do this in JavaScript in the browser console - but that’s a slower, more complex code route.


(Kane York) #11

Here’s sample code that would turn everyone’s Full name into their username in ALL CAPS.

# ./launcher enter app
# rails c

User.real.all.each do |user|
   user.name = user.username.upcase
   user.save
end

#12

Thanks for that. Any ideas about how to import a CSV (or other structured list) of names into the Full Name field, using the email address as a key for the import?


(Kane York) #13

Drop the csv file in /var/discourse/shared/standalone/, then:

data = {}
require 'csv'
CSV.foreach("/shared/names.csv") do |row|
   email = row[0]
   name = row[1]
   data[email] = name
end

User.all.each do |user|
   unless data[user.email].nil?
      user.name = data[user.email]
      user.save
   end
end

#14

I am finally now getting around to running this script, but I have (what is probably) a stupidly simple question. How do I open the rails console to run this script?


(Mittineague) #15

You missed seeing the


#16

Thanks, but when I run the first command I get the following:

-bash: ./launcher: No such file or directory

I am logged in as root to the server. Do I need to switch to the discourse user (does that still exist? I have a docker installation of Discourse).


#17

You need to be inside /var/discourse. The launcher file is in there.


#19

Thanks, that worked great. Quick question - how would I go about loading the script from a file, instead of pasting it into the interactive Ruby console? I put the code into a file called fullnames.rb in the shared\standalone directory and then ran the following in /var/discourse:

ruby shared/fullnames.rb

but the following error appears:

shared/fullnames.rb:9:in `<main>': uninitialized constant User (NameError)

#20

You are not running that in the discourse container.
/var/discourse/ only exists on the host (not on the container).