1 # Redmine - project management software
2 # Copyright (C) 2006-2009 Jean-Philippe Lang
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.
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.
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.
18 class Token < ActiveRecord::Base
20 validates_uniqueness_of :value
22 before_create :delete_previous_tokens
24 @@validity_time = 1.day
27 self.value = Token.generate_token_value
30 # Return true if token has expired
32 return Time.now > self.created_on + @@validity_time
35 # Delete all expired tokens
36 def self.destroy_expired
37 Token.delete_all ["action <> 'feeds' AND created_on < ?", Time.now - @@validity_time]
41 def self.generate_token_value
42 ActiveSupport::SecureRandom.hex(20)
45 # Removes obsolete tokens (same user and action)
46 def delete_previous_tokens
48 Token.delete_all(['user_id = ? AND action = ?', user.id, action])