您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

CakePHP项目中引入Auth & Acl 控制

2025/12/11 6:11:14发布35次查看
在此简单记录操作步骤,以备后期查阅。
     一、引入auth /app/controller/appcontroller.php
php代码  
class appcontroller extends controller { public $components = array( 'acl', 'auth' => array( 'authorize' => array( 'actions' => array('actionpath' => 'controllers') ) ), 'session' ); public $helpers = array('html', 'form', 'session'); public function beforefilter() { //configure authcomponent $this->auth->loginaction = array( 'controller' => 'users', 'action' => 'login' ); $this->auth->logoutredirect = array( 'controller' => 'users', 'action' => 'login' ); $this->auth->loginredirect = array( 'controller' => 'posts', 'action' => 'add' ); } }
二、生成acl表
bash代码
./console/cake schema create dbacl
三、添加组及用户
设置model文件 /app/model/user.php
php代码
class user extends appmodel { public $belongsto = array('group'); public $actsas = array('acl' => array('type' => 'requester')); public function parentnode() { if (!$this->id && emptyempty($this->data)) { return null; } if (isset($this->data['user']['group_id'])) { $groupid = $this->data['user']['group_id']; } else { $groupid = $this->field('group_id'); } if (!$groupid) { return null; } return array('group' => array('id' => $groupid)); } public function bindnode($user) { return array('model' => 'group', 'foreign_key' => $user['user']['group_id']); } }
文件 /app/model/group.php
php代码
class group extends appmodel { public $actsas = array('acl' => array('type' => 'requester')); public function parentnode() { return null; } }
利用bake生成users、groups的mvc文件,添加组及用户,生成aros数据。
四、利用aclextras 生成aco表数据
下载aclextras 安装至/app/plugin/ 目录下
php代码
//app/config/boostrap.php // ... cakeplugin::load('aclextras'); 利用bash命令生成可用的acos数据 bash代码 ./console/cake aclextras.aclextras aco_sync
五、补充login及logout
php代码
login form->create('user', array( 'url' => array( 'controller' => 'users', 'action' => 'login' ) )); echo $this->form->input('user.username'); echo $this->form->input('user.password'); echo $this->form->end('login'); ?> ############分割线######## // action public function login() { if ($this->session->read('auth.user')) { $this->session->setflash('you are logged in!'); return $this->redirect('/'); } }
php代码
public function logout() { $this->redirect($this->auth->logout()); }
六、aco相关
acos 的展示利用treebehavior
php代码
// /app/model/aco.php 文件 public $actsas = array('tree'); public $displayfield = 'alias'; // 输出 $this->aco->generatetreelist(null, null, null, ' ');
七、权限分配
php代码
public function initdb() { $group = $this->user->group; // allow admins to everything $group->id = 1; $this->acl->allow($group, 'controllers'); // allow managers to posts and widgets $group->id = 2; $this->acl->deny($group, 'controllers'); $this->acl->allow($group, 'controllers/posts'); $this->acl->allow($group, 'controllers/widgets'); // allow users to only add and edit on posts and widgets $group->id = 3; $this->acl->deny($group, 'controllers'); $this->acl->allow($group, 'controllers/posts/add'); $this->acl->allow($group, 'controllers/posts/edit'); $this->acl->allow($group, 'controllers/widgets/add'); $this->acl->allow($group, 'controllers/widgets/edit'); // allow basic users to log out $this->acl->allow($group, 'controllers/users/logout'); // we add an exit to avoid an ugly missing views error message echo all done; exit; }
八、整理
php代码 /** * custom beforefilter */ public function beforefilter() { parent::beforefilter(); $this->auth->allow('xxx'); // $this->auth->allow(); }
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product