php cake Auth在课堂上

时间:2017-03-23 07:26:01

标签: php cakephp

我需要检查mgrUser表中是否存在用户。现在,propblem是控制器在adminController中,而模型在mgrUserModel中。我如何使用Auth?这就是我制作通用登录代码的原因。

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'];
        }

    }

2 个答案:

答案 0 :(得分:0)

您需要加倍==来比较事物,

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

您所做的是将"me"字符串分配给变量$user,该变量始终返回true,因为可以进行分配

无论如何你应该在beforeFilter中使用它,它在此控制器的每个动作之前运行,这更有意义

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

答案 1 :(得分:0)

Auth组件可以配置为通过另一个userModel(users表的型号名称)读取用户信息。它默认为Users

请参阅本书以获取适当的cakephp版本:https://book.cakephp.org/3.0/en/controllers/components/authentication.html#configuring-authentication-handlers