felipepanegalli / authenticator-cakephp-3
CakePHP认证插件
dev-master
2018-01-09 21:17 UTC
Requires
- cakephp/cakephp: ^3.4
Requires (Dev)
- phpunit/phpunit: ^5.7|^6.0
This package is not auto-updated.
Last update: 2024-09-26 16:55:52 UTC
README
安装
由Felipe Panegalli开发的插件
http://panegalli.esy.es/
要安装此包,请下载composer并执行以下安装命令
composer require felipepanegalli/authenticator-cakephp-3:dev-master
下载后,需要在config/bootstrap.php
文件的末尾激活插件
Plugin::load('Authenticator', ['bootstrap' => false, 'routes' => true]);
首先,使用以下命令生成数据库
bin/cake migrations migrate --plugin=Authenticator
随后,使用以下命令生成种子
bin/cake migrations seed --plugin=Authenticator
生成插件的种子后,会在数据库中添加两个用户记录,即admin和user,两者的密码与用户名相同,即admin是admin,user是user。
激活插件后,没有任何控制器允许访问,为此只需在您希望控制访问的控制器中添加以下函数
public function isAuthorized($user)
{
//Verifica qual é a action
$action = $this->request->getParam('action');
//Carrega o Model de Regras
$this->loadModel('Authenticator.AuthRoles');
//Carrega a ragra baseado na role_id do usuário
$role = $this->Roles->find()->where(['id' => $this->Auth->user('role_id')])->first();
//Verifica se o usuário é administrador e está nas actions do array, permite o acesso das actions inclusas
if (strtolower($role['title']) == 'administrator' and in_array($action, ['index', 'view', 'add', 'edit', 'delete'])) {
return true;
//Verifica se o usuário é usuario e está nas actions do array, permite o acesso das actions inclusas
} elseif (strtolower($role['title']) == 'user' and in_array($action, ['index', 'view'])) {
return true;
//Se nenhuma das condições foram True, nega o acesso
} else {
return false;
}
//Aqui vai alguma validação caso necessário...
}
如果需要修改模型以访问用户,请检查插件根目录中的/vendor/felipepanegalli/authenticator-cakephp-3/src/Controller/AppController.php
文件
要访问登录页面,只需进入地址site.com.br/login
或本地主机localhost:8765/login
如有任何疑问,可以通过网站上方的联系方式发送。