5. 登入時加入時間及IP位址

如果有安裝  3. Sentinel 2.0 用戶認證應用


由於Sentinel 已經有登入的時間欄位了,所以我們僅需要追加IP
開啓 /database/migrations/2014_07_02_230147_migration_cartalyst_sentinel.php

在users table內增加登入的時間:

$table->string('last_login')->nullable();
$table->string('ip_address')->nullable();  

開啓 /vendor/cartalyst/sentinel/src/Users/IlluminateUserRepository.php

find:
 namespace Cartalyst\Sentinel\Users;

add after:
 use Illuminate\Support\Facades\Request;  

find :
 $user->last_login = Carbon::now();

add after :
 $user->ip_address = Request::ip();

如果你裝的是Laravel 提供的預設 user :

這邊會有兩種版本,首先是Laravel 5.2:

開啓 /vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php

找到 public function login(AuthenticatableContract $user, $remember = false)

     if ($remember) {  
       $this->createRememberTokenIfDoesntExist($user);  
       $this->queueRecallerCookie($user);  
     }  
     // 寫入登入IP & 時間  
     $this->provider->updateUserInfo($user);  

開啓 /vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php

 use Illuminate\Contracts\Auth\Authenticatable as UserContract;  
 use Illuminate\Support\Facades\Request;  
 use Carbon\Carbon;  

   public function updateRememberToken(UserContract $user, $token)  
   {  
     $user->setRememberToken($token);  
     $user->save();  
   }  
   /**  
    * 寫入登入時間及IP 位址  
    *  
    * @param \Illuminate\Contracts\Auth\Authenticatable $user  
    * @return void  
    */  
   public function updateUserInfo(UserContract $user)  
   {  
     // 寫入登入IP & 時間  
     $user->ip_address = Request::ip();  
     $user->last_login = Carbon::now();  
     $user->save();  
   }  

開啓 /vendor/laravel/framework/src/Illuminate/Auth/DatabaseUserProvider.php

 use Illuminate\Contracts\Auth\Authenticatable as UserContract;  
 use Illuminate\Support\Facades\Request;  
 use Carbon\Carbon;  

   public function updateRememberToken(UserContract $user, $token)  
   {  
     $this->conn->table($this->table)  
       ->where('id', $user->getAuthIdentifier())  
       ->update(['remember_token' => $token]);  
   }  
   /**  
    * 寫入登入時間及IP 位址  
    *  
    * @param \Illuminate\Contracts\Auth\Authenticatable $user  
    * @return void  
    */  
   public function updateUserInfo(UserContract $user)  
   {  
     // 寫入登入IP & 時間  
     $this->conn->table($this->table)  
       ->where('id', $user->getAuthIdentifier())  
       ->update(['ip_address' => Request::ip(), 'last_login' => Carbon::now()]);  
   }  

開啓 /database/migrations/2014_10_12_000000_create_users_table.php

       $table->string('password', 60);  
       $table->string('ip_address')->nullable();  
       $table->timestamp('last_login')->nullable();  
       $table->rememberToken();  

Laravel 5.1 晚點再補上~~敬請期待!


留言

這個網誌中的熱門文章

2. 新增自定義的全域函式(helpers function)

3. Sentinel 2.0 用戶認證應用

0. env 基礎設置