diff options
author | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2015-10-24 10:15:22 +0000 |
---|---|---|
committer | Jean-Philippe Lang <jp_lang@yahoo.fr> | 2015-10-24 10:15:22 +0000 |
commit | 4cd22dcc5595f32519fbb43329e33106127c29b6 (patch) | |
tree | 8d8c35201924edfc5ab522e0193342390a94d212 /app/models/user.rb | |
parent | a371c8d850a2d1941e34fcf908d549438fdf72df (diff) | |
download | redmine-4cd22dcc5595f32519fbb43329e33106127c29b6.tar.gz redmine-4cd22dcc5595f32519fbb43329e33106127c29b6.zip |
Keep track of valid user sessions (#21058).
git-svn-id: http://svn.redmine.org/redmine/trunk@14735 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index e133cd02e..4a6109f7d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -394,6 +394,26 @@ class User < Principal api_token.value end + # Generates a new session token and returns its value + def generate_session_token + token = Token.create!(:user_id => id, :action => 'session') + token.value + end + + # Returns true if token is a valid session token for the user whose id is user_id + def self.verify_session_token(user_id, token) + return false if user_id.blank? || token.blank? + + scope = Token.where(:user_id => user_id, :value => token.to_s, :action => 'session') + if Setting.session_lifetime? + scope = scope.where("created_on > ?", Setting.session_lifetime.to_i.minutes.ago) + end + if Setting.session_timeout? + scope = scope.where("updated_on > ?", Setting.session_timeout.to_i.minutes.ago) + end + scope.update_all(:updated_on => Time.now) == 1 + end + # Return an array of project ids for which the user has explicitly turned mail notifications on def notified_projects_ids @notified_projects_ids ||= memberships.select {|m| m.mail_notification?}.collect(&:project_id) @@ -764,8 +784,8 @@ class User < Principal # This helps to keep the account secure in case the associated email account # was compromised. def destroy_tokens - if hashed_password_changed? - tokens = ['recovery', 'autologin'] + if hashed_password_changed? || (status_changed? && !active?) + tokens = ['recovery', 'autologin', 'session'] Token.where(:user_id => id, :action => tokens).delete_all end end |