Ruby-on-rails 在 Devise on Rails beta 3 中创建管理员用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2708417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an admin user in Devise on Rails beta 3
提问by erskingardner
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?!
我在 rails 3 beta 上有一个全新的应用程序,我正在使用 devise 进行身份验证。我已经运行了所有评论,目前一切正常。我已经创建了一个用户角色和一个管理员角色(按照以下说明:https: //github.com/plataformatec/devise/wiki/How-To: -Add-an-Admin- Role)并且我已经将自己注册为第一个用户,但如何注册或创建管理员角色用户?设计人员的指示将管理员角色设置为不可注册,但如果您无法注册,我不确定您应该如何创建管理员?!
Any help would be appreciated! Thanks!
任何帮助,将不胜感激!谢谢!
回答by erskingardner
Yup. I feel dumb.
是的。我觉得很蠢。
If anyone else is having a similarly vapid moment. Just use the rails console to create the admin user:
如果其他人也有类似乏味的时刻。只需使用 rails 控制台创建管理员用户:
? 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.
这样就可以了。现在只需访问您的管理员登录路径并登录。
回答by paneer_tikka
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
您真正想做的是创建种子数据。一个更标准的方法是将你的种子用户(和角色,如果你存储它们)添加到 db/seeds.rb
For exmaple in db/seeds.rb:
例如在 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
回答by Lelon
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...
这可能不适用于 Devise(但我相信它会),但一般来说,如果您想为管理员用户设置种子但不想将您的管理员密码存储在源代码管理中,您可以执行以下操作...
@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 = "a$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.
您可以使用您要查找的密码在本地创建用户 password_digest。
回答by genkilabs
@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:
@斯图尔特你是对的。在用户模型中使用 admin 标志是可以接受的,并且仍然可以与许多授权选项共存。查看 cancan 文档中的 Ability 类,以了解其外观示例:
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.
如果功能确实不同,或者如果授权要求(例如向 authkeys 添加子域)不同,则拥有多个授权模型会很有用。
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/
另一种方法是向您的用户添加 HABTM 角色关系。这是 Tony Amoyal 的一个不错的教程:http: //www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/
回答by kinet
try appending /sign_in to your admin path, whatever you set it to...mine is
尝试将 /sign_in 附加到您的管理路径,无论您将其设置为...我的是
回答by gotqn
There is convenient way for populating tables - db/seed.rbfile. Just add the script for creating users in it and run:
有一种方便的方法来填充表 - db/seed.rb文件。只需添加用于在其中创建用户的脚本并运行:
rake db:seed
Below you can see example of Usermodel with emailand usernamefields:
您可以在下面看到User带有email和username字段的模型示例:
# 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.
请注意,此处应用了设计验证。
Hereyou can find more examples of using the seed.rbfile and hereis the rayn's rails cast.

