我有一个非常奇怪的问题,我再也弄不明白了。我设置了一个cakepp项目,我使用一个用户控制器来处理身份验证,一切都很完美,100%。然而,我决定删除用户控制器、模型和视图,并将其替换为我的Members,因为Members将是我的主要用户,有一两个需要登录的Admin。
我现在在Members模型、控制器和视图中的内容与我在Users中的内容相同,但默认情况下CakePHP希望使用Users模型和Users表。我做了很多调试,最终让CakePHP使用Members而不是Users,但现在我无法登录,因为我一直收到您的用户名和密码不正确,请重试错误消息。
以下是我的代码,我希望有人能给我指明正确的方向。
AppController.php
function beforeFilter()
{
$this->Auth->userModel = 'Member';
$this->Auth->authorize = array('Member');
$this->Auth->allow(array('view', 'index', 'add', 'edit', 'delete'));
}
public $helpers = array ('Html', 'Form', 'Session', 'Time');
public $components = array(
'DebugKit.Toolbar',
'Session',
'Cookie',
'Auth' => array(
'loginAction' => array(
'controller' => 'members',
'action' => 'login'
),
'authError' => 'Your username and password is incorrect, please try again.',
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'username', 'password' => 'password',
'scope' => array('Member.deleted' => '0')),
'passwordHasher' => 'Blowfish'
)
)
)
);
关于beforeFilter()
,需要注意的是,我尝试了Member的复数和单数,还添加了$this-Auth->allow()
函数,这样我就可以在Members表中编辑我的用户密码,以确保我的密码被正确散列。
另一个注意事项是,无论我在哪里看到,当使用loginAction
时,它都应该是这样的:
'loginAction' => array(
'controller' => 'Members',
'action' => 'login',
'plugin' => 'members'
),
使用上面的问题是,我的URL结构最终看起来像/members/Members/login
,因此我在AppController中省略了插件键值对,我猜这可能是错误所在。
成员.php
public function isAuthorized($user) {
if (!empty($this->request->params['admin'])) {
return $user['role'] === 'admin';
}
return !empty($user);
}
public function beforeSave($options = array()) {
if (isset($this->data['Member']['password'])) {
$this->data['Member']['password'] = AuthComponent::password($this->data['Member']['password']);
}
return true;
}
MembersController.php
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()){
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(__('Your username and password is incorrect, please try again.'));
} // end if cannot log in
} // end if no form submitted
} // end login
public function logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}
/成员/登录.ctp
<div id="page-content" class="span9">
<div class="users form">
<?php echo $this->Form->create('Member', array('inputDefaults' => array('label' => false), 'class' => 'form form-horizontal')); ?>
<fieldset>
<h2><?php echo __('Member Login'); ?></h2>
<div class="control-group">
<?php echo $this->Form->label('username', 'username', array('class' => 'control-label'));?>
<div class="controls">
<?php echo $this->Form->input('username', array('class' => 'span12')); ?>
</div><!-- .controls -->
</div><!-- .control-group -->
<div class="control-group">
<?php echo $this->Form->label('password', 'password', array('class' => 'control-label'));?>
<div class="controls">
<?php echo $this->Form->input('password', array('class' => 'span12')); ?>
</div><!-- .controls -->
</div><!-- .control-group -->
<?php echo $this->Form->input('auto_login', array('type' => 'checkbox', 'label' => 'Remember me?')); ?>
</fieldset>
<?php echo $this->Form->submit('Submit', array('class' => 'btn btn-large btn-primary')); ?>
<?php echo $this->Form->end(); ?>
</div>
</div>
正如我所说,任何帮助都将是惊人的,因为我已经经历了我所知道的一切,试图让它发挥作用。。。
对于我想要登录的每种类型的组(例如,管理员、用户或任何其他类型),您必须在控制器和模型中写入以下内容:
假设你有两种类型的用户,他们有不同的平台可以访问,比如管理员和用户
在控制器/AppController.php 中
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers/')
)
),
'Session');
在模型/管理员中.php
public function beforeSave() {
if (isset($this->data['Administrator']['password'])):
$this->data['Administrator']['password'] = AuthComponent::password($this->data['Administrator']['password']);
return true;
endif;
}
public $actsAs = array('Acl' => array('type' => 'requester'));
public function parentNode() {
if (!$this->id && empty($this->data)) {
return null;
}
if (isset($this->data['Administrator']['group_id'])) {
$groupId = $this->data['Administrator']['group_id'];
} else {
$groupId = $this->field('group_id');
}
if (!$groupId) {
return null;
} else {
return array('Group' => array('id' => $groupId));
}
}
在控制器/管理员Controller.php beforeFilter()中
$this->Auth->authorize = array('Actions' => array('actionPath' => 'controllers/', 'userModel' => 'Administrator'));
$this->Auth->authenticate = array('Form' => array('userModel' => 'Administrator'));
$this->Auth->loginAction = array(/*YOUR PATH HERE*/);
$this->Auth->logoutRedirect = array(/*YOUR PATH HERE*/);
$this->Auth->loginRedirect = array(/*YOUR PATH HERE*/);
如果你的所有团队都有这个,它应该会很好
我有三种类型的组:管理员、商店和用户,这对我很有用
尝试将其放入AppController.php文件的beforeFilter中
$this->Auth->loginAction = array('controller' => 'members', 'action' => 'login');
这应该会将您的应用程序引导到正确的url进行登录。
你读过这个吗?如果不现在就做。
这里的例子很清楚:
$this->Auth->authenticate = array(
'Basic' => array('userModel' => 'Member'),
'Form' => array('userModel' => 'Member')
);
您必须指定Form身份验证适配器应该使用的模型。
对于重定向问题,定义
$this->Auth->redirectUrl = array(/* Whatever you need here */);