《PHP編程:Laravel中的Auth模塊詳解》要點:
本文介紹了PHP編程:Laravel中的Auth模塊詳解,希望對您有用。如果有疑問,可以聯系我們。
PHP應用前言
PHP應用本文主要給大家介紹的是關于Laravel中Auth模塊的相關內容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧.
PHP應用本文是基于Laravel 5.4 版本的本地化模塊代碼進行分析書寫;
PHP應用模塊組成
PHP應用Auth模塊從功能上分為用戶認證和權限管理兩個部分;從文件組成上,Illuminate\Auth\Passwords目錄下是密碼重置或忘記密碼處理的小模塊,Illuminate\Auth是負責用戶認證和權限管理的模塊,Illuminate\Foundation\Auth提供了登錄、修改密碼、重置密碼等一系統列具體邏輯實現;
PHP應用下圖展示了Auth模塊各個文件的關系,并進行簡要說明;
PHP應用
PHP應用用戶認證
PHP應用HTTP本身是無狀態,通常在系統交互的過程中,使用賬號或者Token標識來確定認證用戶;
PHP應用配置文件解讀
PHP應用 return [ 'defaults' => [ 'guard' => 'web', ... ], 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], ], 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\User::class, ], ], ], ];
PHP應用從下往上,理解;
PHP應用認證
PHP應用Session綁定認證信息:
PHP應用 // $credentials數組存放認證條件,比如郵箱或者用戶名、密碼 // $remember 表示是否要記住,生成 `remember_token` public function attempt(array $credentials = [], $remember = false) public function login(AuthenticatableContract $user, $remember = false) public function loginUsingId($id, $remember = false)
PHP應用HTTP基本認證,認證信息放在請求頭部;后面的請求訪問通過sessionId;
PHP應用 public function basic($field = 'email', $extraConditions = [])
PHP應用只在當前會話中認證,session中不記錄認證信息:
PHP應用 public function once(array $credentials = []) public function onceUsingId($id) public function onceBasic($field = 'email', $extraConditions = [])
PHP應用認證過程中(包括注冊、忘記密碼),定義的事件有這些:
事件名 | 描述 |
---|---|
Attempting | 嘗試驗證事件 |
Authenticated | 驗證通過事件 |
Failed | 驗證失敗事件 |
Lockout | 失敗次數超過限制,鎖住該請求再次訪問事件 |
Logi | 通過‘remember_token'成功登錄時,調用的事件 |
Logout | 用戶退出事件 |
Registered | 用戶注冊事件 |
PHP應用還有一些其他的認證方法:
Auth::check()
Auth::user()
Auth::logout()
PHP應用密碼處理
PHP應用配置解讀
PHP應用 return [ 'defaults' => [ 'passwords' => 'users', ... ], 'passwords' => [ 'users' => [ 'provider' => 'users', 'table' => 'password_resets', 'expire' => 60, ], ], ]
PHP應用從下往上,看配置;
PHP應用重置密碼的調用與實現
PHP應用先看看Laravel的重置密碼功能是怎么實現的:
PHP應用 public function reset(array $credentials, Closure $callback) { // 驗證用戶名、密碼和 token 是否有效 $user = $this->validateReset($credentials); if (! $user instanceof CanResetPasswordContract) { return $user; } $password = $credentials['password']; // 回調函數執行修改密碼,及持久化存儲 $callback($user, $password); // 刪除重置密碼時持久化存儲保存的 token $this->tokens->delete($user); return static::PASSWORD_RESET; }
PHP應用再看看Foundation\Auth模塊封裝的重置密碼模塊是怎么調用的:
PHP應用 // 暴露的重置密碼 API public function reset(Request $request) { // 驗證請求參數 token、email、password、password_confirmation $this->validate($request, $this->rules(), $this->validationErrorMessages()); // 調用重置密碼的方法,第二個參數是回調,做一些持久化存儲工作 $response = $this->broker()->reset( $this->credentials($request), function ($user, $password) { $this->resetPassword($user, $password); } ); // 封裝 Response return $response == Password::PASSWORD_RESET ? $this->sendResetResponse($response) : $this->sendResetFailedResponse($request, $response); } // 獲取重置密碼時的請求參數 protected function credentials(Request $request) { return $request->only( 'email', 'password', 'password_confirmation', 'token' ); } // 重置密碼的真實性驗證后,進行的持久化工作 protected function resetPassword($user, $password) { // 修改后的密碼、重新生成 remember_token $user->forceFill([ 'password' => bcrypt($password), 'remember_token' => Str::random(60), ])->save(); // session 中的用戶信息也進行重新賦值 $this->guard()->login($user); }
PHP應用“忘記密碼 => 發郵件 => 重置密碼” 的大體流程如下:
PHP應用權限管理
PHP應用權限管理是依靠內存空間維護的一個數組變量abilities來維護,結構如下:
PHP應用 $abilities = array( '定義的動作名,比如以路由的 as 名(common.dashboard.list)' => function($user) { // 方法的參數,第一位是 $user, 當前 user, 后面的參數可以自行決定 return true; // 返回 true 意味有權限, false 意味沒有權限 }, ...... );
PHP應用但只用 $abilities,會使用定義的那部分代碼集中在一起太煩索,所以有policy策略類的出現;
PHP應用policy策略類定義一組實體及實體權限類的對應關系,比如以文章舉例:
PHP應用有一個 Modal實體類叫 Post,可以為這個實體類定義一個PostPolicy權限類,在這個權限類定義一些動作為方法名;
PHP應用 class PostPolicy { // update 權限,文章作者才可以修改 public function update(User $user, Post $post) { return $user->id === $post->user_id; } }
PHP應用然后在ServiceProvider中注冊,這樣系統就知道,如果你要檢查的類是Post對象,加上你給的動作名,系統會找到PostPolicy類的對應方法;
PHP應用 protected $policies = [ Post::class => PostPolicy::class, ];
PHP應用怎么調用呢?
PHP應用對于定義在abilities數組的權限:
PHP應用對于policy策略類調用的權限:
PHP應用有用的技巧
PHP應用獲取當前系統注冊的權限,包括兩部分abilities和policies數組內容,代碼如下:
PHP應用 $gate = app(\Illuminate\Contracts\Auth\Access\Gate::class); $reflection_gate = new ReflectionClass($gate); $policies = $reflection_gate->getProperty('policies'); $policies->setAccessible(true); // 獲取當前注冊的 policies 數組 dump($policies->getValue($gate)); $abilities = $reflection_gate->getProperty('abilities'); $abilities->setAccessible(true); // 獲取當前注冊的 abilities 數組 dump($abilities->getValue($gate));
PHP應用總結
PHP應用以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/286.html