以管理员身份编辑用户信息
Edit User Information as Admin
我正在通过 Rails 4+ 开发一个项目,我希望管理员能够在其中编辑用户信息。目前,我在我的用户模型中将 admin 作为布尔值(在使用 Devise 时)。管理员帐户目前可以从站点删除用户,但在编辑用户信息时我似乎很困惑。
我在网上看了一些书,我不想使用 cancan 或其他 gem,因为我认为它真的不需要吗?此外,我实际上还没有任何代码设置,因为我真的很困惑如何着手实现这一目标。不是在寻找 easy/quick 答案,我只是不知所措 where/how 不知从何开始。
如果您知道如何完成此操作 and/or 任何可以提供帮助的指南,我将不胜感激。
谢谢!
乔
我建议您查看 active_admin,您可以轻松完成。
对于rails4,你只需要使用master分支即可。
gem 'activeadmin', github: 'activeadmin'
更多信息see docs here
这其实并不难实现。我既没有使用 CanCan,也没有在即将向您展示的代码中使用 Devise,它们都是手动制作的。但是,我仍然认为它可能对您有所帮助...
考虑一个示例 users_controller.rb 文件...
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
.
.
def edit
@user = User.find(params[:id])
end
.
.
private
.
.
# Confirms that user is correct.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) || current_user.admin?
end
# Confirms that a user is logged-in
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Below are several methods used in the above two..
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns true if the given user is the current user.
def current_user?(user)
user == current_user
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
如果你仔细查看控制器文件的顶部,你会发现 before_action
这是一个命令,它安排在允许给定操作发生之前调用特定方法。
因此,在 before_action :correct_user, only: [:edit, :update]
行内,我要求 Rails 做的是在允许更新或编辑用户信息之前检查用户是否正确。
根据我对 correct_user
的定义...
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) || current_user.admin?
end
...正确的用户是 拥有此帐户的用户或 admin。结果不会将我重定向到根页面,而是让我编辑用户的问题页面。
希望对您有所帮助。
更新:
When an admin edits a users information, they should not have to know the password to do so. Is this implemented in your code somehow or how would one bypass that?
首先,实际上我无论如何都看不到密码,因为它是作为摘要存储的,但是如果您甚至不想让管理员查看编辑密码,那么你应该做的是以下...
在您的 edit.html.erb 中,只需将密码字段放在 if 语句中...
<% if !current_user.admin? %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<% end %>
...正是这样做的。
如果我想查看用户信息安全的证据,我们可以查看控制台...
2.2.1 :001 > User.first
User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 OFFSET 1
=> #<User id: 1, name: "Fake User", email: "fake@fakemail.org", created_at: "2015-07-11 18:36:00", updated_at: "2015-07-11 18:36:00", password_digest: "a$RpVWwRKWIQObTDow3PLGWOgjxqNNK.LU8p.fEPOVVnT...", remember_digest: nil, admin: nil, activation_digest: "a$jUZiRIiiBnpCSEv0h/KKi.fqdW3hVn94XFxxQS.n62X...", activated: true, activated_at: "2015-07-11 18:36:00", reset_digest: nil, reset_sent_at: nil>
我正在通过 Rails 4+ 开发一个项目,我希望管理员能够在其中编辑用户信息。目前,我在我的用户模型中将 admin 作为布尔值(在使用 Devise 时)。管理员帐户目前可以从站点删除用户,但在编辑用户信息时我似乎很困惑。
我在网上看了一些书,我不想使用 cancan 或其他 gem,因为我认为它真的不需要吗?此外,我实际上还没有任何代码设置,因为我真的很困惑如何着手实现这一目标。不是在寻找 easy/quick 答案,我只是不知所措 where/how 不知从何开始。
如果您知道如何完成此操作 and/or 任何可以提供帮助的指南,我将不胜感激。
谢谢!
乔
我建议您查看 active_admin,您可以轻松完成。
对于rails4,你只需要使用master分支即可。
gem 'activeadmin', github: 'activeadmin'
更多信息see docs here
这其实并不难实现。我既没有使用 CanCan,也没有在即将向您展示的代码中使用 Devise,它们都是手动制作的。但是,我仍然认为它可能对您有所帮助...
考虑一个示例 users_controller.rb 文件...
class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :correct_user, only: [:edit, :update]
.
.
def edit
@user = User.find(params[:id])
end
.
.
private
.
.
# Confirms that user is correct.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) || current_user.admin?
end
# Confirms that a user is logged-in
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
end
# Below are several methods used in the above two..
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Returns true if the given user is the current user.
def current_user?(user)
user == current_user
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
如果你仔细查看控制器文件的顶部,你会发现 before_action
这是一个命令,它安排在允许给定操作发生之前调用特定方法。
因此,在 before_action :correct_user, only: [:edit, :update]
行内,我要求 Rails 做的是在允许更新或编辑用户信息之前检查用户是否正确。
根据我对 correct_user
的定义...
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user) || current_user.admin?
end
...正确的用户是 拥有此帐户的用户或 admin。结果不会将我重定向到根页面,而是让我编辑用户的问题页面。
希望对您有所帮助。
更新:
When an admin edits a users information, they should not have to know the password to do so. Is this implemented in your code somehow or how would one bypass that?
首先,实际上我无论如何都看不到密码,因为它是作为摘要存储的,但是如果您甚至不想让管理员查看编辑密码,那么你应该做的是以下...
在您的 edit.html.erb 中,只需将密码字段放在 if 语句中...
<% if !current_user.admin? %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<% end %>
...正是这样做的。
如果我想查看用户信息安全的证据,我们可以查看控制台...
2.2.1 :001 > User.first
User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1 OFFSET 1
=> #<User id: 1, name: "Fake User", email: "fake@fakemail.org", created_at: "2015-07-11 18:36:00", updated_at: "2015-07-11 18:36:00", password_digest: "a$RpVWwRKWIQObTDow3PLGWOgjxqNNK.LU8p.fEPOVVnT...", remember_digest: nil, admin: nil, activation_digest: "a$jUZiRIiiBnpCSEv0h/KKi.fqdW3hVn94XFxxQS.n62X...", activated: true, activated_at: "2015-07-11 18:36:00", reset_digest: nil, reset_sent_at: nil>