Rails + Devise - 有没有办法禁止用户,使他们无法登录或重置密码? [英] Rails + Devise - Is there a way to BAN a user so they can't login or reset their password?
问题描述
感谢设计,我有很多用户,我想禁止一些问题制造者.Devise 是否内置了这种支持?
I have a lot of users thanks to devise and I want to ban a few problem makers. Does Devise have this support built in?
谢谢
推荐答案
我刚刚在我的项目中实现了这个.我所做的与上面的 Kleber 类似,我在我的 app/controllers/sessions_controller.rb(覆盖 Devise)中定义了这个...
I just implemented this in my project myself. What I did was similar to Kleber above, I defined this in my app/controllers/sessions_controller.rb (overriding Devise)...
class SessionsController < Devise::SessionsController
protected
def after_sign_in_path_for(resource)
if resource.is_a?(User) && resource.banned?
sign_out resource
flash[:error] = "This account has been suspended for violation of...."
root_path
else
super
end
end
end
然后我向用户添加了一个名为banned"的布尔值列,因此版主在后端编辑用户时选中该复选框,布尔值将返回 true.
And then I added a boolean column to Users called 'banned,' so the moderators check the checkbox when editing the user in the backend, and the boolean will return true.
但是有一个缺陷......如果用户已经登录然后被禁止,他们仍然可以访问网站上的内容(评论等),至少直到他们的会话过期或他们退出.所以我在 app/controllers/application_controller.rb 中做了这个...
But there was one flaw...if a user was already logged in and then banned, they still had access to doing stuff on the site (comments, etc) at least until their session expired or they logged out. So I did this in the app/controllers/application_controller.rb...
class ApplicationController < ActionController::Base
before_filter :banned?
def banned?
if current_user.present? && current_user.banned?
sign_out current_user
flash[:error] = "This account has been suspended...."
root_path
end
end
end
如果检测到禁令,这将自动将其注销.无论如何,我不确定这整件事是否是考虑整件事的最佳"方式,因为我是 Rails 的新手,但整件事对我有用,希望它至少能给你一个好的开始.
That'll automatically log them out if a ban is detected. Anyway, not sure this whole thing is the "best" way to factor the whole thing as I'm newer to Rails, but the whole thing works for me and hope it will at least give you a good start.
这篇关于Rails + Devise - 有没有办法禁止用户,使他们无法登录或重置密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!