comparison app/models/token.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children e248c7af89ec
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software 1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 # 3 #
4 # This program is free software; you can redistribute it and/or 4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License 5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2 6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version. 7 # of the License, or (at your option) any later version.
35 # Delete all expired tokens 35 # Delete all expired tokens
36 def self.destroy_expired 36 def self.destroy_expired
37 Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time] 37 Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time]
38 end 38 end
39 39
40 private 40 # 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
41 def self.generate_token_value 71 def self.generate_token_value
42 Redmine::Utils.random_hex(20) 72 Redmine::Utils.random_hex(20)
43 end 73 end
74
75 private
44 76
45 # Removes obsolete tokens (same user and action) 77 # Removes obsolete tokens (same user and action)
46 def delete_previous_tokens 78 def delete_previous_tokens
47 if user 79 if user
48 Token.delete_all(['user_id = ? AND action = ?', user.id, action]) 80 Token.delete_all(['user_id = ? AND action = ?', user.id, action])