我们有一个拥有数千名用户的生产数据库,在上一个版本中,我们添加了 Design Pepper 以提高安全性。
现在,现有用户无法登录到生产环境。 但是在发布后创建的新用户能够登录。
我认为问题是用于在发布前后加密密码的盐是不同的。
有什么方法可以允许两个用户(在 pepper 集成之前创建的用户和在 pepper 集成后创建的用户(登录?
检查您DEVISE_PEPPER以前的令牌值,如果您现在更改了它,那么对于现有用户来说,它是失败的。您必须DEVISE_PEPPER令牌值重置为前一个令牌值,或者您必须通过 rails 控制台使用相同的密码更新用户密码。
您需要使用以下脚本对旧用户记录执行更新:
begin
c_pool = ActiveRecord::Base.establish_connection # initialize connection pool
conn = c_pool.connection # create connection object
# Fetch number of users that were old
result = conn.execute("SELECT count(*) from users WHERE created_at < '2018-03-12 08:37:46'", )
count = result.try(:[], 0).to_i
batch_size = 100
my_offset = 0
while (count > 0) do
users = User.where("created_at < ?", '2018-03-12 08:37:46').limit(batch_size).offset(my_offset)
break unless users.present? # Exit from loop if no users.
users.each do |u|
u.password = u.old_password_field
u.save # This will update password_digest column of the user.
count -= 1
end
my_offset += batch_size
end
rescue => e
Rails.logger.error "#{e.message}"
ensure
ActiveRecord::Base.clear_active_connections!
end
您可以根据需要修改批量大小。请查看我的另一个答案,该答案解释了为什么批量查询更适合更好的内存管理。
您可以使用 rails 运行器运行上述脚本。