2017-04-19 122 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'),但仍然没有任何结果。 –