Creating admin user in Devise on Rails beta 3

Well, I probably will feel completely dumb if someone answers this question with a simple thing that I will miss, but ... here goes:

I have a new application on rails 3 beta and I use the program for authentication. I ran all the comments, and at the moment everything is working fine. I created the user role and the administrator role (following these instructions: https://github.com/plataformatec/devise/wiki/How-To:-Add-an-Admin-Role ) and I registered as the first user, but as I register or create an administrator role user? The guides from the created guys set up the administrator role so as not to register, but I'm not sure how you should create an administrator if you cannot register?

Any help would be appreciated! Thank!

+44
ruby-on-rails devise
Apr 25 2018-10-25T00:
source share
6 answers

Yeah. I feel stupid.

If anyone else has the same dark 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 

It will be done. Now just go to your admin sign and log in.

+90
May 02 '10 at 8:15
source share

What you are really trying to do is create seed data. A more standard way to do this is to add your seed users (and roles if you store 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 
+13
Jun 08 2018-12-12T00:
source share

This may not apply to Devise (but I suppose this will happen), but in general, if you want to sow the admin user, but don’t want to keep 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 a user locally with the password you want to find password_digest.

+5
Sep 12 2018-11-12T00:
source share

@Stewart You are right. Using the admin flag in the user model is acceptable and can coexist with many authorization options. Take a look at the Ability class in 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 the authorization requirements, such as adding a subdomain to authkeys, are different.

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

+3
Nov 08 '10 at
source share

try adding / sign _in to your admin path no matter what you installed it ... mine is

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

0
Apr 26 '10 at 17:47
source share

There is a convenient way to populate tables - db / seed.rb. Just add a script to create users in it and execute:

 rake db:seed 

Below you can see an example of the User model with the 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 

Please note that validation methods apply here.

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

0
Jul 03 '14 at 21:20
source share



All Articles