2012-06-21 63 views
5

我有一个Cake网站,它需要有两个单独的登录,每个登录表单都会有自己的登录表单并且看到不同的页面,因此有两个不同的表格会很好,因为它们之间没有相似之处两种人。CakePHP 2个单独的登录表

每个登录表单只会被某些人使用,他们将永远不会登录到其他表单,反之亦然。

此外,两个登录表之间有一个关系,它需要2个表?

这可能吗?

+1

这看起来像什么,我需要但对于旧版本的蛋糕,说不定@deizel会看到这个:) – 472084

+0

相同的概念,除了'identify'被移到了authenticate对象上,并被称为'authenticate'(或者''特别是'BaseAuthenticate'上的'_findUser'方法)。 – jeremyharris

+0

所以,只要将'identify'改为'authenticate'? http://codepad.viper-7.com/1BX9fE – 472084

回答

10

首先,添加几个空的自定义身份验证对象。我们将重用FormAuthenticate使用的相同逻辑(即使用POST数据为用户检查数据库),但只需在对象设置中更改模型(稍后)即可。

应用程序/控制器/组件/认证/ ModelOneAuthenticate.php

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

class ModelOneAuthenticate extends FormAuthenticate { 
} 

应用程序/控制器/组件/认证/ ModelTwoAuthenticate.php

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

class ModelTwoAuthenticate extends FormAuthenticate { 
} 

然后告诉您的应用程序使用这些对象进行身份验证,并告诉它使用什么模型。您也可以在此自定义字段。在你的AppController:

public $components = array(
    'Auth' => array(
     'authenticate' => array(
      'ModelOne' => array(
       'userModel' => 'ModelOne', 
       'fields' => array(
        'username' => 'my_custom_username_field', 
        'password' => 'some_password_field' 
       ) 
      ), 
      'ModelTwo' => array(
       'userModel' => 'ModelTwo' 
      ) 
     ) 
    ) 
); 

第一认证对象将检查model_ones表中my_custom_username_fieldsome_password_field用户名和密码,而第二个将使用标准usernamepassword领域检查model_twos

+0

感谢您的支持!到目前为止,我已经完成了所有的代码并没有错误,但是当我去一个受限制的页面时,它会尝试去用户/登录时,我已经说过要去代理/登录 - 唉想法为什么?这是我的:http://codepad.org/zvBeJFEX – 472084

+0

你需要设置loginRedirect属性:http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html# AuthComponent :: $ loginRedirect – jeremyharris

+0

我有这样的设置:http://codepad.org/zvBeJFEX - 我需要将它添加到新创建的auth类,而不是某种方式吗? – 472084

1

当他们必须登录时有相似性:两者都会要求它输入凭据,通常是用户名/电子邮件和密码。所以用户表和foo_profiles表和bar_profiles表也取决于用户类型。

如果你真的想用两个不同的表和他们的MVC堆栈,那么只需使用两个不同的控制器FooUsers和BarUsers,并在每个控制器内创建一个定制的登录方法。

+0

这两个用户类型中的每一个都只能登录到他们的一个表单,而不是另一个。每个表单都需要不同的用户帐户。 你知道任何资源来帮助“创建自定义登录方法”吗?谢谢。 – 472084

+0

是的,请查看本教程:http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html在登录方法中,只需执行您的任何要求即可。 – burzum

+0

我不能看到有任何帮助有两个登录表,我只是将它说的把代码放在AppController到它的单独控制器的代码? – 472084

1

我之前通过编写扩展自BaseAuthenticate的自定义身份验证组件来完成此操作。只要他们实现authenticate()方法,那么你就可以为每种不同类型的用户做任何你想做的事情。

在你的AppController你需要做类似

public $components = array(
    "Session", 
    "Auth" => array(
     'authenticate'  => array("UserType1", "UserType2"), 
    ) 
); 

退房的cookbook为它的其余部分进行注册的不同组件。

+0

你能分享你使用的一些代码吗? (用于自定义认证对象) – 472084

+0

我不想TBH。但它的本质是,如果您对经过身份验证的用户满意,则返回一个数组,然后将其存储在$ auth-> user中。如果你不开心,那么你返回'假'和蛋糕处理。我提供的链接会将您带到身份验证控制器的相关部分。 –

+0

因此,在新的认证对象中,我只需要说'(如果登录名和密码匹配表1){return $ row1; } elseif(登录和密码匹配表2){return $ row2; } else {return false; }'? – 472084

2

要做到这一点最简单的方法是只为每个登录类型不同的会话密钥:

if ($loginTypeOne) { 
    $this->Auth->authenticate = array(
    'Form'=> array(
     'userModel'=> 'TypeOne', 
    ) 
    ); 
    AuthComponent::$sessionKey = 'Auth.TypeOne'; 
} else { 
    $this->Auth->authenticate = array(
    'Form'=> array(
     'userModel'=> 'TypeTwo', 
    ) 
    ); 
    AuthComponent::$sessionKey = 'Auth.TypeTwo'; 
} 
0
You can have a look on this. 
Define Model for both login member and then define table which you want to use for the user. 
set variable in model. 


class SearchedCategory extends AppModel { 
    var $name = 'SearchedCategory'; 
    Var useTable = 'give your table name here.'; 
    var $primaryKey = 'id'; 


}