【发布时间】:2011-06-28 14:43:35
【问题描述】:
是否可以在没有用户模型的情况下使用 Auth 组件?我希望我的应用程序只有一个用户,所以我不想制作用户模型我只想使用一个登录名/密码登录,该登录名/密码将存储在 php 文件中。
【问题讨论】:
标签: cakephp components authentication
是否可以在没有用户模型的情况下使用 Auth 组件?我希望我的应用程序只有一个用户,所以我不想制作用户模型我只想使用一个登录名/密码登录,该登录名/密码将存储在 php 文件中。
【问题讨论】:
标签: cakephp components authentication
简单的答案是否。
更长的答案是您想使用模型来存储此信息。在我看来,将用户密码存储在 PHP 文件中是一个非常糟糕的主意。您会将系统设置为完全不灵活。当您再获得 5 个用户时会发生什么?
最好有一个带有 1 条记录的 users 数据库表设置,然后是一个 users PHP 文件。从长远来看,由于 Cake 的 AuthComponent 设置为处理数据库表,因此工作量会少很多。
另外,read this post on Stack Overflow about storing passwords。它将提供一些关于 Cake 的 AuthComponent 为何如此运作的见解。
【讨论】:
虽然你可以创建一个自定义的身份验证类,但我不知道要使用它,除了学习内部的行为。
// Controller/Component/Auth/CustomBasicAuthenticate.php
class CustomBasicAuthenticate {
public function authenticate(CakeRequest $request, CakeResponse $response) {
return true;
}
public function getUser($request) {
if (env('PHP_AUTH_USER') === 'foo' && env('PHP_AUTH_PW') === 'bar') {
return array(
'User' => array(
'id' => '1', 'username' => 'foo', 'password' => 'bar'
)
);
} else {
return false;
}
}
public function unauthenticated(CakeRequest $request, CakeResponse $response) {
$response->statusCode('401');
$response->header('WWW-Authenticate: Basic realm="My Realm"');
$response->body('fail');
$response->send();
}
}
// Controller/HelloController.php
class HelloController extends AppController {
public $autoRender = false;
public $uses = array('User');
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'hello',
'action' => 'index',
),
'authenticate' => array(
'CustomBasic' => array('userModel' => 'User')
)
)
);
public function index() {
echo 'ok';
}
}
【讨论】: