2015-10-13 62 views
2

我有一個基於CakePHP 2.7 Framework構建的登錄程序。我使用Auth組件進行身份驗證,但爲了增加安全性,我也使用了令牌。當我進入登錄頁面並嘗試登錄時,它所做的只是提交表單,但沒有其他事情發生。該頁面基本上重新加載,不重定向,只是沒有。我做了一些挖掘,發現它可能不會超過用戶控制器中登錄函數的第一個條件。我真的很困惑爲什麼會這樣。我已經檢查了所有的錯誤日誌,沒有任何顯示,也沒有任何錯誤信息出現在屏幕上。下面我包含了UsersController,AppController,UserModel和Tour Class(用於令牌)。任何幫助將不勝感激!登錄程序CakePHP 2.7

UsersController:

<?php 

class UsersController extends AppController { 

    var $name = 'Users'; 
    var $uses = array('User', 'Team'); 

    function beforeFilter() { 
    $this->Auth->allow('check_login', 'index', 'wsdl', 'admin_wsdl', 'admin_service'); 
    parent::beforeFilter(); 
    } 


    function login() { 
    if (!empty($this->data) && $this->Auth->user()) { 
     // Delete all old tokens 
     $this->Tour->recursive = -1; 
     $this->Tour->deleteAll(array('Tour.userid' => $this->Auth->user('userid'))); 
     // Create a new token 
     $this->Tour->create(); 
     $this->Tour->save(array('token' => md5(rand()), 'userid' => $this->Auth->user('userid'))); 
     // Update login count 
     $user = $this->User->read(null, $this->Auth->user('userid')); 
     $user['User']['logincount']++; 
     $this->User->saveField('logincount', $user['User']['logincount']); 
     // Update last login time 
     $this->User->saveField('lastlogin', date('Y-m-d h:m:s')); 
     if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirectUrl()); 
     } 
     $this->Flash->error(__('Invalid username or password, try again')); 
     } 

    } 
    } 

function find_home() { 

    $user = $this->User->read(null, $this->Auth->user('userid')); 
    if ($user['User']['mentor'] == '1') { 
     $this->redirect('/admin/teams'); 
    }else if ($user['User']['teacher'] == '1') { 
     $this->redirect('/teacher/teams'); 
    }else{ 

     // Get this student's team 
     $this->redirect('/projects/view/?token=' . urlencode($user['Tour'][0]['token']) . '&projectid=' . urlencode($user['Team'][0]['id'])); 
    } 
    } 
?> 

AppController的:

class AppController extends Controller { 

    var $components = array(
    'Auth'=>array(
     'loginRedirect' => array('controller' => 'users', 'action' => 'find_home'), 
     'autoredirect'=>'false', 
     'authError'=>"Please Log In to Access this Page.", 
     'authorize'=>array('Controller')) 
    , 'Session', 'RequestHandler'); 
    var $uses = array('Tour'); 

    function beforeFilter() { 
    $this->setLayout(); 

    if ($this->Session->check('Auth.User.userid')) { 
     $tour = $this->Tour->findByUserid($this->Session->read('Auth.User.userid')); 
     $user = $this->Auth->user(); 
     $tour = $this->Tour->findByUserid($user['User']['userid']); 
     $user['Tour'] = $tour['Tour']; 
     $this->set('user', $user); 
    }else if (isset($_GET['token'])) { 
     $tour = $this->Tour->read(null, $_GET['token']); 
     if ($tour) { 
     $tour['Tour']['sessionmodified'] = date('Y-m-d H:i:s'); 
     $this->Tour->save($tour); 
     $this->set('user', $tour); 
     } 
    } 
    } 

    private function setLayout() { 
    if (array_key_exists('prefix', $this->params)) { 
     if ($this->params['prefix'] == 'admin') { 
     $this->layout = 'admin'; 
     }else if ($this->params['prefix'] == 'teacher') { 
     $this->layout = 'teacher'; 
     } 
    } 
    } 

} 

?> 

的usermodel:

<?php 

class User extends AppModel { 

    var $name = 'User'; 
    var $primaryKey = 'userid'; 
    var $displayField = 'username'; 

    var $hasAndBelongsToMany = array(
    'Team' => array(
     'className' => 'Team', 
     'joinTable' => 'teamAssignments', 
     'foreignKey' => 'userid', 
     'associationForeignKey' => 'teamid', 
     'unique' => 'true' 
    ) 
); 

    var $hasMany = array(
    'Assessment' => array(
     'className' => 'Assessment', 
     'foreignKey' => 'student_id' 
    ), 
    'Assessment' => array(
     'className' => 'Assessment', 
     'foreignKey' => 'teacher_id' 
    ), 
    'AssessmentComment' => array(
     'className' => 'AssessmentComment', 
     'foreignKey' => 'student_id' 
    ), 
    'AssessmentComment' => array(
     'className' => 'AssessmentComment', 
     'foreignKey' => 'teacher_id' 
    ), 
    'Tour' => array(
     'className' => 'Tour', 
     'foreignKey' => 'userid' 
    ), 
    'Wiki' => array(
     'className' => 'Wiki', 
     'foreignKey' => 'user_id' 
    ) 
); 

    function archive($id) { 
    if (!$id) { 
     return false; 
    }else{ 
     $this->recursive = -1; 
     $user = $this->read(null, $id); 
     $user['User']['active'] = false; 
     $this->save($user); 
     return true; 
    } 
    } 

