From 5bf96897e2462344a3c455ac2113dc2cbd610787 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Thu, 18 Oct 2012 10:10:12 +0200 Subject: [PATCH] Added last login feature. --- app/controllers/application_controller.rb | 3 +++ app/controllers/sessions/collection_base.rb | 11 +++++++++++ app/controllers/sessions_controller.rb | 20 ++++++++++++-------- app/models/user.rb | 7 ++++++- db/migrate/20120101000001_create_base.rb | 1 + db/migrate/20121018074741_users_update.rb | 8 ++++++++ 6 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 app/controllers/sessions/collection_base.rb create mode 100644 db/migrate/20121018074741_users_update.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 066da1139..6c51e8aac 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -84,6 +84,9 @@ class ApplicationController < ActionController::Base # return auth ok if message == '' + # remember last login + userdata.update_last_login + # set basic auth user to current user current_user_set(userdata) return { diff --git a/app/controllers/sessions/collection_base.rb b/app/controllers/sessions/collection_base.rb new file mode 100644 index 000000000..25f7f7af7 --- /dev/null +++ b/app/controllers/sessions/collection_base.rb @@ -0,0 +1,11 @@ +module ExtraCollection + def add(collections) + + # all base stuff + collections['Role'] = Role.all + collections['Group'] = Group.all + collections['Organization'] = Organization.all + + end + module_function :add +end \ No newline at end of file diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 0348c887c..e93270d53 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -6,6 +6,7 @@ class SessionsController < ApplicationController # "Create" a login, aka "log the user in" def create + # authenticate user user = User.authenticate( params[:username], params[:password] ) # auth failed @@ -13,9 +14,12 @@ class SessionsController < ApplicationController render :json => { :error => 'login failed' }, :status => :unprocessable_entity return end - + + # remember last login date + user.update_last_login() + user = User.find_fulldata(user.id) - + # auto population of default collections default_collection = default_collections() @@ -118,21 +122,21 @@ class SessionsController < ApplicationController authorization = Authorization.create_from_hash(auth, current_user) end + # remember last login date + authorization.user.update_last_login() + # Log the authorizing user in. session[:user_id] = authorization.user.id # redirect to app redirect_to '/app#' end - + private def default_collections - # auto population of default collections + # auto population collections, store all here default_collection = {} - default_collection['Role'] = Role.all - default_collection['Group'] = Group.all - default_collection['Organization'] = Organization.all # load collections to deliver from external files dir = File.expand_path('../', __FILE__) @@ -142,6 +146,6 @@ class SessionsController < ApplicationController ExtraCollection.add(default_collection) end - return default_collection + return default_collection end end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index cb17d0138..3e85d040f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -88,7 +88,7 @@ class User < ApplicationModel # try to find user based on login user = User.where( :login => username, :active => true ).first - + # try second lookup with email if !user user = User.where( :email => username, :active => true ).first @@ -292,6 +292,11 @@ Your #{config.product_name} Team end end + def update_last_login + self.last_login = Time.now + self.save + end + private def check_geo diff --git a/db/migrate/20120101000001_create_base.rb b/db/migrate/20120101000001_create_base.rb index 535d02dd6..0e874e9b9 100644 --- a/db/migrate/20120101000001_create_base.rb +++ b/db/migrate/20120101000001_create_base.rb @@ -29,6 +29,7 @@ class CreateBase < ActiveRecord::Migration t.column :verified, :boolean, :null => false, :default => false t.column :active, :boolean, :null => false, :default => true t.column :note, :string, :limit => 250, :null => true + t.column :last_login, :timestamp, :null => true t.column :source, :string, :limit => 200, :null => true t.column :preferences, :string, :limit => 4000,:null => true t.column :updated_by_id, :integer, :null => false diff --git a/db/migrate/20121018074741_users_update.rb b/db/migrate/20121018074741_users_update.rb new file mode 100644 index 000000000..039becaf3 --- /dev/null +++ b/db/migrate/20121018074741_users_update.rb @@ -0,0 +1,8 @@ +class UsersUpdate < ActiveRecord::Migration + def up + add_column :users, :last_login, :timestamp, :null => true + end + + def down + end +end