Create users from script/console
Added by Hans Fangohr over 15 years ago
Dear all, (and in particular redmine developers and more experience redmine users),
because redmine is such a useful tool, we like to use it with a large group of users.
We have already a list of user names, emails and corresponding logins that all need to become users of the redmine installation. I was hoping we could write a (ruby) script that creates Users, rather than having to enter them individually via the Administration Web interface.
Using the console, we can see User entries, for example for the first user (which I guess is always admin):
redmine@eta:~/0.8-stable$ script/console production Loading production environment (Rails 2.1.2) >> User.all[0] => #<User id: 1, login: "admin", hashed_password: "6fba37452643c5e<snip>7087af1d", firstname: "First", lastname: "Administrator", mail: "some_email@somewhere", mail_notification: true, admin: true, status: 1, last_login_on: "2009-09-12 21:46:59", language: "en", auth_source_id: nil, created_on: "2009-07-30 06:49:03", updated_on: "2009-09-12 21:46:59", type: "User"> >>
Is it possible to create users from the console? I was hoping something like:
>> bob = User.new({:login => "Bob", :firstname => "Test", :lastname=>"Dummy",:mail=>"test@testing.test"})
might do the trick, but judging by the output:
=> #<User id: nil, login: "", hashed_password: "", firstname: "Test", lastname: "Dummy", mail: "test@testing.test", mail_notification: true, admin: false, status: 1, last_login_on: nil, language: "", auth_source_id: nil, created_on: nil, updated_on: nil, type: nil>
it is not quite right.
Could anybody point me to a working piece of code for this (or similar), or some documentation? Or tell me that this is impossible?
I have checked the manual, howtos, faqs, and searched the forum with no success yet.
Many thanks,
Hans
Output from script/about:
Ruby version 1.8.7 (i486-linux)
RubyGems version 1.2.0
Rails version 2.1.2
Active Record version 2.1.2
Action Pack version 2.1.2
Active Resource version 2.1.2
Action Mailer version 2.1.2
Active Support version 2.1.2
Application root /home/redmine/0.8-stable
Environment development
Database adapter mysql
Replies (8)
RE: Create users from script/console - Added by Eric Davis over 15 years ago
Some of the fields on User are protected so they can't be mass assigned (e.g. from the Hash of attributes you passed to User.new
). This is done for security, but you can assign values to them individually.
user = User.new({:firstname => "Test", :lastname=>"Dummy",:mail=>"test@testing.test"})
user.login = 'Bob'
user.password = 'password'
user.password_confirmation = 'password'
user.valid? # will return true if the user is valid
user.save
user.id # will be set to an integer automatically when saved.
If user.valid?
returns false, run puts user.errors.full_messages
to print out the errors that need to be fixed on the user. Let me know if you have any other questions, I've done a lot of bulk record creation in Redmine.
Eric Davis
RE: Create users from script/console - Added by Hans Fangohr over 15 years ago
Hi Eric,
fantastic-- just the information I needed; thank you for the prompt, concise and accurate reply. Your example above works flawlessly:
>> redmine@eta:~/0.8-stable$ script/console production Loading production environment (Rails 2.1.2) >> user = User.new({:firstname => "Test", :lastname=>"Dummy",:mail=>"test@testing.test"}) => #<User id: nil, login: "", hashed_password: "", firstname: "Test", lastname: "Dummy", mail: "test@testing.test", mail_notification: true, admin: false, status: 1, last_login_on: nil, language: "", auth_source_id: nil, created_on: nil, updated_on: nil, type: nil> >> user.login = 'Bob' => "Bob" >> user.password = 'password' => "password" >> user.password_confirmation = 'password' => "password" >> user.valid? => true >> user.save => true >> user.id => 14
I would like these users to use an LDAP authentication mechanism, which is the first LDAP source in the redmine installation (at least this is my guess, the relevant entry of user.auth_source_id
is 1
for manually entered users. So I have tried
>> bob.auth_source_id
=> nil
>> bob.auth_source_id=1
=> 1
>> bob.save
=> true
and this seems to work, too.
What I couldn't solve is this:
I'd like Bob to be a 'Developer' for project 3, and a 'Reporter' for project '4'. Just setting [3, 4]
to user.project_ids
fails, presumably because I haven't defined Bob's role for these projects.
Would you have an example for setting the project and role as well?
Many thanks,
Hans
RE: Create users from script/console - Added by Eric Davis about 15 years ago
You will need to create a Member for each user/project. It tracks which Roles each use has. Something like this should work
member = Member.new
member.user = User.find_by_login('Bob')
member.project = Project.find(3)
member.role = Role.find_by_name('Developer')
member.save
(I'm on a newer version where the API changed, but this should work on Redmine 0.8)
Eric Davis
RE: Create users from script/console - Added by Hans Fangohr about 15 years ago
Hi Eric,
that's great -- we got it all to work now.
Many thanks,
Hans
RE: Create users from script/console - Added by Anthony Frost almost 15 years ago
(I'm on a newer version where the API changed, but this should work on Redmine 0.8)
This gives me "undefined method 'role='" on 0.8.7.devel, do I need to be using the newer API and if so could you give me a sample please? I've got 80-something users to add to 5 projects and I really don't want to do them all by hand if I can help it!
Anthony
RE: Create users from script/console - Added by Eric Davis almost 15 years ago
Anthony Frost wrote:
(I'm on a newer version where the API changed, but this should work on Redmine 0.8)
This gives me "undefined method 'role='" on 0.8.7.devel, do I need to be using the newer API and if so could you give me a sample please?
Yes, in the latest 0.9 and trunk version a Member can have multiple roles. Change my line 4 above to:
member.roles = [Role.find_by_name('Developer')]
Eric Davis
RE: Create users from script/console - Added by Anthony Frost almost 15 years ago
Eric Davis wrote:
Yes, in the latest 0.9 and trunk version a Member can have multiple roles. Change my line 4 above to:
Wonderful, works a treat.
Next step: Persuade the rest of the College staff to use it.
RE: Create users from script/console - Added by Pavel Potcheptsov over 11 years ago
I need to delete Redmine's role Role1 but save all members with this role in projects, so I need to move all members from Role1 to Role2.
How to do this?