2017-04-19 72 views
0

我想知道如何通過單擊具有用戶標識的電子郵件URL自動登錄用戶。CakePHP 3.x多種登錄方法

目前,登錄的唯一方法是在登錄屏幕中輸入用戶名和密碼。

基本上我只需要知道是否有方法讓我使用$ this-> Auth-> identify()和用戶標識,而不是用戶名和密碼。

下面是我用它來識別用戶的代碼:

if($this->request->is('post')){ 
     $user = $this->Auth->identify(); 
     if($user){ 
      $this->Auth->setUser($user); 
      return $this->redirect(['controller' => 'dashboards']); 
     }else{ 
      $this->Flash->error(__('Incorrect login!')); 
      $this->redirect(['controller' => 'Users', 'action' => 'login']); 
     } 

    } 
+0

要驗證用戶,只有用戶ID? –

+0

我想通過常規登錄(用戶名,密碼)以及使用電子郵件中附加的鏈接(用戶ID)驗證用戶。 –

回答

0

好的,所以我做了什麼是創建一個全新的功能控制器,並使用電子郵件中的表單,以便通過POST發送用戶標識。這可以防止其他用戶手動輸入URL並附加一個隨機數字以訪問其他帳戶。

首先,我不得不允許該功能繼續進行而無需通過驗證。

public function beforeFilter(Event $event){ 
    $this->Auth->allow('emailLogin'); 
} 

public function emailLogin(){ 
    $this->autoRender = false; 
    $user_id = $_POST['user_id']; 

    if($this->Auth->user()){ 
     $this->Flash->error(__('You are already logged in!')); 
     return $this->redirect(['controller' => 'dashboards']); 
    } 

這裏是我使用的功能:

if($user_id == null){ 
     $this->Flash->error(__('Access has been denied!')); 
     $this->redirect(['controller' => 'Users', 'action' => 'login']); 
    }else{ 
     $user = $this->Users->get($user_id); 
     if($user){ 
      $this->Auth->setUser($user->toArray()); 
      $this->Flash->success('E-mail log in successful.'); 
      $this->redirect(['controller' => 'dashboards']); 
     }else{ 
      $this->Flash->error(__('Access has been denied!')); 
      $this->redirect(['controller' => 'Users', 'action' => 'login']); 
     } 
    } 
} 
+0

我不明白你對此有何擔保。似乎任何人都可以將任何用戶標識放在手動構建的URL的末尾,並以該人員的身份登錄。 –

+0

在URL末尾添加用戶標識實際上拒絕用戶訪問,因爲我通過表單發送了用戶標識。 –

+1

啊,我錯過了。但是,什麼阻止某人制作自己的提交任意用戶標識的表單呢?或者編輯您發送給他們的電子郵件來執行此操作? –

0

我找到了解決方案,

第一允許登錄的方法來與走出登錄訪問,

class UsersController extends AppController 
    { 
     public function initialize(){ 
       parent::initialize(); 
       $this->Auth->allow('login'); 
      } 



public function login($id = null) // here login method can accept parameter you pass with URL from email 
    { 
     if($id){ // If it is link with User Id 
      $this->loadModel('Users'); 
      $user = $this->Users->get($id); // get user data 
      if($user){ 
       $this->Auth->setUser($user); // set user 
       $this->Flash->success(__('Welcome')); 
       return $this->redirect($this->Auth->redirectUrl()); 
      }else{ 
       $this->Flash->error(__('Invalid username or password, try again')); 
      } 
     } 
     if ($this->request->is('post')) { // if use login page with username &password 
      $user = $this->Auth->identify(); // use Auth method to identify 
      if ($user) { 
       $this->Auth->setUser($user); 
       $this->Flash->success(__('Welcome')); 
       return $this->redirect($this->Auth->redirectUrl()); 
      }else{ 
       $this->Flash->error(__('Invalid username or password, try again')); 
      } 
     } 
    } 

    } 

這將工作爲你

+0

不幸的是,這並不適合我。在下面的鏈接,期待它自動登錄我,它只是重定向到登錄頁面,並要求輸入用戶名和密碼。 –

+0

你把$ this-> Auth-> allow('login');初始化()? –

+0

我做到了。我也嘗試在$ beforeFilter()中放入$ this-> Auth-> allow('login'),但仍然沒有任何結果。 –