    function getInfo($id, $team_id, $token) { 
    $user = $this->read(null, $id); 
    // Make sure the token is valid 
    $this->Tour->recursive = 2; 
    $tour = $this->Tour->findByToken($token); 
    if ($team_id == '_definst_') { 
     // This is the global chat application or some other exception 
     $user = $this->read(null, $id); 
     $info['userid'] = $user['User']['userid']; 
     $info['firstName'] = $user['User']['firstName']; 
     $info['lastName'] = $user['User']['lastName']; 
     $info['emailAddress'] = $user['User']['emailAddress']; 
     $info['logincount'] = $user['User']['logincount']; 
     $info['lastlogin'] = $user['User']['lastlogin']; 
     $info['mentor'] = $user['User']['mentor']; 
     return http_build_query($info); 
    }else if ($tour) { 
     // Make sure this user has permission to look at this team 
     $on_team = false; 
     if ($tour['User']['mentor'] == 1) { 
     $on_team = true; 
     }else{ 
     foreach ($tour['User']['Team'] as $team) { 
      if ($team['id'] == $team_id) { 
      $on_team = true; 
      break; 
      } 
     } 
     } 
     if ($on_team) { 
     // Make sure the requested user is on the requested team 
     $user = $this->read(null, $id); 
     $on_team = false; 
     if ($tour['User']['mentor'] == 1) { 
      $on_team = true; 
     }else{ 
      foreach ($user['Team'] as $team) { 
      if ($team['id'] == $team_id) { 
       $on_team = true; 
       break; 
      } 
      } 
     } 
     if ($on_team) { 
      $team = $this->Team->read(null, $team_id); 
      $info['userid'] = $user['User']['userid']; 
      $info['username'] = $user['User']['username']; 
      $info['firstName'] = $user['User']['firstName']; 
      $info['lastName'] = $user['User']['lastName']; 
      $info['emailAddress'] = $user['User']['emailAddress']; 
      $info['teacher'] = $user['User']['teacher']; 
      $info['logincount'] = $user['User']['logincount']; 
      $info['lastlogin'] = $user['User']['lastlogin']; 
      $info['mentor'] = $user['User']['mentor']; 
      $info['teamid'] = $team['Team']['id']; 
      $info['teamName'] = $team['Team']['teamName']; 
      $info['instancename'] = $team['Team']['instanceName']; 
      return http_build_query($info); 
     } 
     } 
     return ''; 
    } 
    } 

    function validate($token, $team_id) { 
    $this->Tour->recursive = 2; 
    $tour = $this->Tour->findByToken($token); 
    if ($team_id == '_definst_') { 
     // This is the global chat application or some other exception 
     return $tour['Tour']['userid']; 
    }else if ($tour) { 
     // Make sure this user is on this team or is a mentor 
     $on_team = false; 
     if ($tour['User']['mentor'] == 1) { 
     $on_team = true; 
     }else{ 
     foreach ($tour['User']['Team'] as $team) { 
      if ($team['id'] == $team_id) { 
      $on_team = true; 
      break; 
      } 
     } 
     } 
     if ($on_team) { 
     return $tour['Tour']['userid']; 
     } 
    } 
    return ''; 
    } 

    function wsKillSession($token) { 
    // Delete any tours 
    $this->Tour->recursive = -1; 
    $tour = $this->Tour->findByToken($token); 
    $this->Tour->deleteAll(array('Tour.userid' => $tour['Tour']['userid'])); 
    return 1; 
    } 

    function getUsers($user_id, $token, $team_name) { 
    $user = $this->read(null, $user_id); 
    $team = $this->Team->find('first', array('conditions' => array('Team.teamName' => $team_name))); 
    $users = array(); 
    foreach ($team['User'] as $user) { 
     $users[] = array('userid' => $user['userid'], 'username' => $user['username'], 'firstName' => $user['firstName'], 'lastName' => $user['lastName'], 'emailAddress' => $user['emailAddress'], 'teacher' => $user['teacher'], 'logincount' => $user['logincount'], 'lastlogin' => $user['lastlogin'], 'mentor' => $user['mentor'], 'teamid' => $team['Team']['id'], 'teamName' => $team['Team']['teamName'], 'instanceName' => $team['Team']['instanceName']); 
    } 
    return serialize($users); 
    } 

} 

?> 

遊類別:

<?php 



    class Tour extends AppModel { 

     var $name = 'Tour'; 
     var $useTable = 'sessions'; 
     var $primaryKey = 'token'; 

     var $belongsTo = array(
     'User' => array(
      'className' => 'User', 
      'foreignKey' => 'userid' 
     ) 
    ); 

    } 

    ?> 

回答

0

$this->Auth->user()返回null如果用戶未登錄,則需要檢查nullhttp://api.cakephp.org/2.7/source-class-AuthComponent.html#648-671

function login() { 
    if (!empty($this->data) && !$this->Auth->user()) { 

    } 
} 
+0

謝謝!工作!但現在由於某種原因,它拋出一個MySQL錯誤說:「完整性約束違規:1048列'userid'不能爲空'任何想法可能會導致這種情況? – Rick

+0

This line:'$ this-> Tour-> save(array('token'=> md5(rand()),'userid'=> $ this-> Auth-> user('userid')));'' ,'$ this-> Auth-> user('userid')'將返回'null'。 – user3082321

+0

我該如何解決這個問題? – Rick