Zend Framework之Zend_Auth,Zend_Acl实现用户的授权和操作
2014-09-28 12:36
369 查看
index.php:
<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set(‘Asia/Shanghai’);
set_include_path(‘.’ .PATH_SEPARATOR .’./library’.PATH_SEPARATOR .’./application/models/’.PATH_SEPARATOR . get_include_path());
require_once ‘Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件
$registry = Zend_Registry::getInstance();
$view = new Zend_View();
$view->setScriptPath(‘./application/views/scripts/’);//设置模板显示路径
$registry['view'] = $view;//注册View
//初始化访问控制连
$acl = new Zend_Acl;
$acl->add(new Zend_Acl_Resource(‘video’));
$acl->add(new Zend_Acl_Resource(‘notices’));
//上面对应我的两个module,一个video,一个notices
$acl->addRole(new Zend_Acl_Role(‘guest’));
$acl->addRole(new Zend_Acl_Role(‘user’), ‘guest’);
$acl->addRole(new Zend_Acl_Role(‘staff’), ‘user’);//继承关系staff,user,guest
$acl->addRole(new Zend_Acl_Role(‘admin’));
$acl->allow(‘guest’, array(‘video’, ‘notices’), ‘view’);//guest只有view权利
$acl->allow(‘user’, array(‘video’), array(‘reply’, ‘download’));
$acl->allow(‘staff’, array(‘video’, ‘notices’), array(‘delete’, ‘update’));
$acl->allow(‘admin’);
//验证权限,如果没有登录则以游客身份登录
$auth = Zend_Auth::getInstance();
if(!$auth->hasIdentity())
{
$auth->getStorage()->write((object)array(‘username’ => ‘Guest’,
‘role’ => ‘guest’,
‘truename’ => ‘游客’
));
}
$router = new Zend_Controller_Router_Rewrite();
//$router->addRoute(‘root’,new Zend_Controller_Router_Route(‘/’,array(‘module’ =>’News’, ‘controller’ => ‘Index’, ‘Action’ => ‘index’))); //也是给出默认控制器的
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl(‘/zendframework’)//设置基本路径
->setParam(‘noViewRenderer’, true)
->setRouter($router)//1
->setParam(‘Zend_Acl’, $acl)//2
->setParam(‘Zend_Auth’, $auth)//3
->returnResponse(false)//4
->setControllerDirectory(‘./application/controllers’)
->throwExceptions(true)
->dispatch();
IndexController.php:
<?php
header(‘Content-Type: text/html; charset=utf-8′);
class IndexController extends Zend_Controller_Action
{
function init()
{
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();
}
function indexAction()
{
$acl = $this->getInvokeArg(‘Zend_Acl’);
$role = $this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role;//取得角色
if(!$acl->isAllowed($role, ‘video’, ‘download’))//判断用户有没有电影的下载权利
$this->getResponse()->appendBody($role.’没有video的下载权利,’.$role.’为游客登录!’);
else
$this->getResponse()->appendBody($role.’有权下载电影.合法访问’);
}
}
<?php
error_reporting(E_ALL|E_STRICT);
date_default_timezone_set(‘Asia/Shanghai’);
set_include_path(‘.’ .PATH_SEPARATOR .’./library’.PATH_SEPARATOR .’./application/models/’.PATH_SEPARATOR . get_include_path());
require_once ‘Zend/Loader.php';
Zend_Loader::registerAutoload();//设置Zend Framework 自动载入类文件
$registry = Zend_Registry::getInstance();
$view = new Zend_View();
$view->setScriptPath(‘./application/views/scripts/’);//设置模板显示路径
$registry['view'] = $view;//注册View
//初始化访问控制连
$acl = new Zend_Acl;
$acl->add(new Zend_Acl_Resource(‘video’));
$acl->add(new Zend_Acl_Resource(‘notices’));
//上面对应我的两个module,一个video,一个notices
$acl->addRole(new Zend_Acl_Role(‘guest’));
$acl->addRole(new Zend_Acl_Role(‘user’), ‘guest’);
$acl->addRole(new Zend_Acl_Role(‘staff’), ‘user’);//继承关系staff,user,guest
$acl->addRole(new Zend_Acl_Role(‘admin’));
$acl->allow(‘guest’, array(‘video’, ‘notices’), ‘view’);//guest只有view权利
$acl->allow(‘user’, array(‘video’), array(‘reply’, ‘download’));
$acl->allow(‘staff’, array(‘video’, ‘notices’), array(‘delete’, ‘update’));
$acl->allow(‘admin’);
//验证权限,如果没有登录则以游客身份登录
$auth = Zend_Auth::getInstance();
if(!$auth->hasIdentity())
{
$auth->getStorage()->write((object)array(‘username’ => ‘Guest’,
‘role’ => ‘guest’,
‘truename’ => ‘游客’
));
}
$router = new Zend_Controller_Router_Rewrite();
//$router->addRoute(‘root’,new Zend_Controller_Router_Route(‘/’,array(‘module’ =>’News’, ‘controller’ => ‘Index’, ‘Action’ => ‘index’))); //也是给出默认控制器的
//设置控制器
$frontController =Zend_Controller_Front::getInstance();
$frontController->setBaseUrl(‘/zendframework’)//设置基本路径
->setParam(‘noViewRenderer’, true)
->setRouter($router)//1
->setParam(‘Zend_Acl’, $acl)//2
->setParam(‘Zend_Auth’, $auth)//3
->returnResponse(false)//4
->setControllerDirectory(‘./application/controllers’)
->throwExceptions(true)
->dispatch();
IndexController.php:
<?php
header(‘Content-Type: text/html; charset=utf-8′);
class IndexController extends Zend_Controller_Action
{
function init()
{
$this->registry = Zend_Registry::getInstance();
$this->view = $this->registry['view'];
$this->view->baseUrl = $this->_request->getBaseUrl();
}
function indexAction()
{
$acl = $this->getInvokeArg(‘Zend_Acl’);
$role = $this->getInvokeArg(‘Zend_Auth’)->getStorage()->read()->role;//取得角色
if(!$acl->isAllowed($role, ‘video’, ‘download’))//判断用户有没有电影的下载权利
$this->getResponse()->appendBody($role.’没有video的下载权利,’.$role.’为游客登录!’);
else
$this->getResponse()->appendBody($role.’有权下载电影.合法访问’);
}
}
相关文章推荐
- Zend framework 之zend_acl,zend_auth实现用户权限控制
- 如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
- Laravel 5.1 中的ACL用户授权及权限检查功能实现教程
- Zend Framework 1.10.1 开始使用 Zend_Session,Zend_Auth,和 Zend_Acl
- 使用Zend_Auth和Zend_Acl进行登录认证及根据用户角色进行权限控制
- zend_framework-----zend_acl实现用户权限的验证
- 使用Zend_Auth和Zend_Acl进行登录认证及根据用户角色进行权限控制
- 使用 Zend_Auth 和 Zend_Db 实现用户认证(ZF Study)
- 如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
- php中Zend_Auth和Zend_Acl进行登录认证及根据用户角色进行权限控制 (1/4)
- 如何实现共享软件网络授权认证,包括注册新用户、登录、修改密码等操作
- C#写的对系统用户和组操作的类(可以实现用户和组的添加、删除、修改)
- MySQL中新建用户,新建数据库,用户授权,删除用户,修改密码的相关操作
- Zend_Auth登陆实现
- Oracle Database的基本操作——创建表空间,用户,授权。。。
- 实现功能:弹出一个DIV层提示等待信息,这个时候禁用用户操作页面中的其他内容
- 基于操作+角色的授权方案(具体实现篇)
- oracle基本操作语法(建表空间,建用户,授权,数据导入导出)
- 操作必须使用一个可更新的查询;HTTP 500 - 内部服务器错误; 您未被授权查看该页;您不具备使用所提供的凭据查看该目录或页的权限;HTTP 错误 401.3 - 访问被资源 ACL 拒绝
- RHEL5.1NFS+NIS+Authconfig+Autofs实现自动挂载NIS用户主目录