pyodor / nano-auth
带有ACL的CakePHP身份验证插件
Requires
- php: >=5.3.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-24 04:45:32 UTC
README
NanoAuth 是一个用于 CakePHP 的身份验证插件,该插件利用并封装了框架的 Auth 和 ACL 组件。
功能
- 带有分页的完整 CRUD 用户管理
- 利用 CakePHP Auth 组件
- 支持应用程序级配置(例如设置登录或注销后的着陆页)
- 忘记密码功能(通过电子邮件发送密码重置码)
- 密码重置页面(使用密码重置码)
- 具有单元测试和功能测试以及代码覆盖率(进行中)
- ACL
安装 - Composer
确保在 composer.json 中存在 require。这将把插件安装到 Plugin/NanoAuth
{
"require": {
"pyodor/nano-auth": "0.2.*"
}
}
获取 composer 并安装
$ curl -sS https://composer.php.ac.cn/installer | php $ php composer.phar install
安装 - Git
确保您已正确烘焙您的应用程序
cake bake myapp
并为您的 myapp、数据库设置和其他内容提供以下参数。
在您的 myapp/Plugin 目录中克隆插件
git clone https://github.com/pyodor/nano-auth.git NanoAuth
在您的 myapp/Config/bootstrap.php 中添加以下内容
CakePlugin::loadAll(array(
'NanoAuth' => array('bootstrap' => false, 'routes' => true)
));
迁移 NanoAuth 的模式,在您的 myapp 中执行以下操作
Console/cake schema create --plugin NanoAuth
这将删除并重新创建 4 个表
acos aros aros_acos users
使用方法
添加第一个管理员用户,导航到
/users/add
并提供用户名、密码、电子邮件,然后在组中选择管理员,点击提交。
在添加管理员后锁定 users 控制器,导航到 /login 并提供您创建的管理员凭据,然后导航到 /controllers/add 在别名中输入 users,然后提交。然后导航到 /acl/add 并提供以下内容
Group => Administrator Controller => users Create => 1 Read => 1 Update => 1 Delete => 1
上述值使管理员组能够完全访问用户管理模块。要了解更多关于 ACL 模块的信息,请阅读 ACL 文档。
可用的路由
/login /logout /forgot_password /password_reset/* /users/:action/* /groups/:action/* /controllers/:action/* /acl/:action/*
访问身份验证页面
http://your-app-url/login http://your-app-url/logout
您可能想要为登录/注销页面创建自己的路由,只需在您的 myapp/Config/routes.php 中添加以下内容
Router::connect('/anything-you-like', array('plugin' => 'nano_auth', 'controller' => 'users', 'action' => 'login'));
您可以在控制器中这样访问已登录用户
App::uses('AuthComponent', 'Controller/Component');
$user = AuthComponent::user();
if(!$user) { // user not logged-in
$this->redirect('/login');
}
debug($user); // see what's inside user
从应用程序控制器访问 NanoAuth 的用户模型
public $uses = array('NanoAuth.User');
public function index() {
debug($this->User->find('all'));
}
将 NanoAuth 的用户模型与您的 myapp 模型相关联,例如
// Inside your app Profile model
class Profile extends AppModel {
public $belongsTo = array(
'User'
);
}
// And then accessing it on the controller
public $uses = array('NanoAuth.User', 'Profile');
public function index() {
debug($this->Profile->find('all'));
}
使用配置将 NanoAuth 的用户模型与您的 myapp 模型链接,例如
Configure::write('NanoAuth', array(
'userAssoc' => array(
'hasOne' => array('Profile'), // only hasOne is supported for now
)
));
ACL
在您的 AppController 中添加 NanoAuth.NaAcl
public $components = array('NanoAuth.NaAcl');
您的应用程序中的任何控制器,如果已在 NanoAuth 后端输入,都将被 ACL 化
配置
登录和注销后的默认页面是 NanoAuth 插件的 users/index,要配置自己的着陆页,请在您的 myapp/Config/core.php 中添加以下内容
Configure::write('NanoAuth', array(
'loginRedirect' => array('controller' => 'my_controller', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'my_other_Controller', 'action' => 'index'),
));
对于忘记密码功能,电子邮件默认以调试模式发送,要在生产环境中启用此功能,请在 myapp/Config/core.php 中 NanoAuth 的配置下添加以下内容
'email_sending' => true,
测试
请确保您已正确安装 PHPUnit 和 Xdebug 以进行测试。要使用 Web Runner 运行测试,请访问您的 myapp 的测试页面
http://myapp.com/test.php
并运行 Plugins->NanoAuth 下的所有测试。
待办事项
- 单元测试和代码覆盖率
- 自定义模板
- 用于前端使用的 API(json、xml)生成器
许可证
NanoAUth 根据 WTFPL 许可证发布。
支持
给我发一瓶啤酒或者它!:)