To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / models / token.rb @ 1532:a0460a3d154f

History | View | Annotate | Download (2.5 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1494:e248c7af89ec Chris
# Copyright (C) 2006-2014  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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
18
class Token < ActiveRecord::Base
19
  belongs_to :user
20
  validates_uniqueness_of :value
21 909:cbb26bc654de Chris
22
  before_create :delete_previous_tokens, :generate_new_token
23
24 0:513646585e45 Chris
  @@validity_time = 1.day
25 909:cbb26bc654de Chris
26
  def generate_new_token
27 0:513646585e45 Chris
    self.value = Token.generate_token_value
28
  end
29
30 909:cbb26bc654de Chris
  # Return true if token has expired
31 0:513646585e45 Chris
  def expired?
32
    return Time.now > self.created_on + @@validity_time
33
  end
34 909:cbb26bc654de Chris
35 0:513646585e45 Chris
  # Delete all expired tokens
36
  def self.destroy_expired
37 1115:433d4f72a19b Chris
    Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time]
38 0:513646585e45 Chris
  end
39 909:cbb26bc654de Chris
40 1464:261b3d9a4903 Chris
  # Returns the active user who owns the key for the given action
41
  def self.find_active_user(action, key, validity_days=nil)
42
    user = find_user(action, key, validity_days)
43
    if user && user.active?
44
      user
45
    end
46
  end
47
48
  # Returns the user who owns the key for the given action
49
  def self.find_user(action, key, validity_days=nil)
50
    token = find_token(action, key, validity_days)
51
    if token
52
      token.user
53
    end
54
  end
55
56
  # Returns the token for action and key with an optional
57
  # validity duration (in number of days)
58
  def self.find_token(action, key, validity_days=nil)
59
    action = action.to_s
60
    key = key.to_s
61
    return nil unless action.present? && key =~ /\A[a-z0-9]+\z/i
62
63
    token = Token.where(:action => action, :value => key).first
64
    if token && (token.action == action) && (token.value == key) && token.user
65
      if validity_days.nil? || (token.created_on > validity_days.days.ago)
66
        token
67
      end
68
    end
69
  end
70
71 0:513646585e45 Chris
  def self.generate_token_value
72 1115:433d4f72a19b Chris
    Redmine::Utils.random_hex(20)
73 0:513646585e45 Chris
  end
74 909:cbb26bc654de Chris
75 1464:261b3d9a4903 Chris
  private
76
77 0:513646585e45 Chris
  # Removes obsolete tokens (same user and action)
78
  def delete_previous_tokens
79
    if user
80
      Token.delete_all(['user_id = ? AND action = ?', user.id, action])
81
    end
82
  end
83
end