|
|
@ -1,3 +1,4 @@
|
|
|
|
|
|
|
|
#coding=utf-8
|
|
|
|
# Redmine - project management software
|
|
|
|
# Redmine - project management software
|
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
|
|
#
|
|
|
|
#
|
|
|
@ -14,7 +15,7 @@
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#
|
|
|
|
class Token < ActiveRecord::Base
|
|
|
|
class Token < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :user
|
|
|
|
validates_uniqueness_of :value
|
|
|
|
validates_uniqueness_of :value
|
|
|
@ -27,6 +28,14 @@ class Token < ActiveRecord::Base
|
|
|
|
self.value = Token.generate_token_value
|
|
|
|
self.value = Token.generate_token_value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.get_or_create_permanent_login_token(user)
|
|
|
|
|
|
|
|
token = Token.get_token_from_user(user, 'autologin')
|
|
|
|
|
|
|
|
unless token
|
|
|
|
|
|
|
|
token = Token.create(:user => user, :action => 'autologin')
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
token
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def self.get_token_from_user(user, action)
|
|
|
|
def self.get_token_from_user(user, action)
|
|
|
|
token = Token.where(:action => action, :user_id => user).first
|
|
|
|
token = Token.where(:action => action, :user_id => user).first
|
|
|
|
unless token
|
|
|
|
unless token
|
|
|
@ -42,7 +51,7 @@ class Token < ActiveRecord::Base
|
|
|
|
|
|
|
|
|
|
|
|
# Delete all expired tokens
|
|
|
|
# Delete all expired tokens
|
|
|
|
def self.destroy_expired
|
|
|
|
def self.destroy_expired
|
|
|
|
Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api'], Time.now - @@validity_time]
|
|
|
|
Token.delete_all ["action NOT IN (?) AND created_on < ?", ['feeds', 'api', 'autologin'], Time.now - @@validity_time]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Returns the active user who owns the key for the given action
|
|
|
|
# Returns the active user who owns the key for the given action
|
|
|
@ -80,6 +89,10 @@ class Token < ActiveRecord::Base
|
|
|
|
Redmine::Utils.random_hex(20)
|
|
|
|
Redmine::Utils.random_hex(20)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def self.delete_user_all_tokens(user)
|
|
|
|
|
|
|
|
Token.delete_all(user_id: user.id)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
private
|
|
|
|
|
|
|
|
|
|
|
|
# Removes obsolete tokens (same user and action)
|
|
|
|
# Removes obsolete tokens (same user and action)
|
|
|
|