论坛 Tripcode 的 Datamapper 回调

发布于 2024-10-05 13:05:40 字数 1521 浏览 3 评论 0原文

上下文:为论坛创建 Tripcode 实现 (http://en.wikipedia.org/wiki/Tripcode)。本质上,是用于无注册识别的弱哈希。

有一种模型,“Post”。帖子以父/子格式排列,新帖子创建父项,回复创建子项到父项。有一种表单,现在有一个发布到控制器/模型的字段,包含内容和密码字段。

require 'bcrypt'
class Shout
  include DataMapper::Resource
  include BCrypt

  property :id, Serial                                     # unique key
  property :content, Text

  property :password_hash, String
  property :trip, String                                  # trip for display    

  belongs_to :forum
  is :tree, :order => [:created_at]

  attr_accessor :password

  #before :save do

  def password
    @password ||= Password.new(password_hash)
  end

  def password=(new_password)
    @password = Password.create(new_password)
    self.password_hash = @password
  end

  def trip
    @trip = '!'<<self.password_hash.to_str[20..33]
    self.trip = @trip
  end

  #end

end

DataMapper.finalize

基本流程是这样的 - 发布/回复,如果密码字段中有密码,则获取该密码并运行 bcrypt,将该结果存储为 password_hash 以供以后比较,创建 Tripcode 以供显示。但我遇到了错误,我一直在努力解决

我遇到的主要错误是

未定义的方法“原始?”对于 nil:NilClass

似乎源自

lib/active_support/whiny_nil.rb:48:in `method_missing'

我不知道如何处理或解决这个问题。我没有用控制器做某事或检查某事,但还不知道做什么。我遇到的另一个错误源于无效的 bcrypt 哈希,但无法立即复制该错误。

挂钩方法就在 bcrypt-ruby 页面之外。

创建 BCryptHash 字段是可行的(dm-types)——但是在本地主机服务器上处理表单的时间增加了 10 倍,并且对每个帖子都执行此操作,因此我需要一种方法来调整 bcrypt 哈希的成本(例如 1 而不是默认的 10)并且仅在存在密码时才运行它,这就是我这样做的原因。

但这现在不起作用,我已经用尽全力去解决它,并继续解决其他问题,如果我能得到一些意见,就会回到它。我正在使用 Rails,因此我添加了该标签,尽管这主要不是 Rails 问题。

The context: creating a tripcode implementation (http://en.wikipedia.org/wiki/Tripcode) for a forum. Essentially, a weak hash for registrationless identification.

There is one model, 'Post'. Posts are arranged in parent/child format, new post creates parent, replies create child to parent. There is one form, right now has a field that posts to the controller/model, contains a content and password field.

require 'bcrypt'
class Shout
  include DataMapper::Resource
  include BCrypt

  property :id, Serial                                     # unique key
  property :content, Text

  property :password_hash, String
  property :trip, String                                  # trip for display    

  belongs_to :forum
  is :tree, :order => [:created_at]

  attr_accessor :password

  #before :save do

  def password
    @password ||= Password.new(password_hash)
  end

  def password=(new_password)
    @password = Password.create(new_password)
    self.password_hash = @password
  end

  def trip
    @trip = '!'<<self.password_hash.to_str[20..33]
    self.trip = @trip
  end

  #end

end

DataMapper.finalize

The basic flow is this - post/reply, if there is a password in the password field, take that and run through bcrypt, store that result as password_hash for later comparison, create tripcode for display. But I'm getting errors I've been beating my head against

The primary error I'm getting is

undefined method `primitive?' for nil:NilClass

seemingly emanating from

lib/active_support/whiny_nil.rb:48:in `method_missing'

I don't know how to handle or work around this. I'm not doing something or checking something with the controller, but don't yet know what. The other error I'm getting stems from an invalid bcrypt hash, but not able to duplicate this immediately.

The hook methods are right off the bcrypt-ruby page.

Creating a BCryptHash field works (dm-types) -- but increases the time to process the form by a factor of 10, on a localhost server, and does it for every post so I need a way to tweak the cost of the bcrypt hash (eg. 1 instead of default 10) and only run it when there is a password present, which is why I'm doing this.

But this doesn't work right now, I've rammed my head against it enough and moving on to other problems and coming back to it if I can get some input. I'm working with rails, so I've added that tag although its not primarily a rails issue.

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

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

发布评论

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

评论(1

腻橙味 2024-10-12 13:05:40

请随意在此处查看、贡献或使用错误。

https://github.com/blueblank/Shout/tree/oneshout

Feel free to review or contribute or use for errors here.

https://github.com/blueblank/Shout/tree/oneshout

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