我有一个非常奇怪的问题,我根本无法弄清楚。我设置了一个cakephp项目,我正在使用Users控制器来处理身份验证,一切都运行良好,100%。但是,我决定删除用户控制器,模型和视图,并将其替换为我的会员,因为会员将是我的主要用户,有一个或两个管理员需要登录。
我现在在我的会员模型,控制器和视图中所拥有的内容与我在用户中所拥有的相同,但默认情况下,CakePHP希望使用Users模型和用户表。我做了很多调试,最终得到了CakePHP来使用会员代替用户,但现在我根本无法登录,因为我一直得到 您的用户名和密码不正确,请再试一次 错误消息。
这是我通过代码的方式,我希望有人可以指出我正确的方向。
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()
的重要注意事项是我尝试了复数和单数的成员,我还添加了$this-Auth->allow()
函数,以便我可以在Members表中编辑我的用户密码确保我的密码正确地进行了哈希。
另外请注意,在我看到的任何地方,我都看到在使用loginAction
时,它应该如下所示:
'loginAction' => array(
'controller' => 'Members',
'action' => 'login',
'plugin' => 'members'
),
使用上面的问题是我的URL结构最终看起来像/members/Members/login
因此我省略了我的AppController中的插件键值对,我猜这可能是错误所在。
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;
}
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());
}
<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>
正如我所说的,任何帮助都会令人惊讶,因为我已经完成了我所知道的所有尝试并让它发挥作用......
答案 0 :(得分:1)
你有read this吗?如果不现在就这样做。
这个例子非常明确:
$this->Auth->authenticate = array(
'Basic' => array('userModel' => 'Member'),
'Form' => array('userModel' => 'Member')
);
您必须指定表单身份验证适配器应使用的模型。
对于重定向问题,请定义
$this->Auth->redirectUrl = array(/* Whatever you need here */);
答案 1 :(得分:1)
尝试将其放在AppController.php文件的beforeFilter中
$this->Auth->loginAction = array('controller' => 'members', 'action' => 'login');
这应该将您的应用程序指向正确的URL以进行登录。
答案 2 :(得分:1)
要登录的foreach类型的组(例如,管理员,用户或任何其他类型),您必须在控制器和模型中编写下一个内容:
假设您有两种类型的用户可以访问不同的平台,例如管理员和用户
在Controller / AppController.php中
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers/')
)
),
'Session');
在Model / Administrator.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 / AdministratorsController.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*/);
如果您的所有群组都有此功能,那么它应该会很棒! 我有三种类型的组:管理员,商店和用户,这对我有用!