Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

token.rb 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # redMine - project management software
  2. # Copyright (C) 2006 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. class Token < ActiveRecord::Base
  18. belongs_to :user
  19. @@validity_time = 1.day
  20. def before_create
  21. self.value = Token.generate_token_value
  22. end
  23. # Return true if token has expired
  24. def expired?
  25. return Time.now > self.created_on + @@validity_time
  26. end
  27. # Delete all expired tokens
  28. def self.destroy_expired
  29. Token.delete_all ["created_on < ?", Time.now - @@validity_time]
  30. end
  31. private
  32. def self.generate_token_value
  33. chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  34. token_value = ''
  35. 40.times { |i| token_value << chars[rand(chars.size-1)] }
  36. token_value
  37. end
  38. end