45

Ok, I'm probably going to feel quite dumb when someone answers this one with a simple thing that I'm missing but... here goes:

I've got a brand new app on rails 3 beta and I'm using devise for the authentication. I've run all the comments and everything is working perfectly at the moment. I've created a user role and an admin role (following these instructions: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role) and I've registered myself as the first user but how to do I register or create an admin role user? The directions from the devise guys setup the admin role to not be registerable but I'm unsure how you're supposed to create the admin if you can't register?!

Any help would be appreciated! Thanks!

6 Answers 6

95

Yup. I feel dumb.

If anyone else is having a similarly vapid moment. Just use the rails console to create the admin user:

➡ rails c
Loading development environment (Rails 3.0.0.beta3)
irb(main):001:0> admin = Admin.create! do |u|
irb(main):002:1* u.email = '[email protected]'
irb(main):003:1> u.password = 'password'
irb(main):004:1> u.password_confirmation = 'password'
irb(main):005:1> end

That will do it. Now just visit your admin sign in path and sign in.

4
  • 23
    The vote up was not referring to the first sentence of your answer.
    – lbz
    May 30, 2010 at 13:39
  • 3
    In reality, how much of a rails site is actually managed using the rails console? You know, those minor things, a flag field here and there?
    – Astra
    Jan 23, 2011 at 5:04
  • Probably a terrifying amount is managed through the console. I know I use it all the time. But I guess we should be thanking @dhh for creating such a great way to access our Rails apps. Oct 31, 2011 at 16:05
  • As of latest Devise and ActiveAdmin, use AdminUser.create! instead of Admin.create! Thanks for the answer!
    – randomor
    Dec 23, 2012 at 17:19
14

What you are really trying to do is create seed data. A more standard way to do this would be to add your seed users (and roles, if you are storing them) to db/seeds.rb

For exmaple in db/seeds.rb:

roles = Role.create([{name: 'super_admin'}, {name: 'staff'}, {name:'customer'}])
users = User.create([{email: '[email protected]', first_name: 'super', last_name: 'admin', password: '@dmin123', password_confirmation: '@dmin123', role: roles[0]}])

Then run:

rake db:seed
1
  • 1
    This is the proper way to do it.
    – kingsfoil
    Jul 22, 2014 at 20:16
5

This may not apply to Devise (but I believe it will), but in general if you want to seed an admin user but don't want to store your admin password in source control, you can do something like this...

@user = User.find_by_email("[email protected]")

unless @user
  # We are going to bypass both our assignment protection and validation
  # so we aren't storing the password in source control.
  #
  # This doesn't replace the need to change the password occasionaly, both
  # on the site and in source control.
  @user = User.create do |u|
    u.name = "Admin User"
    u.email = "[email protected]"
    u.password_digest = "$2a$10$DUv/IUiLB34jhi3j4Z8MwwcaDlBmFe3rvcdXSzPKLzBOAMmD53UqW"
  end

  @user.save(:validate => false)

  # TODO make the user an admin
end

You can create the user locally with the password you want to find the password_digest.

1
  • does devise use md5 to digest password?
    – fatman13
    Apr 7, 2014 at 12:59
3

@Stewart You are correct. Using an admin flag in the user model is acceptable and can still co-exist with many authorization options. Take a look at the Ability class in the cancan docs for an example of how this might look:

def initialize(user)
  if user.admin?
    can :manage, :all
  else
    can :read, :all
  end
end

Having multiple authorization models can be useful if the functionality is really different or if the requirements for authorization, such as adding subdomain to the authkeys, is different.

Another approach is to add a HABTM roles relationship to your user. Here is a nice tutorial by Tony Amoyal: http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/

0

try appending /sign_in to your admin path, whatever you set it to...mine is

http://yoursite.com/admin/sign_in?unauthenticated=true

2
  • oops you likely didn't use :authenticable. Try this page for help on overriding the admin views: wiki.github.com/fortuity/subdomain-authentication/…
    – kinet
    Apr 26, 2010 at 19:31
  • No, I didn't use :authenticatable when setting up the admin model with devise, like I said, I was using the tutorial that divise has on their github page. I would prefer not to use subdomains if I can help it... Apr 27, 2010 at 15:04
0

There is convenient way for populating tables - db/seed.rb file. Just add the script for creating users in it and run:

rake db:seed

Below you can see example of User model with email and username fields:

# Inserting default security users
users = {

    admin: {

        username: 'admin',
        email: '[email protected]',
        password: 'adminpass',
        password_confirmation: 'adminpass',
        is_admin: true
    },

    administrator: {

        username: 'administrator',
        email: '[email protected]',
        password: 'administrator',
        password_confirmation: 'administrator',
        is_admin: true
    }
}

users.each do |user, data|

  user = User.new(data)

  unless User.where(email: user.email).exists?
    user.save!
  end
end

Note, that devise validations are applied here.

Here you can find more examples of using the seed.rb file and here is the rayn's rails cast.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.