RoR:attr_encrypted 赋值未写入?

发布于 2024-10-25 13:21:13 字数 1225 浏览 1 评论 0原文

我在 RoR 3.0.5 中使用 attr_encrypted (v 1.2.0) 来加密我不希望在数据库中以纯文本形式显示的凭据。当我更新加密字段时,它似乎没有保存到数据库中。

我的模型本质上是:(

class Service << ActiveRecord::Base
    attr_encrypted :credentials_aux, :key => KEY, :attribute => 'encrypted_credentials', :encode => true, :marshal => true

  def credentials
    credentials_aux
  end

  def credentials=(c)
    h = {}.update(c)  # coerce HashWithIndifferentAccess to a vanilla hash
    credentials_aux = h
  end

  ...
end

请注意,“credentials=”方法的存在只是为了将 Rails 生成的 HashWithIn DifferentAccess 强制转换为普通哈希。它还为我提供了一个插入调试打印输出以验证我的数据的位置。)

但是当我尝试更新凭据时通过控制台,它不需要:

>> s = Service.find(19)
=> #<Service id: 19, encrypted_credentials: "10VfHU7IkdrFb4Q6Hj18YtY81rbRp3sIuoVUl8CHNj88cq1XFo2...",>
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
>> s.credentials = {"user_id" => "barney.rubble", "password" => "notsosecret"}
=> {"user_id" => "barney.rubble", "password" => "notsosecret"}
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}

为什么 s.credentials 没有更新为新值?

I'm using attr_encrypted (v 1.2.0) in RoR 3.0.5 to encrypt credentials that I don't want appearing as plain text in my db. When I update the encrypted field, it appears that it's not getting saved into the db.

My model is essentially:

class Service << ActiveRecord::Base
    attr_encrypted :credentials_aux, :key => KEY, :attribute => 'encrypted_credentials', :encode => true, :marshal => true

  def credentials
    credentials_aux
  end

  def credentials=(c)
    h = {}.update(c)  # coerce HashWithIndifferentAccess to a vanilla hash
    credentials_aux = h
  end

  ...
end

(Note that the 'credentials=' method exists simply to coerce a Rails-generated HashWithIndifferentAccess into a vanilla hash. It also gives me a place to interpose debugging printout to verify my data.)

But when I try updating credentials via the console, it doesn't take:

>> s = Service.find(19)
=> #<Service id: 19, encrypted_credentials: "10VfHU7IkdrFb4Q6Hj18YtY81rbRp3sIuoVUl8CHNj88cq1XFo2...",>
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}
>> s.credentials = {"user_id" => "barney.rubble", "password" => "notsosecret"}
=> {"user_id" => "barney.rubble", "password" => "notsosecret"}
>> s.credentials
=> {"user_id"=>"fred.flintstone", "password"=>"supersecret"}

Why didn't s.credentials get updated to the new value?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梓梦 2024-11-01 13:21:13

我相信您只是在“credentials=”方法中设置一个名为“credentials_aux”的局部变量,尝试显式使用“self”

def credentials=(c)
  h = {}.update(c)  # coerce HashWithIndifferentAccess to a vanilla hash
  self.credentials_aux = h
end

我实际上是该项目的维护者,所以如果上面的修复不起作用那么我'我会为此开一张新票。

I believe you're just setting a local variable called "credentials_aux" in your "credentials=" method, try explicitly using "self"

def credentials=(c)
  h = {}.update(c)  # coerce HashWithIndifferentAccess to a vanilla hash
  self.credentials_aux = h
end

I'm actually the maintainer of that project, so if the fix above doesn't work then I'll open up a new ticket for this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文