讲述Zend 的权限和身份验证的综合应用.
思路如下:
setModule()
浏览器地址Zend_Controller_Front registerPlugin setController()
setAction()
registerPlugin载入的插件处理程序将完成权限判断后定义程序的流程.
代码详解:
主文件(index.php)
$dbconfig = array(
‘type’ => ‘PDO_MYSQL’,
‘db’ => array(
‘host’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ‘123456’,
‘dbname’ => ‘zend’
)
);
//创建一个数据库连接先
$db = Zend_Db::Factory($dbconfig[‘type’], $dbconfig[‘db’]);
$acl = new MyAcl(); //创建了一个MyAcl对象. MyAcl内完成分配权限功能.
$auth = Zend_Auth::getInstance();
$frontController = new Zend_Controller_Front();
$frontController->throwException(true);
$frontController->setDefaultDirectory(‘./application/controllers’)
->registerPlugin(new MyAuth($auth, $acl)); //注册一个权限处理和身份验证插件,这个插件将对当前用户进行验证,并判断是否具备权限.并分别对程序的module, controller, action进行定义。
$frontController->dispatch();
这是MyAcl类所有的文件(MyAcl.php)
解释这个文件代码前,先交代一下代码中用到的ini文件.内容如下:
[index]
Index = index:index
Add = index:add
Edit = index:edit
Del = index:del
Login = index:login
Logout = index:logout
对这种格式很熟悉吧, Zend_Config_Ini 载入后,等号左边将为数组的指针, 右边为数组的值.
Class MyAcl extents Zend_Acl {
Function __Construct() {
/*载入ini文件后,将得到一个数组
$config = array(
‘index’ = > ‘index:index’,
‘add’ => ‘index:add’,
‘edit’ => ‘index:edit’,
‘del’ => ‘index:del’,
‘login’ => ‘index:login’,
‘logout’ => ‘index:logout’
)
*/
$config = new Zend_Config_Ini(‘resource.ini’, ‘index’);
Foreach($config as $key => $value) {
$this->add(new Zend_Acl_Resource($value)); //将配置文件中的值添加至资源中.完成了的Acl中的资源设置.
}
$this->addRole(new Zend_Acl_Role(‘guest’)); //分三个权限等级,完成对角色的设置.
$this->addRole(new Zend_Acl_Role(‘editor’), ‘guest’);
$this->addRole(new Zend_Acl_Role(‘admin’));
//分配权限, Zend_Acl 提供allow和deny方法.
$this->deny(‘guest’, null); //初级用户将不具备任何权限.
$this->allow(‘editor’, array(‘index:index’, ‘index:add’, ‘index:edit’, ‘index:del’, array(‘index’,’add’,’edit’,’del’);
$this->allow(‘admin’);
}
}
MyAuth.php
Class MyAuth extents Zend_Controller_Plugins_Abstractor {
Private $_acl;
Private $_auth;
Private $noauth = array(
‘module’ => ‘index’,
‘controller’ => ‘index’
‘action’ => ‘login’
);
Private $nopur = array(
‘module’ => ‘index’,
‘controller’ => ‘index’,
‘action’ => ‘login’
);
/*
获取从主文件index.php 中传递过来的对象参数$auth, $acl
*/
Public function __construct($auth, $acl) {
$this->_auth = $auth;
$this->_acl = $acl;
}
Public function preDispatch(Zend_Controller_Request_Abstractor $request) {
If(!$this->_auth->hasIdentity()) {
$role = $this->_auth->getIdentity()->role;
} else {
$role = ‘guest’;
}
$module = $request->module();
$controller = $request->controller();
$action = $request->action();
$resource = “$controller:$action”;
If(!$this->_acl->has($resource)) {
$resource = null;
}
$if(!$this->_acl->isallowed($role, $resource, $action)) {
/*
没有权限或者没有验证时,分别定义Module Controller Action
*/
If(!$this->_auth->hasIdentity()) {
$module = $noauth[‘module’];
$controller = $noauth[‘controller’];
$action = $noauth[‘action’];
} else {
$module = $nopur[‘module’];
$controller = $nopur[‘controller’];
$action = $nopur[‘action’];
}
$request->setModuleName($module);
$request->setControllerName($controller);
$request->setActionName($action);
}
}
}
身份验证文件代码(login.php)
Function loginAction() {
If($this->_request->isPost()) {
$filter = new Zend_Filter_Striptags();
$username = trim($filter->filter($this->_request->getPost(‘username’));
$password = trim($filter->filter($this->_request->getPost(‘password’));
$db = Zend_Registry::get(‘db’);
$authAdapter = new Zend_Auth_Adapter_DbTable($db, ‘users’, ‘username’, ‘password’);
$authAdapter->setIdentity($username)
->setCredential(md5($password));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
If($result->isValid()) {
$data = $authAdapter->getResultRowObject(null, ‘password’);
$auth->getStorage()->write($data);
$this->_redirct(‘/’);
Return;
}
}
}
分享到:
相关推荐
zend-validator, 来自Zend框架的验证器组件 zend zend验证器提供了一组常用的验证。 它还提供一个简单的验证器链接机制,通过它可以将多个确认器应用到用户定义的顺序中的单个数据。在 ...
Zend Framework框架的部署和应用简介 大多数做PHP的公司要求掌握的框架,与pear并成为PHP的两大主流框架,PHP5面向对象的官方API库
基于zend framework的权限验证程序
1. Introduction to Zend Framework 1.1. 概述 1.2. 安装 2. Zend_Acl 2.1. 简介 2.1.1. 关于资源(Resource) 2.1.2. 关于角色(Role) 2.1.3. 创建访问控制列表(ACL) 2.1.4. 注册角色(Role) 2.1.5. 定义访问...
Table of Contents •Introduction to Zend Framework •Overview •Installation •Learning Zend Framework •Zend Framework Quick Start •Autoloading in Zend Framework •Plugins in Zend Framework •...
本文重点介绍了Zend Server和Zend Studio在Windows 平台下搭建本地PHP服务器及开发环境的详细步骤。通俗易懂,很基础。
Zend框架包和一些常用配置。方便大家更快上手Zend框架,想下的赶快哦。呵呵
Zend Studio + Zend Studio Server 安装和配置
一个简单的zend_acl和Zend_auth的应用。对于初学zf框架的人有一定的帮助。
Zend Studio是一个屡获大奖的专业PHP集成开发环境,具备功能强大的专业编辑工具和调试工具,支持PHP语法加亮显示,支持语法自动填充功能,支持书签功能,支持语法自动缩排和代码复制功能,内置一个强大的PHP代码调试...
zend studio 10.6.2破解文件供大家下载破解。将文件com zend php core 10 6 2 v20140605 0147 jar放到 Zend Studio plugins文件夹下面即可记住是jar包。
zend framework zend framework zend framework zend framework zend framework zend framework zend framework
已验证序列号 php编程必备 ZendStudio序列号
ZendFramework 1.12.9 代码和文档.rar
PHP框架高级编程-应用Symfony,CakePHP和Zend.
Zend Encoder 可实现对PHP文件的加密,具有支持多项目配置,自定义参数,支持扩展名过滤等功能,加密后的PHP文件需要客户端的Zend Optimizer支持才能正常运行zend encoder下载zend encoder下载zend encoder下载zend ...
Whether you are learning Zend framework from scratch or looking to sharpen up your skills from previous versions, Zend Framework 2 Application Development will help you to harness the power of Zend ...
本软件为Zend Studio Mac IOS苹果版。Zend Studio是一个屡获大奖的专业PHP集成开发环境,具备功能强大的专业编辑工具和调试... Git和Github支持云开发部署深度,Zend Framework和Zend Server集成更快、更精简的IDE。
Zf2LdapAuth Zend Framework 2 Basic LDAP Authentication Module 提供了一个易于使用的 LDAP 身份验证系统。特征登录回调以将 ldap 数据存储在您选择的函数中。 功能齐全的登录表单基于 Zend\Ldap 的完全可定制的 ...
通过一整套编辑、调试、分析、优化和数据库工具,Zend Studio加速开发周期,并简化复杂的应用方案。 Zend Studio的特点包括: (1)快速代码开发与配置本地和远程调试器; (2)数据库连通性&SQL工具团队合作环境; ...