Adds token finder methods.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11374 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
+21
-4
@@ -39,14 +39,31 @@ class Token < ActiveRecord::Base
|
||||
|
||||
# Returns the active user who owns the key for the given action
|
||||
def self.find_active_user(action, key, validity_days=nil)
|
||||
user = find_user(action, key, validity_days)
|
||||
if user && user.active?
|
||||
user
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the user who owns the key for the given action
|
||||
def self.find_user(action, key, validity_days=nil)
|
||||
token = find_token(action, key, validity_days)
|
||||
if token
|
||||
token.user
|
||||
end
|
||||
end
|
||||
|
||||
# Returns the token for action and key with an optional
|
||||
# validity duration (in number of days)
|
||||
def self.find_token(action, key, validity_days=nil)
|
||||
action = action.to_s
|
||||
key = key.to_s
|
||||
return nil unless action.present? && key =~ /\A[a-f0-9]+\z/
|
||||
return nil unless action.present? && key =~ /\A[a-z0-9]+\z/i
|
||||
|
||||
token = find_by_action_and_value(action, key)
|
||||
if token && token.user && token.user.active?
|
||||
token = Token.where(:action => action, :value => key).first
|
||||
if token && (token.action == action) && (token.value == key) && token.user
|
||||
if validity_days.nil? || (token.created_on > validity_days.days.ago)
|
||||
token.user
|
||||
token
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user