首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个表(模型)的CakePHP 8月

两个表(模型)的CakePHP 8月
EN

Stack Overflow用户
提问于 2014-07-08 15:49:00
回答 1查看 2.6K关注 0票数 0

我的数据库中有两个表,一个用于管理员(命名为Admins),另一个用于普通用户,名为:utilisateur(法语)。我知道我必须使用cakePHP的约定,它要求我必须创建一个名为users的表,其中包含用户名和密码。但问题是我有两张桌子,而不仅仅是一张。每个领域都有自己的特点,所以我真的需要它们分开。而且,就连登录所需的信息也因用户而异:

  • 管理员需要他们的登录+密码
  • 普通用户需要一个特定的id (在他们的身份证中)+密码。

我想要做的是创建两个登录页面,一个用于管理员,另一个用于普通用户。登录后,用户将被重定向到他应该看到的页面。但是,如果用户尝试尝试一个禁止的位置。我想阻止他(beforeFilter + isAuthorized )

我怎么才能把这一切做好呢?

我不是cakephp的初学者,我已经使用Auth组件在anotehr应用程序中创建了一个身份验证系统,但是比较简单,因为用户只需要一个表。

你的帮助将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-08 18:12:07

假设如下:

  • 您有两个与模型UserAdmin相关联的表,其中:
    • Useridcardpassword字段。
    • Adminloginpassowrd字段。

  • 密码使用User::hasPasswordAdmin::hasPassword函数进行散列。
  • 您的登录表单创建如下: 回显$this->Form->create(null,'');echo $this->Form->输入(‘登录’);echo $this->Form->输入(‘password’);echo $this->Form->end(‘Submit’);

您可以在Authenticate下创建一个新的App/Controller/Component/Auth/MyAuthenticate.php组件

代码语言:javascript
复制
<?php

App::uses('FormAuthenticate', 'Controller/Component/Auth');

class MyAuthenticate extends FormAuthenticate {

    public function authenticate(CakeRequest $request, CakeResponse $response) {

        $username = $request->data['login'] ;
        $password = $request->data['password'] ;

        App::import('Model', 'User') ;
        $userModel = new User () ;

        /* Try to authenticate as a user... */
        $user = $userModel->find('first', array(
            'conditions' => array(
                'idcard' => $username,
                'password' => User::hashPassword($password) ;
            )
        )) ;

        if ($user) {
            $user = $user['User'] ; // Get only useful info
            $user['type'] = 'user'; // Save user type
            return $user ;
        }

        /* Same thing for admin. */

        App::import('Model', 'Admin') ;
        $adminModel = new Admin () ;

        $user = $adminModel->find('first', array(
            'conditions' => array(
                'login' => $username,
                'password' => Admin::hashPassword($password) ;
            )
        )) ;

        if ($user) {
            $user = $user['Admin'] ; // Get only useful info
            $user['type'] = 'admin'; // Save user type
            return $user ;
        }

        return null ;

    }

};

您只需要确保管理员不能作为用户进行身份验证,并进行反向验证。

在你的AppController

代码语言:javascript
复制
public $components = array(
    'Auth' => array(
        'authenticate' => array('My'), // The prefix in front of your component
        'loginAction' => array(/* ... */),
        'loginRedirect' => array(/* ... */),
        'logoutRedirect' => array(/* ... */),
        'authError' => "...",
        'authorize' => 'Controller'
    )
) ;

登录操作与普通表相同:

代码语言:javascript
复制
public function login () {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } 
        else {
            $this->request->data['password'] = "" ;
            $this->Session->setFlash('Invalid login/id or password.');
        }
    }
}

然后,在beforeFilterisAuthorized中,您可以检查$this->Auth->user('type');。例如,在AppController中:

代码语言:javascript
复制
public function isAuthorized () {
    /* Assuming you keep CakePHP convention where action starting with admin_ set admin params. */
    if(isset($this->params['admin'])) {
        return $this->Auth->user('type') == 'admin' ;
    }
    return true ;
}

或者,如果要禁用非管理用户对AdminController中所有操作的访问,请使用beforeFilter

代码语言:javascript
复制
class AdminController extends AppController {

    public function beforeFilter () {
        if (!$this->Auth->loggedIn()) {
            $this->Session->setFlash('You need to be logged to access this page.');
            $this->redirect(/* Login url. */) ;
            return ;
        }
        if ($this->Auth->user('type') != 'admin') {
            $this->Session->setFlash('You need to be admin to access this page.');
            $this->redirect(/* Somewhere... */) ;
            return ;
        }
        return parent::beforeFilter () ;
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24636263

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档