[英]Access current_user in model
我有3张桌子
items (columns are: name , type)
history(columns are: date, username, item_id)
user(username, password)
当用户说“ABC”登录并创建新项目时,将使用以下after_create过滤器创建历史记录。 如何通过此过滤器将此用户名“ABC”分配给历史记录表中的用户名字段。
class Item < ActiveRecord::Base
has_many :histories
after_create :update_history
def update_history
histories.create(:date=>Time.now, username=> ?)
end
end
我在session_controller中的登录方法
def login
if request.post?
user=User.authenticate(params[:username])
if user
session[:user_id] =user.id
redirect_to( :action=>'home')
flash[:message] = "Successfully logged in "
else
flash[:notice] = "Incorrect user/password combination"
redirect_to(:action=>"login")
end
end
end
我没有使用任何身份验证插件。 如果有人能告诉我如何在不使用插件(如userstamp等)的情况下实现这一点,我将不胜感激。
声明一个模块
module Current
thread_mattr_accessor :user
end
分配当前用户
class ApplicationController < ActionController::Base
around_action :set_current_user
def set_current_user
Current.user = current_user
yield
ensure
# to address the thread variable leak issues in Puma/Thin webserver
Current.user = nil
end
end
现在您可以将当前用户称为Current.user
访问模型中的current_user
不常见。 话虽如此,这是一个解决方案:
class User < ActiveRecord::Base
def self.current
Thread.current[:current_user]
end
def self.current=(usr)
Thread.current[:current_user] = usr
end
end
在ApplicationController
的around_filter
中设置current_user
属性。
class ApplicationController < ActionController::Base
around_filter :set_current_user
def set_current_user
User.current = User.find_by_id(session[:user_id])
yield
ensure
# to address the thread variable leak issues in Puma/Thin webserver
User.current = nil
end
end
验证成功后设置current_user
:
def login
if User.current=User.authenticate(params[:username], params[:password])
session[:user_id] = User.current.id
flash[:message] = "Successfully logged in "
redirect_to( :action=>'home')
else
flash[:notice] = "Incorrect user/password combination"
redirect_to(:action=>"login")
end
end
最后,请参阅Item
update_history
中的current_user
。
class Item < ActiveRecord::Base
has_many :histories
after_create :update_history
def update_history
histories.create(:date=>Time.now, :username=> User.current.username)
end
end
使用数据库是模型的工作。 处理Web请求(包括了解当前请求的用户)是控制器的工作。
因此,如果模型实例需要知道当前用户,则控制器应该告诉它。
def create
@item = Item.new
@item.current_user = current_user # or whatever your controller method is
...
end
这假定Item
具有current_user
的attr_accessor
。
如果用户创建了一个项目,那么该项目是否应该有belongs_to :user
子句? 这将允许你在after_update
中做
History.create :username => self.user.username
你可以在ApplicationController中编写一个around_filter
around_filter :apply_scope
def apply_scope
Document.where(:user_id => current_user.id).scoping do
yield
end
用于全局访问用户和其他属性的Rails 5.2方法是CurrentAttributes 。
通过实现Thread,可以在几个步骤中轻松完成。
步骤1:
class User < ApplicationRecord
def self.current
Thread.current[:user]
end
def self.current=(user)
Thread.current[:user] = user
end
end
第2步:
class ApplicationController < ActionController::Base
before_filter :set_current_user
def set_current_user
User.current = current_user
end
end
现在,您可以轻松地将当前用户作为User.current
具有讽刺意味的是, Thread
技巧不是线程安全的。
我的解决方案是向后移动堆栈,寻找响应current_user
的帧。 如果没有找到,则返回nil。 例:
def find_current_user
(1..Kernel.caller.length).each do |n|
RubyVM::DebugInspector.open do |i|
current_user = eval "current_user rescue nil", i.frame_binding(n)
return current_user unless current_user.nil?
end
end
return nil
end
通过确认预期的返回类型可以使其更加稳健,并且可能通过确认框架的所有者是一种控制器......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.