English 中文(简体)
Creating an admin user in Devise on Rails beta 3
原标题:

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!

最佳回答

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 =  sample@sample.com 
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.

问题回答

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:  super@test.com , first_name:  super , last_name:  admin , password:  @dmin123 , password_confirmation:  @dmin123 , role: roles[0]}])

Then run:

rake db:seed

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("admin@email.com")

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 = "admin@email.com"
    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.

@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/

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

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

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:  admin@gmail.com ,
        password:  adminpass ,
        password_confirmation:  adminpass ,
        is_admin: true
    },

    administrator: {

        username:  administrator ,
        email:  administrator@gmail.com ,
        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.





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签