token.rb 680 B

123456789101112131415161718192021222324252627282930313233
  1. # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
  2. class Token < ActiveRecord::Base
  3. before_create :generate_token
  4. belongs_to :user
  5. def self.check( data )
  6. # fetch token
  7. token = Token.where( :action => data[:action], :name => data[:name] ).first
  8. return if !token
  9. # check if token is still valid
  10. if token.created_at < 1.day.ago
  11. # delete token
  12. token.delete
  13. token.save
  14. return
  15. end
  16. # return token if valid
  17. return token.user
  18. end
  19. private
  20. def generate_token
  21. begin
  22. self.name = SecureRandom.hex(20)
  23. end while Token.exists?( :name => self.name )
  24. end
  25. end