IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Open source  >

使用 CakePHP 快速打造 Web 站点,第 2 部分: 用 CakePHP 打造更大更好的站点

developerWorks
前一页第 8 页,共 14 页后一页

文档选项

样例代码


对本教程的评价

帮助我们改进这些内容


让 ACL 发挥作用

已经准备好了所有组件,现在可以让 ACL 发挥作用了。完成后,系统将允许所有用户在 Tor 中查看产品,但只有创建产品的用户才能够编辑或删除该产品。

将在 products 控制器的每个操作中添加几行。这几行将检查用户是否具有访问权,然后根据权限允许或拒绝操作。

仅允许用户查看产品

首先处理 view 操作。添加一行以检查对产品的访问权,如果不允许操作,则显示一条消息。


清单 17. 添加一行以检查对产品的访问权
                    
function view($id = null) {
if (!$id) {
  $this->Session->setFlash('Invalid Product.');
  $this->redirect(array('action'=>'index'), null, true);
}
$product = $this->Product->read(null, $id);
if ($this->Acl->check($this->Session->read('user'), 
			    $id . '-' . $product['Product']['title'], 'read')) {
$this->set('product', $product);
} else {
   $this->Session->setFlash('Only registered users may view this product.');
   $this->redirect(array('action'=>'index'), null, true);
       }
}

保存文件,确保退出 Tor 并访问位于 http://localhost/products 的产品列表。当单击任何产品时,应当会被重定向到 User Registration 页面,见图 12。


图 12. 重定向
重定向

现在,使用任意帐户登录并重试。这一次应当能够查看产品,结果见图 13。


图 13. 查看产品
查看产品

已经解决了第一部分权限。现在需要告诉 Tor 拒绝除产品创建者以外的其他人对产品进行编辑和删除。





回页首


仅允许产品创建者编辑或删除产品

为 products 控制器中的 editdelete 操作设置权限的过程是一样的。


清单 18. edit 操作
                    
function edit($id = null) {
      $product = $this->Product->read(null, $id);
      if ($this->Acl->check($this->Session->read('user'), 
	$id.'-'.$product['Product']['title'], 'update')) {
  if (!$id && empty($this->data)) {
       $this->Session->setFlash('Invalid Product');
       $this->redirect(array('action'=>'index'), null, true);
  }
  if (!empty($this->data)) {
       $this->cleanUpFields();
       if ($this->Product->save($this->data)) {
   $this->Session->setFlash('The Product has been saved');
   $this->redirect(array('action'=>'index'), null, true);
} else {
$this->Session->setFlash('The Product could 
							   not be saved.
							Please, try again.');
}
}
  if (empty($this->data)) {
   $this->data = $this->Product->read(null, $id);
  }
  $dealers = $this->Product->Dealer->generateList();
  $this->set(compact('dealers'));
} else {
$this->Session->setFlash('You cannot edit this product.');
$this->redirect(array('action'=>'index'), null, true);
}
}

对于 delete 操作,应当添加几行,用来删除产品的 ACO。delete 操作类似于清单 19。


清单 19. delete 操作
                    
function delete($id = null) {
    if (!$id) {
$this->Session->setFlash('Invalid id for Product');
$this->redirect(array('action'=>'index'), null, true);
    }
    $product = $this->Product->read(null, $id);
   
     if ($this->Acl->check($this->Session->read('user'), 
	 $id.'-'.$product['Product']['title'], 'delete')) {
 if ($this->Product->del($id)) {
   $aco = $this->Acl->Aco->findByAlias($id.'-'
				  .$product['Product']['title']);
   $this->Acl->Aco->delete($aco['Aco']['id']);
   $this->Session->setFlash('Product #'.$id.' deleted');
   $this->redirect(array('action'=>'index'), null, true);
 }
    } else {
 $this->Session->setFlash('You cannot delete this product.');
 $this->redirect(array('action'=>'index'), null, true);
    }
 }

保存 products 控制器并尝试使用它。首先在 http://localhost/users/logout 上注销,然后返回到位于 http://localhost/products/ 的产品列表并尝试编辑或删除一个产品。系统将显示一条消息并将您重定向回产品列表。


图 14. 编辑或删除失败
编辑或删除失败

现在以用户 wrestler 的身份登录并尝试编辑一个产品,然后将其删除。您应当不会遇到任何问题。


图 15. 编辑或删除成功
编辑或删除成功

在 http://localhost/users/logout 上再次注销并以另一个用户的身份登录。然后尝试编辑或删除另一个产品,您会发现不能执行这些操作。在这里创建一个新产品,然后尝试以另一个用户的身份修改或删除该产品。





回页首



前一页第 8 页,共 14 页后一页
    关于 IBM 隐私条约 联系 IBM 使用条款