Kev*_*von 5 ruby database authentication padrino audit-logging
我正在开发一个具有主应用程序和管理员应用程序的Padrino应用程序.当用户登录会话时,我只需运行以下两行来登录.
account = Account.authenticate(params[:email], params[:password])
set_current_account(account)
Run Code Online (Sandbox Code Playgroud)
在Admin中的任何控制器中使用调试器
current_account
#<Account @id=1 @name="John" @surname="Davies" @email="john.davies@gmail.com" @crypted_password="3456789" @role="admin">
Run Code Online (Sandbox Code Playgroud)
在任何型号的调试器
current_account
*** NameError Exception: undefined local variable or method `current_account' for #<Post @id=1 @question="Why is this not working?" @answer="I have no idea">
Run Code Online (Sandbox Code Playgroud)
我能够访问current_account以找出在Admin应用程序中登录的用户,但是在主应用程序中无法访问此变量.
我想要做的是我为所有模型创建一个活动源或审计跟踪,因此当在任何模型中创建/更新/销毁记录时,会在模型活动中创建一条新记录.话虽这么说,我需要访问模型中的current_account变量.
我搜索了解决方案,并提出了一个建议:
在admin/app.rb中
enable :sessions
set :session_id, "my-global-session"
Run Code Online (Sandbox Code Playgroud)
在app/app.rb中
register Padrino::Admin::AccessControl
register Padrino::Admin::Helpers
enable :sessions
set :session_id, "my-global-session"
Run Code Online (Sandbox Code Playgroud)
它对我不起作用.有没有办法在我的模型中访问current_account?
感谢您的任何指导或建议.
我不太确定你想做什么,但我解决了类似的问题,如下所示:
class Account
# ...
class << self
attr_accessor :current
end
# ...
end
class Admin < Padrino::Application
#...
before do
Account.current = current_account
end
# ...
end
Run Code Online (Sandbox Code Playgroud)
然后使用Account.current它来访问它。