2017-03-23 14 views
-3

ユーザがmgrUserテーブルに存在するかどうかを確認する必要があります。モデルがmgrUserModelにある間、コントローラはadminControllerにあります。私はこのために認証をどのように使用するのですか?それは私が一般的なログインコードを作った理由です。php cake auth inside class

あなたは割り当てがとにかくあなたが beforeFilterでそれを使用する必要があります

可能だったので、何をやったことは、常に trueを返す変数 $userから "me"文字列を割り当てた

function checkMe() 
{ 
    if($user == 'me'){ 
     $this->Auth->allow('detail'); 
    } 
} 

、物事を比較するために、二重==が必要

public function login() { 
     // if ($this->Auth->login()) { 
     //  return $this->redirect($this->Auth->redirectUrl()); 
     // } 
     // $this->Flash->error(
     //  __('Username ou password incorrect') 
     //); 


     //since the model is in a different view, I needed to includ the mgrModel and create a generic login 
     //will revamp the code to fit the built in Aut code for php cake 
     if(isset($_POST['submit'])) { 
      $User_ID = htmlspecialchars($_POST['user_id']); 
      $Pass = htmlspecialchars($_POST['pass']); 

      try { 
        $mgrUserModel = new MgrUser(); 
        $isValid = $mgrUserModel->find('first', array(
         'conditions' => array("user_id" => $User_ID) 
        )); 
        if($isValid != null){ 
         if (($isValid['MgrUser']['pass']) == $Pass) { 
//this doesnot work 
$this->Auth->allow(); 
          $this->redirect($this->Auth->redirectUrl()); 
         } 
         else{ 

         } 
        } 

       } catch (Exception $e) { 
        //echo "not logged in"; 
       } 
      // this echo will show the id and pass that was taken based on the user_id and pass that the user will input 
      //for testing only 
      // echo $isValid2['MgrUser']['id']; 
      // echo $isValid2['MgrUser']['pass']; 
     } 

    } 

答えて

0

このコントローラーからのすべてのアクションの前に実行されています。これははるかに意味があります。

public function beforeFilter() { 
    parent::beforeFilter(); 
    if($user == 'me'){ 
     $this->Auth->allow('detail'); 
    } 
} 
+0

、本当の問題はあります$ this-> Auth->(「詳細」)が可能。条件が満たされていても動作していません(別のコードで) –

+0

@ReyNorbertBesmonte ok私の編集を確認してください – Sojtin

+0

はすでにそれを試しました。コマンドはボタンがクリックされたときにのみ実行されます。したがって、それは関数verify(){//} –